The Gaudi Framework  v30r3 (a5ef0a68)
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 <cfloat>
25 #include <cmath>
26 #include <ctime>
27 #include <iostream>
28 
29 // Framework include files
31 #include "GaudiKernel/MsgStream.h"
32 #include "GaudiKernel/System.h"
33 
34 #include "HepRndmEngine.h"
35 #include "RndmGenSvc.h"
36 
37 #include "CLHEP/Random/DRand48Engine.h"
38 #include "CLHEP/Random/DualRand.h"
39 #include "CLHEP/Random/Hurd160Engine.h"
40 #include "CLHEP/Random/Hurd288Engine.h"
41 #include "CLHEP/Random/JamesRandom.h"
42 #include "CLHEP/Random/MTwistEngine.h"
43 #include "CLHEP/Random/RanecuEngine.h"
44 #include "CLHEP/Random/Ranlux64Engine.h"
45 #include "CLHEP/Random/RanluxEngine.h"
46 #include "CLHEP/Random/RanshiEngine.h"
47 #include "CLHEP/Random/TripleRand.h"
48 
49 // Handle CLHEP 2.0.x move to CLHEP namespace
50 namespace CLHEP
51 {
52 }
53 using namespace CLHEP;
54 
55 namespace HepRndm
56 {
57 
58  // Initialize engine
59  template <class TYPE>
61  {
62  auto& seeds = m_seeds.value();
63  seeds.clear();
65  if ( m_seeds.size() == 0 ) {
66  // Default seeds
67  long theSeed = 1234567;
68  seeds.push_back( theSeed );
69  seeds.push_back( 0 );
70  }
71  if ( status.isSuccess() ) {
72  initEngine();
73  info() << "Generator engine type:" << System::typeinfoName( typeid( *hepEngine() ) ) << endmsg;
74  if ( m_useTable ) {
75  if ( m_row > 214 || m_col > 1 ) {
76  error() << "Generator engine seed table has dimension [215][2], you gave:"
77  << " Row=" << m_row << " Column:" << m_col << endmsg;
78  status = StatusCode::FAILURE;
79  } else {
80  info() << "Generator engine seeds from table."
81  << " Row=" << m_row << " Column:" << m_col << endmsg;
82  }
83  }
84  info() << "Current Seed:" << hepEngine()->getSeed();
85  info() << " Luxury:" << m_lux.value();
86  info() << endmsg;
87  // Use the default static engine if required (e.g. for GEANT4)
88  if ( m_setSingleton ) {
89  HepRandom::setTheEngine( hepEngine() );
90  info() << "This is the GEANT4 engine!" << endmsg;
91  }
92  return status;
93  }
94  error() << "Cannot initialze random engine of type:" << System::typeinfoName( typeid( TYPE ) ) << endmsg;
95  return status;
96  }
97 
98  // Finalize engine
99  template <class TYPE>
101  {
102  m_seeds.value().clear();
103  return BaseEngine::finalize();
104  }
105 
106  // Retrieve seeds
107  template <class TYPE>
109  {
110  auto& seeds = m_seeds.value();
111  seeds.clear();
112  std::copy( seed.begin(), seed.end(), std::back_inserter( seeds ) );
113  if ( !seeds.empty() ) {
114  if ( seeds.back() != 0 ) {
115  seeds.push_back( 0 );
116  }
117  hepEngine()->setSeeds( &seeds[0], m_lux );
118  return StatusCode::SUCCESS;
119  }
120  return StatusCode::FAILURE;
121  }
122 
123  // Retrieve seeds
124  template <class TYPE>
126  {
127  /*
128  const long *s = hepEngine()->getSeeds();
129  for ( size_t i = 0; i < NUMBER_OF_SEEDS; i++ ) {
130  seed.push_back(s[i]);
131  if ( m_seeds.size() > i )
132  m_seeds[i] = s[i];
133  else
134  m_seeds.push_back(s[i]);
135  }
136  */
137  seed.push_back( hepEngine()->getSeed() );
138  return StatusCode::SUCCESS;
139  }
140 
141  // Specialized create function for DualRand engine
142  template <>
144  {
145  return m_useTable ? std::make_unique<DualRand>( m_row, m_col ) : std::make_unique<DualRand>( m_seeds[0] );
146  }
147  // Specialized create function for TripleRand engine
148  template <>
150  {
151  return m_useTable ? std::make_unique<TripleRand>( m_row, m_col ) : std::make_unique<TripleRand>( m_seeds[0] );
152  }
153  // Specialized create function for DRand48Engine
154  template <>
156  {
157  return m_useTable ? std::make_unique<DRand48Engine>( m_row, m_col ) : std::make_unique<DRand48Engine>( m_seeds[0] );
158  }
159  // Specialized create function for Hurd160Engine
160  template <>
162  {
163  return m_useTable ? std::make_unique<Hurd160Engine>( m_row, m_col ) : std::make_unique<Hurd160Engine>( m_seeds[0] );
164  }
165  // Specialized create function for Hurd288Engine
166  template <>
168  {
169  return m_useTable ? std::make_unique<Hurd288Engine>( m_row, m_col ) : std::make_unique<Hurd288Engine>( m_seeds[0] );
170  }
171  // Specialized create function for RanecuEngine
172  template <>
174  {
175  return m_useTable ? std::make_unique<RanecuEngine>( m_row ) : std::make_unique<RanecuEngine>( m_seeds[0] );
176  }
177  // Specialized create function for RanshiEngine
178  template <>
180  {
181  return m_useTable ? std::make_unique<RanshiEngine>( m_row, m_col ) : std::make_unique<RanshiEngine>( m_seeds[0] );
182  }
183  // Specialized create function for RanluxEngine
184  template <>
186  {
187  return m_useTable ? std::make_unique<RanluxEngine>( m_row, m_col, m_lux )
188  : std::make_unique<RanluxEngine>( m_seeds[0], m_lux );
189  }
190  // Specialized create function for Ranlux64Engine
191  template <>
193  {
194  return m_useTable ? std::make_unique<Ranlux64Engine>( m_row, m_col, m_lux )
195  : std::make_unique<Ranlux64Engine>( m_seeds[0], m_lux );
196  }
197  // Specialized create function for MTwistEngine
198  template <>
200  {
201  return m_useTable ? std::make_unique<MTwistEngine>( m_row, m_col ) : std::make_unique<MTwistEngine>( m_seeds[0] );
202  }
203  // Specialized create function for HepJamesRandom
204  template <>
206  {
207  return m_useTable ? std::make_unique<HepJamesRandom>( m_row, m_col )
208  : std::make_unique<HepJamesRandom>( m_seeds[0] );
209  }
210 }
211 
214 typedef HepRndm::Engine<TripleRand> e2;
215 DECLARE_COMPONENT( e2 )
216 typedef HepRndm::Engine<DRand48Engine> e3;
217 DECLARE_COMPONENT( e3 )
218 typedef HepRndm::Engine<Hurd160Engine> e4;
219 DECLARE_COMPONENT( e4 )
220 typedef HepRndm::Engine<Hurd288Engine> e5;
221 DECLARE_COMPONENT( e5 )
222 typedef HepRndm::Engine<HepJamesRandom> e6;
223 DECLARE_COMPONENT( e6 )
224 typedef HepRndm::Engine<MTwistEngine> e7;
225 DECLARE_COMPONENT( e7 )
226 typedef HepRndm::Engine<RanecuEngine> e8;
227 DECLARE_COMPONENT( e8 )
228 typedef HepRndm::Engine<Ranlux64Engine> e9;
229 DECLARE_COMPONENT( e9 )
230 typedef HepRndm::Engine<RanluxEngine> e10;
231 DECLARE_COMPONENT( e10 )
232 typedef HepRndm::Engine<RanshiEngine> e11;
233 DECLARE_COMPONENT( e11 )
constexpr static const auto FAILURE
Definition: StatusCode.h:88
T copy(T...args)
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:332
bool isSuccess() const
Definition: StatusCode.h:287
T end(T...args)
HepRndm::Engine< DualRand > e1
#define DECLARE_COMPONENT(type)
T push_back(T...args)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
StatusCode initialize() override
Service override: initialization.
Definition: RndmEngine.cpp:31
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
T begin(T...args)
T back_inserter(T...args)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209