CMS 3D CMS Logo

RandomEngineSentry.h
Go to the documentation of this file.
1 #ifndef FWCore_ServiceRegistry_RandomEngineSentry_h
2 #define FWCore_ServiceRegistry_RandomEngineSentry_h
3 
9 //
10 // Original Author: W. David Dagenhart
11 // Created: 11/26/2013
12 
17 
18 namespace CLHEP {
19  class HepRandomEngine;
20 }
21 
22 namespace edm {
23 
24  class LuminosityBlockIndex;
25  class StreamID;
26 
27  template <class T>
29  public:
30  explicit RandomEngineSentry(T* t, CLHEP::HepRandomEngine* engine) : t_(t), engine_(engine) {
31  if (t) {
32  t->setRandomEngine(engine);
33  }
34  }
35 
36  explicit RandomEngineSentry(T* t, StreamID const& streamID) : t_(t), engine_(nullptr) {
37  if (t) {
39  if (!rng.isAvailable()) {
40  throw cms::Exception("Configuration")
41  << "Attempt to get a random engine when the RandomNumberGeneratorService is not configured.\n"
42  "You must configure the service if you want an engine.\n";
43  }
44  engine_ = &rng->getEngine(streamID);
45  t->setRandomEngine(engine_);
46  }
47  }
48 
49  explicit RandomEngineSentry(T* t, LuminosityBlockIndex const& lumi) : t_(t), engine_(nullptr) {
50  if (t) {
52  if (!rng.isAvailable()) {
53  throw cms::Exception("Configuration")
54  << "Attempt to get a random engine when the RandomNumberGeneratorService is not configured.\n"
55  "You must configure the service if you want an engine.\n";
56  }
57  engine_ = &rng->getEngine(lumi);
58  t->setRandomEngine(engine_);
59  }
60  }
61 
63  if (t_)
64  t_->setRandomEngine(nullptr);
65  }
66 
67  CLHEP::HepRandomEngine const* randomEngine() const { return get_underlying_safe(engine_); }
68  CLHEP::HepRandomEngine*& randomEngine() { return get_underlying_safe(engine_); }
69 
70  private:
73  };
74 } // namespace edm
75 #endif
edm::StreamID
Definition: StreamID.h:30
edm::RandomEngineSentry::RandomEngineSentry
RandomEngineSentry(T *t, StreamID const &streamID)
Definition: RandomEngineSentry.h:36
edm
HLT enums.
Definition: AlignableModifier.h:19
RandomNumberGenerator.h
edm::get_underlying_safe
constexpr std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
Definition: get_underlying_safe.h:41
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
edm::RandomEngineSentry::randomEngine
CLHEP::HepRandomEngine *& randomEngine()
Definition: RandomEngineSentry.h:68
edm::RandomEngineSentry::randomEngine
CLHEP::HepRandomEngine const * randomEngine() const
Definition: RandomEngineSentry.h:67
edm::propagate_const< T * >
edm::LuminosityBlockIndex
Definition: LuminosityBlockIndex.h:33
Service.h
edm::RandomEngineSentry::RandomEngineSentry
RandomEngineSentry(T *t, CLHEP::HepRandomEngine *engine)
Definition: RandomEngineSentry.h:30
CLHEP
Definition: CocoaGlobals.h:27
edm::Service
Definition: Service.h:30
edm::RandomEngineSentry
Definition: RandomEngineSentry.h:28
edm::RandomEngineSentry::t_
edm::propagate_const< T * > t_
Definition: RandomEngineSentry.h:71
T
long double T
Definition: Basic3DVectorLD.h:48
Exception
Definition: hltDiff.cc:246
Exception.h
edm::RandomEngineSentry::~RandomEngineSentry
~RandomEngineSentry()
Definition: RandomEngineSentry.h:62
edm::RandomEngineSentry::RandomEngineSentry
RandomEngineSentry(T *t, LuminosityBlockIndex const &lumi)
Definition: RandomEngineSentry.h:49
get_underlying_safe.h
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
lumi
Definition: LumiSectionData.h:20
edm::RandomEngineSentry::engine_
edm::propagate_const< CLHEP::HepRandomEngine * > engine_
Definition: RandomEngineSentry.h:72