The Gaudi Framework  master (da3d77e1)
HepRndmEngines.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 //====================================================================
12 // Random Engine implementations
13 //--------------------------------------------------------------------
14 //
15 // Package : HepRndm ( The LHCb Offline System)
16 // Author : M.Frank
17 // History :
18 // +---------+----------------------------------------------+---------
19 // | Date | Comment | Who
20 // +---------+----------------------------------------------+---------
21 // | 29/10/99| Initial version | MF
22 // +---------+----------------------------------------------+---------
23 //
24 // Note: Currently all engines require only ONE seed
25 // We aill return only the first number, because
26 // the implementation in CLHEP does not allow to
27 // determine the proper number of seeds used.
28 #define NUMBER_OF_SEEDS 1
29 //
30 //====================================================================
31 #define HEPRNDM_HEPRNDMENGINES_CPP
32 
33 // STL include files
34 #include <cfloat>
35 #include <cmath>
36 #include <ctime>
37 #include <iostream>
38 
39 // Framework include files
41 #include <GaudiKernel/MsgStream.h>
42 #include <GaudiKernel/System.h>
43 
44 #include "HepRndmEngine.h"
45 #include "RndmGenSvc.h"
46 
47 #include <CLHEP/Random/DRand48Engine.h>
48 #include <CLHEP/Random/DualRand.h>
49 #include <CLHEP/Random/Hurd160Engine.h>
50 #include <CLHEP/Random/Hurd288Engine.h>
51 #include <CLHEP/Random/JamesRandom.h>
52 #include <CLHEP/Random/MTwistEngine.h>
53 #include <CLHEP/Random/RanecuEngine.h>
54 #include <CLHEP/Random/Ranlux64Engine.h>
55 #include <CLHEP/Random/RanluxEngine.h>
56 #include <CLHEP/Random/RanshiEngine.h>
57 #include <CLHEP/Random/TripleRand.h>
58 
59 // Handle CLHEP 2.0.x move to CLHEP namespace
60 namespace CLHEP {}
61 using namespace CLHEP;
62 
63 namespace HepRndm {
64 
65  // Initialize engine
66  template <class TYPE>
68  auto& seeds = m_seeds.value();
70  if ( m_seeds.size() == 0 ) {
71  // Default seeds
72  long theSeed = 1234567;
73  seeds.push_back( theSeed );
74  seeds.push_back( 0 );
75  }
76  if ( status.isSuccess() ) {
77  initEngine();
78  info() << "Generator engine type:" << System::typeinfoName( typeid( *hepEngine() ) ) << endmsg;
79  if ( m_useTable ) {
80  if ( m_row > 214 || m_col > 1 ) {
81  error() << "Generator engine seed table has dimension [215][2], you gave:"
82  << " Row=" << m_row << " Column:" << m_col << endmsg;
83  status = StatusCode::FAILURE;
84  } else {
85  info() << "Generator engine seeds from table."
86  << " Row=" << m_row << " Column:" << m_col << endmsg;
87  }
88  }
89  info() << "Current Seed:" << hepEngine()->getSeed();
90  info() << " Luxury:" << m_lux.value();
91  info() << endmsg;
92  // Use the default static engine if required (e.g. for GEANT4)
93  if ( m_setSingleton ) {
94  HepRandom::setTheEngine( hepEngine() );
95  info() << "This is the GEANT4 engine!" << endmsg;
96  }
97  return status;
98  }
99  error() << "Cannot initialze random engine of type:" << System::typeinfoName( typeid( TYPE ) ) << endmsg;
100  return status;
101  }
102 
103  // Finalize engine
104  template <class TYPE>
106  m_seeds.value().clear();
107  return BaseEngine::finalize();
108  }
109 
110  // Retrieve seeds
111  template <class TYPE>
113  auto& seeds = m_seeds.value();
114  seeds.clear();
115  std::copy( seed.begin(), seed.end(), std::back_inserter( seeds ) );
116  if ( !seeds.empty() ) {
117  if ( seeds.back() != 0 ) { seeds.push_back( 0 ); }
118  hepEngine()->setSeeds( &seeds[0], m_lux );
119  return StatusCode::SUCCESS;
120  }
121  return StatusCode::FAILURE;
122  }
123 
124  // Retrieve seeds
125  template <class TYPE>
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  return m_useTable ? std::make_unique<DualRand>( m_row, m_col ) : std::make_unique<DualRand>( m_seeds[0] );
145  }
146  // Specialized create function for TripleRand engine
147  template <>
149  return m_useTable ? std::make_unique<TripleRand>( m_row, m_col ) : std::make_unique<TripleRand>( m_seeds[0] );
150  }
151  // Specialized create function for DRand48Engine
152  template <>
154  return m_useTable ? std::make_unique<DRand48Engine>( m_row, m_col ) : std::make_unique<DRand48Engine>( m_seeds[0] );
155  }
156  // Specialized create function for Hurd160Engine
157  template <>
159  return m_useTable ? std::make_unique<Hurd160Engine>( m_row, m_col ) : std::make_unique<Hurd160Engine>( m_seeds[0] );
160  }
161  // Specialized create function for Hurd288Engine
162  template <>
164  return m_useTable ? std::make_unique<Hurd288Engine>( m_row, m_col ) : std::make_unique<Hurd288Engine>( m_seeds[0] );
165  }
166  // Specialized create function for RanecuEngine
167  template <>
169  return m_useTable ? std::make_unique<RanecuEngine>( m_row ) : std::make_unique<RanecuEngine>( m_seeds[0] );
170  }
171  // Specialized create function for RanshiEngine
172  template <>
174  return m_useTable ? std::make_unique<RanshiEngine>( m_row, m_col ) : std::make_unique<RanshiEngine>( m_seeds[0] );
175  }
176  // Specialized create function for RanluxEngine
177  template <>
179  return m_useTable ? std::make_unique<RanluxEngine>( m_row, m_col, m_lux )
180  : std::make_unique<RanluxEngine>( m_seeds[0], m_lux );
181  }
182  // Specialized create function for Ranlux64Engine
183  template <>
185  return m_useTable ? std::make_unique<Ranlux64Engine>( m_row, m_col, m_lux )
186  : std::make_unique<Ranlux64Engine>( m_seeds[0], m_lux );
187  }
188  // Specialized create function for MTwistEngine
189  template <>
191  return m_useTable ? std::make_unique<MTwistEngine>( m_row, m_col ) : std::make_unique<MTwistEngine>( m_seeds[0] );
192  }
193  // Specialized create function for HepJamesRandom
194  template <>
196  return m_useTable ? std::make_unique<HepJamesRandom>( m_row, m_col )
197  : std::make_unique<HepJamesRandom>( m_seeds[0] );
198  }
199 } // namespace HepRndm
200 
203 typedef HepRndm::Engine<TripleRand> e2;
205 typedef HepRndm::Engine<DRand48Engine> e3;
207 typedef HepRndm::Engine<Hurd160Engine> e4;
209 typedef HepRndm::Engine<Hurd288Engine> e5;
211 typedef HepRndm::Engine<HepJamesRandom> e6;
213 typedef HepRndm::Engine<MTwistEngine> e7;
215 typedef HepRndm::Engine<RanecuEngine> e8;
217 typedef HepRndm::Engine<Ranlux64Engine> e9;
219 typedef HepRndm::Engine<RanluxEngine> e10;
221 typedef HepRndm::Engine<RanshiEngine> e11;
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
System.h
std::vector< long >
std::back_inserter
T back_inserter(T... args)
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:315
RndmGenSvc.h
IIncidentSvc.h
HepRndm
Definition: HepRndmBaseEngine.h:37
e1
HepRndm::Engine< DualRand > e1
Definition: HepRndmEngines.cpp:201
HepRndmEngine.h
StatusCode
Definition: StatusCode.h:65
std::copy
T copy(T... args)
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
CLHEP
Definition: HepRndmBaseEngine.h:33
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
OffloadAtlasMCRecoScenario.seed
seed
Definition: OffloadAtlasMCRecoScenario.py:52
HepRndm::Engine
Definition: HepRndmEngine.h:35
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
std::unique_ptr< CLHEP::HepRandomEngine >
MsgStream.h