The Gaudi Framework  master (181af51f)
Loading...
Searching...
No Matches
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
21#include <GaudiKernel/SmartIF.h>
22
24
25#include <AIDA/IHistogram1D.h>
26using AIDA::IHistogram1D;
27
28// Example related include files
29#include "RandomNumberAlg.h"
30
31namespace {
32 namespace QuasiRandom = Gaudi::Utils::QuasiRandom;
33}
34
36
37
40RandomNumberAlg::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 {
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
111 m_initial = QuasiRandom::mixString( name().size(), name() );
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
151 m_numbers.finalize().ignore();
152 return StatusCode::SUCCESS;
153}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
SmartIF< IHistogramSvc > & histoSvc() const
The standard histogram service.
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
StatusCode initialize() override
the default (empty) implementation of IStateful::initialize() method
Definition Algorithm.h:175
SmartIF< IRndmGenSvc > & randSvc() const
The standard RandomGen service, Return a pointer to the service if present.
const std::string & name() const override
The identifying name of the algorithm object.
SmartIF< INTupleSvc > & ntupleSvc() const
The standard N tuple service.
virtual AIDA::IHistogram1D * book(const std::string &fullPath, const std::string &title, int binsX, double lowX, double highX)=0
Book histogram and register it with the histogram data store.
virtual NTuple::Tuple * book(const std::string &fullPath, const CLID &type, const std::string &title)=0
Book Ntuple and register it with the data store.
virtual StatusCode generator(const IRndmGen::Param &par, IRndmGen *&refpGen)=0
Add a Generator factory.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
A small algorithm class using the random number service.
NTuple::Item< float > m_exponential
Rndm::Numbers m_numbers
Allocate wrapper for random number generator.
~RandomNumberAlg() override
Standard Destructor.
StatusCode finalize() override
Customized finalisation.
NTuple::Item< float > m_poisson
uint32_t m_initial
Initial seed to fill deterministic random numbers.
StatusCode initialize() override
Customized initialisation.
NTuple::Item< float > m_gauss
NTuple::Item< int > m_int
N-tuple items.
RandomNumberAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor: A constructor of this form must be provided.
NTuple::Item< uint32_t > m_deter
StatusCode execute() override
Event callback.
NTuple::Tuple * m_ntuple
Pointer to N-tuple.
Parameters for the Gauss random number generation.
Parameters for the Gauss random number generation.
Parameters for the Gaussian tail number generation.
Random number accessor This small class encapsulates the use of the random number generator.
Parameters for the Poisson distributed random number generation with a given mean.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
GAUDI_API const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
Definition System.cpp:310
STL namespace.