Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:13

0001 #include "FWCore/Services/interface/ExternalRandomNumberGeneratorService.h"
0002 
0003 #include "FWCore/Utilities/interface/EDMException.h"
0004 #include "FWCore/Utilities/interface/Exception.h"
0005 
0006 #include "FWCore/Utilities/interface/LuminosityBlockIndex.h"
0007 #include "FWCore/Utilities/interface/StreamID.h"
0008 
0009 #include "CLHEP/Random/engineIDulong.h"
0010 #include "CLHEP/Random/JamesRandom.h"
0011 #include "CLHEP/Random/RanecuEngine.h"
0012 #include "CLHEP/Random/MixMaxRng.h"
0013 
0014 #include "SimDataFormats/RandomEngine/interface/RandomEngineState.h"
0015 
0016 using namespace edm;
0017 
0018 namespace {
0019   const std::vector<RandomEngineState> s_dummyStates;
0020 }
0021 
0022 ExternalRandomNumberGeneratorService::ExternalRandomNumberGeneratorService() {}
0023 
0024 void ExternalRandomNumberGeneratorService::setState(std::vector<unsigned long> const& iState, long iSeed) {
0025   if (not engine_) {
0026     engine_ = createFromState(iState, iSeed);
0027   } else {
0028     engine_->get(iState);
0029   }
0030 }
0031 
0032 std::vector<unsigned long> ExternalRandomNumberGeneratorService::getState() const { return engine_->put(); }
0033 
0034 CLHEP::HepRandomEngine& ExternalRandomNumberGeneratorService::getEngine(StreamID const&) { return *engine_; }
0035 CLHEP::HepRandomEngine& ExternalRandomNumberGeneratorService::getEngine(LuminosityBlockIndex const&) {
0036   return *engine_;
0037 }
0038 
0039 std::unique_ptr<CLHEP::HepRandomEngine> ExternalRandomNumberGeneratorService::cloneEngine(LuminosityBlockIndex const&) {
0040   std::vector<unsigned long> stateL = engine_->put();
0041 
0042   long seedL = engine_->getSeed();
0043   return createFromState(stateL, seedL);
0044 }
0045 
0046 std::unique_ptr<CLHEP::HepRandomEngine> ExternalRandomNumberGeneratorService::createFromState(
0047     std::vector<unsigned long> const& stateL, long seedL) const {
0048   std::unique_ptr<CLHEP::HepRandomEngine> newEngine;
0049   if (stateL[0] == CLHEP::engineIDulong<CLHEP::HepJamesRandom>()) {
0050     newEngine = std::make_unique<CLHEP::HepJamesRandom>(seedL);
0051   } else if (stateL[0] == CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
0052     newEngine = std::make_unique<CLHEP::RanecuEngine>();
0053   } else if (stateL[0] == CLHEP::engineIDulong<CLHEP::MixMaxRng>()) {
0054     newEngine = std::make_unique<CLHEP::MixMaxRng>(seedL);
0055     //} else if (stateL[0] == CLHEP::engineIDulong<TRandomAdaptor>()) {
0056     //  newEngine = std::make_unique<TRandomAdaptor>(seedL);
0057   } else {
0058     // Sanity check, it should not be possible for this to happen.
0059     throw Exception(errors::Unknown)
0060         << "The ExternalRandomNumberGeneratorService is trying to clone unknown engine type\n";
0061   }
0062   if (stateL[0] != CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
0063     newEngine->setSeed(seedL, 0);
0064   }
0065   newEngine->get(stateL);
0066   return newEngine;
0067 }
0068 
0069 std::uint32_t ExternalRandomNumberGeneratorService::mySeed() const { return 0; }
0070 
0071 void ExternalRandomNumberGeneratorService::preBeginLumi(LuminosityBlock const&) {}
0072 
0073 void ExternalRandomNumberGeneratorService::postEventRead(Event const&) {}
0074 
0075 void ExternalRandomNumberGeneratorService::setLumiCache(LuminosityBlockIndex,
0076                                                         std::vector<RandomEngineState> const& iStates) {}
0077 void ExternalRandomNumberGeneratorService::setEventCache(StreamID, std::vector<RandomEngineState> const& iStates) {}
0078 
0079 std::vector<RandomEngineState> const& ExternalRandomNumberGeneratorService::getEventCache(StreamID const&) const {
0080   return s_dummyStates;
0081 }
0082 
0083 std::vector<RandomEngineState> const& ExternalRandomNumberGeneratorService::getLumiCache(
0084     LuminosityBlockIndex const&) const {
0085   return s_dummyStates;
0086 }
0087 
0088 void ExternalRandomNumberGeneratorService::consumes(ConsumesCollector&& iC) const {}
0089 
0090 /// For debugging purposes only.
0091 void ExternalRandomNumberGeneratorService::print(std::ostream& os) const {}