CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
RandomEngineSentry(T *t, LuminosityBlockIndex const &lumi)
constexpr std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
RandomEngineSentry(T *t, StreamID const &streamID)
bool isAvailable() const
Definition: Service.h:40
edm::propagate_const< CLHEP::HepRandomEngine * > engine_
RandomEngineSentry(T *t, CLHEP::HepRandomEngine *engine)
list lumi
Definition: dqmdumpme.py:53
CLHEP::HepRandomEngine *& randomEngine()
CLHEP::HepRandomEngine const * randomEngine() const
edm::propagate_const< T * > t_
long double T