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) {
30  if(t) {
31  t->setRandomEngine(engine);
32  }
33  }
34 
35  explicit RandomEngineSentry(T* t, StreamID const& streamID): t_(t) {
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  CLHEP::HepRandomEngine& engine = rng->getEngine(streamID);
44  t->setRandomEngine(&engine);
45  }
46  }
47 
48  explicit RandomEngineSentry(T* t, LuminosityBlockIndex const& lumi): t_(t) {
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  CLHEP::HepRandomEngine& engine = rng->getEngine(lumi);
57  t->setRandomEngine(&engine);
58  }
59  }
60 
61  ~RandomEngineSentry() { if(t_) t_->setRandomEngine(nullptr); }
62 
63  private:
64  T* t_;
65  };
66 }
67 #endif
RandomEngineSentry(T *t, LuminosityBlockIndex const &lumi)
tuple lumi
Definition: fjr2json.py:35
RandomEngineSentry(T *t, StreamID const &streamID)
bool isAvailable() const
Definition: Service.h:46
RandomEngineSentry(T *t, CLHEP::HepRandomEngine *engine)
long double T