The Gaudi Framework  master (37c0b60a)
RandomNumberAlg.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 // Framework include files
12 #include <GaudiKernel/DataObject.h>
15 #include <GaudiKernel/INTupleSvc.h>
16 #include <GaudiKernel/IRndmGen.h>
19 #include <GaudiKernel/MsgStream.h>
21 #include <GaudiKernel/SmartIF.h>
22 
23 #include <GaudiUtils/QuasiRandom.h>
24 
25 #include <AIDA/IHistogram1D.h>
26 using AIDA::IHistogram1D;
27 
28 // Example related include files
29 #include "RandomNumberAlg.h"
30 
31 namespace {
33 }
34 
36 
37 
40 RandomNumberAlg::RandomNumberAlg( const std::string& name, ISvcLocator* pSvcLocator )
41  : Algorithm( name, pSvcLocator ) {}
42 
43 // Standard destructor
45  // do not print messages if we are created in genconf
46  const std::string cmd = System::cmdLineArgs()[0];
47  if ( cmd.find( "genconf" ) != std::string::npos ) return;
48 
49  std::cout << "Destructor Called for " << name() << std::endl;
50 }
51 
52 // The "functional" part of the class: For the EmptyAlgorithm example they do
53 // nothing apart from print out info messages.
56  if ( !status ) return status;
57 
58  //
59  // The first example is for purists:
60  // Every step is done by hand....tends to become complicated,
61  // but shows the usage of the raw interfaces
62  //
63  // Get random number generator:
64  auto gen = randSvc()->generator( Rndm::Gauss( 0.5, 0.2 ) );
65  if ( gen ) {
66  std::vector<double> numbers;
67  gen->shootArray( numbers, 5000 ).ignore();
68  IHistogram1D* his = histoSvc()->book( "1", "Gauss", 40, 0., 3. );
69  for ( unsigned int i = 0; i < numbers.size(); i++ ) his->fill( numbers[i], 1.0 );
70 
71  for ( int j = 0; j < 5000; j++ ) his->fill( gen->shoot(), 1.0 );
72  }
73 
74  //
75  // Now we do it as proposed for LOCAL usage of the wrapper
76  //
77  Rndm::Numbers exponential( randSvc(), Rndm::Exponential( 0.2 ) );
78  if ( exponential ) {
79  IHistogram1D* his = histoSvc()->book( "2", "Exponential", 40, 0., 3. );
80  for ( long j = 0; j < 5000; j++ ) his->fill( exponential(), 1.0 );
81  } else {
82  return StatusCode::FAILURE;
83  }
84 
85  //
86  // Now we do it as proposed for GLOBAL usage of the wrapper
87  // - Initialize the wrapper allocated in the header file
88  //
89  status = m_numbers.initialize( randSvc(), Rndm::Poisson( 0.3 ) );
90  if ( !status.isSuccess() ) { return status; }
91 
92  // The GLOBAL wrapper is now initialized and ready for use.
93  // The code below could go anywhere. It is only for simplicity
94  // in the "initialize" method!
95  {
96  IHistogram1D* hispoisson = histoSvc()->book( "3", "Poisson", 40, 0., 3. );
97  for ( long j = 0; j < 5000; j++ ) hispoisson->fill( m_numbers(), 1.0 );
98  }
99  //
100  // Test Gaussian Tail distribution
101  //
102  Rndm::Numbers gaussiantail( randSvc(), Rndm::GaussianTail( 20., 10. ) );
103  if ( gaussiantail ) {
104  IHistogram1D* his = histoSvc()->book( "4", "GaussianTail", 50, 0., 50. );
105  for ( long j = 0; j < 50009; j++ ) his->fill( gaussiantail(), 1.0 );
106  } else {
107  return StatusCode::FAILURE;
108  }
109 
110  // Initial randomness for deterministic random numbers
112 
113  // Book N-tuple
114  m_ntuple = ntupleSvc()->book( "/NTUPLES/FILE1/100", CLID_RowWiseTuple, "Hello World" );
115  if ( m_ntuple ) {
116  status = m_ntuple->addItem( "Event#", m_int );
117  status = m_ntuple->addItem( "DeterInt", m_deter );
118  status = m_ntuple->addItem( "Gauss", m_gauss );
119  status = m_ntuple->addItem( "Exp", m_exponential );
120  status = m_ntuple->addItem( "Poisson", m_poisson );
121  }
122  return status;
123 }
124 
126  StatusCode status;
127  static int count = 0;
128 
129  Rndm::Numbers gauss( randSvc(), Rndm::Gauss( 0.5, 0.2 ) );
130  Rndm::Numbers exponential( randSvc(), Rndm::Exponential( 0.2 ) );
131  Rndm::Numbers poisson( randSvc(), Rndm::Poisson( 0.3 ) );
132 
133  // Return integer in interval [0, size) from random integer in interval [0, MAX_INT]
134  auto scale = []( uint32_t x, uint32_t size ) {
135  const uint32_t denom = boost::integer_traits<uint32_t>::const_max / size;
136  return x / denom;
137  };
138 
139  m_int = ++count;
140  m_deter = scale( QuasiRandom::mix32( m_initial, m_int ), 100 );
141  m_gauss = (float)gauss();
142  m_exponential = (float)exponential();
143  m_poisson = (float)poisson();
144 
145  status = m_ntuple->write();
146  if ( !status.isSuccess() ) { error() << "Cannot fill NTuple" << endmsg; }
147  return StatusCode::SUCCESS;
148 }
149 
152  return StatusCode::SUCCESS;
153 }
RandomNumberAlg::m_deter
NTuple::Item< uint32_t > m_deter
Definition: RandomNumberAlg.h:35
std::string
STL class.
details::size
constexpr auto size(const T &, Args &&...) noexcept
Definition: AnyDataWrapper.h:23
Gaudi::Algorithm::randSvc
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
Definition: Algorithm.cpp:563
RandomNumberAlg::m_gauss
NTuple::Item< float > m_gauss
Definition: RandomNumberAlg.h:36
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:526
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
RandomNumberAlg.h
RandomNumberAlg::m_initial
uint32_t m_initial
Initial seed to fill deterministic random numbers.
Definition: RandomNumberAlg.h:30
std::vector< double >
std::string::find
T find(T... args)
std::vector::size
T size(T... args)
ISvcLocator
Definition: ISvcLocator.h:46
Gaudi::Algorithm::initialize
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition: Algorithm.h:178
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
Gaudi::Algorithm::histoSvc
SmartIF< IHistogramSvc > & histoSvc() const
The standard histogram service.
Definition: Algorithm.cpp:561
IRndmGenSvc.h
RandomNumberAlg::execute
StatusCode execute() override
Event callback.
Definition: RandomNumberAlg.cpp:125
Rndm::GaussianTail
Parameters for the Gaussian tail number generation.
Definition: RndmGenerators.h:324
IDataProviderSvc.h
RandomNumberAlg::m_int
NTuple::Item< int > m_int
N-tuple items.
Definition: RandomNumberAlg.h:34
SmartIF.h
IRndmGen.h
RandomNumberAlg::m_numbers
Rndm::Numbers m_numbers
Allocate wrapper for random number generator.
Definition: RandomNumberAlg.h:28
StatusCode
Definition: StatusCode.h:65
Rndm::Gauss
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:32
Rndm::Numbers
Random number accessor This small class encapsulates the use of the random number generator.
Definition: RndmGenerators.h:359
ProduceConsume.j
j
Definition: ProduceConsume.py:104
std::cout
INTuple::write
virtual StatusCode write()=0
Write record of the NTuple (Shortcut of writeRecord)
Rndm::Numbers::finalize
virtual StatusCode finalize()
Finalization.
Definition: RndmGenerators.cpp:36
Gaudi::Utils::QuasiRandom::mixString
uint32_t mixString(uint32_t state, const std::string &extra)
mix some 'extra' entropy into 'state' and return result
Definition: QuasiRandom.cpp:41
RandomNumberAlg::~RandomNumberAlg
~RandomNumberAlg() override
Standard Destructor.
Definition: RandomNumberAlg.cpp:44
SmartDataPtr.h
RandomNumberAlg::finalize
StatusCode finalize() override
Customized finalisation.
Definition: RandomNumberAlg.cpp:150
Gaudi::Utils::QuasiRandom::mix32
uint32_t mix32(uint32_t state, uint32_t extra)
mix some 'extra' entropy into 'state' and return result
Definition: QuasiRandom.cpp:33
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
NTuple::Tuple::addItem
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:516
RandomNumberAlg::initialize
StatusCode initialize() override
Customized initialisation.
Definition: RandomNumberAlg.cpp:54
RandomNumberAlg
Definition: RandomNumberAlg.h:25
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
Rndm::Numbers::initialize
virtual StatusCode initialize(const SmartIF< IRndmGenSvc > &svc, const IRndmGen::Param &par)
Initialization.
Definition: RndmGenerators.cpp:30
Gaudi::Utils::QuasiRandom
Definition: QuasiRandom.h:38
Gaudi::Units::gauss
constexpr double gauss
Definition: SystemOfUnits.h:252
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
QuasiRandom.h
std::endl
T endl(T... args)
DataObject.h
std
STL namespace.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
System::cmdLineArgs
GAUDI_API const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
Definition: System.cpp:365
Rndm::Exponential
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:56
RandomNumberAlg::m_poisson
NTuple::Item< float > m_poisson
Definition: RandomNumberAlg.h:38
Rndm::Poisson
Parameters for the Poisson distributed random number generation with a given mean.
Definition: RndmGenerators.h:209
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
RandomNumberAlg::m_exponential
NTuple::Item< float > m_exponential
Definition: RandomNumberAlg.h:37
ISvcLocator.h
RandomNumberAlg::m_ntuple
NTuple::Tuple * m_ntuple
Pointer to N-tuple.
Definition: RandomNumberAlg.h:32
Gaudi::Algorithm::ntupleSvc
SmartIF< INTupleSvc > & ntupleSvc() const
The standard N tuple service.
Definition: Algorithm.cpp:562
IHistogramSvc.h
INTupleSvc.h
MsgStream.h