12 #ifndef NUTOOLS_RANDOMUTILS_PROVIDERS_BASEPOLICY_H 13 #define NUTOOLS_RANDOMUTILS_PROVIDERS_BASEPOLICY_H 1 33 #include "nutools/RandomUtils/Providers/EngineId.h" 34 #include "nutools/RandomUtils/Providers/EventSeedInputData.h" 42 template <
typename SEED>
76 virtual void print(std::ostream& out)
const 77 { out <<
"Random policy: '" <<
getName() <<
"'"; }
98 template <
typename SEED>
109 { SetConfigLabels(maxSeedsLabel, baseSeedLabel, checkRangeLabel); }
112 void SetConfigLabels(
125 { bCheck = doCheck; hasParameters.set(pmDoCheck); }
129 { BaseSeed = base_seed; hasParameters.set(pmBaseSeed); }
133 { MaxSeeds = nSeeds; hasParameters.set(pmMaxSeeds); }
139 !bCheck || ((seed >= BaseSeed) && (seed < BaseSeed + MaxSeeds));
148 bool isConfigured()
const;
151 std::vector<std::string> missingConfig()
const;
154 template <
typename STREAM>
164 static constexpr
unsigned int NParams = (
unsigned int) pmNParams;
176 template <
typename SEED>
182 paramLabels[pmMaxSeeds] = maxSeedsLabel;
183 paramLabels[pmBaseSeed] = baseSeedLabel;
184 paramLabels[pmDoCheck] = checkRangeLabel;
188 template <
typename SEED>
190 if (!paramLabels[pmMaxSeeds].
empty()) {
192 if (pset.
get_if_present(paramLabels[pmMaxSeeds], seed)) SetNSeeds(seed);
194 if (!paramLabels[pmBaseSeed].
empty()) {
199 if (!paramLabels[pmDoCheck].
empty()) {
201 if (pset.
get_if_present(paramLabels[pmDoCheck], flag)) SetCheck(flag);
203 return isConfigured();
207 template <
typename SEED>
209 return (hasParameters.test(pmDoCheck) && !bCheck) || hasParameters.all();
213 template <
typename SEED>
218 if (
operator()(seed))
return;
219 seed_t offset = seed - BaseSeed;
221 <<
"NuRandomService (policy: " << policy <<
") for engine: " 222 <<
id <<
" the offset of seed " << seed <<
" is: " << offset <<
"." 223 "\nAllowed seed offsets are in the range 0....(N-1) where N is: " 224 << MaxSeeds <<
" (as configured in maxUniqueEngines)";
228 template <
typename SEED>
template <
typename STREAM>
233 out << indent <<
"seed range checker not configured!";
235 out << indent <<
"maximum number of seeds: " << MaxSeeds;
237 out << indent <<
"no limit on number of seeds.";
241 template <
typename SEED>
243 if (hasParameters.test(pmDoCheck) && !bCheck)
return {};
244 std::vector<std::string> missing;
245 for (
unsigned int i = 0; i < NParams; ++i)
246 if (!hasParameters.test(i)) missing.push_back(paramLabels[i]);
253 template <
typename SEED>
264 { this_t::configure(pset); }
278 { base_t::configure(pset); static_configure(pset); }
284 ensureRange(
id, seed);
289 virtual void print(std::ostream& out)
const override 292 range_check.print(out <<
"\n",
" ");
302 virtual void ensureRange
307 void CheckRangeConfiguration()
const;
315 template <
typename SEED>
322 template <
typename SEED>
325 std::ostringstream sstr;
326 sstr <<
"configuration of policy '" << this->
getName()
342 template <
typename SEED>
353 { this_t::configure(pset); }
369 base_t::range_check.SetConfigLabels
370 (
"maxUniqueEngines",
"",
"checkRange");
371 base_t::configure(pset);
372 static_configure(pset);
376 virtual void print(std::ostream& out)
const override;
384 {
return next_seed++; }
390 template <
typename SEED>
394 first_seed = pset.
get<
seed_t>(
"baseSeed");
395 base_t::range_check.SetBaseSeed(first_seed);
396 base_t::CheckRangeConfiguration();
397 next_seed = first_seed;
401 template <
typename SEED>
404 out <<
"\n first seed: " << first_seed;
414 template <
typename SEED>
425 { this_t::configure(pset); }
441 base_t::range_check.SetConfigLabels(
"",
"",
"checkRange");
442 base_t::configure(pset);
443 static_configure(pset);
447 virtual void print(std::ostream& out)
const override;
456 {
return next_seed++; }
463 template <
typename SEED>
477 "\nDEPRECATION WARNING: 'baseSeed' parameter has been deprecated" 478 " for linearMapping policy, in favour of 'nJob'." 479 "\nPlease update your configuration accordingly." 484 nSeedsPerJob = pset.
get<
seed_t>(
"maxUniqueEngines");
485 first_seed *= nSeedsPerJob;
487 next_seed = first_seed;
488 base_t::range_check.SetBaseSeed(next_seed);
489 base_t::range_check.SetNSeeds(nSeedsPerJob);
490 base_t::CheckRangeConfiguration();
494 template <
typename SEED>
498 <<
"\n first seed: " << first_seed
499 <<
"\n seeds per job: " << nSeedsPerJob;
509 template <
typename SEED>
520 { this_t::configure(pset); }
528 { base_t::configure(pset); static_configure(pset); }
538 {
return getInstanceParameter<seed_t>(parameters, id); }
542 template <
typename T>
543 static T getInstanceParameter
552 template <
typename SEED>
561 template <
typename SEED>
template <
typename T>
568 if (std::find(cfgKeys.begin(), cfgKeys.end(),
id.moduleLabel) == cfgKeys.end()) {
570 <<
"A seed for the nameless instance '" <<
id 571 <<
"' was requested, but there is no configuration for '" 572 <<
id.moduleLabel <<
"' module label.";
576 if (!
id.hasInstanceName()) {
582 <<
"A seed for the nameless instance '" <<
id 583 <<
"' was requested, but the configuration sets named instances (" 585 <<
").\nNameless and named engine instances can't coexist.";
589 <<
"NuRandomService: unable to find the parameter for '" <<
id <<
"'";
597 <<
"A seed for '" <<
std::string(
id) <<
"' was requested," 598 " but the configuration sets a nameless instance of '" 599 <<
id.moduleLabel <<
"'.\n" 600 <<
"Nameless and named engine instances can't coexist.";
605 <<
"NuRandomService: unable to find the parameter block for: '" 611 <<
"NuRandomService: unable to find the parameter value for: '" 624 template <
typename SEED>
635 { this_t::configure(pset); }
657 { base_t::configure(pset); static_configure(pset); }
660 virtual void print(std::ostream& out)
const override;
670 {
return base_t::getInstanceSeed(
id); }
673 { base_t::range_check.SetCheck(
false); }
678 template <
typename SEED>
681 out <<
"\n seeds directly from the configuration";
692 template <
typename SEED>
702 base_t(
"preDefinedOffset")
703 { this_t::configure(pset); }
732 base_t::range_check.SetConfigLabels
733 (
"maxUniqueEngines",
"",
"checkRange");
734 base_t::configure(pset);
735 static_configure(pset);
739 virtual void print(std::ostream& out)
const override;
746 {
return base_seed + base_t::getInstanceSeed(
id); }
753 template <
typename SEED>
757 base_seed = pset.
get<
seed_t>(
"baseSeed");
758 base_t::range_check.SetBaseSeed(base_seed);
759 base_t::CheckRangeConfiguration();
763 template <
typename SEED>
766 out <<
"\n base seed: " << base_seed;
780 template <
typename SEED>
790 { this_t::configure(pset); }
804 virtual void print(std::ostream& out)
const override;
812 generator(master_seed), distribution(min_seed, max_seed)
830 {
return (*random_seed)(); }
835 template <
typename SEED>
837 constexpr
seed_t MagicMaxSeed = 900000000;
846 random_seed.reset(
new RandomImpl(master_seed, 1, MagicMaxSeed));
851 template <
typename SEED>
855 <<
"\n master seed: " << random_seed->master_seed()
856 <<
"\n seed within: [ " << random_seed->min()
857 <<
" ; " << random_seed->max() <<
" ]" 867 #endif // NUTOOLS_RANDOMUTILS_PROVIDERS_BASEPOLICY_H virtual seed_t createEventSeed(SeedMasterHelper::EngineId const &, EventData_t const &)
Extracts a seed for specified event information; returns InvalidSeed.
Base class for policies reacting at engine instance level.
seed_t getInstanceSeed(SeedMasterHelper::EngineId const &id) const
Retrieves the parameter (seed) for the specified engine ID.
seed_t next_seed
next seed delivered
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
PerInstancePolicy(std::string name)
Internal constructor: does not configure. For use in derived classes.
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Returns the next random number.
SEED seed_t
type of the random seed
virtual bool yieldsUniqueSeeds() const override
Returns whether the returned seed should be unique: for us it "no".
std::bitset< NParams > hasParameters
virtual seed_t createSeed(SeedMasterHelper::EngineId const &id) override
Returns the seed stored in the parameter set.
std::unique_ptr< RandomImpl > random_seed
seed_t seed
seed given at construction, for the record
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Extracts a random seed.
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
std::vector< std::string > missingConfig() const
Returns the items currently not configured.
bool isConfigured() const
Returns whether all the parameters are configured.
seed_t first_seed
base seed
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
seed_t MaxSeeds
number of valid seeds
void static_configure(fhicl::ParameterSet const &pset)
Implementation of the "preDefinedOffset" policy.
virtual void print(std::ostream &out) const
Prints information on the configuration of this policy.
virtual bool yieldsUniqueSeeds() const override
Returns whether the returned seed should be unique.
void static_configure(fhicl::ParameterSet const &)
virtual seed_t getSeed(SeedMasterHelper::EngineId const &id)
Returns the next random number.
seed_t master_seed() const
Implementation of the "random" policy.
void SetCheck(bool doCheck=true)
Sets whether to perform the check or not.
virtual void print(std::ostream &out) const override
Prints information on the configuration of this policy.
CheckedRangePolicy(std::string policy_name)
Lets the derived class to start configuration.
virtual seed_t createSeed(SeedMasterHelper::EngineId const &)=0
Extracts the next random number seed.
bool is_key_to_table(std::string const &key) const
RandomImpl(seed_t master_seed, seed_t min_seed, seed_t max_seed)
bool configure(fhicl::ParameterSet const &pset)
void SetNSeeds(seed_t nSeeds)
Sets the number of seeds directly.
Interface for a policy implementation.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
virtual seed_t getEventSeed(SeedMasterHelper::EngineId const &id, EventData_t const &eventInfo)
Returns a random number specific to an event.
Implementation of the "autoIncrement" policy.
Helper class to support range checking.
std::array< std::string, NParams > paramLabels
std::string name
name of the policy
T get(std::string const &key) const
PredefinedOffsetPolicy(fhicl::ParameterSet const &pset)
RangeCheckHelper(std::string maxSeedsLabel="maxUniqueEngines", std::string baseSeedLabel="baseSeed", std::string checkRangeLabel="checkRange")
Constructor; specify configuration labels.
Implementation of the "preDefinedSeed" policy.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
RangeCheckHelper< seed_t > range_check
RandomSeedPolicyBase(std::string policy_name)
Constructor; requires the policy name.
Identifier for a engine, made of module name and optional instance name.
bool get_if_present(std::string const &key, T &value) const
void print(STREAM &out, std::string indent=std::string()) const
Prints the configuration int the specified stream.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
seed_t next_seed
next seed delivered
std::uniform_int_distribution< seed_t > distribution
flat
seed_t BaseSeed
minimum valid seed
virtual seed_t getSeed(SeedMasterHelper::EngineId const &id)
Returns the next random number.
Implementation of the "linearMapping" policy.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
fhicl::ParameterSet parameters
configuration parameters
virtual void configure(fhicl::ParameterSet const &)
Configure this policy.
void static_configure(fhicl::ParameterSet const &pset)
Local configuration; does not require the range config to be complete.
virtual seed_t createSeed(SeedMasterHelper::EngineId const &) override
Returns the next random number.
std::default_random_engine generator
random engine
PerInstancePolicy(std::string name, fhicl::ParameterSet const &pset)
unsigned int nSeedsPerJob
std::vector< std::string > get_names() const
PredefinedSeedPolicy(fhicl::ParameterSet const &pset)
Range-checked policy (abstract)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
bool is_key_to_atom(std::string const &key) const
LinearMappingPolicy(fhicl::ParameterSet const &pset)
static constexpr seed_t InvalidSeed
An invalid seed.
virtual seed_t createSeed(SeedMasterHelper::EngineId const &id) override
Returns the seed stored in the parameter set.
virtual void print(std::ostream &out) const override
Prints the details of the configuration of the random generator.
virtual ~RandomSeedPolicyBase()
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
void static_configure(fhicl::ParameterSet const &)
RandomPolicy(fhicl::ParameterSet const &pset)
void EnsureRange(std::string policy, SeedMasterHelper::EngineId const &id, seed_t seed) const
Throws an exception if the range check on seed fails.
void SetConfigLabels(std::string maxSeedsLabel="maxUniqueEngines", std::string baseSeedLabel="baseSeed", std::string checkRangeLabel="checkRange")
void SetBaseSeed(seed_t base_seed)
Sets the base seed directly.
virtual void print(std::ostream &out) const override
Prints the configuration of this policy.
std::string getName() const
Returns the given name of the policy.
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
void CheckRangeConfiguration() const
Check that the configuration is complete.
AutoIncrementPolicy(fhicl::ParameterSet const &pset)
void static_configure(fhicl::ParameterSet const &pset)
static T getInstanceParameter(fhicl::ParameterSet const &pset, SeedMasterHelper::EngineId const &id)
Retrieves the parameter (seed) for the specified engine ID.
void static_configure(fhicl::ParameterSet const &)