LDMX Software
Public Member Functions | Static Public Attributes | Private Attributes | Friends | List of all members
framework::RandomNumberSeedService Class Reference

System for consistent seeding of random number generators. More...

#include <RandomNumberSeedService.h>

Public Member Functions

 RandomNumberSeedService (const std::string &name, const std::string &tagname, const framework::config::Parameters &parameters, Process &process)
 Create the random number seed service conditions object.
 
virtual void onNewRun (ldmx::RunHeader &header)
 Configure the seed service when a new run starts.
 
uint64_t getSeed (const std::string &name) const
 Access a given seed by name.
 
std::vector< std::string > getSeedNames () const
 Get a list of all the known seeds.
 
uint64_t getMasterSeed () const
 Access the master seed.
 
virtual std::pair< const ConditionsObject *, ConditionsIOVgetCondition (const ldmx::EventHeader &context)
 Get the seed service as a conditions object.
 
virtual void releaseConditionsObject (const ConditionsObject *co)
 This object is both the provider of the seed service and the conditions object itself, so it does nothing when asked to release the object.
 
void stream (std::ostream &s) const
 Stream the configuration of this object to the input ostream.
 
- Public Member Functions inherited from framework::ConditionsObject
 ConditionsObject (const std::string &name)
 Class constructor.
 
virtual ~ConditionsObject ()
 Destructor.
 
std::string getName () const
 Get the name of this object.
 
- Public Member Functions inherited from framework::ConditionsObjectProvider
 ConditionsObjectProvider (const std::string &objname, const std::string &tagname, const framework::config::Parameters &parameters, Process &process)
 Class constructor.
 
virtual ~ConditionsObjectProvider ()
 Class destructor.
 
virtual void onProcessStart ()
 Callback for the ConditionsObjectProvider to take any necessary action when the processing of events starts.
 
virtual void onProcessEnd ()
 Callback for the ConditionsObjectProvider to take any necessary action when the processing of events finishes, such as closing database connections.
 
const std::string & getConditionObjectName () const
 Get the list of conditions objects available from this provider.
 
const std::string & getTagName () const
 Access the tag name.
 

Static Public Attributes

static const std::string CONDITIONS_OBJECT_NAME
 Conditions object name.
 
- Static Public Attributes inherited from framework::ConditionsObjectProvider
static const int CLASSTYPE {10}
 Constant used to types by the PluginFactory.
 

Private Attributes

bool initialized_ {false}
 whether the master seed has been initialized
 
int seedMode_ {0}
 what mode of master seed are we using
 
uint64_t masterSeed_ {0}
 what the master seed actually is
 
std::map< std::string, uint64_t > seeds_
 cache of seeds by name
 

Friends

std::ostream & operator<< (std::ostream &s, const RandomNumberSeedService &o)
 Output streaming operator.
 

Additional Inherited Members

- Static Public Member Functions inherited from framework::ConditionsObjectProvider
static void declare (const std::string &classname, ConditionsObjectProviderMaker *)
 Internal function which is part of the PluginFactory machinery.
 
- Protected Member Functions inherited from framework::ConditionsObjectProvider
std::pair< const ConditionsObject *, ConditionsIOVrequestParentCondition (const std::string &name, const ldmx::EventHeader &context)
 Request another condition needed to construct this condition.
 
const Processprocess () const
 Get the process handle.
 
- Protected Attributes inherited from framework::ConditionsObjectProvider
logging::logger theLog_
 The logger for this ConditionsObjectProvider.
 

Detailed Description

System for consistent seeding of random number generators.

The system can be configured in a number of different ways. In general, seeds are constructed based on a master seed. That master seed can be constructed in a number of ways, chosen in the python configuration. (a) The master seed can be the run number of the first run observed by the service ("Run") (b) The master seed can be based on the current time ("Time") (c) The master seed can be provided in the python configuration ("External")

Individual seeds are then constructed using the master seed and a simple hash based on the name of the seed. Seeds can also be specified in the python file, in which case no autoseeding will be performed.

Definition at line 33 of file RandomNumberSeedService.h.

Constructor & Destructor Documentation

◆ RandomNumberSeedService()

framework::RandomNumberSeedService::RandomNumberSeedService ( const std::string &  name,
const std::string &  tagname,
const framework::config::Parameters parameters,
Process process 
)

Create the random number seed service conditions object.

This is where we decide what seed mode we will be running in.

@TODO allow loading of hand-provided seeds from python

Parameters
[in]namethe name of this provider
[in]tagnamethe name of the tag generation of this condition
[in]parametersconfiguration parameters from python
[in]processreference to the running process object

Definition at line 29 of file RandomNumberSeedService.cxx.

34 process) {
35 auto seeding = parameters.getParameter<std::string>("seedMode", "run");
36 if (!strcasecmp(seeding.c_str(), "run")) {
37 seedMode_ = SEED_RUN;
38 } else if (!strcasecmp(seeding.c_str(), "external")) {
39 seedMode_ = SEED_EXTERNAL;
40 masterSeed_ = parameters.getParameter<int>("masterSeed");
41 initialized_ = true;
42 } else if (!strcasecmp(seeding.c_str(), "time")) {
43 masterSeed_ = time(0);
44 seedMode_ = SEED_TIME;
45 initialized_ = true;
46 }
47}
be configured to load conditions objects from CSV files.
const Process & process() const
Get the process handle.
ConditionsObject(const std::string &name)
Class constructor.
bool initialized_
whether the master seed has been initialized
static const std::string CONDITIONS_OBJECT_NAME
Conditions object name.
int seedMode_
what mode of master seed are we using
uint64_t masterSeed_
what the master seed actually is
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:89

References framework::config::Parameters::getParameter(), initialized_, masterSeed_, and seedMode_.

Member Function Documentation

◆ getCondition()

std::pair< const ConditionsObject *, ConditionsIOV > framework::RandomNumberSeedService::getCondition ( const ldmx::EventHeader context)
virtual

Get the seed service as a conditions object.

This object is both the provider of the seed service and the conditions object itself, so after checking if it has been initialized, we return a refernce to ourselves with the unlimited interval of validity.

Parameters
[in]contextEventHeader for the event context
Returns
reference to ourselves and unlimited interval of validity

Implements framework::ConditionsObjectProvider.

Definition at line 82 of file RandomNumberSeedService.cxx.

82 {
83 if (!initialized_) {
84 if (seedMode_ == SEED_RUN) {
85 masterSeed_ = context.getRun();
86 }
87 initialized_ = true;
88 }
89 return std::pair<const ConditionsObject*, ConditionsIOV>(
90 this, ConditionsIOV(true, true));
91}
int getRun() const
Return the run number.
Definition EventHeader.h:84

References ldmx::EventHeader::getRun(), initialized_, masterSeed_, and seedMode_.

◆ getMasterSeed()

uint64_t framework::RandomNumberSeedService::getMasterSeed ( ) const
inline

Access the master seed.

Returns
master seed that is used to derive all other seeds

Definition at line 93 of file RandomNumberSeedService.h.

93{ return masterSeed_; }

References masterSeed_.

◆ getSeed()

uint64_t framework::RandomNumberSeedService::getSeed ( const std::string &  name) const

Access a given seed by name.

Checks the cache for the input name. If the input name is not in the cache, it generates the seed for that name by combining the master seed with a hash of the name.

Parameters
[in]nameName of seed
Returns
seed derived from master seed using the input name

Definition at line 58 of file RandomNumberSeedService.cxx.

58 {
59 uint64_t seed(0);
60 std::map<std::string, uint64_t>::const_iterator i = seeds_.find(name);
61 if (i == seeds_.end()) {
62 // hash is sum of characters shifted by position, mod 8
63 for (size_t j = 0; j < name.size(); j++)
64 seed += (uint64_t(name[j]) << (j % 8));
65 seed += masterSeed_;
66 // break const here only to cache the seed
67 seeds_[name] = seed;
68 } else
69 seed = i->second;
70 return seed;
71}
std::map< std::string, uint64_t > seeds_
cache of seeds by name

References masterSeed_, and seeds_.

Referenced by simcore::Simulator::onNewRun().

◆ getSeedNames()

std::vector< std::string > framework::RandomNumberSeedService::getSeedNames ( ) const

Get a list of all the known seeds.

Returns
vector of seed names that have already been requested (in cache)

Definition at line 73 of file RandomNumberSeedService.cxx.

73 {
74 std::vector<std::string> rv;
75 for (auto i : seeds_) {
76 rv.push_back(i.first);
77 }
78 return rv;
79}

References seeds_.

◆ onNewRun()

void framework::RandomNumberSeedService::onNewRun ( ldmx::RunHeader header)
virtual

Configure the seed service when a new run starts.

If we are using the run number as the master seed, then we get the run number and set the master seed to it.

No matter what, we put the master seed into the RunHeader to be persisted into the output file.

Parameters
[in,out]headerRunHeader for the new run that is starting

Reimplemented from framework::ConditionsObjectProvider.

Definition at line 49 of file RandomNumberSeedService.cxx.

49 {
50 if (seedMode_ == SEED_RUN) {
51 masterSeed_ = rh.getRunNumber();
52 initialized_ = true;
53 }
54 std::string key = "RandomNumberMasterSeed[" + process().getPassName() + "]";
55 rh.setIntParameter(key, int(masterSeed_));
56}
const std::string & getPassName() const
Get the processing pass label.
Definition Process.h:56

References framework::Process::getPassName(), ldmx::RunHeader::getRunNumber(), initialized_, masterSeed_, framework::ConditionsObjectProvider::process(), seedMode_, and ldmx::RunHeader::setIntParameter().

◆ releaseConditionsObject()

virtual void framework::RandomNumberSeedService::releaseConditionsObject ( const ConditionsObject co)
inlinevirtual

This object is both the provider of the seed service and the conditions object itself, so it does nothing when asked to release the object.

Parameters
[in]coConditionsObject to release, unused

Reimplemented from framework::ConditionsObjectProvider.

Definition at line 114 of file RandomNumberSeedService.h.

114 {
115 } // it is us, never destroy it.

◆ stream()

void framework::RandomNumberSeedService::stream ( std::ostream &  s) const

Stream the configuration of this object to the input ostream.

Parameters
[in,out]soutput stream to print this object to

Definition at line 18 of file RandomNumberSeedService.cxx.

18 {
19 s << "RandomNumberSeedService(";
20 if (seedMode_ == SEED_RUN) s << "Seed on RUN";
21 if (seedMode_ == SEED_TIME) s << "Seed on TIME";
22 if (seedMode_ == SEED_EXTERNAL) s << "Seeded EXTERNALLY";
23 s << ") Master seed = " << masterSeed_ << std::endl;
24 for (auto i : seeds_) {
25 s << " " << i.first << "=>" << i.second << std::endl;
26 }
27}

References masterSeed_, seedMode_, and seeds_.

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream &  s,
const RandomNumberSeedService o 
)
friend

Output streaming operator.

See also
framework::RandomNumberSeedService::stream
Parameters
[in]soutput stream to print to
[in]oseed service to print
Returns
modified output stream

Definition at line 132 of file RandomNumberSeedService.h.

133 {
134 o.stream(s);
135 return s;
136 }

Member Data Documentation

◆ CONDITIONS_OBJECT_NAME

const std::string framework::RandomNumberSeedService::CONDITIONS_OBJECT_NAME
static

◆ initialized_

bool framework::RandomNumberSeedService::initialized_ {false}
private

whether the master seed has been initialized

Definition at line 140 of file RandomNumberSeedService.h.

140{false};

Referenced by getCondition(), onNewRun(), and RandomNumberSeedService().

◆ masterSeed_

uint64_t framework::RandomNumberSeedService::masterSeed_ {0}
private

what the master seed actually is

Definition at line 146 of file RandomNumberSeedService.h.

146{0};

Referenced by getCondition(), getMasterSeed(), getSeed(), onNewRun(), RandomNumberSeedService(), and stream().

◆ seedMode_

int framework::RandomNumberSeedService::seedMode_ {0}
private

what mode of master seed are we using

Definition at line 143 of file RandomNumberSeedService.h.

143{0};

Referenced by getCondition(), onNewRun(), RandomNumberSeedService(), and stream().

◆ seeds_

std::map<std::string, uint64_t> framework::RandomNumberSeedService::seeds_
mutableprivate

cache of seeds by name

Definition at line 149 of file RandomNumberSeedService.h.

Referenced by getSeed(), getSeedNames(), and stream().


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