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
00061
00062
00063
00064
00065
00066
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&);
00110
00111 RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&);
00112
00113
00114
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
00153
00154
00155
00156 typedef std::map<std::string, boost::shared_ptr<CLHEP::HepRandomEngine> > EngineMap;
00157 EngineMap engineMap_;
00158
00159
00160
00161
00162 std::vector<EngineMap::const_iterator> engineStack_;
00163 EngineMap::const_iterator currentEngine_;
00164
00165 VString labelStack_;
00166 std::string currentLabel_;
00167
00168
00169
00170
00171 std::vector<std::vector<unsigned long> > engineStateStack_;
00172
00173
00174
00175
00176
00177 edm::InputTag restoreStateTag_;
00178 edm::InputTag restoreStateBeginLumiTag_;
00179
00180 std::vector<RandomEngineState> lumiCache_;
00181 std::vector<RandomEngineState> eventCache_;
00182
00183
00184
00185 std::map<std::string, VUint32> seedMap_;
00186 std::map<std::string, std::string> engineNameMap_;
00187
00188
00189
00190
00191
00192 std::string saveFileName_;
00193 bool saveFileNameRecorded_;
00194 std::ofstream outFile_;
00195
00196
00197
00198
00199 std::string restoreFileName_;
00200
00201
00202
00203
00204 bool enableChecking_;
00205
00206
00207 bool firstLumi_;
00208
00209
00210
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