CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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> class RandomEngineSentry {
28  public:
29 
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 
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 
62  ~RandomEngineSentry() { if(t_) t_->setRandomEngine(nullptr); }
63 
64  CLHEP::HepRandomEngine const* randomEngine() const {return get_underlying_safe(engine_);}
65  CLHEP::HepRandomEngine*& randomEngine() {return get_underlying_safe(engine_);}
66 
67  private:
70  };
71 }
72 #endif
RandomEngineSentry(T *t, LuminosityBlockIndex const &lumi)
tuple lumi
Definition: fjr2json.py:35
#define nullptr
RandomEngineSentry(T *t, StreamID const &streamID)
bool isAvailable() const
Definition: Service.h:46
edm::propagate_const< CLHEP::HepRandomEngine * > engine_
RandomEngineSentry(T *t, CLHEP::HepRandomEngine *engine)
std::shared_ptr< T > & get_underlying_safe(propagate_const< std::shared_ptr< T >> &iP)
CLHEP::HepRandomEngine *& randomEngine()
CLHEP::HepRandomEngine const * randomEngine() const
edm::propagate_const< T * > t_
long double T