HepRndmEngines.cpp
Go to the documentation of this file.
1 //====================================================================
2 // Random Engine implementations
3 //--------------------------------------------------------------------
4 //
5 // Package : HepRndm ( The LHCb Offline System)
6 // Author : M.Frank
7 // History :
8 // +---------+----------------------------------------------+---------
9 // | Date | Comment | Who
10 // +---------+----------------------------------------------+---------
11 // | 29/10/99| Initial version | MF
12 // +---------+----------------------------------------------+---------
13 //
14 // Note: Currently all engines require only ONE seed
15 // We aill return only the first number, because
16 // the implementation in CLHEP does not allow to
17 // determine the proper number of seeds used.
18 #define NUMBER_OF_SEEDS 1
19 //
20 //====================================================================
21 #define HEPRNDM_HEPRNDMENGINES_CPP
22 
23 // STL include files
24 #include <iostream>
25 #include <cfloat>
26 #include <ctime>
27 #include <cmath>
28 
29 // Framework include files
30 #include "GaudiKernel/System.h"
31 #include "GaudiKernel/MsgStream.h"
33 
34 #include "RndmGenSvc.h"
35 #include "HepRndmEngine.h"
36 
37 #include "CLHEP/Random/DualRand.h"
38 #include "CLHEP/Random/TripleRand.h"
39 #include "CLHEP/Random/DRand48Engine.h"
40 #include "CLHEP/Random/Hurd160Engine.h"
41 #include "CLHEP/Random/Hurd288Engine.h"
42 #include "CLHEP/Random/JamesRandom.h"
43 #include "CLHEP/Random/MTwistEngine.h"
44 #include "CLHEP/Random/RanecuEngine.h"
45 #include "CLHEP/Random/Ranlux64Engine.h"
46 #include "CLHEP/Random/RanluxEngine.h"
47 #include "CLHEP/Random/RanshiEngine.h"
48 
49 // Handle CLHEP 2.0.x move to CLHEP namespace
50 namespace CLHEP { }
51 using namespace CLHEP;
52 
53 namespace HepRndm {
54 
55  // Standard constructor
56  template <class TYPE> Engine<TYPE>::Engine(const std::string& nam, ISvcLocator* loc)
57  : BaseEngine (nam, loc) {
58  declareProperty("Seeds", m_seeds);
59  declareProperty("Column", m_col = 0);
60  declareProperty("Row", m_row = 1);
61  declareProperty("Luxury", m_lux = 3);
62  declareProperty("UseTable", m_useTable = false);
63  declareProperty("SetSingleton",m_setSingleton = false);
64  }
65 
66  // Initialize engine
67  template <class TYPE> StatusCode Engine<TYPE>::initialize() {
68  m_seeds.erase(m_seeds.begin(), m_seeds.end());
70  if ( m_seeds.size() == 0 ) {
71  // Default seeds
72  long theSeed = 1234567;
73  m_seeds.push_back(theSeed);
74  m_seeds.push_back(0);
75  }
76  if ( status.isSuccess() ) {
77  initEngine();
78  info() << "Generator engine type:"
79  << System::typeinfoName(typeid(*hepEngine()))
80  << endmsg;
81  if ( m_useTable ) {
82  if ( m_row > 214 || m_col > 1 ) {
83  error() << "Generator engine seed table has dimension [215][2], you gave:"
84  << " Row=" << m_row << " Column:" << m_col << endmsg;
85  status = StatusCode::FAILURE;
86  }
87  else {
88  info() << "Generator engine seeds from table."
89  << " Row=" << m_row << " Column:" << m_col << endmsg;
90  }
91  }
92  info() << "Current Seed:" << hepEngine()->getSeed();
93  info() << " Luxury:" << m_lux;
94  info() << endmsg;
95  // Use the default static engine if required (e.g. for GEANT4)
96  if ( m_setSingleton ) {
97  HepRandom::setTheEngine(hepEngine());
98  info() << "This is the GEANT4 engine!" << endmsg;
99  }
100  return status;
101  }
102  error() << "Cannot initialze random engine of type:"
103  << System::typeinfoName(typeid(TYPE))
104  << endmsg;
105  return status;
106  }
107 
108  // Finalize engine
109  template <class TYPE> StatusCode Engine<TYPE>::finalize() {
110  m_seeds.clear();
111  return BaseEngine::finalize();
112  }
113 
114  // Retrieve seeds
115  template <class TYPE> StatusCode Engine<TYPE>::setSeeds(const std::vector<long>& seed) {
116  m_seeds.clear();
117  std::copy(seed.begin(),seed.end(),std::back_inserter(m_seeds));
118  if ( !m_seeds.empty() ) {
119  if ( m_seeds.back() != 0 ) {
120  m_seeds.push_back(0);
121  }
122  hepEngine()->setSeeds(&m_seeds[0], m_lux);
123  return StatusCode::SUCCESS;
124  }
125  return StatusCode::FAILURE;
126  }
127 
128  // Retrieve seeds
129  template <class TYPE> StatusCode Engine<TYPE>::seeds(std::vector<long>& seed) const {
130  /*
131  const long *s = hepEngine()->getSeeds();
132  for ( size_t i = 0; i < NUMBER_OF_SEEDS; i++ ) {
133  seed.push_back(s[i]);
134  if ( m_seeds.size() > i )
135  m_seeds[i] = s[i];
136  else
137  m_seeds.push_back(s[i]);
138  }
139  */
140  seed.push_back(hepEngine()->getSeed());
141  return StatusCode::SUCCESS;
142  }
143 
144  // Specialized create function for DualRand engine
146  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new DualRand(m_row, m_col)
147  : new DualRand(m_seeds[0]) );
148  }
149  // Specialized create function for TripleRand engine
151  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new TripleRand(m_row, m_col)
152  : new TripleRand(m_seeds[0]));
153  }
154  // Specialized create function for DRand48Engine
156  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new DRand48Engine(m_row, m_col)
157  : new DRand48Engine(m_seeds[0]));
158  }
159  // Specialized create function for Hurd160Engine
161  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new Hurd160Engine(m_row, m_col)
162  : new Hurd160Engine(m_seeds[0]));
163  }
164  // Specialized create function for Hurd288Engine
166  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new Hurd288Engine(m_row, m_col)
167  : new Hurd288Engine(m_seeds[0]));
168  }
169  // Specialized create function for RanecuEngine
171  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new RanecuEngine(m_row)
172  : new RanecuEngine(m_seeds[0]));
173  }
174  // Specialized create function for RanshiEngine
176  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new RanshiEngine(m_row, m_col)
177  : new RanshiEngine(m_seeds[0]));
178  }
179  // Specialized create function for RanluxEngine
181  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new RanluxEngine(m_row, m_col, m_lux)
182  : new RanluxEngine(m_seeds[0], m_lux));
183  }
184  // Specialized create function for Ranlux64Engine
186  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new Ranlux64Engine(m_row, m_col, m_lux)
187  : new Ranlux64Engine(m_seeds[0], m_lux));
188  }
189  // Specialized create function for MTwistEngine
191  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new MTwistEngine(m_row, m_col)
192  : new MTwistEngine(m_seeds[0]));
193  }
194  // Specialized create function for HepJamesRandom
196  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new HepJamesRandom(m_row, m_col)
197  : new HepJamesRandom(m_seeds[0]) );
198  }
199 
200 }
201 
203 typedef HepRndm::Engine<TripleRand> e2; DECLARE_COMPONENT(e2)
204 typedef HepRndm::Engine<DRand48Engine> e3; DECLARE_COMPONENT(e3)
205 typedef HepRndm::Engine<Hurd160Engine> e4; DECLARE_COMPONENT(e4)
206 typedef HepRndm::Engine<Hurd288Engine> e5; DECLARE_COMPONENT(e5)
207 typedef HepRndm::Engine<HepJamesRandom> e6; DECLARE_COMPONENT(e6)
208 typedef HepRndm::Engine<MTwistEngine> e7; DECLARE_COMPONENT(e7)
209 typedef HepRndm::Engine<RanecuEngine> e8; DECLARE_COMPONENT(e8)
210 typedef HepRndm::Engine<Ranlux64Engine> e9; DECLARE_COMPONENT(e9)
211 typedef HepRndm::Engine<RanluxEngine> e10; DECLARE_COMPONENT(e10)
212 typedef HepRndm::Engine<RanshiEngine> e11; DECLARE_COMPONENT(e11)
T copy(T...args)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode finalize() override
Service override: finalization.
StatusCode seeds(std::vector< long > &seed) const override
Retrieve seeds.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
std::vector< long > m_seeds
Definition: HepRndmEngine.h:28
T end(T...args)
HepRndm::Engine< DualRand > e1
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
STL class.
T push_back(T...args)
StatusCode finalize() override
Finalize the Engine.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::unique_ptr< CLHEP::HepRandomEngine > createEngine() override
Create new HepEngine....
T begin(T...args)
T back_inserter(T...args)
StatusCode setSeeds(const std::vector< long > &seed) override
Set seeds.
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:215
virtual StatusCode initialize()
Service override: initialization.
Definition: RndmEngine.cpp:41
StatusCode initialize() override
Initialize the Engine.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244