/
RandArrayFunction.h
118 lines (96 loc) · 3.78 KB
/
RandArrayFunction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef RANDARRAYFUNCTION_INCLUDED
#define RANDARRAYFUNCTION_INCLUDED
#include <vector>
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "CLHEP/Random/RandomEngine.h"
#include "CLHEP/Random/RandFlat.h"
extern CLHEP::HepRandomEngine* hjRandomEngine;
/*
Nikolai Amelin, Ludmila Malinina, Timur Pocheptsov (C) JINR/Dubna
amelin@sunhe.jinr.ru, malinina@sunhe.jinr.ru, pocheptsov@sunhe.jinr.ru
November. 2, 2005
*/
//This class is taken from the GEANT4 tool kit and changed!!!!!
//========================================================================================
//RandArrayFunction defines several methods for shooting generally distributed random values,
//given a user-defined probability distribution function.
//The probability distribution function Pdf must be provided by the user as an array of
//positive real numbers. The array size must also be provided. Pdf doesn't need to be
//normalized to 1.
// if IntType = 0 ( default value ) a uniform random number is
// generated using the StandardRand() engine. The uniform number is then transformed
// to the user's distribution using the cumulative probability
// distribution constructed from his histogram. The cumulative
// distribution is inverted using a binary search for the nearest
// bin boundary and a linear interpolation within the
// bin. RandArrayFunction therefore generates a constant density within
// each bin.
// if IntType = 1 no interpolation is performed and the result is a
// discrete distribution.
//A speculate set of Shoot()/ShootArray() and Fire()/FireArray() methods is provided
//to Shoot random numbers via an instantiated RandArrayFunction object. These methods
//act directly on the flat distribution provided by a StandardRand() engine.
//An Operator () is also provided.
// example.
// ...
// double* Pdf;
// int fNBins;
// ...
// RandArrayFunction FunctDist(Pdf,fNBins);
// ...
// double num = FunctDist.Shoot();//Shoot() provides the same functionality as Fire()
// example.
// ...
// double* Pdf;
// int fNBins;
// ...
// RandArrayFunction FunctDist(Pdf,fNBins);
// ...
// double num = FunctDist();
// example.
// ...
// double* Pdf;
// int fNBins;
// ...
// RandArrayFunction FunctDist(Pdf,fNBins);
// ...
// int size = 50;
// double* vect = new double[size];
// FunctDist.FireArray (size, vect);
//========================================================================================
class RandArrayFunction {
private:
std::vector<double> fIntegralPdf;
int fNBins;
double fOneOverNbins;
int fInterpolationType;
public:
RandArrayFunction(const double *aProbFunc, int theProbSize, int interpolationType = 0);
RandArrayFunction(int probSize, int interpolationType = 0);
double Shoot()const;
double Fire()const;
double operator()()const;
void ShootArray(int size, double *array)const;
void FireArray(int size, double *array)const;
void PrepareTable(const double *aProbFunc);
private:
void UseFlatDistribution();
double MapRandom(double rand)const;
double StandardRand()const;
};
inline double RandArrayFunction::StandardRand() const {
return CLHEP::RandFlat::shoot(hjRandomEngine);
}
inline double RandArrayFunction::Fire() const {
return MapRandom(StandardRand());
}
inline double RandArrayFunction::Shoot() const {
return Fire();
}
inline double RandArrayFunction::operator()() const {
return Fire();
}
inline void RandArrayFunction::ShootArray(int size, double *array) const {
FireArray(size, array);
}
#endif