Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
rndm::details::PerEventPolicy< SEED > Class Template Reference

Implementation of the "perEvent" policy. More...

#include <PerEventPolicy.h>

Inheritance diagram for rndm::details::PerEventPolicy< SEED >:
rndm::details::RandomSeedPolicyBase< SEED >

Public Types

enum  SeedAlgo_t { saEventTimestamp_v1, NAlgos, saUndefined, saDefault = saEventTimestamp_v1 }
 
using base_t = RandomSeedPolicyBase< SEED >
 
using this_t = PerEventPolicy< SEED >
 
using seed_t = typename base_t::seed_t
 
using EventData_t = NuRandomServiceHelper::EventSeedInputData
 type for contextual event information More...
 
- Public Types inherited from rndm::details::RandomSeedPolicyBase< SEED >
using seed_t = SEED
 type of the random seed More...
 
using EventData_t = NuRandomServiceHelper::EventSeedInputData
 type of data used for event seeds More...
 
using seed_t = SEED
 type of the random seed More...
 
using EventData_t = NuRandomServiceHelper::EventSeedInputData
 type of data used for event seeds More...
 

Public Member Functions

 PerEventPolicy (fhicl::ParameterSet const &pset)
 
virtual bool yieldsUniqueSeeds () const override
 Returns whether the returned seed should be unique: for us it "no". More...
 
virtual void configure (fhicl::ParameterSet const &pset) override
 Configure this policy. More...
 
virtual void print (std::ostream &out) const override
 Prints the details of the configuration of the random generator. More...
 
- Public Member Functions inherited from rndm::details::RandomSeedPolicyBase< SEED >
 RandomSeedPolicyBase (std::string policy_name)
 Constructor; requires the policy name. More...
 
virtual ~RandomSeedPolicyBase ()
 
virtual seed_t getSeed (SeedMasterHelper::EngineId const &id)
 Returns the next random number. More...
 
virtual seed_t getEventSeed (SeedMasterHelper::EngineId const &id, EventData_t const &eventInfo)
 Returns a random number specific to an event. More...
 
std::string getName () const
 Returns the given name of the policy. More...
 
 RandomSeedPolicyBase (std::string policy_name)
 Constructor; requires the policy name. More...
 
virtual ~RandomSeedPolicyBase ()
 
virtual seed_t getSeed (SeedMasterHelper::EngineId const &id)
 Returns the next random number. More...
 
virtual seed_t getEventSeed (SeedMasterHelper::EngineId const &id, EventData_t const &eventInfo)
 Returns a random number specific to an event. More...
 
std::string getName () const
 Returns the given name of the policy. More...
 

Static Public Member Functions

template<typename Hashable >
static seed_t SeedFromHash (Hashable const &info)
 Converts some information into a valid seed by means of hash values. More...
 
static std::string UniqueEventIDString (EventData_t const &info)
 Converts run, subrun and event numbers into a string. More...
 
static std::string UniqueEventString (EventData_t const &info)
 Converts event ID and timestamp information into a string. More...
 

Static Public Attributes

static constexpr const char * DefaultVersion = "v1"
 Default algorithm version. More...
 
- Static Public Attributes inherited from rndm::details::RandomSeedPolicyBase< SEED >
static constexpr seed_t InvalidSeed = 0
 An invalid seed. More...
 

Private Types

using SeedOffset_t = typename std::make_signed< seed_t >::type
 type for seed offset More...
 

Private Member Functions

virtual seed_t createSeed (SeedMasterHelper::EngineId const &) override
 Per-job seed: invalid seeds are returned. More...
 
virtual seed_t createEventSeed (SeedMasterHelper::EngineId const &id, EventData_t const &info) override
 Returns a seed proper for the specified event information. More...
 

Static Private Member Functions

template<typename T >
static seed_t makeValid (T value)
 Renders a seed valid. More...
 
static seed_t EventTimestamp_v1 (SeedMasterHelper::EngineId const &id, EventData_t const &info)
 Implementation of the EventTimestamp_v1 algorithm. More...
 

Private Attributes

SeedAlgo_t algo
 the algorithm to extract the seed More...
 
SeedOffset_t offset
 offset added to all the seeds More...
 
static const std::vector< std::stringalgoNames = PerEventPolicy<SEED>::InitAlgoNames()
 Algorithm name (manual) handling. More...
 
static std::vector< std::stringInitAlgoNames ()
 

Additional Inherited Members

- Protected Attributes inherited from rndm::details::RandomSeedPolicyBase< SEED >
std::string name
 name of the policy More...
 

Detailed Description

template<typename SEED>
class rndm::details::PerEventPolicy< SEED >

Implementation of the "perEvent" policy.


This policy extracts seeds depending on contextual information from the event. The information that enters the seed is the event ID (run, subrun, event), the process name, and the engine ID.

Definition at line 45 of file PerEventPolicy.h.

Member Typedef Documentation

template<typename SEED>
using rndm::details::PerEventPolicy< SEED >::base_t = RandomSeedPolicyBase<SEED>

Definition at line 47 of file PerEventPolicy.h.

type for contextual event information

Definition at line 52 of file PerEventPolicy.h.

template<typename SEED>
using rndm::details::PerEventPolicy< SEED >::seed_t = typename base_t::seed_t

Definition at line 49 of file PerEventPolicy.h.

template<typename SEED>
using rndm::details::PerEventPolicy< SEED >::SeedOffset_t = typename std::make_signed<seed_t>::type
private

type for seed offset

Definition at line 109 of file PerEventPolicy.h.

template<typename SEED>
using rndm::details::PerEventPolicy< SEED >::this_t = PerEventPolicy<SEED>

Definition at line 48 of file PerEventPolicy.h.

Member Enumeration Documentation

template<typename SEED>
enum rndm::details::PerEventPolicy::SeedAlgo_t
Enumerator
saEventTimestamp_v1 

event timestamp algorithm (v1)

NAlgos 

total number of seed algorithms

saUndefined 

algorithm not defined

saDefault 

default algorithm

Definition at line 54 of file PerEventPolicy.h.

54  {
55  saEventTimestamp_v1, ///< event timestamp algorithm (v1)
56  NAlgos, ///< total number of seed algorithms
57  saUndefined, ///< algorithm not defined
58  saDefault = saEventTimestamp_v1 ///< default algorithm
59  } SeedAlgo_t; ///< seed algorithms; see reseed() documentation for details
total number of seed algorithms

Constructor & Destructor Documentation

template<typename SEED>
rndm::details::PerEventPolicy< SEED >::PerEventPolicy ( fhicl::ParameterSet const &  pset)
inline

Configures from a parameter set

See also
configure()

Definition at line 63 of file PerEventPolicy.h.

63  : base_t("perEvent")
64  { this_t::configure(pset); }
virtual void configure(fhicl::ParameterSet const &pset) override
Configure this policy.
RandomSeedPolicyBase< SEED > base_t

Member Function Documentation

template<typename SEED >
void rndm::details::PerEventPolicy< SEED >::configure ( fhicl::ParameterSet const &  pset)
overridevirtual

Configure this policy.

Parameters
psetthe parameter set for the configuration

Parameters:

  • algorithm (string, default: "EventTimestamp_v1"): the name of the algorithm to get the seed
  • offset (integer, optional): if specified, all the final seeds are incremented by this value; the resulting seed is not checked, it might even be invalid. This is considered an emergency hack when one absolutely needs to have a different seed than the one assigned to the event. This also defies the purpose of the policy, since after this, to reproduce the random sequences the additional knowledge of which offset was used is necessary.

Reimplemented from rndm::details::RandomSeedPolicyBase< SEED >.

Definition at line 218 of file PerEventPolicy.h.

218  {
219  // set the algorithm
220  algo = saUndefined;
221  std::string algorithm_name
222  = pset.get<std::string>("algorithm", "default");
223 
224  if (algorithm_name == "default") algo = saDefault;
225  else {
226  for (size_t iAlgo = 0; iAlgo < (size_t) NAlgos; ++iAlgo) {
227  if (algorithm_name != algoNames[iAlgo]) continue;
228  algo = (SeedAlgo_t) iAlgo;
229  break;
230  } // for
231  }
232  if (algo == saUndefined) {
234  << "No valid event random seed algorithm specified!\n";
235  }
236 
237  // read an optional overall offset
238  offset = pset.get<SeedOffset_t>("offset", 0);
239 
240  // EventTimestamp_v1 does not require specific configuration
241 
242  } // PerEventPolicy<SEED>::configure()
std::string string
Definition: nybbler.cc:12
SeedOffset_t offset
offset added to all the seeds
total number of seed algorithms
typename std::make_signed< seed_t >::type SeedOffset_t
type for seed offset
static const std::vector< std::string > algoNames
Algorithm name (manual) handling.
SeedAlgo_t algo
the algorithm to extract the seed
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
template<typename SEED >
PerEventPolicy< SEED >::seed_t rndm::details::PerEventPolicy< SEED >::createEventSeed ( SeedMasterHelper::EngineId const &  id,
EventData_t const &  info 
)
overrideprivatevirtual

Returns a seed proper for the specified event information.

Parameters
idrandom number engine ID (moule label and instance name)
infoevent information
Returns
a seed specific to the information provided

The algorithm used to combine the provided information into a seed is defined by the configuration. The following algorithms are supported:

  • EventTimestamp_v1: includes event ID (run, subrun and event numbers), event timestamp, process name and engine ID into a hash value, used for the seed

Reimplemented from rndm::details::RandomSeedPolicyBase< SEED >.

Definition at line 260 of file PerEventPolicy.h.

261  {
263  switch (algo) {
264  case saEventTimestamp_v1:
265  seed = EventTimestamp_v1(id, info);
266  break;
267  case saUndefined:
269  << "Per-event random number seeder not configured!\n";
270  default:
272  << "Unsupported per-event random number seeder (#"
273  << ((int) algo) << ")\n";
274  } // switch
275  return seed + offset;
276  } // PerEventPolicy<SEED>::createEventSeed()
static seed_t EventTimestamp_v1(SeedMasterHelper::EngineId const &id, EventData_t const &info)
Implementation of the EventTimestamp_v1 algorithm.
SeedOffset_t offset
offset added to all the seeds
unsigned long seed_t
SeedAlgo_t algo
the algorithm to extract the seed
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: BasePolicies.h:51
template<typename SEED>
virtual seed_t rndm::details::PerEventPolicy< SEED >::createSeed ( SeedMasterHelper::EngineId const &  )
inlineoverrideprivatevirtual

Per-job seed: invalid seeds are returned.

Implements rndm::details::RandomSeedPolicyBase< SEED >.

Definition at line 116 of file PerEventPolicy.h.

117  { return base_t::InvalidSeed; }
static constexpr seed_t InvalidSeed
An invalid seed.
Definition: BasePolicies.h:51
template<typename SEED >
auto rndm::details::PerEventPolicy< SEED >::EventTimestamp_v1 ( SeedMasterHelper::EngineId const &  id,
EventData_t const &  info 
)
staticprivate

Implementation of the EventTimestamp_v1 algorithm.

Definition at line 197 of file PerEventPolicy.h.

199  {
200  if (!info.isTimeValid) {
202  << "Input event has an invalid timestamp,"
203  " random seed per-event policy EventTimestamp_v1 can't be used.\n";
204  }
206  + " Process: " + info.processName
207  + " Module: " + id.moduleLabel;
208  if (!id.instanceName.empty())
209  s.append(" Instance: ").append(id.instanceName);
210  seed_t seed = SeedFromHash(s);
211  MF_LOG_DEBUG("PerEventPolicy") << "Seed from: '" << s << "': " << seed;
212  return seed;
213  } // PerEventPolicy<SEED>::EventTimestamp_v1()
std::string string
Definition: nybbler.cc:12
unsigned long seed_t
static seed_t SeedFromHash(Hashable const &info)
Converts some information into a valid seed by means of hash values.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
#define MF_LOG_DEBUG(id)
static QCString * s
Definition: config.cpp:1042
static std::string UniqueEventString(EventData_t const &info)
Converts event ID and timestamp information into a string.
template<typename SEED >
std::vector< std::string > rndm::details::PerEventPolicy< SEED >::InitAlgoNames ( )
staticprivate

Definition at line 158 of file PerEventPolicy.h.

158  {
159 
160  std::vector<std::string> names((size_t) NAlgos);
161 
162  names[saEventTimestamp_v1] = "EventTimestamp_v1";
163 
164  return names;
165  } // PerEventPolicy<SEED>::InitAlgoNames()
total number of seed algorithms
template<typename SEED>
template<typename T >
static seed_t rndm::details::PerEventPolicy< SEED >::makeValid ( value)
inlinestaticprivate

Renders a seed valid.

Definition at line 137 of file PerEventPolicy.h.

137 { return ValidSeed<seed_t>(value); }
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
template<typename SEED >
void rndm::details::PerEventPolicy< SEED >::print ( std::ostream &  out) const
overridevirtual

Prints the details of the configuration of the random generator.

Reimplemented from rndm::details::RandomSeedPolicyBase< SEED >.

Definition at line 248 of file PerEventPolicy.h.

248  {
249  base_t::print(out);
250  out
251  << "\n algorithm version: " << algoNames[algo];
252  if (offset != 0)
253  out << "\n constant offset: " << offset;
254  } // PerEventPolicy<SEED>::print()
SeedOffset_t offset
offset added to all the seeds
virtual void print(std::ostream &out) const
Prints information on the configuration of this policy.
Definition: BasePolicies.h:76
static const std::vector< std::string > algoNames
Algorithm name (manual) handling.
SeedAlgo_t algo
the algorithm to extract the seed
template<typename SEED>
template<typename Hashable >
static seed_t rndm::details::PerEventPolicy< SEED >::SeedFromHash ( Hashable const &  info)
inlinestatic

Converts some information into a valid seed by means of hash values.

Definition at line 97 of file PerEventPolicy.h.

98  { return makeValid(std::hash<Hashable>()(info)); }
static seed_t makeValid(T value)
Renders a seed valid.
template<typename SEED >
std::string rndm::details::PerEventPolicy< SEED >::UniqueEventIDString ( EventData_t const &  info)
static

Converts run, subrun and event numbers into a string.

Definition at line 176 of file PerEventPolicy.h.

177  {
178  std::ostringstream sstr;
179  sstr << "Run: " << info.runNumber << " Subrun: " << info.subRunNumber
180  << " Event: " << info.eventNumber;
181  return sstr.str();
182  } // PerEventPolicy<SEED>::UniqueEventIDString()
template<typename SEED >
std::string rndm::details::PerEventPolicy< SEED >::UniqueEventString ( EventData_t const &  info)
static

Converts event ID and timestamp information into a string.

Definition at line 187 of file PerEventPolicy.h.

188  {
189  std::ostringstream sstr;
190  sstr << " Timestamp: " << info.time;
191  return UniqueEventIDString(info) + sstr.str();
192  } // PerEventPolicy<SEED>::UniqueEventString()
static std::string UniqueEventIDString(EventData_t const &info)
Converts run, subrun and event numbers into a string.
template<typename SEED>
virtual bool rndm::details::PerEventPolicy< SEED >::yieldsUniqueSeeds ( ) const
inlineoverridevirtual

Returns whether the returned seed should be unique: for us it "no".

Reimplemented from rndm::details::RandomSeedPolicyBase< SEED >.

Definition at line 67 of file PerEventPolicy.h.

67 { return false; }

Member Data Documentation

template<typename SEED>
SeedAlgo_t rndm::details::PerEventPolicy< SEED >::algo
private

the algorithm to extract the seed

Definition at line 111 of file PerEventPolicy.h.

template<typename SEED>
const std::vector< std::string > rndm::details::PerEventPolicy< SEED >::algoNames = PerEventPolicy<SEED>::InitAlgoNames()
staticprivate

Algorithm name (manual) handling.

Definition at line 146 of file PerEventPolicy.h.

template<typename SEED>
constexpr const char* rndm::details::PerEventPolicy< SEED >::DefaultVersion = "v1"
static

Default algorithm version.

Definition at line 92 of file PerEventPolicy.h.

template<typename SEED>
SeedOffset_t rndm::details::PerEventPolicy< SEED >::offset
private

offset added to all the seeds

Definition at line 113 of file PerEventPolicy.h.


The documentation for this class was generated from the following file: