CMS 3D CMS Logo

RandomNumberGeneratorService.h

Go to the documentation of this file.
00001 #ifndef RandomEngine_RandomNumberGeneratorService_h
00002 #define RandomEngine_RandomNumberGeneratorService_h
00003 // -*- C++ -*-
00004 //
00005 // Package:     RandomEngine
00006 // Class  :     RandomNumberGeneratorService
00007 // 
00015 //
00016 // Original Authors:  Chris Jones, W. David Dagenhart
00017 //   Created:  Tue Mar  7 09:43:43 EST 2006 (originally in FWCore/Services)
00018 // $Id: RandomNumberGeneratorService.h,v 1.9 2008/05/08 16:12:36 marafino Exp $
00019 //
00020 
00021 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
00022 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00023 
00024 #include <map>
00025 
00026 namespace edm {
00027   class ParameterSet;
00028   class ModuleDescription;
00029   class EventID;
00030   class Timestamp;
00031   class Event;
00032   class EventSetup;
00033   
00034   namespace service {
00035 
00036     class RandomNumberGeneratorService : public RandomNumberGenerator
00037     {
00038       
00039     public:
00040 
00041       RandomNumberGeneratorService(const ParameterSet& iPSet, ActivityRegistry& iRegistry);
00042       virtual ~RandomNumberGeneratorService();
00043 
00044       virtual CLHEP::HepRandomEngine& getEngine() const;    
00045 
00046       virtual uint32_t mySeed() const;
00047 
00048       // The following functions should not be used by general users.  They
00049       // should only be called by code designed to work with the service while
00050       // it is saving the engine state to an event or restoring it from an event
00051       // and also used to keep track of which module is currently active.
00052       // The first 10 functions are called at various points during he main
00053       // processing loop.  The next 3 are called by a dedicated producer
00054       // module (RandomEngineStateProducer).  The other two by the InputSource
00055       // base class.
00056 
00057       void preModuleConstruction(const ModuleDescription& iDesc);
00058       void postModuleConstruction(const ModuleDescription& iDesc);
00059 
00060       void preSourceConstruction(const ModuleDescription& iDesc);
00061       void postSourceConstruction(const ModuleDescription& iDesc);
00062 
00063       void postBeginJob();
00064       void postEndJob();
00065 
00066       void preEventProcessing(const edm::EventID&, const edm::Timestamp&);
00067       void postEventProcessing(const Event&, const EventSetup&);
00068 
00069       void preModule(const ModuleDescription& iDesc);
00070       void postModule(const ModuleDescription& iDesc);
00071 
00072       void preModuleBeginJob(const ModuleDescription& iDesc);
00073       void postModuleBeginJob(const ModuleDescription& iDesc);
00074 
00075       void preModuleEndJob(const ModuleDescription& iDesc);
00076       void postModuleEndJob(const ModuleDescription& iDesc);
00077 
00078       virtual const std::vector<std::string>& getCachedLabels() const;
00079       virtual const std::vector<std::vector<uint32_t> >& getCachedStates() const;
00080       virtual const std::vector<std::vector<uint32_t> >& getCachedSeeds() const;
00081 
00082       virtual void snapShot();
00083       virtual void restoreState(const Event& event);
00084 
00085       // For debugging purposes only
00086       virtual void print();
00087 
00088       void saveEngineState(const std::string& fileName );
00089 
00090       void restoreEngineState(const std::string& fileName);
00091 
00092     private:
00093 
00094       RandomNumberGeneratorService(const RandomNumberGeneratorService&); // disallow default
00095       
00096       const RandomNumberGeneratorService& operator=(const RandomNumberGeneratorService&); // disallow default
00097       
00098       // These two functions are called internally to keep track
00099       // of which module is currently active
00100 
00101       void push(const std::string& iLabel);
00102       void pop();
00103 
00104       void checkEngineType(const std::string& typeFromConfig,
00105                            const std::string& typeFromEvent,
00106                            const std::string& engineLabel);
00107 
00108       void dumpVector(const std::vector<uint32_t> &v);
00109 
00110       void stashVector(const std::vector<unsigned long> &v, std::ostream &os);
00111 
00112       std::vector<unsigned long> restoreVector(std::istream &is, const int32_t n);
00113 
00114       bool processStanza(std::istream &is);
00115 
00116       bool isEngineNameValid(std::string const &name);
00117 
00118       int32_t expectedSeedCount(std::string const &name);
00119 
00120       std::string constructSaveFileName();
00121 
00122       void oldStyleConfig(const ParameterSet& iPSet);
00123 
00124       // ---------- member data --------------------------------
00125 
00126       // We store the engines using the corresponding module label
00127       // as a key into a map
00128       typedef std::map<std::string, CLHEP::HepRandomEngine*> EngineMap;
00129       EngineMap engineMap_;
00130 
00131       // The next four help to keep track of the currently active
00132       // module (its label and associated engine)
00133 
00134       std::vector<EngineMap::const_iterator> engineStack_;
00135       EngineMap::const_iterator currentEngine_;
00136 
00137       std::vector<std::string> labelStack_;
00138       std::string currentLabel_;
00139 
00140       // This holds the module label used in a previous process
00141       // to store the state of the random number engines.  The
00142       // empty string is used to signal that we are not trying
00143       // to restore the random numbers.
00144       std::string restoreStateLabel_;
00145 
00146       // The state of the engines is cached at the beginning the
00147       // processing loop for each event.  The producer module
00148       // gets called later and writes these cached vectors into
00149       // the event.
00150       std::vector<std::string> cachedLabels_;
00151       std::vector<std::vector<uint32_t> > cachedStates_;
00152       std::vector<std::vector<uint32_t> > cachedSeeds_;
00153 
00154       // Keeps track of the seeds used to initialize the engines.
00155       // Also uses the module label as a key
00156       std::map<std::string, std::vector<uint32_t> > seedMap_;
00157 
00158       // Keep the name of the file where we want to save the state
00159       // of all declared engines at the end of each event. A blank
00160       // name means don't bother.  Also, keep a record of whether
00161       // the save file name has been recorded in the job report.
00162       std::string saveFileName_;
00163       bool saveFileNameRecorded_;
00164 
00165       // Keep the name of the file from which we restore the state
00166       // of all declared engines at the befinnig of a run. A
00167       // blank name means there isn't one.
00168       std::string restoreFileName_;
00169 
00170      // Remember if we are dealing with a new-style or old-style
00171      // configuration file.
00172      bool oldStyle_;
00173     };
00174   }
00175 }
00176 
00177 #endif

Generated on Tue Jun 9 17:39:08 2009 for CMSSW by  doxygen 1.5.4