All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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"
32 #include "GaudiKernel/IIncidentSvc.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  MsgStream log(msgSvc(), name());
77  if ( status.isSuccess() ) {
78  initEngine();
79  log << MSG::INFO << "Generator engine type:"
80  << System::typeinfoName(typeid(*hepEngine()))
81  << endmsg;
82  if ( m_useTable ) {
83  if ( m_row > 214 || m_col > 1 ) {
84  log << MSG::ERROR << "Generator engine seed table has dimension [215][2], you gave:"
85  << " Row=" << m_row << " Column:" << m_col << endmsg;
86  status = StatusCode::FAILURE;
87  }
88  else {
89  log << MSG::INFO << "Generator engine seeds from table."
90  << " Row=" << m_row << " Column:" << m_col << endmsg;
91  }
92  }
93  log << "Current Seed:" << hepEngine()->getSeed();
94  log << " Luxury:" << m_lux;
95  log << endmsg;
96  // Use the default static engine if required (e.g. for GEANT4)
97  if ( m_setSingleton ) {
98  HepRandom::setTheEngine(hepEngine());
99  log << "This is the GEANT4 engine!" << endmsg;
100  }
101  return status;
102  }
103  log << MSG::ERROR << "Cannot initialze random engine of type:"
104  << System::typeinfoName(typeid(TYPE))
105  << endmsg;
106  return status;
107  }
108 
109  // Finalize engine
110  template <class TYPE> StatusCode Engine<TYPE>::finalize() {
111  m_seeds.clear();
112  return BaseEngine::finalize();
113  }
114 
115  // Retrieve seeds
116  template <class TYPE> StatusCode Engine<TYPE>::setSeeds(const std::vector<long>& seed) {
117  m_seeds.clear();
118  std::copy(seed.begin(),seed.end(),std::back_inserter(m_seeds));
119  if ( !m_seeds.empty() ) {
120  if ( m_seeds.back() != 0 ) {
121  m_seeds.push_back(0);
122  }
123  hepEngine()->setSeeds(&m_seeds[0], m_lux);
124  return StatusCode::SUCCESS;
125  }
126  return StatusCode::FAILURE;
127  }
128 
129  // Retrieve seeds
130  template <class TYPE> StatusCode Engine<TYPE>::seeds(std::vector<long>& seed) const {
131  /*
132  const long *s = hepEngine()->getSeeds();
133  for ( size_t i = 0; i < NUMBER_OF_SEEDS; i++ ) {
134  seed.push_back(s[i]);
135  if ( m_seeds.size() > i )
136  m_seeds[i] = s[i];
137  else
138  m_seeds.push_back(s[i]);
139  }
140  */
141  seed.push_back(hepEngine()->getSeed());
142  return StatusCode::SUCCESS;
143  }
144 
145  // Specialized create function for DualRand engine
146  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<DualRand>::createEngine() {
147  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new DualRand(m_row, m_col)
148  : new DualRand(m_seeds[0]) );
149  }
150  // Specialized create function for TripleRand engine
151  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<TripleRand>::createEngine() {
152  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new TripleRand(m_row, m_col)
153  : new TripleRand(m_seeds[0]));
154  }
155  // Specialized create function for DRand48Engine
156  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<DRand48Engine>::createEngine() {
157  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new DRand48Engine(m_row, m_col)
158  : new DRand48Engine(m_seeds[0]));
159  }
160  // Specialized create function for Hurd160Engine
161  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<Hurd160Engine>::createEngine() {
162  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new Hurd160Engine(m_row, m_col)
163  : new Hurd160Engine(m_seeds[0]));
164  }
165  // Specialized create function for Hurd288Engine
166  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<Hurd288Engine>::createEngine() {
167  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new Hurd288Engine(m_row, m_col)
168  : new Hurd288Engine(m_seeds[0]));
169  }
170  // Specialized create function for RanecuEngine
171  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<RanecuEngine>::createEngine() {
172  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new RanecuEngine(m_row)
173  : new RanecuEngine(m_seeds[0]));
174  }
175  // Specialized create function for RanshiEngine
176  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<RanshiEngine>::createEngine() {
177  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new RanshiEngine(m_row, m_col)
178  : new RanshiEngine(m_seeds[0]));
179  }
180  // Specialized create function for RanluxEngine
181  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<RanluxEngine>::createEngine() {
182  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new RanluxEngine(m_row, m_col, m_lux)
183  : new RanluxEngine(m_seeds[0], m_lux));
184  }
185  // Specialized create function for Ranlux64Engine
186  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<Ranlux64Engine>::createEngine() {
187  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new Ranlux64Engine(m_row, m_col, m_lux)
188  : new Ranlux64Engine(m_seeds[0], m_lux));
189  }
190  // Specialized create function for MTwistEngine
191  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<MTwistEngine>::createEngine() {
192  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new MTwistEngine(m_row, m_col)
193  : new MTwistEngine(m_seeds[0]));
194  }
195  // Specialized create function for HepJamesRandom
196  template <> std::unique_ptr<CLHEP::HepRandomEngine> Engine<HepJamesRandom>::createEngine() {
197  return std::unique_ptr<CLHEP::HepRandomEngine>( m_useTable ? new HepJamesRandom(m_row, m_col)
198  : new HepJamesRandom(m_seeds[0]) );
199  }
200 
201 }
202 
204 typedef HepRndm::Engine<TripleRand> e2; DECLARE_COMPONENT(e2)
205 typedef HepRndm::Engine<DRand48Engine> e3; DECLARE_COMPONENT(e3)
206 typedef HepRndm::Engine<Hurd160Engine> e4; DECLARE_COMPONENT(e4)
207 typedef HepRndm::Engine<Hurd288Engine> e5; DECLARE_COMPONENT(e5)
208 typedef HepRndm::Engine<HepJamesRandom> e6; DECLARE_COMPONENT(e6)
209 typedef HepRndm::Engine<MTwistEngine> e7; DECLARE_COMPONENT(e7)
210 typedef HepRndm::Engine<RanecuEngine> e8; DECLARE_COMPONENT(e8)
211 typedef HepRndm::Engine<Ranlux64Engine> e9; DECLARE_COMPONENT(e9)
212 typedef HepRndm::Engine<RanluxEngine> e10; DECLARE_COMPONENT(e10)
213 typedef HepRndm::Engine<RanshiEngine> e11; DECLARE_COMPONENT(e11)
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode finalize() override
Service override: finalization.
StatusCode seeds(std::vector< long > &seed) const override
Retrieve seeds.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
std::vector< long > m_seeds
Definition: HepRndmEngine.h:28
HepRndm::Engine< DualRand > e1
StatusCode finalize() override
Finalize the Engine.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
std::unique_ptr< CLHEP::HepRandomEngine > createEngine() override
Create new HepEngine....
StatusCode setSeeds(const std::vector< long > &seed) override
Set seeds.
virtual StatusCode initialize()
Service override: initialization.
Definition: RndmEngine.cpp:41
StatusCode initialize() override
Initialize the Engine.