CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/IOMC/RandomEngine/src/RandomNumberGeneratorService.h

Go to the documentation of this file.
00001 #ifndef IOMC_RandomEngine_RandomNumberGeneratorService_h
00002 #define IOMC_RandomEngine_RandomNumberGeneratorService_h
00003 
00014 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00015 
00016 #include "boost/shared_ptr.hpp"
00017 
00018 #include <fstream>
00019 #include <map>
00020 #include <stdint.h>
00021 #include <string>
00022 #include <vector>
00023 
00024 class RandomEngineState;
00025 
00026 namespace CLHEP {
00027   class HepRandomEngine;
00028 }
00029 
00030 namespace edm {
00031   class ActivityRegistry;
00032   class ConfigurationDescriptions;
00033   class Event;
00034   class EventSetup;
00035   class LuminosityBlock;
00036   class ModuleDescription;
00037   class ParameterSet;
00038 
00039   namespace service {
00040 
00041     class RandomNumberGeneratorService : public RandomNumberGenerator {
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       void preModuleConstruction(ModuleDescription const& description);
00071       void postModuleConstruction(ModuleDescription const& description);
00072 
00073       void preModuleBeginJob(ModuleDescription const& description);
00074       void postModuleBeginJob(ModuleDescription const& description);
00075 
00076       void preModuleBeginRun(ModuleDescription const& description);
00077       void postModuleBeginRun(ModuleDescription const& description);
00078 
00079       void preModuleBeginLumi(ModuleDescription const& description);
00080       void postModuleBeginLumi(ModuleDescription const& description);
00081 
00082       void preModule(ModuleDescription const& description);
00083       void postModule(ModuleDescription const& description);
00084 
00085       void preModuleEndLumi(ModuleDescription const& description);
00086       void postModuleEndLumi(ModuleDescription const& description);
00087 
00088       void preModuleEndRun(ModuleDescription const& description);
00089       void postModuleEndRun(ModuleDescription const& description);
00090 
00091       void preModuleEndJob(ModuleDescription const& description);
00092       void postModuleEndJob(ModuleDescription const& description);
00093 
00094       virtual std::vector<RandomEngineState> const& getLumiCache() const;
00095       virtual std::vector<RandomEngineState> const& getEventCache() const;
00096 
00098       virtual void print();
00099 
00100     private:
00101 
00102       typedef std::vector<std::string> VString;
00103       typedef std::vector<uint32_t> VUint32;
00104 
00105       RandomNumberGeneratorService(RandomNumberGeneratorService const&); // disallow default
00106 
00107       RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&); // disallow default
00108 
00109       // These two functions are called internally to keep track
00110       // of which module is currently active
00111 
00112       void push(std::string const& iLabel);
00113       void pop();
00114 
00115       void readFromLuminosityBlock(LuminosityBlock const& lumi);
00116       void readFromEvent(Event const& event);
00117       bool backwardCompatibilityRead(Event const& event);
00118 
00119       void snapShot(std::vector<RandomEngineState>& cache);
00120       void restoreFromCache(std::vector<RandomEngineState> const& cache);
00121 
00122       void checkEngineType(std::string const& typeFromConfig,
00123                            std::string const& typeFromEvent,
00124                            std::string const& engineLabel);
00125 
00126       void saveStatesToFile(std::string const& fileName);
00127       void writeStates(std::vector<RandomEngineState> const& v,
00128                        std::ofstream& outFile);
00129       void writeVector(VUint32 const& v,
00130                        std::ofstream& outFile);
00131       std::string constructSaveFileName();
00132 
00133       void readEventStatesFromTextFile(std::string const& fileName);
00134       void readLumiStatesFromTextFile(std::string const& fileName);
00135       void readStatesFromFile(std::string const& fileName,
00136                               std::vector<RandomEngineState>& cache,
00137                               std::string const& whichStates);
00138       bool readEngineState(std::istream& is,
00139                            std::vector<RandomEngineState>& cache,
00140                            std::string const& whichStates,
00141                            bool& saveToCache);
00142       void readVector(std::istream& is, unsigned numItems, std::vector<uint32_t>& v);
00143 
00144       void startNewSequencesForEvents();
00145 
00146       void oldStyleConfig(ParameterSet const& pset);
00147 
00148       // ---------- member data --------------------------------
00149 
00150       // We store the engines using the corresponding module label
00151       // as a key into a map
00152       typedef std::map<std::string, boost::shared_ptr<CLHEP::HepRandomEngine> > EngineMap;
00153       EngineMap engineMap_;
00154 
00155       // The next four help to keep track of the currently active
00156       // module (its label and associated engine)
00157 
00158       std::vector<EngineMap::const_iterator> engineStack_;
00159       EngineMap::const_iterator currentEngine_;
00160 
00161       VString labelStack_;
00162       std::string currentLabel_;
00163 
00164       // This is used for beginRun, endRun, endLumi, beginJob, endJob
00165       // and constructors in the check that prevents random numbers
00166       // from being thrown in those methods.
00167       std::vector<std::vector<unsigned long> > engineStateStack_;
00168 
00169       // This holds the module label used in a previous process
00170       // to store the state of the random number engines.  The
00171       // empty string is used to signal that we are not trying
00172       // to restore the random numbers.
00173       std::string restoreStateLabel_;
00174 
00175       std::vector<RandomEngineState> lumiCache_;
00176       std::vector<RandomEngineState> eventCache_;
00177 
00178       // Keeps track of the seeds used to initialize the engines.
00179       // Also uses the module label as a key
00180       std::map<std::string, VUint32> seedMap_;
00181       std::map<std::string, std::string> engineNameMap_;
00182 
00183       // Keep the name of the file where we want to save the state
00184       // of all declared engines at the end of each event. A blank
00185       // name means don't bother.  Also, keep a record of whether
00186       // the save file name has been recorded in the job report.
00187       std::string saveFileName_;
00188       bool saveFileNameRecorded_;
00189       std::ofstream outFile_;
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