LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
NuRandomService.h
Go to the documentation of this file.
1 
11 #ifndef NUTOOLS_RANDOMUTILS_NuRandomService_H
12 #define NUTOOLS_RANDOMUTILS_NuRandomService_H 1
13 
14 #ifndef NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP
15 # define NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 1
17 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP
18 
19 // ROOT libraries
20 #ifndef NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT
21 # define NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT 0
23 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT
24 
25 
26 // C/C++ standard libraries
27 #include <string>
28 #include <utility> // std::forward()
29 #include <initializer_list>
30 
31 // Some helper classes.
34 
35 // CLHEP libraries
36 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP)
37 # include "CLHEP/Random/RandomEngine.h"
38 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP
39 
40 // ROOT libraries
41 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT)
42 # include "TRandom.h"
43 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT
44 
45 // From art and its tool chain.
46 #include "fhiclcpp/ParameterSet.h"
49 
50 // Forward declarations
51 namespace art {
52  class ActivityRegistry;
53  class ModuleDescription;
54  class Run;
55  class SubRun;
56  class EngineCreator;
57 }
58 
59 namespace rndm {
60 
361  public:
363 
365 
367 
369  static constexpr seed_t InvalidSeed = SeedMaster_t::InvalidSeed;
370 
372 
373  // Accept compiler written d'tor. Not copyable or assignable.
374  // This class is not copyable or assignable: these methods are not implemented.
375  NuRandomService(NuRandomService const&) = delete;
376  NuRandomService const& operator=(NuRandomService const&) = delete;
377  NuRandomService(NuRandomService&&) = delete;
378  NuRandomService const& operator=(NuRandomService&&) = delete;
379  ~NuRandomService() = default;
380 
382  static constexpr bool isSeedValid(seed_t seed)
383  { return seed != InvalidSeed; }
384 
385 
405  seed_t getSeed(std::string instanceName);
406 
414  seed_t getSeed();
415 
416 
440  seed_t getGlobalSeed(std::string instanceName);
441 
442 
444  seed_t getCurrentSeed(std::string instanceName) const
445  { return seeds.getCurrentSeed(qualify_engine_label(instanceName)); }
446 
449  { return seeds.getCurrentSeed(qualify_engine_label()); }
450 
452  seed_t getGlobalCurrentSeed(std::string instanceName) const
453  { return seeds.getCurrentSeed(qualify_global_engine(instanceName)); }
454 
455 
457 
509  seed_t createEngine
510  (art::EngineCreator& module, std::string type, std::string instance = "");
511  seed_t createEngine(art::EngineCreator& module);
513 
515 
540  art::EngineCreator& module, std::string type, std::string instance,
541  fhicl::ParameterSet const& pset, std::string pname
542  )
543  { return createEngine(module, type, instance, pset, { pname }); }
544  seed_t createEngine(
545  art::EngineCreator& module, std::string type, std::string instance,
546  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
547  );
549  art::EngineCreator& module, std::string type,
550  fhicl::ParameterSet const& pset, std::string pname
551  )
552  { return createEngine(module, type, "", pset, pname); }
554  art::EngineCreator& module, std::string type,
555  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
556  )
557  { return createEngine(module, type, "", pset, pnames); }
559  art::EngineCreator& module,
560  fhicl::ParameterSet const& pset, std::string pname
561  )
562  { return createEngine(module, pset, { pname }); }
563  seed_t createEngine(
564  art::EngineCreator& module,
565  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
566  );
568 
570 
577 
610  seed_t registerEngine
611  (SeedMaster_t::Seeder_t seeder, std::string instance = "" );
612 
614 
631  SeedMaster_t::Seeder_t seeder, std::string instance,
632  fhicl::ParameterSet const& pset, std::string pname
633  )
634  { return registerEngine(seeder, instance, pset, { pname }); }
635  seed_t registerEngine(
636  SeedMaster_t::Seeder_t seeder, std::string instance,
637  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
638  );
640  SeedMaster_t::Seeder_t seeder,
641  fhicl::ParameterSet const& pset, std::string pname
642  )
643  { return registerEngine(seeder, "", pset, pname); }
645  SeedMaster_t::Seeder_t seeder,
646  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
647  )
648  { return registerEngine(seeder, "", pset, pnames); }
650 
651 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP)
652 
666  seed_t registerEngine
667  (CLHEP::HepRandomEngine& engine, std::string instance = "")
668  { return registerEngine(CLHEPengineSeeder(engine), instance); }
670  CLHEP::HepRandomEngine& engine, std::string instance,
671  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
672  )
673  {
674  return registerEngine
675  (CLHEPengineSeeder(engine), instance, pset, pnames);
676  }
677 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP
678 
680 
683 
697  seed_t declareEngine(std::string instance = "");
698 
719  seed_t declareEngine
720  (std::string instance, fhicl::ParameterSet const& pset, std::string pname)
721  { return declareEngine(instance, pset, { pname }); }
722 
736  seed_t declareEngine(
737  std::string instance,
738  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
739  );
740 
752  seed_t declareEngine(fhicl::ParameterSet const& pset, std::string pname)
753  { return declareEngine("", pset, pname); }
754 
769  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
770  )
771  { return declareEngine("", pset, pnames); }
773 
775 
791  seed_t defineEngine
792  (SeedMaster_t::Seeder_t seeder, std::string instance = {});
793 
794  #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP)
795 
806  seed_t defineEngine
807  (CLHEP::HepRandomEngine& engine, std::string instance = {})
808  { return defineEngine(CLHEPengineSeeder(engine), instance); }
809  #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP
810 
812 
814  template<class Stream>
815  void print(Stream&& out) const
816  { seeds.print(std::forward<Stream>(out)); }
817 
819  void print() const { print(mf::LogInfo("NuRandomService")); }
820 
822  static void RandomNumberGeneratorSeeder(EngineId const& id, seed_t seed);
823 
824 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT)
825  class TRandomSeeder {
827  public:
828  TRandomSeeder(TRandom* engine): pRandom(engine) {}
829  void operator() (EngineId const&, seed_t seed)
830  { if (pRandom) pRandom->SetSeed(seed); }
831  protected:
832  TRandom* pRandom = nullptr;
833  }; // class TRandomSeeder
834 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT
835 
836 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP)
837  class CLHEPengineSeeder {
839  public:
840  CLHEPengineSeeder(CLHEP::HepRandomEngine& e): engine(e) {}
841  CLHEPengineSeeder(CLHEP::HepRandomEngine* e): engine(*e) {}
842  void operator() (EngineId const&, seed_t seed)
843  {
844  engine.setSeed(seed, 0);
845  LOG_DEBUG("CLHEPengineSeeder")
846  << "CLHEP engine: '" << engine.name() << "'[" << ((void*) &engine)
847  << "].setSeed(" << seed << ", 0)";
848  }
849  protected:
850  CLHEP::HepRandomEngine& engine;
851  }; // class CLHEPengineSeeder
852 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP
853 
854  private:
855 
856 
859 
870 
872  int verbosity = 0;
873  bool bPrintEndOfJobSummary = false;
874 
876  seed_t registerEngineID(
877  EngineId const& id,
879  );
880 
882  seed_t defineEngineID(EngineId const& id, SeedMaster_t::Seeder_t seeder);
883 
884 
886  bool hasEngine(EngineId const& id) const { return seeds.hasEngine(id); }
887 
888  // Main logic for computing and validating a seed.
889  seed_t getSeed(EngineId const&);
890 
891  // Main logic for computing and validating a seed.
892  seed_t getEventSeed(EngineId const&);
893 
899  seed_t reseedInstance(EngineId const& id);
900 
902  void reseedModule(std::string currentModule);
904  void reseedModule();
906 
908  void reseedGlobal();
909 
911  seed_t prepareEngine(EngineId const& id, SeedMaster_t::Seeder_t seeder);
912 
913  // Helper functions for all policies
914  void ensureValidState(bool bGlobal = false) const;
915 
917  EngineId qualify_engine_label
919  (std::string moduleLabel, std::string instanceName) const;
920  EngineId qualify_engine_label(std::string instanceName = "") const;
921  EngineId qualify_global_engine(std::string instanceName = "") const
922  { return EngineId(instanceName, EngineId::global); }
924 
926  static bool readSeedParameter
929  (seed_t& seed, fhicl::ParameterSet const& pset, std::string pname)
930  { return readSeedParameter(seed, pset, { pname }); }
931  static bool readSeedParameter(
932  seed_t& seed, fhicl::ParameterSet const& pset,
933  std::initializer_list<std::string> pnames
934  );
936 
938  seed_t querySeed(EngineId const& id);
939 
942  std::pair<seed_t, bool> findSeed(
943  EngineId const& id,
944  fhicl::ParameterSet const& pset, std::initializer_list<std::string> pnames
945  );
946 
948  void freezeSeed(EngineId const& id, seed_t frozen_seed);
949 
951  void registerEngineAndSeeder
952  (EngineId const& id, SeedMaster_t::Seeder_t seeder);
953 
955  seed_t seedEngine(EngineId const& id) { return seeds.reseed(id); }
956 
957  // Call backs that will be called by art.
958  void preModuleConstruction (art::ModuleDescription const& md);
959  void postModuleConstruction(art::ModuleDescription const&);
960  void preModuleBeginRun (art::ModuleDescription const& md);
961  void postModuleBeginRun (art::ModuleDescription const&);
962  void preProcessEvent (art::Event const& evt);
963  void preModule (art::ModuleDescription const& md);
964  void postModule (art::ModuleDescription const&);
965  void postProcessEvent (art::Event const&);
966  void preModuleEndJob (art::ModuleDescription const& md);
967  void postModuleEndJob (art::ModuleDescription const&);
968  void postEndJob ();
969 
970 
971  }; // class NuRandomService
972 
973 
974 
975 } // namespace rndm
976 
978 
979 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_H
A class to assist in the distribution of guaranteed unique seeds.
void print(Stream &&out) const
Prints known (EngineId,seed) pairs.
Seeder_t functor setting the seed of a CLHEP::HepRandomEngine engine (untested!)
seed_t registerEngine(CLHEP::HepRandomEngine &engine, std::string instance, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
seed_t getCurrentSeed() const
Returns the last computed seed for the default engine of current module.
Class holding the current state of art processing.
seed_t getCurrentSeed(std::string instanceName) const
Returns the last computed seed for specified engine of current module.
seed_t seedEngine(EngineId const &id)
Calls the seeder with the specified seed and engine ID.
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
Registers an existing engine with NuRandomService.
#define DECLARE_ART_SERVICE(svc, scope)
Definition: ServiceMacros.h:91
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, fhicl::ParameterSet const &pset, std::string pname)
Registers an existing engine with NuRandomService.
art::RandomNumberGenerator::seed_t seed_t
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
Definition: SeedMaster.h:277
EngineId qualify_global_engine(std::string instanceName="") const
Returns a fully qualified EngineId.
long seed
Definition: chem4.cc:68
void print() const
Prints to the framework Info logger.
std::function< void(EngineId const &, seed_t)> Seeder_t
type of a function setting a seed
Definition: SeedMaster.h:216
Describe the current state of art processing, as understood by the NuRandomService.
Definition: ArtState.h:32
CLHEPengineSeeder(CLHEP::HepRandomEngine *e)
NuRandomServiceHelper::ArtState state
Identifier for a engine, made of module name and optional instance name.
Definition: EngineId.h:22
seed_t createEngine(art::EngineCreator &module, std::string type, std::string instance, fhicl::ParameterSet const &pset, std::string pname)
Creates an engine with RandomNumberGenerator service.
static constexpr bool isSeedValid(seed_t seed)
Returns whether the specified seed is valid.
seed_t declareEngine(fhicl::ParameterSet const &pset, std::string pname)
Declares the presence of an engine with a default instance name.
CLHEPengineSeeder(CLHEP::HepRandomEngine &e)
seed_t getGlobalCurrentSeed(std::string instanceName) const
Returns the last computed seed for the specified global engine.
seed_t createEngine(art::EngineCreator &module, std::string type, fhicl::ParameterSet const &pset, std::string pname)
Creates an engine with RandomNumberGenerator service.
seed_t createEngine(art::EngineCreator &module, fhicl::ParameterSet const &pset, std::string pname)
Creates an engine with RandomNumberGenerator service.
seed_t createEngine(art::EngineCreator &module, std::string type, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
Creates an engine with RandomNumberGenerator service.
Definition: G4Helper.h:37
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string instance, fhicl::ParameterSet const &pset, std::string pname)
Registers an existing engine with NuRandomService.
#define LOG_DEBUG(id)
HLT enums.
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
seed_t reseed(EngineId const &id)
Reseeds the specified engine with a global seed (if any)
Definition: SeedMaster.h:558
Float_t e
Definition: plot.C:34
seed_t declareEngine(fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
Declares the presence of an engine with a default instance name.
An art service to assist in the distribution of guaranteed unique seeds to all engines within an art ...
SeedMaster_t seeds
Class managing the seeds.