CMS 3D CMS Logo

ExternalRandomNumberGeneratorService.cc
Go to the documentation of this file.
2 
5 
8 
9 #include "CLHEP/Random/engineIDulong.h"
10 #include "CLHEP/Random/JamesRandom.h"
11 #include "CLHEP/Random/RanecuEngine.h"
12 #include "CLHEP/Random/MixMaxRng.h"
13 
15 
16 using namespace edm;
17 
18 namespace {
19  const std::vector<RandomEngineState> s_dummyStates;
20 }
21 
23 
24 void ExternalRandomNumberGeneratorService::setState(std::vector<unsigned long> const& iState, long iSeed) {
25  if (not engine_) {
26  engine_ = createFromState(iState, iSeed);
27  } else {
28  engine_->get(iState);
29  }
30 }
31 
32 std::vector<unsigned long> ExternalRandomNumberGeneratorService::getState() const { return engine_->put(); }
33 
34 CLHEP::HepRandomEngine& ExternalRandomNumberGeneratorService::getEngine(StreamID const&) { return *engine_; }
36  return *engine_;
37 }
38 
39 std::unique_ptr<CLHEP::HepRandomEngine> ExternalRandomNumberGeneratorService::cloneEngine(LuminosityBlockIndex const&) {
40  std::vector<unsigned long> stateL = engine_->put();
41 
42  long seedL = engine_->getSeed();
43  return createFromState(stateL, seedL);
44 }
45 
46 std::unique_ptr<CLHEP::HepRandomEngine> ExternalRandomNumberGeneratorService::createFromState(
47  std::vector<unsigned long> const& stateL, long seedL) const {
48  std::unique_ptr<CLHEP::HepRandomEngine> newEngine;
49  if (stateL[0] == CLHEP::engineIDulong<CLHEP::HepJamesRandom>()) {
50  newEngine = std::make_unique<CLHEP::HepJamesRandom>(seedL);
51  } else if (stateL[0] == CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
52  newEngine = std::make_unique<CLHEP::RanecuEngine>();
53  } else if (stateL[0] == CLHEP::engineIDulong<CLHEP::MixMaxRng>()) {
54  newEngine = std::make_unique<CLHEP::MixMaxRng>(seedL);
55  //} else if (stateL[0] == CLHEP::engineIDulong<TRandomAdaptor>()) {
56  // newEngine = std::make_unique<TRandomAdaptor>(seedL);
57  } else {
58  // Sanity check, it should not be possible for this to happen.
60  << "The ExternalRandomNumberGeneratorService is trying to clone unknown engine type\n";
61  }
62  if (stateL[0] != CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
63  newEngine->setSeed(seedL, 0);
64  }
65  newEngine->get(stateL);
66  return newEngine;
67 }
68 
69 std::uint32_t ExternalRandomNumberGeneratorService::mySeed() const { return 0; }
70 
72 
74 
76  std::vector<RandomEngineState> const& iStates) {}
77 void ExternalRandomNumberGeneratorService::setEventCache(StreamID, std::vector<RandomEngineState> const& iStates) {}
78 
79 std::vector<RandomEngineState> const& ExternalRandomNumberGeneratorService::getEventCache(StreamID const&) const {
80  return s_dummyStates;
81 }
82 
83 std::vector<RandomEngineState> const& ExternalRandomNumberGeneratorService::getLumiCache(
84  LuminosityBlockIndex const&) const {
85  return s_dummyStates;
86 }
87 
89 
91 void ExternalRandomNumberGeneratorService::print(std::ostream& os) const {}
edm::StreamID
Definition: StreamID.h:30
edm::ExternalRandomNumberGeneratorService::createFromState
std::unique_ptr< CLHEP::HepRandomEngine > createFromState(std::vector< unsigned long > const &, long seed) const
Definition: ExternalRandomNumberGeneratorService.cc:46
edm::ExternalRandomNumberGeneratorService::getEventCache
std::vector< RandomEngineState > const & getEventCache(StreamID const &) const final
Definition: ExternalRandomNumberGeneratorService.cc:79
edm::LuminosityBlock
Definition: LuminosityBlock.h:50
edm::ExternalRandomNumberGeneratorService::cloneEngine
std::unique_ptr< CLHEP::HepRandomEngine > cloneEngine(LuminosityBlockIndex const &) final
Definition: ExternalRandomNumberGeneratorService.cc:39
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::errors::Unknown
Definition: EDMException.h:29
edm::ExternalRandomNumberGeneratorService::mySeed
std::uint32_t mySeed() const final
Definition: ExternalRandomNumberGeneratorService.cc:69
edm::ExternalRandomNumberGeneratorService::setEventCache
void setEventCache(StreamID, std::vector< RandomEngineState > const &iStates) final
Definition: ExternalRandomNumberGeneratorService.cc:77
edm::ExternalRandomNumberGeneratorService::getEngine
CLHEP::HepRandomEngine & getEngine(StreamID const &) final
Use this engine in event methods.
Definition: ExternalRandomNumberGeneratorService.cc:34
EDMException.h
edm::ExternalRandomNumberGeneratorService::setState
void setState(std::vector< unsigned long > const &, long seed)
Definition: ExternalRandomNumberGeneratorService.cc:24
edm::LuminosityBlockIndex
Definition: LuminosityBlockIndex.h:33
edm::ExternalRandomNumberGeneratorService::postEventRead
void postEventRead(Event const &event) final
Definition: ExternalRandomNumberGeneratorService.cc:73
ExternalRandomNumberGeneratorService.h
edm::ExternalRandomNumberGeneratorService::print
void print(std::ostream &os) const final
For debugging purposes only.
Definition: ExternalRandomNumberGeneratorService.cc:91
RandomEngineState.h
edm::ExternalRandomNumberGeneratorService::getLumiCache
std::vector< RandomEngineState > const & getLumiCache(LuminosityBlockIndex const &) const final
Definition: ExternalRandomNumberGeneratorService.cc:83
edm::ExternalRandomNumberGeneratorService::setLumiCache
void setLumiCache(LuminosityBlockIndex, std::vector< RandomEngineState > const &iStates) final
Definition: ExternalRandomNumberGeneratorService.cc:75
edm::ExternalRandomNumberGeneratorService::getState
std::vector< unsigned long > getState() const
Definition: ExternalRandomNumberGeneratorService.cc:32
Exception
Definition: hltDiff.cc:245
edm::ExternalRandomNumberGeneratorService::consumes
void consumes(ConsumesCollector &&iC) const final
Definition: ExternalRandomNumberGeneratorService.cc:88
Exception.h
LuminosityBlockIndex.h
edm::ExternalRandomNumberGeneratorService::preBeginLumi
void preBeginLumi(LuminosityBlock const &lumi) final
Definition: ExternalRandomNumberGeneratorService.cc:71
edm::ExternalRandomNumberGeneratorService::engine_
std::unique_ptr< CLHEP::HepRandomEngine > engine_
Definition: ExternalRandomNumberGeneratorService.h:58
edm::ExternalRandomNumberGeneratorService::ExternalRandomNumberGeneratorService
ExternalRandomNumberGeneratorService()
Definition: ExternalRandomNumberGeneratorService.cc:22
edm::Event
Definition: Event.h:73
StreamID.h
edm::ConsumesCollector
Definition: ConsumesCollector.h:45