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 RandomEngine_RandomNumberGeneratorService_h
2 #define RandomEngine_RandomNumberGeneratorService_h
3 
16 
17 #include "boost/shared_ptr.hpp"
18 
19 #include <map>
20 #include <vector>
21 #include <string>
22 #include <stdint.h>
23 #include <iosfwd>
24 
25 namespace CLHEP {
26  class HepRandomEngine;
27 }
28 
29 namespace edm {
30  class ParameterSet;
31  class ModuleDescription;
32  class LuminosityBlock;
33  class Event;
34  class EventSetup;
36  class ActivityRegistry;
37 
38  namespace service {
39 
41  {
42 
43  public:
44 
47 
49  virtual CLHEP::HepRandomEngine& getEngine() const;
50 
52  virtual uint32_t mySeed() const;
53 
54  static void fillDescriptions(ConfigurationDescriptions & descriptions);
55 
56  // The following functions should not be used by general users. They
57  // should only be called by code designed to work with the service while
58  // it is saving the engine state to an event or restoring it from an event
59  // and also used to keep track of which module is currently active.
60  // The first 20 are called either by the InputSource base class
61  // or via the ActivityRegistry. The next 2 are called by a dedicated
62  // producer module (RandomEngineStateProducer).
63 
64  void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren);
65 
66  virtual void preBeginLumi(LuminosityBlock const& lumi);
67  void postBeginLumi(LuminosityBlock const& lumi, EventSetup const& es);
68  virtual void postEventRead(Event const& event);
69 
70 
72  void postModuleConstruction(ModuleDescription const& description);
73 
74  void preModuleBeginJob(ModuleDescription const& description);
75  void postModuleBeginJob(ModuleDescription const& description);
76 
77  void preModuleBeginRun(ModuleDescription const& description);
78  void postModuleBeginRun(ModuleDescription const& description);
79 
80  void preModuleBeginLumi(ModuleDescription const& description);
81  void postModuleBeginLumi(ModuleDescription const& description);
82 
83  void preModule(ModuleDescription const& description);
84  void postModule(ModuleDescription const& description);
85 
86  void preModuleEndLumi(ModuleDescription const& description);
87  void postModuleEndLumi(ModuleDescription const& description);
88 
89  void preModuleEndRun(ModuleDescription const& description);
90  void postModuleEndRun(ModuleDescription const& description);
91 
92  void preModuleEndJob(ModuleDescription const& description);
93  void postModuleEndJob(ModuleDescription const& description);
94 
95  virtual std::vector<RandomEngineState> const& getLumiCache() const;
96  virtual std::vector<RandomEngineState> const& getEventCache() const;
97 
99  virtual void print();
100 
101  private:
102 
103  typedef std::vector<std::string> VString;
104  typedef std::vector<uint32_t> VUint32;
105 
107 
108  RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&); // disallow default
109 
110  // These two functions are called internally to keep track
111  // of which module is currently active
112 
113  void push(std::string const& iLabel);
114  void pop();
115 
116  void readFromLuminosityBlock(LuminosityBlock const& lumi);
117  void readFromEvent(Event const& event);
118  bool backwardCompatibilityRead(Event const& event);
119 
120  void snapShot(std::vector<RandomEngineState> & cache);
121  void restoreFromCache(std::vector<RandomEngineState> const& cache);
122 
123  void checkEngineType(std::string const& typeFromConfig,
124  std::string const& typeFromEvent,
125  std::string const& engineLabel);
126 
127  void saveStatesToFile(std::string const& fileName);
128  void writeStates(std::vector<RandomEngineState> const& v,
129  std::ofstream & outFile);
130  void writeVector(VUint32 const& v,
131  std::ofstream & outFile);
132  std::string constructSaveFileName();
133 
134  void readEventStatesFromTextFile(std::string const& fileName);
135  void readLumiStatesFromTextFile(std::string const& fileName);
136  void readStatesFromFile(std::string const& fileName,
137  std::vector<RandomEngineState> & cache,
138  std::string const& whichStates);
139  bool readEngineState(std::istream &is,
140  std::vector<RandomEngineState> & cache,
141  std::string const& whichStates,
142  bool & saveToCache);
143  void readVector(std::istream &is, unsigned numItems, std::vector<uint32_t> & v);
144 
146 
147  void oldStyleConfig(ParameterSet const& pset);
148 
149  // ---------- member data --------------------------------
150 
151  // We store the engines using the corresponding module label
152  // as a key into a map
153  typedef std::map<std::string, boost::shared_ptr<CLHEP::HepRandomEngine> > EngineMap;
155 
156  // The next four help to keep track of the currently active
157  // module (its label and associated engine)
158 
159  std::vector<EngineMap::const_iterator> engineStack_;
160  EngineMap::const_iterator currentEngine_;
161 
163  std::string currentLabel_;
164 
165  // This is used for beginRun, endRun, endLumi, beginJob, endJob
166  // and constructors in the check that prevents random numbers
167  // from being thrown in those methods.
168  std::vector<std::vector<unsigned long> > engineStateStack_;
169 
170  // This holds the module label used in a previous process
171  // to store the state of the random number engines. The
172  // empty string is used to signal that we are not trying
173  // to restore the random numbers.
174  std::string restoreStateLabel_;
175 
176  std::vector<RandomEngineState> lumiCache_;
177  std::vector<RandomEngineState> eventCache_;
178 
179  // Keeps track of the seeds used to initialize the engines.
180  // Also uses the module label as a key
181  std::map<std::string, VUint32> seedMap_;
182  std::map<std::string, std::string> engineNameMap_;
183 
184  // Keep the name of the file where we want to save the state
185  // of all declared engines at the end of each event. A blank
186  // name means don't bother. Also, keep a record of whether
187  // the save file name has been recorded in the job report.
188  std::string saveFileName_;
190 
191  // Keep the name of the file from which we restore the state
192  // of all declared engines at the beginning of a run. A
193  // blank name means there isn't one.
194  std::string restoreFileName_;
195 
196  // This turns on or off the checks that ensure no random
197  // numbers are generated in a module during construction,
198  // beginJob, beginRun, endLuminosityBlock, endRun or endJob.
200 
201  // True before the first beginLumi call
203 
204  // In a multiprocess job this will have the index of the child process
205  // incremented by one as each child is forked
206  unsigned childIndex_;
207 
209 
211 
212  static uint32_t maxSeedRanecu;
213  static uint32_t maxSeedHepJames;
214  static uint32_t maxSeedTRandom3;
215  };
216  }
217 }
218 
219 #endif
void writeStates(std::vector< RandomEngineState > const &v, std::ofstream &outFile)
void restoreFromCache(std::vector< RandomEngineState > const &cache)
void postModuleEndJob(ModuleDescription const &description)
void preModuleEndLumi(ModuleDescription const &description)
tuple lumi
Definition: fjr2json.py:41
virtual void print()
For debugging purposes only.
std::vector< std::vector< unsigned long > > engineStateStack_
void preModuleConstruction(ModuleDescription const &description)
void postModuleEndLumi(ModuleDescription const &description)
void preModule(ModuleDescription const &description)
void postBeginLumi(LuminosityBlock const &lumi, EventSetup const &es)
void postModule(ModuleDescription const &description)
void preModuleBeginJob(ModuleDescription const &description)
void readFromLuminosityBlock(LuminosityBlock const &lumi)
void readLumiStatesFromTextFile(std::string const &fileName)
std::map< std::string, std::string > engineNameMap_
void checkEngineType(std::string const &typeFromConfig, std::string const &typeFromEvent, std::string const &engineLabel)
void preModuleBeginLumi(ModuleDescription const &description)
void postModuleBeginRun(ModuleDescription const &description)
virtual CLHEP::HepRandomEngine & getEngine() const
Use this to get the random number engine, this is the only function most users should call...
RandomNumberGeneratorService(ParameterSet const &pset, ActivityRegistry &activityRegistry)
virtual std::vector< RandomEngineState > const & getEventCache() const
void postModuleEndRun(ModuleDescription const &description)
bool readEngineState(std::istream &is, std::vector< RandomEngineState > &cache, std::string const &whichStates, bool &saveToCache)
virtual uint32_t mySeed() const
Exists for backward compatibility.
void readEventStatesFromTextFile(std::string const &fileName)
virtual void preBeginLumi(LuminosityBlock const &lumi)
void readStatesFromFile(std::string const &fileName, std::vector< RandomEngineState > &cache, std::string const &whichStates)
tuple pset
Definition: CrabTask.py:85
void snapShot(std::vector< RandomEngineState > &cache)
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 postModuleBeginJob(ModuleDescription const &description)
std::vector< EngineMap::const_iterator > engineStack_
virtual std::vector< RandomEngineState > const & getLumiCache() const
void preModuleEndRun(ModuleDescription const &description)
tuple description
Definition: idDealer.py:66
void postModuleBeginLumi(ModuleDescription const &description)
void preModuleEndJob(ModuleDescription const &description)
void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren)
void readVector(std::istream &is, unsigned numItems, std::vector< uint32_t > &v)
void postModuleConstruction(ModuleDescription const &description)
RandomNumberGeneratorService const & operator=(RandomNumberGeneratorService const &)
void writeVector(VUint32 const &v, std::ofstream &outFile)
std::map< std::string, boost::shared_ptr< CLHEP::HepRandomEngine > > EngineMap
void preModuleBeginRun(ModuleDescription const &description)
mathSSE::Vec4< T > v