ATLAS Offline Software
AtlasCLHEP_RandomGenerators_test.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
8 #include "CLHEP/Random/Ranlux64Engine.h"
9 #include "CLHEP/Random/RanecuEngine.h"
10 #include "CLHEP/Random/MTwistEngine.h"
11 #include "CLHEP/Random/RandGauss.h"
12 #include "CLHEP/Random/RandGaussQ.h"
13 #include "CLHEP/Random/RandExponential.h"
14 #include "CLHEP/Random/RandGaussZiggurat.h"
15 #include "CLHEP/Random/RandExpZiggurat.h"
16 
18 
19 // Gaudi includes
20 #include "GaudiKernel/DataSvc.h"
21 #include "GaudiKernel/Chrono.h"
22 #include "GaudiKernel/ISvcLocator.h"
23 #include "GaudiKernel/MsgStream.h"
24 #include "GaudiKernel/TypeNameString.h"
25 
26 #include <string>
27 
28 //#include "rnorrexp_org.icc"
29 
31 
32  AtlasCLHEP_RandomGenerators_test::AtlasCLHEP_RandomGenerators_test(const std::string& name, ISvcLocator* pSvcLocator):
33  AthAlgorithm(name,pSvcLocator),
34  m_chrono(0),
35  m_ranlux64(nullptr),
36  m_ranecu(nullptr),
37  m_mtwist(nullptr),
38  m_histSvc(nullptr),
39  m_rndmSvc1("AtRanluxGenSvc", name),
40  m_randomEngine1(0),
41  m_randomEngineName1("rnd_AtRanluxGenSvc"),
42  m_rndmSvc2("AtDSFMTGenSvc", name),
43  m_randomEngine2(0),
44  m_randomEngineName2("rnd_AtDSFMTGenSvc")
45  {
46  declareProperty("ntest", m_ntest);
47  }
48 
49  //__________________________________________________________________________
51  }
52 
53  //__________________________________________________________________________
55  {
56  SmartIF<IChronoStatSvc> smartChrono{serviceLocator()->service("ChronoStatSvc")};
57  m_chrono = smartChrono.get();
58  if (!m_chrono) {
59  ATH_MSG_FATAL( "Cannot retrieve ChronoStatSvc ");
60  return StatusCode::FAILURE;
61  }
62 
63  m_ranlux64=new CLHEP::Ranlux64Engine();
64  m_ranecu=new CLHEP::RanecuEngine();
65  m_mtwist=new CLHEP::MTwistEngine();
66 
67  // Random number service
68  if ( m_rndmSvc1.retrieve().isFailure() ) {
69  msg(MSG::ERROR)<< "Could not retrieve " << m_rndmSvc1 << endmsg;
70  return StatusCode::FAILURE;
71  }
72  if ( m_rndmSvc2.retrieve().isFailure() ) {
73  msg(MSG::ERROR)<< "Could not retrieve " << m_rndmSvc2 << endmsg;
74  return StatusCode::FAILURE;
75  }
76 
77  //Get own engine with own seeds:
79  if (!m_randomEngine1) {
80  msg(MSG::ERROR)<< "Could not get random engine '" << m_randomEngineName1 << "'" << endmsg;
81  return StatusCode::FAILURE;
82  }
84  if (!m_randomEngine2) {
85  msg(MSG::ERROR)<< "Could not get random engine '" << m_randomEngineName2 << "'" << endmsg;
86  return StatusCode::FAILURE;
87  }
88  SmartIF<ITHistSvc> smartHistSvc{serviceLocator()->service("THistSvc")};
89  m_histSvc = smartHistSvc.get();
90  if (!m_histSvc) {
91  msg(MSG::ERROR) << "Cannot allocate THistSvc service" << endmsg;
92  return StatusCode::FAILURE;
93  }
94 
95  m_hflat=new TH1D("RandFlat","RandFlat",1000,0,1);
96  if(!m_histSvc->regHist(std::string("/PLOTS/") + m_hflat->GetName(), m_hflat).isSuccess()) {
97  msg(MSG::WARNING) << "Could not register histogram "<< m_hflat->GetName() << endmsg;
98  }
99 
100  m_hgauss1=new TH1D("RandGauss","RandGauss",700,-7,7);
101  if(!m_histSvc->regHist(std::string("/PLOTS/") + m_hgauss1->GetName(), m_hgauss1).isSuccess()) {
102  msg(MSG::WARNING) << "Could not register histogram "<< m_hgauss1->GetName() << endmsg;
103  }
104 
105  m_hgauss2=new TH1D("RandGaussQ","RandGaussQ",700,-7,7);
106  if(!m_histSvc->regHist(std::string("/PLOTS/") + m_hgauss2->GetName(), m_hgauss2).isSuccess()) {
107  msg(MSG::WARNING) << "Could not register histogram "<< m_hgauss2->GetName() << endmsg;
108  }
109 
110  m_hgauss3=new TH1D("RandGaussZigurat","RandGaussZigurat",700,-7,7);
111  if(!m_histSvc->regHist(std::string("/PLOTS/") + m_hgauss3->GetName(), m_hgauss3).isSuccess()) {
112  msg(MSG::WARNING) << "Could not register histogram "<< m_hgauss3->GetName() << endmsg;
113  }
114 
115  m_hexp1=new TH1D("RandExponential","RandExponential",900,0,30);
116  if(!m_histSvc->regHist(std::string("/PLOTS/") + m_hexp1->GetName(), m_hexp1).isSuccess()) {
117  msg(MSG::WARNING) << "Could not register histogram "<< m_hexp1->GetName() << endmsg;
118  }
119 
120  m_hexp2=new TH1D("RandExpZiggurat","RandExpZiggurat",900,0,30);
121  if(!m_histSvc->regHist(std::string("/PLOTS/") + m_hexp2->GetName(), m_hexp2).isSuccess()) {
122  msg(MSG::WARNING) << "Could not register histogram "<< m_hexp2->GetName() << endmsg;
123  }
124 
125  m_hbin1=new TH1D("RandBinomial","RandBinomial",4,-0.5,3.5);
126  if(!m_histSvc->regHist(std::string("/PLOTS/") + m_hbin1->GetName(), m_hbin1).isSuccess()) {
127  msg(MSG::WARNING) << "Could not register histogram "<< m_hbin1->GetName() << endmsg;
128  }
129 
130  m_hbin2=new TH1D("RandBinomialFixedP","RandBinomialFixedP",4,-0.5,3.5);
131  if(!m_histSvc->regHist(std::string("/PLOTS/") + m_hbin2->GetName(), m_hbin2).isSuccess()) {
132  msg(MSG::WARNING) << "Could not register histogram "<< m_hbin2->GetName() << endmsg;
133  }
134 
135 
136  return StatusCode::SUCCESS;
137  }
138 
140  {
141 
142  return StatusCode::SUCCESS;
143  }
144 
145  //_________________________________________________________________________
147  {
148  int ntest=m_ntest;
149 
150  msg(MSG::DEBUG)<<"event="<<m_ievent<<" ntest="<<ntest<<endmsg;
151 
152  m_chrono -> chronoStart("flat");
153  double sum_flat=0;
154  for(int i=0;i<ntest;++i) {
155  double g=m_randomEngine2->flat();
156  m_hflat->Fill(g);
157  sum_flat+=g;
158  }
159  sum_flat/=ntest;
160  m_chrono -> chronoStop("flat");
161  msg(MSG::DEBUG)<<" avg "<<"flat"<<"="<<sum_flat<<endmsg;
162 
163  m_chrono -> chronoStart("RandGauss");
164  double sum_rnd1=0;
165  for(int i=0;i<ntest;++i) {
166  double g=CLHEP::RandGauss::shoot(m_randomEngine2);
167  m_hgauss1->Fill(g);
168  sum_rnd1+=g;
169  }
170  sum_rnd1/=ntest;
171  m_chrono -> chronoStop("RandGauss");
172  msg(MSG::DEBUG)<<" avg "<<"RandGauss"<<"="<<sum_rnd1<<endmsg;
173 
174  m_chrono -> chronoStart("RandGaussQ");
175  double sum_rnd2=0;
176  for(int i=0;i<ntest;++i) {
177  double g=CLHEP::RandGaussQ::shoot(m_randomEngine2);
178  m_hgauss2->Fill(g);
179  sum_rnd2+=g;
180  }
181  sum_rnd2/=ntest;
182  m_chrono -> chronoStop("RandGaussQ");
183  msg(MSG::DEBUG)<<" avg "<<"RandGaussQ"<<"="<<sum_rnd2<<endmsg;
184 
185  m_chrono -> chronoStart("RandGaussZiggurat");
186  double sum_zig=0;
187  for(int i=0;i<ntest;++i) {
188  double g=CLHEP::RandGaussZiggurat::shoot(m_randomEngine2);
189  m_hgauss3->Fill(g);
190  sum_zig+=g;
191  }
192  sum_zig/=ntest;
193  m_chrono -> chronoStop("RandGaussZiggurat");
194  msg(MSG::DEBUG)<<" avg "<<"RandGaussZiggurat"<<"="<<sum_zig<<endmsg;
195 
196  m_chrono -> chronoStart("RandExponential");
197  double sum_exp=0;
198  for(int i=0;i<ntest;++i) {
199  double g=CLHEP::RandExponential::shoot(m_randomEngine2);
200  m_hexp1->Fill(g);
201  sum_exp+=g;
202  }
203  sum_exp/=ntest;
204  m_chrono -> chronoStop("RandExponential");
205  msg(MSG::DEBUG)<<" avg "<<"RandExponential"<<"="<<sum_exp<<endmsg;
206 
207  m_chrono -> chronoStart("RandExpZiggurat");
208  double sum_expZ=0;
209  for(int i=0;i<ntest;++i) {
210  double g=CLHEP::RandExpZiggurat::shoot(m_randomEngine2);
211  m_hexp2->Fill(g);
212  sum_expZ+=g;
213  }
214  sum_expZ/=ntest;
215  m_chrono -> chronoStop("RandExpZiggurat");
216  msg(MSG::DEBUG)<<" avg "<<"RandExpZiggurat"<<"="<<sum_expZ<<endmsg;
217 
218  CLHEP::RandBinomialFixedP lu(*m_randomEngine2,1,0.4,12); // Needs to be called with the random engine as reference, as a pointer gets deleted by CLHEP::RandBinomial!
219 
220  for(int n=1;n<6;n+=1) {
221  double sum_bin=0;
222  m_chrono -> chronoStart("RandBinomial_"+std::to_string(n));
223  for(int i=0;i<ntest;++i) {
224  double g=CLHEP::RandBinomial::shoot(m_randomEngine2,n,0.4);
225  if(n==3) m_hbin1->Fill(g);
226  sum_bin+=g;
227  }
228  m_chrono -> chronoStop("RandBinomial_"+std::to_string(n));
229  sum_bin/=ntest;
230  msg(MSG::DEBUG) << " avg RandBinomial("<<n<<")="<<sum_bin<< endmsg;
231 
232  double sum_stdbin=0;
233  m_chrono -> chronoStart("RBinomialFP_"+std::to_string(n));
234  for(int i=0;i<ntest;++i) {
235  double g=lu.fire(m_randomEngine2, n );
236  if(n==3) m_hbin2->Fill(g);
237  sum_stdbin+=g;
238  }
239  m_chrono -> chronoStop("RBinomialFP_"+std::to_string(n));
240  sum_stdbin/=ntest;
241  msg(MSG::DEBUG) << " avg RandBinomialFixedP("<<n<<")="<<sum_stdbin<< endmsg;
242  }
243 
244 
245  /*
246 
247  m_chrono -> chronoStart(m_randomEngineName1);
248  double sum_rnd1=0;
249  for(int i=0;i<ntest;++i) {
250  sum_rnd1+=m_randomEngine1->flat();
251  }
252  sum_rnd1/=ntest;
253  m_chrono -> chronoStop(m_randomEngineName1);
254  msg(MSG::DEBUG)<<" avg "<<m_randomEngineName1<<"="<<sum_rnd1<<endmsg;
255 
256  m_chrono -> chronoStart(m_randomEngineName2);
257  double sum_rnd2=0;
258  for(int i=0;i<ntest;++i) {
259  sum_rnd2+=m_randomEngine2->flat();
260  }
261  sum_rnd2/=ntest;
262  m_chrono -> chronoStop(m_randomEngineName2);
263  msg(MSG::DEBUG)<<" avg "<<m_randomEngineName2<<"="<<sum_rnd2<<endmsg;
264 
265  m_chrono -> chronoStart("ranlux64");
266  m_ranlux64->setSeed(seed,1);
267  double sum_ranlux64=0;
268  for(int i=0;i<ntest;++i) {
269  sum_ranlux64+=m_ranlux64->flat();
270  }
271  sum_ranlux64/=ntest;
272  m_chrono -> chronoStop("ranlux64");
273  msg(MSG::DEBUG)<<" avg ranlux64="<<sum_ranlux64<<endmsg;
274 
275  m_chrono -> chronoStart("ranecu");
276  m_ranecu->setSeed(seed,1);
277  double sum_ranecu=0;
278  for(int i=0;i<ntest;++i) {
279  sum_ranecu+=m_ranecu->flat();
280  }
281  sum_ranecu/=ntest;
282  m_chrono -> chronoStop("ranecu");
283  msg(MSG::DEBUG)<<" avg ranecu="<<sum_ranecu<<endmsg;
284 
285  m_chrono -> chronoStart("mtwist");
286  m_mtwist->setSeed(seed,1);
287  double sum_mtwist=0;
288  for(int i=0;i<ntest;++i) {
289  sum_mtwist+=m_mtwist->flat();
290  }
291  sum_mtwist/=ntest;
292  m_chrono -> chronoStop("mtwist");
293  msg(MSG::DEBUG)<<" avg mtwist="<<sum_mtwist<<endmsg;
294 
295  */
296 
297  ++m_ievent;
298 
299  return StatusCode::SUCCESS;
300  }
301 
302 } // end of namespace bracket
303 
304 
305 
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_hbin2
TH1 * m_hbin2
Definition: AtlasCLHEP_RandomGenerators_test.h:57
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::finalize
StatusCode finalize()
Definition: AtlasCLHEP_RandomGenerators_test.cxx:139
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_hexp1
TH1 * m_hexp1
Definition: AtlasCLHEP_RandomGenerators_test.h:53
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_histSvc
ITHistSvc * m_histSvc
Definition: AtlasCLHEP_RandomGenerators_test.h:38
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_rndmSvc1
ServiceHandle< IAtRndmGenSvc > m_rndmSvc1
Definition: AtlasCLHEP_RandomGenerators_test.h:40
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_hgauss3
TH1 * m_hgauss3
Definition: AtlasCLHEP_RandomGenerators_test.h:51
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_rndmSvc2
ServiceHandle< IAtRndmGenSvc > m_rndmSvc2
Definition: AtlasCLHEP_RandomGenerators_test.h:43
AtlasCLHEP_RandomGenerators
Definition: AtlasCLHEP_RandomGenerators_test.cxx:30
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_hgauss1
TH1 * m_hgauss1
Definition: AtlasCLHEP_RandomGenerators_test.h:49
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_hbin1
TH1 * m_hbin1
Definition: AtlasCLHEP_RandomGenerators_test.h:56
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_chrono
IChronoStatSvc * m_chrono
central crono service
Definition: AtlasCLHEP_RandomGenerators_test.h:33
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::AtlasCLHEP_RandomGenerators_test
AtlasCLHEP_RandomGenerators_test(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
Definition: AtlasCLHEP_RandomGenerators_test.cxx:32
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_randomEngine1
CLHEP::HepRandomEngine * m_randomEngine1
Definition: AtlasCLHEP_RandomGenerators_test.h:41
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_ranecu
CLHEP::HepRandomEngine * m_ranecu
Definition: AtlasCLHEP_RandomGenerators_test.h:36
CLHEP::RandBinomialFixedP::fire
double fire()
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_ievent
int m_ievent
Definition: AtlasCLHEP_RandomGenerators_test.h:61
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_ntest
int m_ntest
Definition: AtlasCLHEP_RandomGenerators_test.h:59
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_mtwist
CLHEP::HepRandomEngine * m_mtwist
Definition: AtlasCLHEP_RandomGenerators_test.h:37
CLHEP::RandBinomialFixedP
Definition: RandBinomialFixedP.h:18
AthAlgorithm
Definition: AthAlgorithm.h:47
RandBinomialFixedP.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
DEBUG
#define DEBUG
Definition: page_access.h:11
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_randomEngineName1
std::string m_randomEngineName1
Definition: AtlasCLHEP_RandomGenerators_test.h:42
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_hexp2
TH1 * m_hexp2
Definition: AtlasCLHEP_RandomGenerators_test.h:54
AtlasCLHEP_RandomGenerators_test.h
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_randomEngineName2
std::string m_randomEngineName2
Definition: AtlasCLHEP_RandomGenerators_test.h:45
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::execute
StatusCode execute()
Definition: AtlasCLHEP_RandomGenerators_test.cxx:146
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::initialize
StatusCode initialize()
Definition: AtlasCLHEP_RandomGenerators_test.cxx:54
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_randomEngine2
CLHEP::HepRandomEngine * m_randomEngine2
Definition: AtlasCLHEP_RandomGenerators_test.h:44
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_hflat
TH1 * m_hflat
Definition: AtlasCLHEP_RandomGenerators_test.h:47
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::~AtlasCLHEP_RandomGenerators_test
~AtlasCLHEP_RandomGenerators_test()
Definition: AtlasCLHEP_RandomGenerators_test.cxx:50
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_ranlux64
CLHEP::HepRandomEngine * m_ranlux64
Definition: AtlasCLHEP_RandomGenerators_test.h:35
AtlasCLHEP_RandomGenerators::AtlasCLHEP_RandomGenerators_test::m_hgauss2
TH1 * m_hgauss2
Definition: AtlasCLHEP_RandomGenerators_test.h:50