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 
131  void writeStates(std::vector<RandomEngineState> const& v,
132  std::ofstream& outFile);
133  void writeVector(VUint32 const& v,
134  std::ofstream& outFile);
136 
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  // ---------- member data --------------------------------
151 
152  // We store the engines using the corresponding module label
153  // as a key into a map
154  typedef std::map<std::string, boost::shared_ptr<CLHEP::HepRandomEngine> > EngineMap;
156 
157  // The next four help to keep track of the currently active
158  // module (its label and associated engine)
159 
160  std::vector<EngineMap::const_iterator> engineStack_;
161  EngineMap::const_iterator currentEngine_;
162 
165 
166  // This is used for beginRun, endRun, endLumi, beginJob, endJob
167  // and constructors in the check that prevents random numbers
168  // from being thrown in those methods.
169  std::vector<std::vector<unsigned long> > engineStateStack_;
170 
171  // These hold the input tags needed to retrieve the states
172  // of the random number engines stored in a previous process.
173  // If the label in the tag is the empty string (the default),
174  // then the service does not try to restore the random numbers.
177 
178  std::vector<RandomEngineState> lumiCache_;
179  std::vector<RandomEngineState> eventCache_;
180 
181  // Keeps track of the seeds used to initialize the engines.
182  // Also uses the module label as a key
183  std::map<std::string, VUint32> seedMap_;
184  std::map<std::string, std::string> engineNameMap_;
185 
186  // Keep the name of the file where we want to save the state
187  // of all declared engines at the end of each event. A blank
188  // name means don't bother. Also, keep a record of whether
189  // the save file name has been recorded in the job report.
192  std::ofstream outFile_;
193 
194  // Keep the name of the file from which we restore the state
195  // of all declared engines at the beginning of a run. A
196  // blank name means there isn't one.
198 
199  // This turns on or off the checks that ensure no random
200  // numbers are generated in a module during construction,
201  // beginJob, beginRun, endLuminosityBlock, endRun or endJob.
203 
204  // True before the first beginLumi call
206 
207  // In a multiprocess job this will have the index of the child process
208  // incremented by one as each child is forked
209  unsigned childIndex_;
210 
212 
214 
215  static uint32_t maxSeedRanecu;
216  static uint32_t maxSeedHepJames;
217  static uint32_t maxSeedTRandom3;
218  };
219  }
220 }
221 
222 #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)