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