9 #ifndef NURANDOM_RANDOMUTILS_PROVIDERS_SEEDMASTER_H 10 #define NURANDOM_RANDOMUTILS_PROVIDERS_SEEDMASTER_H 1 197 template <
typename SEED>
206 using Seeder_t = std::function<void(EngineId const&, seed_t)>;
225 template <
typename... Args>
230 template <
typename... Args>
253 static const std::vector<std::string>&
policyNames();
272 (iEngineInfo !=
engineData.end()) && iEngineInfo->second.hasSeeder();
279 seed_t getSeed(std::string moduleLabel, std::string instanceName);
287 (
EventData_t const& data, std::string instanceName);
349 template<
typename Stream>
void print(Stream&&)
const;
398 auto iSeed = seeds.find(
id);
399 return (iSeed == seeds.end())? InvalidSeed: iSeed->second;
427 template <
typename SEED>
434 template <
typename SEED>
444 policy_impl = std::move(details::makeRandomSeedPolicy<seed_t>(pSet).ptr);
454 template <
typename SEED>
457 (std::string moduleLabel)
464 template <
typename SEED>
466 (std::string moduleLabel, std::string instanceName)
473 template <
typename SEED>
482 template <
typename SEED>
488 <<
"SeedMaster(): Engine with ID='" <<
id <<
"' already registered";
495 template <
typename SEED>
504 template <
typename SEED>
512 engineInfo.applySeed(
id, seed);
518 template <
typename SEED>
526 engineInfo.autoApplySeed(
id, seed);
534 template <
typename SEED>
template <
typename Stream>
536 log <<
"\nSummary of seeds computed by the NuRandomService";
539 std::ostringstream sstr;
541 if (!sstr.str().empty()) log <<
'\n' << sstr.str();
545 constexpr
unsigned int ConfSeedWidth = 18;
546 constexpr
unsigned int SepWidth1 = 2;
547 constexpr
unsigned int LastSeedWidth = 18;
548 constexpr
unsigned int SepWidth2 = SepWidth1 + 1;
551 << std::setw(ConfSeedWidth) <<
"Configured value" 552 << std::setw(SepWidth1) <<
"" 553 << std::setw(LastSeedWidth) <<
"Last value" 554 << std::setw(SepWidth2) <<
"" 555 <<
"ModuleLabel.InstanceName";
560 seed_t currentSeed = p.second;
565 << std::setw(ConfSeedWidth) <<
"INVALID!!!" 566 << std::setw(SepWidth1) <<
"" 567 << std::setw(LastSeedWidth) <<
"" 568 << std::setw(SepWidth2) <<
"" 573 << std::setw(ConfSeedWidth) <<
"(per event)" 574 << std::setw(SepWidth1) <<
"" 575 << std::setw(LastSeedWidth) << currentSeed
576 << std::setw(SepWidth2) <<
"" 581 if (configuredSeed == currentSeed) {
583 << std::setw(ConfSeedWidth) << configuredSeed
584 << std::setw(SepWidth1) <<
"" 585 << std::setw(LastSeedWidth) <<
"(same)" 586 << std::setw(SepWidth2) <<
"" 591 << std::setw(ConfSeedWidth) << configuredSeed
592 << std::setw(SepWidth1) <<
"" 593 << std::setw(LastSeedWidth) << currentSeed
594 << std::setw(SepWidth2) <<
"" 595 << ID <<
" [[ERROR!!!]]";
598 if (ID.
isGlobal()) log <<
" (global)";
602 log <<
'\n' << std::endl;
607 template <
typename SEED>
635 template <
typename SEED>
663 template <
typename SEED>
673 template <
typename SEED>
681 template <
typename SEED>
690 template <
typename SEED>
695 for (
auto const& p: seeds) {
698 if ( p.first ==
id )
continue;
700 if ( p.second == seed ){
702 <<
"NuRandomService::ensureUnique() seed: "<<seed
703 <<
" already used by module.instance: " << p.first <<
"\n" 704 <<
"May not be reused by module.instance: " << id;
710 #endif // NURANDOM_RANDOMUTILS_PROVIDERS_SEEDMASTER_H std::vector< std::string > const & policyNames()
Returns a list of names of policies, in the same order as Policy enum.
void setPolicy(std::string policyName)
Helper function to parse the policy name.
A class to assist in the distribution of guaranteed unique seeds to all engine IDs.
void applySeed(Args...args) const
Execute the seeder (whatever arguments it has...)
NuRandomServiceHelper::EventSeedInputData EventData_t
type of data used for event seeds
bool autoseed
whether seeding can be automatic
Declaration of policy enumerator and names.
std::map< EngineId, EngineInfo_t > EngineData_t
type of map of seeders associated with the engines
void print() const
Prints to the framework Info logger.
bool hasSeeder(EngineId const &id) const
Returns whether the specified engine has a valid seeder.
Policy
Enumeration of all supported random seed policies.
map_type knownEventSeeds
List of event seeds already computed.
int verbosity
Control the level of information messages.
static constexpr seed_t InvalidSeed
An invalid seed.
std::string const & policyName(Policy policy)
Returns the name of the specified policy.
std::map< EngineId, seed_t > map_type
Type for seed data base.
seed_t getEventSeed(EventData_t const &data, std::string instanceName)
Returns the seed value for the event with specified data.
map_type currentSeeds
List of seeds already computed.
bool hasEngine(EngineId const &id) const
Returns whether the specified engine is already registered.
static seed_t getSeedFromMap(map_type const &seeds, EngineId const &id)
Returns a seed from the specified map, or InvalidSeed if not present.
seed_t seed_t
type of served seeds
SeedMasterHelper::EngineId EngineId
type of engine ID
void registerNewSeeder(EngineId const &id, Seeder_t seeder)
Register the specified function to reseed the engine id.
std::function< void(EngineId const &, seed_t)> Seeder_t
type of a function setting a seed
Collection of all the random seed assignment policies.
typename PolicyImpl_t::EventData_t EventData_t
type of data used for event seeds
void freezeSeed(EngineId const &id, seed_t seed)
Forces SeedMaster not to change the seed of a registered engine.
Identifier for a engine, made of module name and optional instance name.
static const std::vector< std::string > & policyNames()
void freeze(bool doFreeze=true)
Provides a key iterator from a map-type object.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
std::vector< TrajPoint > seeds
Policy policy
Which of the supported policies to use?
SeedMaster(const fhicl::ParameterSet &)
Helper to instantiate a random number policy class.
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
seed_t reseedEvent(EngineId const &id, EventData_t const &data)
Reseeds the specified engine with an event seed (if any)
seed_t getSeed(std::string moduleLabel)
Returns the seed value for this module label.
void onNewEvent()
Prepares for a new event.
void ensureUnique(EngineId const &id, seed_t seed, map_type const &map) const
Throws if the seed has already been used.
bool isGlobal() const
Returns whether the label is "global" (no module context)
static constexpr seed_t InvalidSeed
An invalid seed.
Seeder_t seeder
engine seeder
map_type configuredSeeds
List of seeds computed from configuration information.
void setSeeder(Seeder_t new_seeder)
std::unique_ptr< PolicyImpl_t > policy_impl
the instance of the random policy
EngineInfoIteratorBox engineIDsRange() const
Returns an object to iterate in range-for through configured engine IDs.
EngineData_t engineData
list of all engine information
void registerSeeder(EngineId const &id, Seeder_t seeder)
Register the specified function to reseed the engine id.
seed_t reseed(EngineId const &id)
Reseeds the specified engine with a global seed (if any)
seed_t getCurrentSeed(EngineId const &id) const
Returns the last computed seed value for the specified engine ID.
void autoApplySeed(Args...args) const
Applies the seed unless frozen.
void ensureUnique(EngineId const &id, seed_t seed) const
Throws if the seed has already been used.
Provides iterators for std::begin() and std::end()
Policy policyFromName(std::string const &policyName)
Returns the policy with the specified name.
An identifier for random engines.
Information for each engine.