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 
57  ~RandomNumberGeneratorService() override;
58 
61 
63  CLHEP::HepRandomEngine& getEngine(StreamID const& streamID) override;
64 
66  CLHEP::HepRandomEngine& getEngine(LuminosityBlockIndex const& luminosityBlockIndex) override;
67 
68  std::unique_ptr<CLHEP::HepRandomEngine> cloneEngine(LuminosityBlockIndex const&) override;
69 
70  // This returns the seed from the configuration. In the unusual case where an
71  // an engine type takes multiple seeds to initialize a sequence, this function
72  // only returns the first. As a general rule, this function should not be used,
73  // but is available for backward compatibility and debugging. It might be useful
74  // for some types of tests. Using this to seed engines constructed in modules is
75  // not recommended because (unless done very carefully) it will create duplicate
76  // sequences in different threads and/or data races. Also, if engines are created
77  // by modules the replay mechanism will be broken.
78  // Because it is dangerous and could be misused, this function might be deleted
79  // someday if we ever find time to delete all uses of it in CMSSW. There are of
80  // order 10 last time I checked ...
81  std::uint32_t mySeed() const override;
82 
83  static void fillDescriptions(ConfigurationDescriptions& descriptions);
84 
85  void preModuleConstruction(ModuleDescription const& description);
86  void preallocate(SystemBounds const&);
87 
88  void preBeginLumi(LuminosityBlock const& lumi) override;
89  void postEventRead(Event const& event) override;
90  void setLumiCache(LuminosityBlockIndex, std::vector<RandomEngineState> const& iStates) override;
91  void setEventCache(StreamID, std::vector<RandomEngineState> const& iStates) override;
92 
95  void preModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
96  void postModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
97 
98  void preModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
99  void postModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
100 
101  void preModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
102  void postModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
103 
104  void preModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
105  void postModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
106 
107  void preModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
108  void postModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
109 
110  void preModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
111  void postModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
112 
114  std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
115  std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;
116 
117  void consumes(ConsumesCollector&& iC) const override;
118 
120  void print(std::ostream& os) const override;
121 
122  private:
123 
124  typedef std::vector<std::uint32_t> VUint32;
125 
127  public:
128  LabelAndEngine(std::string const& theLabel, VUint32 const& theSeeds, std::shared_ptr<CLHEP::HepRandomEngine> const& theEngine) :
129  label_(theLabel), seeds_(theSeeds), engine_(theEngine) { }
130  std::string const& label() const { return label_; }
131  VUint32 const& seeds() const { return seeds_; }
132  std::shared_ptr<CLHEP::HepRandomEngine const> engine() const { return get_underlying_safe(engine_); }
133  std::shared_ptr<CLHEP::HepRandomEngine>& engine() { return get_underlying_safe(engine_); }
134  void setSeed(std::uint32_t v, unsigned int index) { seeds_.at(index) = v; }
135  private:
137  VUint32 seeds_;
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_; }
150  LabelAndEngine const* labelAndEngine() const { return get_underlying_safe(labelAndEngine_); }
151  LabelAndEngine*& labelAndEngine() { return get_underlying_safe(labelAndEngine_); }
152  unsigned int moduleID() const { return moduleID_; }
153  void setEngineState(std::vector<unsigned long> const& v) { engineState_ = v; }
154  // Used to sort so binary lookup can be used on a container of these.
155  bool operator<(ModuleIDToEngine const& r) const { return moduleID() < r.moduleID(); }
156  private:
157  std::vector<unsigned long> engineState_; // Used only for check in stream transitions
159  unsigned int moduleID_;
160  };
161 
163  RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&) = delete;
164 
165  void preModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
166  void postModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
167 
168  void readFromLuminosityBlock(LuminosityBlock const& lumi);
169  void readFromEvent(Event const& event);
170 
171  void snapShot(std::vector<LabelAndEngine> const& engines, std::vector<RandomEngineState>& cache);
172  void restoreFromCache(std::vector<RandomEngineState> const& cache,
173  std::vector<LabelAndEngine>& engines);
174 
175  void checkEngineType(std::string const& typeFromConfig,
176  std::string const& typeFromEvent,
177  std::string const& engineLabel) const;
178 
179  void saveStatesToFile(std::string const& fileName,
180  StreamID const& streamID,
181  LuminosityBlockIndex const& lumiIndex);
182  void writeStates(std::vector<RandomEngineState> const& v,
183  std::ofstream& outFile);
184  void writeVector(VUint32 const& v,
185  std::ofstream& outFile);
186  std::string constructSaveFileName() const;
187 
188  void readEventStatesFromTextFile(std::string const& fileName,
189  std::vector<RandomEngineState>& cache);
190  void readLumiStatesFromTextFile(std::string const& fileName,
191  std::vector<RandomEngineState>& cache);
192  void readStatesFromFile(std::string const& fileName,
193  std::vector<RandomEngineState>& cache,
194  std::string const& whichStates);
195  bool readEngineState(std::istream& is,
196  std::vector<RandomEngineState>& cache,
197  std::string const& whichStates,
198  bool& saveToCache);
199  void readVector(std::istream& is, unsigned numItems, std::vector<std::uint32_t>& v);
200 
201  void createEnginesInVector(std::vector<LabelAndEngine>& engines,
202  unsigned int seedOffset,
203  unsigned int eventSeedOffset,
204  std::vector<ModuleIDToEngine>& moduleIDVector);
205 
206  void resetEngineSeeds(LabelAndEngine& labelAndEngine,
207  std::string const& engineName,
208  VUint32 const& seeds,
209  std::uint32_t offset1,
210  std::uint32_t offset2);
211 
212  // ---------- member data --------------------------------
213 
214  unsigned int nStreams_;
215 
216  // This exists because we can look things up faster using the moduleID
217  // than using string comparisons with the moduleLabel
218  std::vector<std::vector<ModuleIDToEngine> > streamModuleIDToEngine_; // streamID, sorted by moduleID
219  std::vector<std::vector<ModuleIDToEngine> > lumiModuleIDToEngine_; // luminosityBlockIndex, sortedByModuleID
220 
221  // Holds the engines, plus the seeds and module label also
222  std::vector<std::vector<LabelAndEngine> > streamEngines_; // streamID, sorted by label
223  std::vector<std::vector<LabelAndEngine> > lumiEngines_; // luminosityBlockIndex, sorted by label
224 
225  // These hold the input tags needed to retrieve the states
226  // of the random number engines stored in a previous process.
227  // If the label in the tag is the empty string (the default),
228  // then the service does not try to restore the random numbers.
231 
232  std::vector<std::vector<RandomEngineState> > eventCache_; // streamID, sorted by module label
233  std::vector<std::vector<RandomEngineState> > lumiCache_; // luminosityBlockIndex, sorted by module label
234 
235  // This is used to keep track of the seeds and engine name from
236  // the configuration. The map key is the module label.
237  // The module ID is filled in as modules are constructed.
238  // It is left as max unsigned if the module is never constructed and not in the process
239  class SeedsAndName {
240  public:
241  SeedsAndName(VUint32 const& theSeeds, std::string const& theEngineName) :
242  seeds_(theSeeds), engineName_(theEngineName), moduleID_(std::numeric_limits<unsigned int>::max()) { }
243  VUint32 const& seeds() const { return seeds_; }
244  std::string const& engineName() const { return engineName_; }
245  unsigned int moduleID() const { return moduleID_; }
246  void setModuleID(unsigned int v) { moduleID_ = v; }
247  private:
248  VUint32 seeds_;
250  unsigned int moduleID_;
251  };
252  std::map<std::string, SeedsAndName> seedsAndNameMap_;
253 
254  // Keep the name of the file where we want to save the state
255  // of all declared engines at the end of each event. A blank
256  // name means don't bother. Also, keep a record of whether
257  // the save file name has been recorded in the job report.
259  std::atomic<bool> saveFileNameRecorded_;
260  std::vector<edm::propagate_const<std::shared_ptr<std::ofstream>>> outFiles_; // streamID
261 
262  // Keep the name of the file from which we restore the state
263  // of all declared engines at the beginning of a run. A
264  // blank name means there isn't one.
266 
267  // This turns on or off the checks that ensure no random
268  // numbers are generated in a module during stream
269  // beginStream, beginRun, endLuminosityBlock, or endRun.
271 
272  std::uint32_t eventSeedOffset_;
273 
274  bool verbose_;
275 
278  static const std::uint32_t maxSeedRanecu;
279  static const std::uint32_t maxSeedHepJames;
280  static const std::uint32_t maxSeedTRandom3;
281  };
282  }
283 }
284 #endif
std::shared_ptr< CLHEP::HepRandomEngine const > engine() const
ModuleIDToEngine(LabelAndEngine *theLabelAndEngine, unsigned int theModuleID)
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:66
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
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
LabelAndEngine(std::string const &theLabel, VUint32 const &theSeeds, std::shared_ptr< CLHEP::HepRandomEngine > const &theEngine)
HLT enums.
def cache(function)
Definition: utilities.py:3
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