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