File indexing completed on 2024-04-06 12:19:04
0001 #ifndef IOMC_RandomEngine_RandomNumberGeneratorService_h
0002 #define IOMC_RandomEngine_RandomNumberGeneratorService_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
0015 #include "FWCore/Utilities/interface/InputTag.h"
0016 #include "FWCore/Utilities/interface/get_underlying_safe.h"
0017
0018 #include <atomic>
0019 #include <cstdint>
0020 #include <fstream>
0021 #include <iosfwd>
0022 #include <istream>
0023 #include <limits>
0024 #include <map>
0025 #include <memory>
0026 #include <string>
0027 #include <vector>
0028
0029 class RandomEngineState;
0030
0031 namespace CLHEP {
0032 class HepRandomEngine;
0033 }
0034
0035 namespace edm {
0036 class ActivityRegistry;
0037 class ConfigurationDescriptions;
0038 class ConsumesCollector;
0039 class Event;
0040 class LuminosityBlock;
0041 class LuminosityBlockIndex;
0042 class ModuleCallingContext;
0043 class ModuleDescription;
0044 class ParameterSet;
0045 class StreamContext;
0046 class StreamID;
0047
0048 namespace service {
0049
0050 class SystemBounds;
0051
0052 class RandomNumberGeneratorService : public RandomNumberGenerator {
0053 public:
0054 RandomNumberGeneratorService(ParameterSet const& pset, ActivityRegistry& activityRegistry);
0055 ~RandomNumberGeneratorService() override;
0056
0057 RandomNumberGeneratorService(RandomNumberGeneratorService const&) = delete;
0058 RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&) = delete;
0059
0060
0061
0062
0063
0064 CLHEP::HepRandomEngine& getEngine(StreamID const& streamID) override;
0065
0066
0067 CLHEP::HepRandomEngine& getEngine(LuminosityBlockIndex const& luminosityBlockIndex) override;
0068
0069 std::unique_ptr<CLHEP::HepRandomEngine> cloneEngine(LuminosityBlockIndex const&) override;
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 std::uint32_t mySeed() const override;
0083
0084 static void fillDescriptions(ConfigurationDescriptions& descriptions);
0085
0086 void preModuleConstruction(ModuleDescription const& description);
0087 void preModuleDestruction(ModuleDescription const& description);
0088 void preallocate(SystemBounds const&);
0089
0090 void preBeginLumi(LuminosityBlock const& lumi) override;
0091 void postEventRead(Event const& event) override;
0092 void setLumiCache(LuminosityBlockIndex, std::vector<RandomEngineState> const& iStates) override;
0093 void setEventCache(StreamID, std::vector<RandomEngineState> const& iStates) override;
0094
0095
0096
0097 void preModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
0098 void postModuleBeginStream(StreamContext const& sc, ModuleCallingContext const& mcc);
0099
0100 void preModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
0101 void postModuleEndStream(StreamContext const& sc, ModuleCallingContext const& mcc);
0102
0103 void preModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
0104 void postModuleStreamBeginRun(StreamContext const& sc, ModuleCallingContext const& mcc);
0105
0106 void preModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
0107 void postModuleStreamEndRun(StreamContext const& sc, ModuleCallingContext const& mcc);
0108
0109 void preModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
0110 void postModuleStreamBeginLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
0111
0112 void preModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
0113 void postModuleStreamEndLumi(StreamContext const& sc, ModuleCallingContext const& mcc);
0114
0115
0116 std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
0117 std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;
0118
0119 void consumes(ConsumesCollector&& iC) const override;
0120
0121
0122 void print(std::ostream& os) const override;
0123
0124 private:
0125 typedef std::vector<std::uint32_t> VUint32;
0126
0127 class LabelAndEngine {
0128 public:
0129 LabelAndEngine(std::string const& theLabel,
0130 VUint32 const& theSeeds,
0131 std::shared_ptr<CLHEP::HepRandomEngine> const& theEngine)
0132 : label_(theLabel), seeds_(theSeeds), engine_(theEngine) {}
0133 std::string const& label() const { return label_; }
0134 VUint32 const& seeds() const { return seeds_; }
0135 std::shared_ptr<CLHEP::HepRandomEngine const> engine() const { return get_underlying_safe(engine_); }
0136 std::shared_ptr<CLHEP::HepRandomEngine>& engine() { return get_underlying_safe(engine_); }
0137 void setSeed(std::uint32_t v, unsigned int index) { seeds_.at(index) = v; }
0138
0139 private:
0140 std::string label_;
0141 VUint32 seeds_;
0142 edm::propagate_const<std::shared_ptr<CLHEP::HepRandomEngine>> engine_;
0143 };
0144
0145
0146
0147
0148 class ModuleIDToEngine {
0149 public:
0150 ModuleIDToEngine(LabelAndEngine* theLabelAndEngine, unsigned int theModuleID)
0151 : engineState_(), labelAndEngine_(theLabelAndEngine), moduleID_(theModuleID) {}
0152
0153 std::vector<unsigned long> const& engineState() const { return engineState_; }
0154 LabelAndEngine const* labelAndEngine() const { return get_underlying_safe(labelAndEngine_); }
0155 LabelAndEngine*& labelAndEngine() { return get_underlying_safe(labelAndEngine_); }
0156 unsigned int moduleID() const { return moduleID_; }
0157 void setEngineState(std::vector<unsigned long> const& v) { engineState_ = v; }
0158
0159 bool operator<(ModuleIDToEngine const& r) const { return moduleID() < r.moduleID(); }
0160
0161 private:
0162 std::vector<unsigned long> engineState_;
0163 edm::propagate_const<LabelAndEngine*> labelAndEngine_;
0164 unsigned int moduleID_;
0165 };
0166
0167 void preModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
0168 void postModuleStreamCheck(StreamContext const& sc, ModuleCallingContext const& mcc);
0169
0170 void readFromLuminosityBlock(LuminosityBlock const& lumi);
0171 void readFromEvent(Event const& event);
0172
0173 void snapShot(std::vector<LabelAndEngine> const& engines, std::vector<RandomEngineState>& cache);
0174 void restoreFromCache(std::vector<RandomEngineState> const& cache, std::vector<LabelAndEngine>& engines);
0175
0176 void checkEngineType(std::string const& typeFromConfig,
0177 std::string const& typeFromEvent,
0178 std::string const& engineLabel) const;
0179
0180 void saveStatesToFile(std::string const& fileName,
0181 StreamID const& streamID,
0182 LuminosityBlockIndex const& lumiIndex);
0183 void writeStates(std::vector<RandomEngineState> const& v, std::ofstream& outFile);
0184 void writeVector(VUint32 const& v, std::ofstream& outFile);
0185 std::string constructSaveFileName() const;
0186
0187 void readEventStatesFromTextFile(std::string const& fileName, std::vector<RandomEngineState>& cache);
0188 void readLumiStatesFromTextFile(std::string const& fileName, std::vector<RandomEngineState>& cache);
0189 void readStatesFromFile(std::string const& fileName,
0190 std::vector<RandomEngineState>& cache,
0191 std::string const& whichStates);
0192 bool readEngineState(std::istream& is,
0193 std::vector<RandomEngineState>& cache,
0194 std::string const& whichStates,
0195 bool& saveToCache);
0196 void readVector(std::istream& is, unsigned numItems, std::vector<std::uint32_t>& v);
0197
0198 void createEnginesInVector(std::vector<LabelAndEngine>& engines,
0199 unsigned int seedOffset,
0200 unsigned int eventSeedOffset,
0201 std::vector<ModuleIDToEngine>& moduleIDVector);
0202
0203 void resetEngineSeeds(LabelAndEngine& labelAndEngine,
0204 std::string const& engineName,
0205 VUint32 const& seeds,
0206 std::uint32_t offset1,
0207 std::uint32_t offset2);
0208
0209
0210
0211 unsigned int nStreams_;
0212
0213
0214
0215 std::vector<std::vector<ModuleIDToEngine>> streamModuleIDToEngine_;
0216 std::vector<std::vector<ModuleIDToEngine>> lumiModuleIDToEngine_;
0217
0218
0219 std::vector<std::vector<LabelAndEngine>> streamEngines_;
0220 std::vector<std::vector<LabelAndEngine>> lumiEngines_;
0221
0222
0223
0224
0225
0226 edm::InputTag restoreStateTag_;
0227 edm::InputTag restoreStateBeginLumiTag_;
0228
0229 std::vector<std::vector<RandomEngineState>> eventCache_;
0230 std::vector<std::vector<RandomEngineState>> lumiCache_;
0231
0232
0233
0234
0235
0236 class SeedsAndName {
0237 public:
0238 static constexpr unsigned int kInvalid = std::numeric_limits<unsigned int>::max();
0239
0240 SeedsAndName(VUint32 const& theSeeds, std::string const& theEngineName)
0241 : seeds_(theSeeds), engineName_(theEngineName), moduleID_(kInvalid) {}
0242 VUint32 const& seeds() const { return seeds_; }
0243 std::string const& engineName() const { return engineName_; }
0244 unsigned int moduleID() const { return moduleID_; }
0245 void setModuleID(unsigned int v) { moduleID_ = v; }
0246
0247 private:
0248 VUint32 seeds_;
0249 std::string engineName_;
0250 unsigned int moduleID_;
0251 };
0252 std::map<std::string, SeedsAndName> seedsAndNameMap_;
0253
0254
0255
0256
0257
0258 std::string saveFileName_;
0259 std::atomic<bool> saveFileNameRecorded_;
0260 std::vector<edm::propagate_const<std::shared_ptr<std::ofstream>>> outFiles_;
0261
0262
0263
0264
0265 std::string restoreFileName_;
0266
0267
0268
0269
0270 bool enableChecking_;
0271
0272 std::uint32_t eventSeedOffset_;
0273
0274 bool verbose_;
0275
0276 static const std::vector<std::uint32_t>::size_type maxSeeds;
0277 static const std::vector<std::uint32_t>::size_type maxStates;
0278 static const std::uint32_t maxSeedRanecu;
0279 static const std::uint32_t maxSeedHepJames;
0280 static const std::uint32_t maxSeedTRandom3;
0281 };
0282 }
0283 }
0284 #endif