CMS 3D CMS Logo

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