CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RandomNumberGeneratorService.h
Go to the documentation of this file.
1 #ifndef IOMC_RandomEngine_RandomNumberGeneratorService_h
2 #define IOMC_RandomEngine_RandomNumberGeneratorService_h
3 
16 
17 #include <atomic>
18 #include <cstdint>
19 #include <fstream>
20 #include <iosfwd>
21 #include <istream>
22 #include <limits>
23 #include <map>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 class RandomEngineState;
29 
30 namespace CLHEP {
31  class HepRandomEngine;
32 }
33 
34 namespace edm {
35  class ActivityRegistry;
37  class Event;
38  class LuminosityBlock;
39  class LuminosityBlockIndex;
40  class ModuleCallingContext;
41  class ModuleDescription;
42  class ParameterSet;
43  class StreamContext;
44  class StreamID;
45 
46  namespace service {
47 
48  class SystemBounds;
49 
51 
52  public:
53 
54  RandomNumberGeneratorService(ParameterSet const& pset, ActivityRegistry& activityRegistry);
56 
59 
61  virtual CLHEP::HepRandomEngine& getEngine(StreamID const& streamID) const override;
62 
64  virtual CLHEP::HepRandomEngine& getEngine(LuminosityBlockIndex const& luminosityBlockIndex) const override;
65 
66  // This returns the seed from the configuration. In the unusual case where an
67  // an engine type takes multiple seeds to initialize a sequence, this function
68  // only returns the first. As a general rule, this function should not be used,
69  // but is available for backward compatibility and debugging. It might be useful
70  // for some types of tests. Using this to seed engines constructed in modules is
71  // not recommended because (unless done very carefully) it will create duplicate
72  // sequences in different threads and/or data races. Also, if engines are created
73  // by modules the replay mechanism will be broken.
74  // Because it is dangerous and could be misused, this function might be deleted
75  // someday if we ever find time to delete all uses of it in CMSSW. There are of
76  // order 10 last time I checked ...
77  virtual std::uint32_t mySeed() const override;
78 
79  static void fillDescriptions(ConfigurationDescriptions& descriptions);
80 
82  void preallocate(SystemBounds const&);
83  void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren);
84 
85  virtual void preBeginLumi(LuminosityBlock const& lumi) override;
86  virtual void postEventRead(Event const& event) override;
87 
90  void preModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
91  void postModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
92 
93  void preModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
94  void postModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
95 
98 
99  void preModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
100  void postModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
101 
102  void preModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
104 
105  void preModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
106  void postModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
107 
108  // The next 5 functions support the mySeed function
109  // DELETE THEM when/if that function is deleted.
110  void postModuleConstruction(ModuleDescription const& description);
111  void preModuleBeginJob(ModuleDescription const& description);
112  void postModuleBeginJob(ModuleDescription const& description);
113  void preModuleEndJob(ModuleDescription const& description);
114  void postModuleEndJob(ModuleDescription const& description);
115 
117  virtual std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
118  virtual std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;
119 
121  virtual void print(std::ostream& os) const override;
122 
123  private:
124 
125  typedef std::vector<std::uint32_t> VUint32;
126 
128  public:
129  LabelAndEngine(std::string const& theLabel, VUint32 const& theSeeds, std::shared_ptr<CLHEP::HepRandomEngine> const& theEngine) :
130  label_(theLabel), seeds_(theSeeds), engine_(theEngine) { }
131  std::string const& label() const { return label_; }
132  VUint32 const& seeds() const { return seeds_; }
133  std::shared_ptr<CLHEP::HepRandomEngine> const& engine() const { return engine_; }
134  void setSeed(std::uint32_t v, unsigned int index) { seeds_.at(index) = v; }
135  private:
138  std::shared_ptr<CLHEP::HepRandomEngine> engine_;
139  };
140 
141  // This class exists because it is faster to lookup a module using
142  // the moduleID (an integer) than the label (a string). There is a
143  // one to one association between LabelAndEngine objects and ModuleIDToEngine objects.
145  public:
146  ModuleIDToEngine(LabelAndEngine* theLabelAndEngine, unsigned int theModuleID) :
147  engineState_(), labelAndEngine_(theLabelAndEngine), moduleID_(theModuleID) { }
148 
149  std::vector<unsigned long> const& engineState() const { return engineState_; }
151  unsigned int moduleID() const { return moduleID_; }
152  void setEngineState(std::vector<unsigned long> const& v) { engineState_ = v; }
153  // Used to sort so binary lookup can be used on a container of these.
154  bool operator<(ModuleIDToEngine const& r) const { return moduleID() < r.moduleID(); }
155  private:
156  std::vector<unsigned long> engineState_; // Used only for check in stream transitions
158  unsigned int moduleID_;
159  };
160 
163 
164  void preModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
165  void postModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
166 
167  void readFromLuminosityBlock(LuminosityBlock const& lumi);
168  void readFromEvent(Event const& event);
169 
170  void snapShot(std::vector<LabelAndEngine> const& engines, std::vector<RandomEngineState>& cache);
171  void restoreFromCache(std::vector<RandomEngineState> const& cache,
172  std::vector<LabelAndEngine>& engines);
173 
174  void checkEngineType(std::string const& typeFromConfig,
175  std::string const& typeFromEvent,
176  std::string const& engineLabel) const;
177 
179  StreamID const& streamID,
180  LuminosityBlockIndex const& lumiIndex);
181  void writeStates(std::vector<RandomEngineState> const& v,
182  std::ofstream& outFile);
183  void writeVector(VUint32 const& v,
184  std::ofstream& outFile);
186 
188  std::vector<RandomEngineState>& cache);
190  std::vector<RandomEngineState>& cache);
192  std::vector<RandomEngineState>& cache,
193  std::string const& whichStates);
194  bool readEngineState(std::istream& is,
195  std::vector<RandomEngineState>& cache,
196  std::string const& whichStates,
197  bool& saveToCache);
198  void readVector(std::istream& is, unsigned numItems, std::vector<std::uint32_t>& v);
199 
200  void createEnginesInVector(std::vector<LabelAndEngine>& engines,
201  unsigned int seedOffset,
202  unsigned int eventSeedOffset,
203  std::vector<ModuleIDToEngine>& moduleIDVector);
204 
205  void resetEngineSeeds(LabelAndEngine& labelAndEngine,
206  std::string const& engineName,
207  VUint32 const& seeds,
208  std::uint32_t offset1,
209  std::uint32_t offset2);
210 
211  // ---------- member data --------------------------------
212 
213  unsigned int nStreams_;
214 
215  // This exists because we can look things up faster using the moduleID
216  // than using string comparisons with the moduleLabel
217  std::vector<std::vector<ModuleIDToEngine> > streamModuleIDToEngine_; // streamID, sorted by moduleID
218  std::vector<std::vector<ModuleIDToEngine> > lumiModuleIDToEngine_; // luminosityBlockIndex, sortedByModuleID
219 
220  // Holds the engines, plus the seeds and module label also
221  std::vector<std::vector<LabelAndEngine> > streamEngines_; // streamID, sorted by label
222  std::vector<std::vector<LabelAndEngine> > lumiEngines_; // luminosityBlockIndex, sorted by label
223 
224  // These hold the input tags needed to retrieve the states
225  // of the random number engines stored in a previous process.
226  // If the label in the tag is the empty string (the default),
227  // then the service does not try to restore the random numbers.
230 
231  std::vector<std::vector<RandomEngineState> > eventCache_; // streamID, sorted by module label
232  std::vector<std::vector<RandomEngineState> > lumiCache_; // luminosityBlockIndex, sorted by module label
233 
234  // This is used to keep track of the seeds and engine name from
235  // the configuration. The map key is the module label.
236  // The module ID is filled in as modules are constructed.
237  // It is left as max unsigned if the module is never constructed and not in the process
238  class SeedsAndName {
239  public:
240  SeedsAndName(VUint32 const& theSeeds, std::string const& theEngineName) :
241  seeds_(theSeeds), engineName_(theEngineName), moduleID_(std::numeric_limits<unsigned int>::max()) { }
242  VUint32 const& seeds() const { return seeds_; }
243  std::string const& engineName() const { return engineName_; }
244  unsigned int moduleID() const { return moduleID_; }
245  void setModuleID(unsigned int v) { moduleID_ = v; }
246  private:
249  unsigned int moduleID_;
250  };
251  std::map<std::string, SeedsAndName> seedsAndNameMap_;
252 
253  // Keep the name of the file where we want to save the state
254  // of all declared engines at the end of each event. A blank
255  // name means don't bother. Also, keep a record of whether
256  // the save file name has been recorded in the job report.
258  std::atomic<bool> saveFileNameRecorded_;
259  std::vector<std::shared_ptr<std::ofstream> > outFiles_; // streamID
260 
261  // Keep the name of the file from which we restore the state
262  // of all declared engines at the beginning of a run. A
263  // blank name means there isn't one.
265 
266  // This turns on or off the checks that ensure no random
267  // numbers are generated in a module during stream
268  // beginStream, beginRun, endLuminosityBlock, or endRun.
270 
271  // In a multiprocess job this will have the index of the child process
272  // incremented by one as each child is forked
273  unsigned childIndex_;
274 
275  std::uint32_t eventSeedOffset_;
276 
277  bool verbose_;
278 
279  // The next data member supports the mySeed function
280  // DELETE IT when/if that function is deleted.
281  static thread_local std::string moduleLabel_;
282 
285  static const std::uint32_t maxSeedRanecu;
286  static const std::uint32_t maxSeedHepJames;
287  static const std::uint32_t maxSeedTRandom3;
288  };
289  }
290 }
291 #endif
void writeStates(std::vector< RandomEngineState > const &v, std::ofstream &outFile)
void preModuleStreamEndLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
RandomNumberGeneratorService const & operator=(RandomNumberGeneratorService const &)=delete
void readVector(std::istream &is, unsigned numItems, std::vector< std::uint32_t > &v)
std::shared_ptr< CLHEP::HepRandomEngine > const & engine() const
void postModuleEndJob(ModuleDescription const &description)
tuple lumi
Definition: fjr2json.py:35
void preModuleEndStream(StreamContext const &sc, ModuleCallingContext const &mcc)
void preModuleConstruction(ModuleDescription const &description)
void postModuleStreamEndRun(StreamContext const &sc, ModuleCallingContext const &mcc)
void postModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
std::vector< std::shared_ptr< std::ofstream > > outFiles_
ModuleIDToEngine(LabelAndEngine *theLabelAndEngine, unsigned int theModuleID)
void resetEngineSeeds(LabelAndEngine &labelAndEngine, std::string const &engineName, VUint32 const &seeds, std::uint32_t offset1, std::uint32_t offset2)
void preModuleBeginJob(ModuleDescription const &description)
void readFromLuminosityBlock(LuminosityBlock const &lumi)
uint16_t size_type
void preModuleBeginStream(StreamContext const &sc, ModuleCallingContext const &mcc)
void postModuleStreamBeginRun(StreamContext const &sc, ModuleCallingContext const &mcc)
void snapShot(std::vector< LabelAndEngine > const &engines, std::vector< RandomEngineState > &cache)
void postModuleEndStream(StreamContext const &sc, ModuleCallingContext const &mcc)
std::vector< std::vector< ModuleIDToEngine > > streamModuleIDToEngine_
void saveStatesToFile(std::string const &fileName, StreamID const &streamID, LuminosityBlockIndex const &lumiIndex)
RandomNumberGeneratorService(ParameterSet const &pset, ActivityRegistry &activityRegistry)
void preModuleStreamBeginLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
void createEnginesInVector(std::vector< LabelAndEngine > &engines, unsigned int seedOffset, unsigned int eventSeedOffset, std::vector< ModuleIDToEngine > &moduleIDVector)
bool readEngineState(std::istream &is, std::vector< RandomEngineState > &cache, std::string const &whichStates, bool &saveToCache)
void preModuleStreamBeginRun(StreamContext const &sc, ModuleCallingContext const &mcc)
virtual void print(std::ostream &os) const override
For debugging.
std::vector< std::vector< LabelAndEngine > > streamEngines_
std::vector< std::vector< RandomEngineState > > eventCache_
void readStatesFromFile(std::string const &fileName, std::vector< RandomEngineState > &cache, std::string const &whichStates)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
void readEventStatesFromTextFile(std::string const &fileName, std::vector< RandomEngineState > &cache)
std::vector< std::vector< RandomEngineState > > lumiCache_
void postModuleBeginJob(ModuleDescription const &description)
static const std::vector< std::uint32_t >::size_type maxStates
void preModuleStreamEndRun(StreamContext const &sc, ModuleCallingContext const &mcc)
tuple description
Definition: idDealer.py:66
virtual std::uint32_t mySeed() const override
LabelAndEngine(std::string const &theLabel, VUint32 const &theSeeds, std::shared_ptr< CLHEP::HepRandomEngine > const &theEngine)
void preModuleEndJob(ModuleDescription const &description)
void checkEngineType(std::string const &typeFromConfig, std::string const &typeFromEvent, std::string const &engineLabel) const
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &streamID) const override
Use this engine in event methods.
void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren)
void postModuleConstruction(ModuleDescription const &description)
void postModuleStreamEndLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
virtual void postEventRead(Event const &event) override
virtual std::vector< RandomEngineState > const & getLumiCache(LuminosityBlockIndex const &) const override
These two are used by the RandomEngineStateProducer.
void writeVector(VUint32 const &v, std::ofstream &outFile)
static const std::vector< std::uint32_t >::size_type maxSeeds
void readLumiStatesFromTextFile(std::string const &fileName, std::vector< RandomEngineState > &cache)
void restoreFromCache(std::vector< RandomEngineState > const &cache, std::vector< LabelAndEngine > &engines)
std::vector< std::vector< LabelAndEngine > > lumiEngines_
void postModuleBeginStream(StreamContext const &sc, ModuleCallingContext const &mcc)
std::map< std::string, SeedsAndName > seedsAndNameMap_
std::vector< std::vector< ModuleIDToEngine > > lumiModuleIDToEngine_
void preModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
SeedsAndName(VUint32 const &theSeeds, std::string const &theEngineName)
virtual void preBeginLumi(LuminosityBlock const &lumi) override
virtual std::vector< RandomEngineState > const & getEventCache(StreamID const &) const override
void postModuleStreamBeginLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
EventID const & max(EventID const &lh, EventID const &rh)
Definition: EventID.h:142