11 #ifndef NUTOOLS_RANDOMUTILS_NuRandomService_H 12 #define NUTOOLS_RANDOMUTILS_NuRandomService_H 1 14 #ifndef NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 16 # define NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 1 17 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 20 #ifndef NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT 22 # define NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT 0 23 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT 30 #include <initializer_list> 33 #include "nutools/RandomUtils/ArtState.h" 34 #include "nutools/RandomUtils/Providers/SeedMaster.h" 37 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP) 38 # include "CLHEP/Random/RandomEngine.h" 39 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 42 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT) 44 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT 55 class ActivityRegistry;
56 class ModuleDescription;
386 {
return seed != InvalidSeed; }
448 {
return seeds.getCurrentSeed(qualify_engine_label(instanceName)); }
452 {
return seeds.getCurrentSeed(qualify_engine_label()); }
456 {
return seeds.getCurrentSeed(qualify_global_engine(instanceName)); }
459 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP) 512 template <
typename Module>
513 [[nodiscard]] std::reference_wrapper<engine_t> createEngine
516 template <
typename Module>
517 [[nodiscard]] std::reference_wrapper<engine_t> createEngine(
Module& module);
545 template <
typename Module>
550 {
return createEngine(module, type, instance, pset, { pname }); }
552 template <
typename Module>
553 [[nodiscard]] std::reference_wrapper<engine_t> createEngine(
558 template <
typename Module>
563 {
return createEngine(module, type,
"", pset, pname); }
565 template <
typename Module>
570 {
return createEngine(module, type,
"", pset, pnames); }
572 template <
typename Module>
577 {
return createEngine(module, pset, { pname }); }
579 template <
typename Module>
580 [[nodiscard]] std::reference_wrapper<engine_t> createEngine(
587 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 652 {
return registerEngine(seeder, instance, pset, { pname }); }
661 {
return registerEngine(seeder,
"", pset, pname); }
666 {
return registerEngine(seeder,
"", pset, pnames); }
669 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP) 688 CLHEP::HepRandomEngine& engine,
std::string instance,
692 return registerEngine
695 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 739 {
return declareEngine(instance, pset, { pname }); }
771 {
return declareEngine(
"", pset, pname); }
789 {
return declareEngine(
"", pset, pnames); }
812 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP) 827 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 832 template<
class Stream>
834 {
seeds.print(std::forward<Stream>(out)); }
839 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT) 841 class TRandomSeeder {
843 TRandomSeeder(TRandom* engine): pRandom(engine) {}
845 {
if (pRandom) pRandom->SetSeed(seed); }
847 TRandom* pRandom =
nullptr;
849 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USEROOT 851 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP) 859 engine.setSeed(seed, 0);
861 <<
"CLHEP engine: '" << engine.name() <<
"'[" << ((
void*) &engine)
862 <<
"].setSeed(" << seed <<
", 0)";
867 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 887 bool bPrintEndOfJobSummary =
false;
928 void ensureValidState(
bool bGlobal =
false)
const;
936 {
return EngineId(instanceName, EngineId::global); }
942 static bool readSeedParameter
944 {
return readSeedParameter(seed, pset, { pname }); }
945 static bool readSeedParameter(
947 std::initializer_list<std::string> pnames
956 std::pair<seed_t, bool> findSeed(
965 void registerEngineAndSeeder
988 #if (NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP) 995 template <
typename Module>
996 std::reference_wrapper<NuRandomService::engine_t>
997 NuRandomService::createEngine(
Module& module,
1001 EngineId id = qualify_engine_label(instance);
1002 auto& engine = module.createEngine(0, type, instance);
1004 engine.setSeed(seed, 0);
1006 <<
"Seeding " << type <<
" engine \"" <<
id.artName()
1007 <<
"\" with seed " << seed <<
".";
1011 template <
typename Module>
1012 std::reference_wrapper<NuRandomService::engine_t>
1013 NuRandomService::createEngine(
Module& module)
1015 EngineId id = qualify_engine_label();
1016 auto& engine = module.createEngine(0);
1018 engine.setSeed(seed, 0);
1020 <<
"Seeding default-type engine \"" <<
id.artName()
1021 <<
"\" with seed " << seed <<
".";
1025 template <
typename Module>
1026 std::reference_wrapper<NuRandomService::engine_t>
1027 NuRandomService::createEngine(
Module& module,
1031 std::initializer_list<std::string> pnames)
1033 EngineId id = qualify_engine_label(instance);
1034 auto& engine = module.createEngine(0, type, instance);
1036 auto const [seed, frozen] = findSeed(
id, pset, pnames);
1037 engine.setSeed(seed, 0);
1039 <<
"Seeding " << type <<
" engine \"" <<
id.artName()
1040 <<
"\" with seed " << seed <<
".";
1041 if (frozen) freezeSeed(
id, seed);
1046 template <
typename Module>
1047 std::reference_wrapper<NuRandomService::engine_t>
1048 NuRandomService::createEngine(
Module& module,
1050 std::initializer_list<std::string> pnames)
1052 EngineId id = qualify_engine_label();
1053 auto& engine = module.createEngine(0);
1055 auto const [seed, frozen] = findSeed(
id, pset, pnames);
1056 engine.setSeed(seed, 0);
1058 <<
"Seeding default-type engine \"" <<
id.artName()
1059 <<
"\" with seed " << seed <<
".";
1060 if (frozen) freezeSeed(
id, seed);
1064 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_USECLHEP 1070 #endif // NUTOOLS_RANDOMUTILS_NuRandomService_H std::reference_wrapper< engine_t > createEngine(Module &module, fhicl::ParameterSet const &pset, std::string pname)
CLHEP::HepRandomEngine engine_t
void print(Stream &&out) const
Prints known (EngineId,seed) pairs.
std::reference_wrapper< engine_t > createEngine(Module &module, std::string type, fhicl::ParameterSet const &pset, std::string pname)
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.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
seed_t getCurrentSeed(std::string instanceName) const
Returns the last computed seed for specified engine of current module.
const std::string instance
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)
#define DECLARE_ART_SERVICE(svc, scope)
static constexpr seed_t InvalidSeed
An invalid seed.
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, fhicl::ParameterSet const &pset, std::string pname)
CLHEP::HepRandomEngine & engine
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
EngineId qualify_global_engine(std::string instanceName="") const
SeedMasterHelper::EngineId EngineId
type of engine ID
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
Describe the current state of art processing, as understood by the NuRandomService.
CLHEPengineSeeder(CLHEP::HepRandomEngine *e)
NuRandomServiceHelper::ArtState state
std::reference_wrapper< engine_t > createEngine(Module &module, std::string type, std::string instance, fhicl::ParameterSet const &pset, std::string pname)
Creates an engine with RandomNumberGenerator service.
Identifier for a engine, made of module name and optional instance name.
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.
std::vector< TrajPoint > seeds
static QCString currentModule
name of the current enclosing module
seed_t registerEngine(SeedMaster_t::Seeder_t seeder, std::string instance, fhicl::ParameterSet const &pset, std::string pname)
Registers an existing engine with NuRandomService.
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
std::reference_wrapper< engine_t > createEngine(Module &module, std::string type, fhicl::ParameterSet const &pset, std::initializer_list< std::string > pnames)
seed_t reseed(EngineId const &id)
Reseeds the specified engine with a global seed (if any)
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.
art::detail::EngineCreator::seed_t seed_t