test
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 
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 Event;
39  class LuminosityBlock;
40  class LuminosityBlockIndex;
41  class ModuleCallingContext;
42  class ModuleDescription;
43  class ParameterSet;
44  class StreamContext;
45  class StreamID;
46 
47  namespace service {
48 
49  class SystemBounds;
50 
52 
53  public:
54 
57 
60 
62  virtual CLHEP::HepRandomEngine& getEngine(StreamID const& streamID) override;
63 
65  virtual CLHEP::HepRandomEngine& getEngine(LuminosityBlockIndex const& luminosityBlockIndex) override;
66 
67  // This returns the seed from the configuration. In the unusual case where an
68  // an engine type takes multiple seeds to initialize a sequence, this function
69  // only returns the first. As a general rule, this function should not be used,
70  // but is available for backward compatibility and debugging. It might be useful
71  // for some types of tests. Using this to seed engines constructed in modules is
72  // not recommended because (unless done very carefully) it will create duplicate
73  // sequences in different threads and/or data races. Also, if engines are created
74  // by modules the replay mechanism will be broken.
75  // Because it is dangerous and could be misused, this function might be deleted
76  // someday if we ever find time to delete all uses of it in CMSSW. There are of
77  // order 10 last time I checked ...
78  virtual std::uint32_t mySeed() const override;
79 
80  static void fillDescriptions(ConfigurationDescriptions& descriptions);
81 
83  void preallocate(SystemBounds const&);
84  void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren);
85 
86  virtual void preBeginLumi(LuminosityBlock const& lumi) override;
87  virtual void postEventRead(Event const& event) override;
88 
91  void preModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
92  void postModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
93 
94  void preModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
95  void postModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
96 
99 
100  void preModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
101  void postModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
102 
103  void preModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
105 
106  void preModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
107  void postModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
108 
110  virtual std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
111  virtual std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;
112 
114  virtual void print(std::ostream& os) const override;
115 
116  private:
117 
118  typedef std::vector<std::uint32_t> VUint32;
119 
121  public:
122  LabelAndEngine(std::string const& theLabel, VUint32 const& theSeeds, std::shared_ptr<CLHEP::HepRandomEngine> const& theEngine) :
123  label_(theLabel), seeds_(theSeeds), engine_(theEngine) { }
124  std::string const& label() const { return label_; }
125  VUint32 const& seeds() const { return seeds_; }
126  std::shared_ptr<CLHEP::HepRandomEngine const> engine() const { return get_underlying_safe(engine_); }
127  std::shared_ptr<CLHEP::HepRandomEngine>& engine() { return get_underlying_safe(engine_); }
128  void setSeed(std::uint32_t v, unsigned int index) { seeds_.at(index) = v; }
129  private:
133  };
134 
135  // This class exists because it is faster to lookup a module using
136  // the moduleID (an integer) than the label (a string). There is a
137  // one to one association between LabelAndEngine objects and ModuleIDToEngine objects.
139  public:
140  ModuleIDToEngine(LabelAndEngine* theLabelAndEngine, unsigned int theModuleID) :
141  engineState_(), labelAndEngine_(theLabelAndEngine), moduleID_(theModuleID) { }
142 
143  std::vector<unsigned long> const& engineState() const { return engineState_; }
146  unsigned int moduleID() const { return moduleID_; }
147  void setEngineState(std::vector<unsigned long> const& v) { engineState_ = v; }
148  // Used to sort so binary lookup can be used on a container of these.
149  bool operator<(ModuleIDToEngine const& r) const { return moduleID() < r.moduleID(); }
150  private:
151  std::vector<unsigned long> engineState_; // Used only for check in stream transitions
153  unsigned int moduleID_;
154  };
155 
158 
159  void preModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
160  void postModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
161 
162  void readFromLuminosityBlock(LuminosityBlock const& lumi);
163  void readFromEvent(Event const& event);
164 
165  void snapShot(std::vector<LabelAndEngine> const& engines, std::vector<RandomEngineState>& cache);
166  void restoreFromCache(std::vector<RandomEngineState> const& cache,
167  std::vector<LabelAndEngine>& engines);
168 
169  void checkEngineType(std::string const& typeFromConfig,
170  std::string const& typeFromEvent,
171  std::string const& engineLabel) const;
172 
174  StreamID const& streamID,
175  LuminosityBlockIndex const& lumiIndex);
176  void writeStates(std::vector<RandomEngineState> const& v,
177  std::ofstream& outFile);
178  void writeVector(VUint32 const& v,
179  std::ofstream& outFile);
181 
183  std::vector<RandomEngineState>& cache);
185  std::vector<RandomEngineState>& cache);
187  std::vector<RandomEngineState>& cache,
188  std::string const& whichStates);
189  bool readEngineState(std::istream& is,
190  std::vector<RandomEngineState>& cache,
191  std::string const& whichStates,
192  bool& saveToCache);
193  void readVector(std::istream& is, unsigned numItems, std::vector<std::uint32_t>& v);
194 
195  void createEnginesInVector(std::vector<LabelAndEngine>& engines,
196  unsigned int seedOffset,
197  unsigned int eventSeedOffset,
198  std::vector<ModuleIDToEngine>& moduleIDVector);
199 
200  void resetEngineSeeds(LabelAndEngine& labelAndEngine,
201  std::string const& engineName,
202  VUint32 const& seeds,
203  std::uint32_t offset1,
204  std::uint32_t offset2);
205 
206  // ---------- member data --------------------------------
207 
208  unsigned int nStreams_;
209 
210  // This exists because we can look things up faster using the moduleID
211  // than using string comparisons with the moduleLabel
212  std::vector<std::vector<ModuleIDToEngine> > streamModuleIDToEngine_; // streamID, sorted by moduleID
213  std::vector<std::vector<ModuleIDToEngine> > lumiModuleIDToEngine_; // luminosityBlockIndex, sortedByModuleID
214 
215  // Holds the engines, plus the seeds and module label also
216  std::vector<std::vector<LabelAndEngine> > streamEngines_; // streamID, sorted by label
217  std::vector<std::vector<LabelAndEngine> > lumiEngines_; // luminosityBlockIndex, sorted by label
218 
219  // These hold the input tags needed to retrieve the states
220  // of the random number engines stored in a previous process.
221  // If the label in the tag is the empty string (the default),
222  // then the service does not try to restore the random numbers.
225 
226  std::vector<std::vector<RandomEngineState> > eventCache_; // streamID, sorted by module label
227  std::vector<std::vector<RandomEngineState> > lumiCache_; // luminosityBlockIndex, sorted by module label
228 
229  // This is used to keep track of the seeds and engine name from
230  // the configuration. The map key is the module label.
231  // The module ID is filled in as modules are constructed.
232  // It is left as max unsigned if the module is never constructed and not in the process
233  class SeedsAndName {
234  public:
235  SeedsAndName(VUint32 const& theSeeds, std::string const& theEngineName) :
236  seeds_(theSeeds), engineName_(theEngineName), moduleID_(std::numeric_limits<unsigned int>::max()) { }
237  VUint32 const& seeds() const { return seeds_; }
238  std::string const& engineName() const { return engineName_; }
239  unsigned int moduleID() const { return moduleID_; }
240  void setModuleID(unsigned int v) { moduleID_ = v; }
241  private:
244  unsigned int moduleID_;
245  };
246  std::map<std::string, SeedsAndName> seedsAndNameMap_;
247 
248  // Keep the name of the file where we want to save the state
249  // of all declared engines at the end of each event. A blank
250  // name means don't bother. Also, keep a record of whether
251  // the save file name has been recorded in the job report.
253  std::atomic<bool> saveFileNameRecorded_;
254  std::vector<edm::propagate_const<std::shared_ptr<std::ofstream>>> outFiles_; // streamID
255 
256  // Keep the name of the file from which we restore the state
257  // of all declared engines at the beginning of a run. A
258  // blank name means there isn't one.
260 
261  // This turns on or off the checks that ensure no random
262  // numbers are generated in a module during stream
263  // beginStream, beginRun, endLuminosityBlock, or endRun.
265 
266  // In a multiprocess job this will have the index of the child process
267  // incremented by one as each child is forked
268  unsigned childIndex_;
269 
270  std::uint32_t eventSeedOffset_;
271 
272  bool verbose_;
273 
276  static const std::uint32_t maxSeedRanecu;
277  static const std::uint32_t maxSeedHepJames;
278  static const std::uint32_t maxSeedTRandom3;
279  };
280  }
281 }
282 #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)
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &streamID) override
Use this engine in event methods.
tuple lumi
Definition: fjr2json.py:35
std::shared_ptr< CLHEP::HepRandomEngine const > engine() const
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)
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 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)
Definition: Event.h:16
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)
std::vector< edm::propagate_const< std::shared_ptr< std::ofstream > > > outFiles_
edm::propagate_const< std::shared_ptr< CLHEP::HepRandomEngine > > engine_
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)
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
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_
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 checkEngineType(std::string const &typeFromConfig, std::string const &typeFromEvent, std::string const &engineLabel) const
void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren)
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