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 
16 
17 namespace CLHEP {
18  class HepRandomEngine;
19 }
20 
21 namespace edm {
22 
23  class LuminosityBlockIndex;
24  class StreamID;
25 
26  template <class T> class RandomEngineSentry {
27  public:
28 
29  explicit RandomEngineSentry(T* t, CLHEP::HepRandomEngine* engine): t_(t), engine_(engine) {
30  if(t) {
31  t->setRandomEngine(engine);
32  }
33  }
34 
35  explicit RandomEngineSentry(T* t, StreamID const& streamID): t_(t), engine_(nullptr) {
36  if(t) {
38  if (!rng.isAvailable()) {
39  throw cms::Exception("Configuration")
40  << "Attempt to get a random engine when the RandomNumberGeneratorService is not configured.\n"
41  "You must configure the service if you want an engine.\n";
42  }
43  engine_ = &rng->getEngine(streamID);
44  t->setRandomEngine(engine_);
45  }
46  }
47 
49  if(t) {
51  if (!rng.isAvailable()) {
52  throw cms::Exception("Configuration")
53  << "Attempt to get a random engine when the RandomNumberGeneratorService is not configured.\n"
54  "You must configure the service if you want an engine.\n";
55  }
56  engine_ = &rng->getEngine(lumi);
57  t->setRandomEngine(engine_);
58  }
59  }
60 
61  ~RandomEngineSentry() { if(t_) t_->setRandomEngine(nullptr); }
62 
63  CLHEP::HepRandomEngine* randomEngine() const { return engine_; }
64 
65  private:
66  T* t_;
67  CLHEP::HepRandomEngine* engine_;
68  };
69 }
70 #endif
tuple t
Definition: tree.py:139
RandomEngineSentry(T *t, LuminosityBlockIndex const &lumi)
tuple lumi
Definition: fjr2json.py:35
#define nullptr
CLHEP::HepRandomEngine * randomEngine() const
RandomEngineSentry(T *t, StreamID const &streamID)
bool isAvailable() const
Definition: Service.h:46
RandomEngineSentry(T *t, CLHEP::HepRandomEngine *engine)
CLHEP::HepRandomEngine * engine_
long double T