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
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 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&);
00106
00107 RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&);
00108
00109
00110
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
00149
00150
00151
00152 typedef std::map<std::string, boost::shared_ptr<CLHEP::HepRandomEngine> > EngineMap;
00153 EngineMap engineMap_;
00154
00155
00156
00157
00158 std::vector<EngineMap::const_iterator> engineStack_;
00159 EngineMap::const_iterator currentEngine_;
00160
00161 VString labelStack_;
00162 std::string currentLabel_;
00163
00164
00165
00166
00167 std::vector<std::vector<unsigned long> > engineStateStack_;
00168
00169
00170
00171
00172
00173 std::string restoreStateLabel_;
00174
00175 std::vector<RandomEngineState> lumiCache_;
00176 std::vector<RandomEngineState> eventCache_;
00177
00178
00179
00180 std::map<std::string, VUint32> seedMap_;
00181 std::map<std::string, std::string> engineNameMap_;
00182
00183
00184
00185
00186
00187 std::string saveFileName_;
00188 bool saveFileNameRecorded_;
00189 std::ofstream outFile_;
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