CMS 3D CMS Logo

RandomNumberGeneratorService.h
Go to the documentation of this file.
1 #ifndef IOMC_RandomEngine_RandomNumberGeneratorService_h
2 #define IOMC_RandomEngine_RandomNumberGeneratorService_h
3 
17 
18 #include <atomic>
19 #include <cstdint>
20 #include <fstream>
21 #include <iosfwd>
22 #include <istream>
23 #include <limits>
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 class RandomEngineState;
30 
31 namespace CLHEP {
32  class HepRandomEngine;
33 }
34 
35 namespace edm {
36  class ActivityRegistry;
38  class ConsumesCollector;
39  class Event;
40  class LuminosityBlock;
41  class LuminosityBlockIndex;
42  class ModuleCallingContext;
43  class ModuleDescription;
44  class ParameterSet;
45  class StreamContext;
46  class StreamID;
47 
48  namespace service {
49 
50  class SystemBounds;
51 
53 
54  public:
55 
58 
61 
63  virtual CLHEP::HepRandomEngine& getEngine(StreamID const& streamID) override;
64 
66  virtual CLHEP::HepRandomEngine& getEngine(LuminosityBlockIndex const& luminosityBlockIndex) override;
67 
68  // This returns the seed from the configuration. In the unusual case where an
69  // an engine type takes multiple seeds to initialize a sequence, this function
70  // only returns the first. As a general rule, this function should not be used,
71  // but is available for backward compatibility and debugging. It might be useful
72  // for some types of tests. Using this to seed engines constructed in modules is
73  // not recommended because (unless done very carefully) it will create duplicate
74  // sequences in different threads and/or data races. Also, if engines are created
75  // by modules the replay mechanism will be broken.
76  // Because it is dangerous and could be misused, this function might be deleted
77  // someday if we ever find time to delete all uses of it in CMSSW. There are of
78  // order 10 last time I checked ...
79  virtual std::uint32_t mySeed() const override;
80 
81  static void fillDescriptions(ConfigurationDescriptions& descriptions);
82 
83  void preModuleConstruction(ModuleDescription const& description);
84  void preallocate(SystemBounds const&);
85  void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren);
86 
87  virtual void preBeginLumi(LuminosityBlock const& lumi) override;
88  virtual void postEventRead(Event const& event) override;
89 
92  void preModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
93  void postModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
94 
95  void preModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
96  void postModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
97 
98  void preModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
99  void postModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
100 
101  void preModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
102  void postModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
103 
104  void preModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
105  void postModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
106 
107  void preModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
108  void postModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
109 
111  virtual std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
112  virtual std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;
113 
114  virtual void consumes(ConsumesCollector&& iC) const override;
115 
117  virtual void print(std::ostream& os) const override;
118 
119  private:
120 
121  typedef std::vector<std::uint32_t> VUint32;
122 
124  public:
125  LabelAndEngine(std::string const& theLabel, VUint32 const& theSeeds, std::shared_ptr<CLHEP::HepRandomEngine> const& theEngine) :
126  label_(theLabel), seeds_(theSeeds), engine_(theEngine) { }
127  std::string const& label() const { return label_; }
128  VUint32 const& seeds() const { return seeds_; }
129  std::shared_ptr<CLHEP::HepRandomEngine const> engine() const { return get_underlying_safe(engine_); }
130  std::shared_ptr<CLHEP::HepRandomEngine>& engine() { return get_underlying_safe(engine_); }
131  void setSeed(std::uint32_t v, unsigned int index) { seeds_.at(index) = v; }
132  private:
134  VUint32 seeds_;
136  };
137 
138  // This class exists because it is faster to lookup a module using
139  // the moduleID (an integer) than the label (a string). There is a
140  // one to one association between LabelAndEngine objects and ModuleIDToEngine objects.
142  public:
143  ModuleIDToEngine(LabelAndEngine* theLabelAndEngine, unsigned int theModuleID) :
144  engineState_(), labelAndEngine_(theLabelAndEngine), moduleID_(theModuleID) { }
145 
146  std::vector<unsigned long> const& engineState() const { return engineState_; }
147  LabelAndEngine const* labelAndEngine() const { return get_underlying_safe(labelAndEngine_); }
148  LabelAndEngine*& labelAndEngine() { return get_underlying_safe(labelAndEngine_); }
149  unsigned int moduleID() const { return moduleID_; }
150  void setEngineState(std::vector<unsigned long> const& v) { engineState_ = v; }
151  // Used to sort so binary lookup can be used on a container of these.
152  bool operator<(ModuleIDToEngine const& r) const { return moduleID() < r.moduleID(); }
153  private:
154  std::vector<unsigned long> engineState_; // Used only for check in stream transitions
156  unsigned int moduleID_;
157  };
158 
160  RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&) = delete;
161 
162  void preModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
163  void postModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
164 
165  void readFromLuminosityBlock(LuminosityBlock const& lumi);
166  void readFromEvent(Event const& event);
167 
168  void snapShot(std::vector<LabelAndEngine> const& engines, std::vector<RandomEngineState>& cache);
169  void restoreFromCache(std::vector<RandomEngineState> const& cache,
170  std::vector<LabelAndEngine>& engines);
171 
172  void checkEngineType(std::string const& typeFromConfig,
173  std::string const& typeFromEvent,
174  std::string const& engineLabel) const;
175 
176  void saveStatesToFile(std::string const& fileName,
177  StreamID const& streamID,
178  LuminosityBlockIndex const& lumiIndex);
179  void writeStates(std::vector<RandomEngineState> const& v,
180  std::ofstream& outFile);
181  void writeVector(VUint32 const& v,
182  std::ofstream& outFile);
183  std::string constructSaveFileName() const;
184 
185  void readEventStatesFromTextFile(std::string const& fileName,
186  std::vector<RandomEngineState>& cache);
187  void readLumiStatesFromTextFile(std::string const& fileName,
188  std::vector<RandomEngineState>& cache);
189  void readStatesFromFile(std::string const& fileName,
190  std::vector<RandomEngineState>& cache,
191  std::string const& whichStates);
192  bool readEngineState(std::istream& is,
193  std::vector<RandomEngineState>& cache,
194  std::string const& whichStates,
195  bool& saveToCache);
196  void readVector(std::istream& is, unsigned numItems, std::vector<std::uint32_t>& v);
197 
198  void createEnginesInVector(std::vector<LabelAndEngine>& engines,
199  unsigned int seedOffset,
200  unsigned int eventSeedOffset,
201  std::vector<ModuleIDToEngine>& moduleIDVector);
202 
203  void resetEngineSeeds(LabelAndEngine& labelAndEngine,
204  std::string const& engineName,
205  VUint32 const& seeds,
206  std::uint32_t offset1,
207  std::uint32_t offset2);
208 
209  // ---------- member data --------------------------------
210 
211  unsigned int nStreams_;
212 
213  // This exists because we can look things up faster using the moduleID
214  // than using string comparisons with the moduleLabel
215  std::vector<std::vector<ModuleIDToEngine> > streamModuleIDToEngine_; // streamID, sorted by moduleID
216  std::vector<std::vector<ModuleIDToEngine> > lumiModuleIDToEngine_; // luminosityBlockIndex, sortedByModuleID
217 
218  // Holds the engines, plus the seeds and module label also
219  std::vector<std::vector<LabelAndEngine> > streamEngines_; // streamID, sorted by label
220  std::vector<std::vector<LabelAndEngine> > lumiEngines_; // luminosityBlockIndex, sorted by label
221 
222  // These hold the input tags needed to retrieve the states
223  // of the random number engines stored in a previous process.
224  // If the label in the tag is the empty string (the default),
225  // then the service does not try to restore the random numbers.
228 
229  std::vector<std::vector<RandomEngineState> > eventCache_; // streamID, sorted by module label
230  std::vector<std::vector<RandomEngineState> > lumiCache_; // luminosityBlockIndex, sorted by module label
231 
232  // This is used to keep track of the seeds and engine name from
233  // the configuration. The map key is the module label.
234  // The module ID is filled in as modules are constructed.
235  // It is left as max unsigned if the module is never constructed and not in the process
236  class SeedsAndName {
237  public:
238  SeedsAndName(VUint32 const& theSeeds, std::string const& theEngineName) :
239  seeds_(theSeeds), engineName_(theEngineName), moduleID_(std::numeric_limits<unsigned int>::max()) { }
240  VUint32 const& seeds() const { return seeds_; }
241  std::string const& engineName() const { return engineName_; }
242  unsigned int moduleID() const { return moduleID_; }
243  void setModuleID(unsigned int v) { moduleID_ = v; }
244  private:
245  VUint32 seeds_;
247  unsigned int moduleID_;
248  };
249  std::map<std::string, SeedsAndName> seedsAndNameMap_;
250 
251  // Keep the name of the file where we want to save the state
252  // of all declared engines at the end of each event. A blank
253  // name means don't bother. Also, keep a record of whether
254  // the save file name has been recorded in the job report.
256  std::atomic<bool> saveFileNameRecorded_;
257  std::vector<edm::propagate_const<std::shared_ptr<std::ofstream>>> outFiles_; // streamID
258 
259  // Keep the name of the file from which we restore the state
260  // of all declared engines at the beginning of a run. A
261  // blank name means there isn't one.
263 
264  // This turns on or off the checks that ensure no random
265  // numbers are generated in a module during stream
266  // beginStream, beginRun, endLuminosityBlock, or endRun.
268 
269  // In a multiprocess job this will have the index of the child process
270  // incremented by one as each child is forked
271  unsigned childIndex_;
272 
273  std::uint32_t eventSeedOffset_;
274 
275  bool verbose_;
276 
279  static const std::uint32_t maxSeedRanecu;
280  static const std::uint32_t maxSeedHepJames;
281  static const std::uint32_t maxSeedTRandom3;
282  };
283  }
284 }
285 #endif
std::string print(const Track &, edm::Verbosity=edm::Concise)
Track print utility.
Definition: print.cc:10
std::shared_ptr< CLHEP::HepRandomEngine const > engine() const
ModuleIDToEngine(LabelAndEngine *theLabelAndEngine, unsigned int theModuleID)
uint16_t size_type
void writeVector(std::ostream &os, int indentation, std::vector< T > const &value_, ValueFormat format)
std::vector< std::vector< ModuleIDToEngine > > streamModuleIDToEngine_
std::vector< edm::propagate_const< std::shared_ptr< std::ofstream > > > outFiles_
edm::propagate_const< std::shared_ptr< CLHEP::HepRandomEngine > > engine_
std::vector< std::vector< LabelAndEngine > > streamEngines_
std::vector< std::vector< RandomEngineState > > eventCache_
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
std::vector< std::vector< RandomEngineState > > lumiCache_
static const std::vector< std::uint32_t >::size_type maxStates
LabelAndEngine(std::string const &theLabel, VUint32 const &theSeeds, std::shared_ptr< CLHEP::HepRandomEngine > const &theEngine)
HLT enums.
static const std::vector< std::uint32_t >::size_type maxSeeds
RandomNumberGeneratorService
Modules for shifting and smearing 4-vectors&#39; energies of Objects.
std::vector< std::vector< LabelAndEngine > > lumiEngines_
std::map< std::string, SeedsAndName > seedsAndNameMap_
std::vector< std::vector< ModuleIDToEngine > > lumiModuleIDToEngine_
SeedsAndName(VUint32 const &theSeeds, std::string const &theEngineName)
Definition: event.py:1