CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/IOMC/RandomEngine/src/RandomNumberGeneratorService.h

Go to the documentation of this file.
00001 #ifndef RandomEngine_RandomNumberGeneratorService_h
00002 #define RandomEngine_RandomNumberGeneratorService_h
00003 
00014 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00015 #include "SimDataFormats/RandomEngine/interface/RandomEngineState.h"
00016 
00017 #include "boost/shared_ptr.hpp"
00018 
00019 #include <map>
00020 #include <vector>
00021 #include <string>
00022 #include <stdint.h>
00023 #include <iosfwd>
00024 
00025 namespace CLHEP {
00026   class HepRandomEngine;
00027 }
00028 
00029 namespace edm {
00030   class ParameterSet;
00031   class ModuleDescription;
00032   class LuminosityBlock;
00033   class Event;
00034   class EventSetup;
00035   class ConfigurationDescriptions;
00036   class ActivityRegistry;
00037 
00038   namespace service {
00039 
00040     class RandomNumberGeneratorService : public RandomNumberGenerator
00041     {      
00042 
00043     public:
00044 
00045       RandomNumberGeneratorService(ParameterSet const& pset, ActivityRegistry& activityRegistry);
00046       virtual ~RandomNumberGeneratorService();
00047 
00049       virtual CLHEP::HepRandomEngine& getEngine() const;    
00050 
00052       virtual uint32_t mySeed() const;
00053 
00054       static void fillDescriptions(ConfigurationDescriptions & descriptions);
00055 
00056       // The following functions should not be used by general users.  They
00057       // should only be called by code designed to work with the service while
00058       // it is saving the engine state to an event or restoring it from an event
00059       // and also used to keep track of which module is currently active.
00060       // The first 20 are called either by the InputSource base class
00061       // or via the ActivityRegistry.  The next 2 are called by a dedicated
00062       // producer module (RandomEngineStateProducer).
00063 
00064       void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren);
00065 
00066       virtual void preBeginLumi(LuminosityBlock const& lumi);
00067       void postBeginLumi(LuminosityBlock const& lumi, EventSetup const& es);
00068       virtual void postEventRead(Event const& event);
00069 
00070 
00071       void preModuleConstruction(ModuleDescription const& description);
00072       void postModuleConstruction(ModuleDescription const& description);
00073 
00074       void preModuleBeginJob(ModuleDescription const& description);
00075       void postModuleBeginJob(ModuleDescription const& description);
00076 
00077       void preModuleBeginRun(ModuleDescription const& description);
00078       void postModuleBeginRun(ModuleDescription const& description);
00079 
00080       void preModuleBeginLumi(ModuleDescription const& description);
00081       void postModuleBeginLumi(ModuleDescription const& description);
00082 
00083       void preModule(ModuleDescription const& description);
00084       void postModule(ModuleDescription const& description);
00085 
00086       void preModuleEndLumi(ModuleDescription const& description);
00087       void postModuleEndLumi(ModuleDescription const& description);
00088 
00089       void preModuleEndRun(ModuleDescription const& description);
00090       void postModuleEndRun(ModuleDescription const& description);
00091 
00092       void preModuleEndJob(ModuleDescription const& description);
00093       void postModuleEndJob(ModuleDescription const& description);
00094 
00095       virtual std::vector<RandomEngineState> const& getLumiCache() const;
00096       virtual std::vector<RandomEngineState> const& getEventCache() const;
00097 
00099       virtual void print();
00100 
00101     private:
00102 
00103       typedef std::vector<std::string> VString;
00104       typedef std::vector<uint32_t> VUint32;
00105 
00106       RandomNumberGeneratorService(RandomNumberGeneratorService const&); // disallow default
00107 
00108       RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&); // disallow default
00109 
00110       // These two functions are called internally to keep track
00111       // of which module is currently active
00112 
00113       void push(std::string const& iLabel);
00114       void pop();
00115 
00116       void readFromLuminosityBlock(LuminosityBlock const& lumi);
00117       void readFromEvent(Event const& event);
00118       bool backwardCompatibilityRead(Event const& event);
00119 
00120       void snapShot(std::vector<RandomEngineState> & cache);
00121       void restoreFromCache(std::vector<RandomEngineState> const& cache);
00122 
00123       void checkEngineType(std::string const& typeFromConfig,
00124                            std::string const& typeFromEvent,
00125                            std::string const& engineLabel);
00126 
00127       void saveStatesToFile(std::string const& fileName);
00128       void writeStates(std::vector<RandomEngineState> const& v,
00129                        std::ofstream & outFile);
00130       void writeVector(VUint32 const& v,
00131                        std::ofstream & outFile);
00132       std::string constructSaveFileName();
00133 
00134       void readEventStatesFromTextFile(std::string const& fileName);
00135       void readLumiStatesFromTextFile(std::string const& fileName);
00136       void readStatesFromFile(std::string const& fileName,
00137                               std::vector<RandomEngineState> & cache,
00138                               std::string const& whichStates);
00139       bool readEngineState(std::istream &is,
00140                            std::vector<RandomEngineState> & cache,
00141                            std::string const& whichStates, 
00142                            bool & saveToCache);
00143       void readVector(std::istream &is, unsigned numItems, std::vector<uint32_t> & v);
00144 
00145       void startNewSequencesForEvents();
00146 
00147       void oldStyleConfig(ParameterSet const& pset);
00148 
00149       // ---------- member data --------------------------------
00150 
00151       // We store the engines using the corresponding module label
00152       // as a key into a map
00153       typedef std::map<std::string, boost::shared_ptr<CLHEP::HepRandomEngine> > EngineMap;
00154       EngineMap engineMap_;
00155 
00156       // The next four help to keep track of the currently active
00157       // module (its label and associated engine)
00158 
00159       std::vector<EngineMap::const_iterator> engineStack_;
00160       EngineMap::const_iterator currentEngine_;
00161 
00162       VString labelStack_;
00163       std::string currentLabel_;
00164 
00165       // This is used for beginRun, endRun, endLumi, beginJob, endJob
00166       // and constructors in the check that prevents random numbers
00167       // from being thrown in those methods.
00168       std::vector<std::vector<unsigned long> > engineStateStack_;
00169 
00170       // This holds the module label used in a previous process
00171       // to store the state of the random number engines.  The
00172       // empty string is used to signal that we are not trying
00173       // to restore the random numbers.
00174       std::string restoreStateLabel_;
00175 
00176       std::vector<RandomEngineState> lumiCache_;
00177       std::vector<RandomEngineState> eventCache_;
00178 
00179       // Keeps track of the seeds used to initialize the engines.
00180       // Also uses the module label as a key
00181       std::map<std::string, VUint32> seedMap_;
00182       std::map<std::string, std::string> engineNameMap_;
00183 
00184       // Keep the name of the file where we want to save the state
00185       // of all declared engines at the end of each event. A blank
00186       // name means don't bother.  Also, keep a record of whether
00187       // the save file name has been recorded in the job report.
00188       std::string saveFileName_;
00189       bool saveFileNameRecorded_;
00190 
00191       // Keep the name of the file from which we restore the state
00192       // of all declared engines at the beginning of a run. A
00193       // blank name means there isn't one.
00194       std::string restoreFileName_;
00195 
00196       // This turns on or off the checks that ensure no random
00197       // numbers are generated in a module during construction,
00198       // beginJob, beginRun, endLuminosityBlock, endRun or endJob.
00199       bool enableChecking_;
00200 
00201       // True before the first beginLumi call
00202       bool firstLumi_;
00203 
00204       // In a multiprocess job this will have the index of the child process
00205       // incremented by one as each child is forked
00206       unsigned childIndex_;
00207 
00208       uint32_t eventSeedOffset_;
00209 
00210       bool failedToFindStatesInLumi_;
00211 
00212       static uint32_t maxSeedRanecu;
00213       static uint32_t maxSeedHepJames;
00214       static uint32_t maxSeedTRandom3;
00215     };
00216   }
00217 }
00218 
00219 #endif