14 #ifndef NURANDOM_RANDOMUTILS_PROVIDERS_RANDOMSEEDPOLICYBASE_H 15 #define NURANDOM_RANDOMUTILS_PROVIDERS_RANDOMSEEDPOLICYBASE_H 1 44 template <
typename SEED>
59 static constexpr
bool isValid(T s) {
return (s >= Min) && (s <=
Max); }
81 {
return (s < min)? s + (max - min + 1): s; }
87 template <
typename SEED>
96 static constexpr
seed_t InvalidSeed = 0;
110 {
return createSeed(
id); }
113 virtual seed_t getEventSeed
115 {
return createEventSeed(
id, eventInfo); }
121 virtual void print(std::ostream& out)
const 122 { out <<
"Random policy: '" << getName() <<
"'"; }
134 virtual seed_t createEventSeed
136 {
return InvalidSeed; }
143 template <
typename SEED>
150 std::string maxSeedsLabel =
"maxUniqueEngines",
151 std::string baseSeedLabel =
"baseSeed",
152 std::string checkRangeLabel =
"checkRange" 154 { SetConfigLabels(maxSeedsLabel, baseSeedLabel, checkRangeLabel); }
157 void SetConfigLabels(
158 std::string maxSeedsLabel =
"maxUniqueEngines",
159 std::string baseSeedLabel =
"baseSeed",
160 std::string checkRangeLabel =
"checkRange" 170 { bCheck = doCheck; hasParameters.set(pmDoCheck); }
174 { BaseSeed = base_seed; hasParameters.set(pmBaseSeed); }
178 { MaxSeeds = nSeeds; hasParameters.set(pmMaxSeeds); }
184 !bCheck || ((seed >= BaseSeed) && (seed < BaseSeed + MaxSeeds));
193 bool isConfigured()
const;
196 std::vector<std::string> missingConfig()
const;
199 template <
typename STREAM>
200 void print(STREAM& out, std::string
indent = std::string())
const;
209 static constexpr
unsigned int NParams = (
unsigned int) pmNParams;
211 std::array<std::string, NParams> paramLabels;
212 std::bitset<NParams> hasParameters;
221 template <
typename SEED>
223 std::string maxSeedsLabel ,
224 std::string baseSeedLabel ,
225 std::string checkRangeLabel
227 paramLabels[pmMaxSeeds] = maxSeedsLabel;
228 paramLabels[pmBaseSeed] = baseSeedLabel;
229 paramLabels[pmDoCheck] = checkRangeLabel;
233 template <
typename SEED>
235 if (!paramLabels[pmMaxSeeds].
empty()) {
237 if (pset.
get_if_present(paramLabels[pmMaxSeeds], seed)) SetNSeeds(seed);
239 if (!paramLabels[pmBaseSeed].
empty()) {
244 if (!paramLabels[pmDoCheck].
empty()) {
246 if (pset.
get_if_present(paramLabels[pmDoCheck], flag)) SetCheck(flag);
248 return isConfigured();
252 template <
typename SEED>
254 return (hasParameters.test(pmDoCheck) && !bCheck) || hasParameters.all();
258 template <
typename SEED>
263 if (
operator()(seed))
return;
264 seed_t offset = seed - BaseSeed;
266 <<
"NuRandomService (policy: " << policy <<
") for engine: " 267 <<
id <<
" the offset of seed " << seed <<
" is: " << offset <<
"." 268 "\nAllowed seed offsets are in the range 0....(N-1) where N is: " 269 << MaxSeeds <<
" (as configured in maxUniqueEngines)";
273 template <
typename SEED>
template <
typename STREAM>
275 (STREAM& out, std::string
indent )
const 278 out << indent <<
"seed range checker not configured!";
280 out << indent <<
"maximum number of seeds: " << MaxSeeds;
282 out << indent <<
"no limit on number of seeds.";
286 template <
typename SEED>
288 if (hasParameters.test(pmDoCheck) && !bCheck)
return {};
289 std::vector<std::string> missing;
290 for (
unsigned int i = 0; i < NParams; ++i)
291 if (!hasParameters.test(i)) missing.push_back(paramLabels[i]);
298 template <
typename SEED>
309 { this_t::configure(pset); }
323 { base_t::configure(pset); static_configure(pset); }
328 seed_t seed = this->createSeed(
id);
329 ensureRange(
id, seed);
334 virtual void print(std::ostream& out)
const override 337 range_check.print(out <<
"\n",
" ");
347 virtual void ensureRange
349 { range_check.
EnsureRange(this->getName(),
id, seed); }
352 void CheckRangeConfiguration()
const;
360 template <
typename SEED>
367 template <
typename SEED>
370 std::ostringstream sstr;
371 sstr <<
"configuration of policy '" << this->getName()
386 template <
typename SEED>
397 { this_t::configure(pset); }
405 { base_t::configure(pset); static_configure(pset); }
415 {
return getInstanceParameter<seed_t>(parameters, id); }
419 template <
typename T>
420 static T getInstanceParameter
429 template <
typename SEED>
438 template <
typename SEED>
template <
typename T>
451 <<
"A seed for the global instance '" <<
id 452 <<
"' was requested, but the configuration sets named instances (" 459 <<
"NuRandomService: unable to find the parameter for global instance'" 466 if (!pset.
has_key(
id.moduleLabel)) {
468 <<
"A seed for the instance '" <<
id 469 <<
"' was requested, but there is no configuration at all for '" 470 <<
id.moduleLabel <<
"' module label.";
474 if (!
id.hasInstanceName()) {
480 <<
"A seed for the nameless instance '" <<
id 481 <<
"' was requested, but the configuration sets named instances (" 483 <<
").\nNameless and named engine instances can't coexist.";
487 <<
"NuRandomService: unable to find the parameter for '" <<
id <<
"'";
495 <<
"A seed for '" << std::string(
id) <<
"' was requested," 496 " but the configuration sets a nameless instance of '" 497 <<
id.moduleLabel <<
"'.\n" 498 <<
"Nameless and named engine instances can't coexist.";
503 <<
"NuRandomService: unable to find the parameter block for: '" 509 <<
"NuRandomService: unable to find the parameter value for: '" 523 #endif // NURANDOM_RANDOMUTILS_PROVIDERS_RANDOMSEEDPOLICYBASE_H 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.
PerInstancePolicy(std::string name)
Internal constructor: does not configure. For use in derived classes.
SEED seed_t
type of random seed
seed_t seed_t
type of the random seed
std::vector< std::string > missingConfig() const
Returns the items currently not configured.
bool isConfigured() const
Returns whether all the parameters are configured.
static constexpr bool isValid(T s)
Returns whether the specified seed is valid.
ValidSeed & operator=(const ValidSeed &)=delete
virtual void print(std::ostream &out) const
Prints information on the configuration of this policy.
static constexpr seed_t Max
Largest allowed seed.
art::detail::EngineCreator::seed_t seed_t
Type of seed used in art and by us.
virtual seed_t getSeed(SeedMasterHelper::EngineId const &id)
Returns the next random number.
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.
bool is_key_to_table(std::string const &key) const
bool configure(fhicl::ParameterSet const &pset)
void SetNSeeds(seed_t nSeeds)
Sets the number of seeds directly.
Interface for a policy implementation.
static constexpr seed_t Min
Smallest allowed seed.
Helper class to support range checking.
T get(std::string const &key) const
std::string indent(std::size_t const i)
RangeCheckHelper(std::string maxSeedsLabel="maxUniqueEngines", std::string baseSeedLabel="baseSeed", std::string checkRangeLabel="checkRange")
Constructor; specify configuration labels.
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
RandomSeedPolicyBase(std::string policy_name)
Constructor; requires the policy name.
Identifier for a engine, made of module name and optional instance name.
void print(STREAM &out, std::string indent=std::string()) const
Prints the configuration int the specified stream.
virtual bool yieldsUniqueSeeds() const
Returns whether the returned seed should be unique.
bool has_key(std::string const &key) const
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
static constexpr seed_t BounceUp(T s, T min, T max)
May be merged into MakeValid() with C++14.
virtual seed_t getSeed(SeedMasterHelper::EngineId const &id) override
Returns the next random number.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
ValidSeed(T s)
Constructor: converts from a value.
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.
PerInstancePolicy(std::string name, fhicl::ParameterSet const &pset)
Class storing a seed in the valid range.
std::optional< T > get_if_present(std::string const &key) const
Range-checked policy (abstract)
bool is_key_to_atom(std::string const &key) const
virtual ~RandomSeedPolicyBase()
void EnsureRange(std::string policy, SeedMasterHelper::EngineId const &id, seed_t seed) const
Throws an exception if the range check on seed fails.
seed_t seed
the converted seed
void SetConfigLabels(std::string maxSeedsLabel="maxUniqueEngines", std::string baseSeedLabel="baseSeed", std::string checkRangeLabel="checkRange")
static constexpr seed_t MakeValid(T s)
Forces the specified value into the allowed seed range.
void SetBaseSeed(seed_t base_seed)
Sets the base seed directly.
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.
An identifier for random engines.
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.