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
00057
00058
00059
00060
00061
00062
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&);
00107
00108 RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&);
00109
00110
00111
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
00150
00151
00152
00153 typedef std::map<std::string, boost::shared_ptr<CLHEP::HepRandomEngine> > EngineMap;
00154 EngineMap engineMap_;
00155
00156
00157
00158
00159 std::vector<EngineMap::const_iterator> engineStack_;
00160 EngineMap::const_iterator currentEngine_;
00161
00162 VString labelStack_;
00163 std::string currentLabel_;
00164
00165
00166
00167
00168 std::vector<std::vector<unsigned long> > engineStateStack_;
00169
00170
00171
00172
00173
00174 std::string restoreStateLabel_;
00175
00176 std::vector<RandomEngineState> lumiCache_;
00177 std::vector<RandomEngineState> eventCache_;
00178
00179
00180
00181 std::map<std::string, VUint32> seedMap_;
00182 std::map<std::string, std::string> engineNameMap_;
00183
00184
00185
00186
00187
00188 std::string saveFileName_;
00189 bool saveFileNameRecorded_;
00190
00191
00192
00193
00194 std::string restoreFileName_;
00195
00196
00197
00198
00199 bool enableChecking_;
00200
00201
00202 bool firstLumi_;
00203
00204
00205
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