9 #ifndef NUTOOLS_RANDOMUTILS_PROVIDERS_SEEDMASTER_H 10 #define NUTOOLS_RANDOMUTILS_PROVIDERS_SEEDMASTER_H 1 33 #define SEED_SERVICE_POLICIES \ 34 SEED_SERVICE_POLICY(unDefined) \ 35 SEED_SERVICE_POLICY(autoIncrement) \ 36 SEED_SERVICE_POLICY(linearMapping) \ 37 SEED_SERVICE_POLICY(preDefinedOffset) \ 38 SEED_SERVICE_POLICY(preDefinedSeed) \ 39 SEED_SERVICE_POLICY(random) \ 40 SEED_SERVICE_POLICY(perEvent) \ 207 template <
typename SEED>
216 using Seeder_t = std::function<void(EngineId const&, seed_t)>;
235 template <
typename... Args>
240 template <
typename... Args>
261 #define SEED_SERVICE_POLICY(x) x, 263 #undef SEED_SERVICE_POLICY 266 static const std::vector<std::string>&
policyNames();
285 (iEngineInfo !=
engineData.end()) && iEngineInfo->second.hasSeeder();
292 seed_t getSeed(std::string moduleLabel, std::string instanceName);
300 (
EventData_t const& data, std::string instanceName);
362 template<
typename Stream>
void print(Stream&&)
const;
411 auto iSeed = seeds.find(
id);
412 return (iSeed == seeds.end())? InvalidSeed: iSeed->second;
440 template <
typename SEED>
442 static std::vector<std::string> names;
444 const char *cnames[] = {
445 #define SEED_SERVICE_POLICY(x) #x, 447 #undef SEED_SERVICE_POLICY 449 names = std::vector<std::string>
450 (cnames, cnames +
sizeof(cnames)/
sizeof(cnames[0]));
459 template <
typename SEED>
470 const std::string strPolicy = pSet.
get<std::string>(
"policy");
481 case preDefinedOffset:
495 <<
"SeedMaster(): Internal error: unknown policy_ value";
506 template <
typename SEED>
509 (std::string moduleLabel)
516 template <
typename SEED>
518 (std::string moduleLabel, std::string instanceName)
525 template <
typename SEED>
534 template <
typename SEED>
540 <<
"SeedMaster(): Engine with ID='" <<
id <<
"' already registered";
547 template <
typename SEED>
556 template <
typename SEED>
564 engineInfo.applySeed(
id, seed);
570 template <
typename SEED>
578 engineInfo.autoApplySeed(
id, seed);
586 template <
typename SEED>
template <
typename Stream>
588 log <<
"\nSummary of seeds computed by the NuRandomService";
591 std::ostringstream sstr;
593 if (!sstr.str().empty()) log <<
'\n' << sstr.str();
597 constexpr
unsigned int ConfSeedWidth = 18;
598 constexpr
unsigned int SepWidth1 = 2;
599 constexpr
unsigned int LastSeedWidth = 18;
600 constexpr
unsigned int SepWidth2 = SepWidth1 + 1;
603 << std::setw(ConfSeedWidth) <<
"Configured value" 604 << std::setw(SepWidth1) <<
"" 605 << std::setw(LastSeedWidth) <<
"Last value" 606 << std::setw(SepWidth2) <<
"" 607 <<
"ModuleLabel.InstanceName";
612 seed_t currentSeed = p.second;
617 << std::setw(ConfSeedWidth) <<
"INVALID!!!" 618 << std::setw(SepWidth1) <<
"" 619 << std::setw(LastSeedWidth) <<
"" 620 << std::setw(SepWidth2) <<
"" 625 << std::setw(ConfSeedWidth) <<
"(per event)" 626 << std::setw(SepWidth1) <<
"" 627 << std::setw(LastSeedWidth) << currentSeed
628 << std::setw(SepWidth2) <<
"" 633 if (configuredSeed == currentSeed) {
635 << std::setw(ConfSeedWidth) << configuredSeed
636 << std::setw(SepWidth1) <<
"" 637 << std::setw(LastSeedWidth) <<
"(same)" 638 << std::setw(SepWidth2) <<
"" 643 << std::setw(ConfSeedWidth) << configuredSeed
644 << std::setw(SepWidth1) <<
"" 645 << std::setw(LastSeedWidth) << currentSeed
646 << std::setw(SepWidth2) <<
"" 647 << ID <<
" [[ERROR!!!]]";
650 if (ID.
isGlobal()) log <<
" (global)";
654 log <<
'\n' << std::endl;
659 template <
typename SEED>
687 template <
typename SEED>
715 template <
typename SEED>
725 template <
typename SEED>
733 template <
typename SEED>
742 if (
policy == unDefined) {
743 std::ostringstream os;
744 os<<
"NuRandomService::setPolicy(): Unrecognized policy: " 746 <<
"\n Known policies are: ";
749 std::ostream_iterator<std::string>(os,
", "));
757 template <
typename SEED>
762 for (
auto const& p: seeds) {
765 if ( p.first ==
id )
continue;
767 if ( p.second == seed ){
769 <<
"NuRandomService::ensureUnique() seed: "<<seed
770 <<
" already used by module.instance: " << p.first <<
"\n" 771 <<
"May not be reused by module.instance: " << id;
777 #endif // NUTOOLS_RANDOMUTILS_PROVIDERS_SEEDMASTER_H 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
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.
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::map< EngineId, seed_t > map_type
Type for seed data base.
Implementation of the "preDefinedOffset" policy.
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.
Implementation of the "random" policy.
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.
Interface for a policy implementation.
std::function< void(EngineId const &, seed_t)> Seeder_t
type of a function setting a seed
Implementation of the "autoIncrement" policy.
Collection of all the random seed assignment policies.
typename PolicyImpl_t::EventData_t EventData_t
type of data used for event seeds
T get(std::string const &key) const
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
void freezeSeed(EngineId const &id, seed_t seed)
Forces SeedMaster not to change the seed of a registered engine.
Implementation of the "preDefinedSeed" policy.
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.
Implementation of the "linearMapping" policy.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Policy policy
Which of the supported policies to use?
SeedMaster(const fhicl::ParameterSet &)
Implementation of the "perEvent" policy.
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
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
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()
An identifier for random engines.
Information for each engine.