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 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 
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 
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 
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);
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:
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_; }
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 
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 
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);
184 
186  std::vector<RandomEngineState>& cache);
188  std::vector<RandomEngineState>& cache);
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:
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
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_
tuple engineName
Definition: IOMC_cff.py:175
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)
virtual void consumes(ConsumesCollector &&iC) const override
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