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 
16 
17 #include "boost/shared_ptr.hpp"
18 
19 #include <fstream>
20 #include <map>
21 #include <stdint.h>
22 #include <string>
23 #include <vector>
24 
25 class RandomEngineState;
26 
27 namespace CLHEP {
28  class HepRandomEngine;
29 }
30 
31 namespace edm {
32  class ActivityRegistry;
34  class Event;
35  class EventSetup;
36  class LuminosityBlock;
37  class ModuleDescription;
38  class ParameterSet;
39 
40  namespace service {
41 
43 
44  public:
45  static size_t const maxSeeds = 65536U;
46 
47  static size_t const maxStates = 65536U;
48 
49  RandomNumberGeneratorService(ParameterSet const& pset, ActivityRegistry& activityRegistry);
51 
53  virtual CLHEP::HepRandomEngine& getEngine() const;
54 
56  virtual uint32_t mySeed() const;
57 
58  static void fillDescriptions(ConfigurationDescriptions& descriptions);
59 
60  // The following functions should not be used by general users. They
61  // should only be called by code designed to work with the service while
62  // it is saving the engine state to an event or restoring it from an event
63  // and also used to keep track of which module is currently active.
64  // The first 20 are called either by the InputSource base class
65  // or via the ActivityRegistry. The next 2 are called by a dedicated
66  // producer module (RandomEngineStateProducer).
67 
68  void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren);
69 
70  virtual void preBeginLumi(LuminosityBlock const& lumi);
71  void postBeginLumi(LuminosityBlock const& lumi, EventSetup const& es);
72  virtual void postEventRead(Event const& event);
73 
76 
79 
82 
85 
88 
91 
94 
97 
98  virtual std::vector<RandomEngineState> const& getLumiCache() const;
99  virtual std::vector<RandomEngineState> const& getEventCache() const;
100 
102  virtual void print();
103 
104  private:
105 
106  typedef std::vector<std::string> VString;
107  typedef std::vector<uint32_t> VUint32;
108 
110 
111  RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&); // disallow default
112 
113  // These two functions are called internally to keep track
114  // of which module is currently active
115 
116  void push(std::string const& iLabel);
117  void pop();
118 
120  void readFromEvent(Event const& event);
122 
123  void snapShot(std::vector<RandomEngineState>& cache);
124  void restoreFromCache(std::vector<RandomEngineState> const& cache);
125 
126  void checkEngineType(std::string const& typeFromConfig,
127  std::string const& typeFromEvent,
128  std::string const& engineLabel);
129 
130  void saveStatesToFile(std::string const& fileName);
131  void writeStates(std::vector<RandomEngineState> const& v,
132  std::ofstream& outFile);
133  void writeVector(VUint32 const& v,
134  std::ofstream& outFile);
135  std::string constructSaveFileName();
136 
137  void readEventStatesFromTextFile(std::string const& fileName);
138  void readLumiStatesFromTextFile(std::string const& fileName);
139  void readStatesFromFile(std::string const& fileName,
140  std::vector<RandomEngineState>& cache,
141  std::string const& whichStates);
142  bool readEngineState(std::istream& is,
143  std::vector<RandomEngineState>& cache,
144  std::string const& whichStates,
145  bool& saveToCache);
146  void readVector(std::istream& is, unsigned numItems, std::vector<uint32_t>& v);
147 
149 
150  void oldStyleConfig(ParameterSet const& pset);
151 
152  // ---------- member data --------------------------------
153 
154  // We store the engines using the corresponding module label
155  // as a key into a map
156  typedef std::map<std::string, boost::shared_ptr<CLHEP::HepRandomEngine> > EngineMap;
158 
159  // The next four help to keep track of the currently active
160  // module (its label and associated engine)
161 
162  std::vector<EngineMap::const_iterator> engineStack_;
163  EngineMap::const_iterator currentEngine_;
164 
166  std::string currentLabel_;
167 
168  // This is used for beginRun, endRun, endLumi, beginJob, endJob
169  // and constructors in the check that prevents random numbers
170  // from being thrown in those methods.
171  std::vector<std::vector<unsigned long> > engineStateStack_;
172 
173  // These hold the input tags needed to retrieve the states
174  // of the random number engines stored in a previous process.
175  // If the label in the tag is the empty string (the default),
176  // then the service does not try to restore the random numbers.
179 
180  std::vector<RandomEngineState> lumiCache_;
181  std::vector<RandomEngineState> eventCache_;
182 
183  // Keeps track of the seeds used to initialize the engines.
184  // Also uses the module label as a key
185  std::map<std::string, VUint32> seedMap_;
186  std::map<std::string, std::string> engineNameMap_;
187 
188  // Keep the name of the file where we want to save the state
189  // of all declared engines at the end of each event. A blank
190  // name means don't bother. Also, keep a record of whether
191  // the save file name has been recorded in the job report.
192  std::string saveFileName_;
194  std::ofstream outFile_;
195 
196  // Keep the name of the file from which we restore the state
197  // of all declared engines at the beginning of a run. A
198  // blank name means there isn't one.
199  std::string restoreFileName_;
200 
201  // This turns on or off the checks that ensure no random
202  // numbers are generated in a module during construction,
203  // beginJob, beginRun, endLuminosityBlock, endRun or endJob.
205 
206  // True before the first beginLumi call
208 
209  // In a multiprocess job this will have the index of the child process
210  // incremented by one as each child is forked
211  unsigned childIndex_;
212 
214 
216 
217  static uint32_t maxSeedRanecu;
218  static uint32_t maxSeedHepJames;
219  static uint32_t maxSeedTRandom3;
220  };
221  }
222 }
223 
224 #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:35
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)
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