LDMX Software
RandomNumberSeedService.cxx
2
3#include <time.h>
4
6#include "Framework/Process.h"
7#include "Framework/RunHeader.h"
8
9namespace framework {
10
12 "RandomNumberSeedService";
13
14static const int SEED_EXTERNAL = 2;
15static const int SEED_RUN = 3;
16static const int SEED_TIME = 4;
17
18void RandomNumberSeedService::stream(std::ostream& s) const {
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}
28
30 const std::string& name, const std::string& tagname,
31 const framework::config::Parameters& parameters, Process& process)
32 : ConditionsObject(CONDITIONS_OBJECT_NAME),
33 ConditionsObjectProvider(CONDITIONS_OBJECT_NAME, tagname, parameters,
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}
48
50 if (seedMode_ == SEED_RUN) {
52 initialized_ = true;
53 }
54 std::string key = "RandomNumberMasterSeed[" + process().getPassName() + "]";
55 rh.setIntParameter(key, int(masterSeed_));
56}
57
58uint64_t RandomNumberSeedService::getSeed(const std::string& name) const {
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}
72
73std::vector<std::string> RandomNumberSeedService::getSeedNames() const {
74 std::vector<std::string> rv;
75 for (auto i : seeds_) {
76 rv.push_back(i.first);
77 }
78 return rv;
79}
80
81std::pair<const ConditionsObject*, ConditionsIOV>
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}
92
93} // namespace framework
94DECLARE_CONDITIONS_PROVIDER_NS(framework, RandomNumberSeedService)
#define DECLARE_CONDITIONS_PROVIDER_NS(NS, CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Class that provides header information about an event such as event number and timestamp.
Class which represents the process under execution.
Conditions object for random number seeds.
Class which defines the run/event/type range for which a given condition is valid,...
Base class for all providers of conditions objects.
const Process & process() const
Get the process handle.
Base class for all conditions objects, very simple.
Class which represents the process under execution.
Definition Process.h:36
const std::string & getPassName() const
Get the processing pass label.
Definition Process.h:56
virtual std::pair< const ConditionsObject *, ConditionsIOV > getCondition(const ldmx::EventHeader &context)
Get the seed service as a conditions object.
bool initialized_
whether the master seed has been initialized
std::map< std::string, uint64_t > seeds_
cache of seeds by name
RandomNumberSeedService(const std::string &name, const std::string &tagname, const framework::config::Parameters &parameters, Process &process)
Create the random number seed service conditions object.
std::vector< std::string > getSeedNames() const
Get a list of all the known seeds.
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
uint64_t getSeed(const std::string &name) const
Access a given seed by name.
virtual void onNewRun(ldmx::RunHeader &header)
Configure the seed service when a new run starts.
void stream(std::ostream &s) const
Stream the configuration of this object to the input ostream.
Class encapsulating parameters for configuring a processor.
Definition Parameters.h:27
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
Definition Parameters.h:89
Provides header information an event such as event number and timestamp.
Definition EventHeader.h:44
int getRun() const
Return the run number.
Definition EventHeader.h:84
Run-specific configuration and data stored in its own output TTree alongside the event TTree in the o...
Definition RunHeader.h:54
void setIntParameter(const std::string &name, int value)
Set an int parameter value.
Definition RunHeader.h:164
int getRunNumber() const
Definition RunHeader.h:74
All classes in the ldmx-sw project use this namespace.
Definition PerfDict.cxx:45