CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GammaFunctionGenerator.cc
Go to the documentation of this file.
1 //FAMOS headers
4 
6  random(engine)
7 {
8 
9  xmax = 30.;
10 
11  for(unsigned i=1;i<=12;++i)
12  {
13  // For all let's put the limit at 2.*(alpha-1) (alpha-1 is the max of the dist)
14  approxLimit.push_back(2*((double)i));
15  myIncompleteGamma.a().setValue((double)i);
17  theGammas.push_back(
18  GammaNumericalGenerator(random,(double)i,1.,0,approxLimit[i-1]+1.));
19  }
20  coreCoeff.push_back(0.); // alpha=1 not used
21  coreCoeff.push_back(1./8.24659e-01);
22  coreCoeff.push_back(1./7.55976e-01);
23  coreCoeff.push_back(1./7.12570e-01);
24  coreCoeff.push_back(1./6.79062e-01);
25  coreCoeff.push_back(1./6.65496e-01);
26  coreCoeff.push_back(1./6.48736e-01);
27  coreCoeff.push_back(1./6.25185e-01);
28  coreCoeff.push_back(1./6.09188e-01);
29  coreCoeff.push_back(1./6.06221e-01);
30  coreCoeff.push_back(1./6.05057e-01);
31 }
32 
34 
36 {
37  if(alpha<0.) return -1.;
38  if(badRange) return xmin/beta;
39  if(alpha<12)
40  {
41 
42  if (alpha == na)
43  {
44  return gammaInt ()/beta;
45  }
46  else if (na == 0)
47  {
48  return gammaFrac ()/beta;
49  }
50  else
51  {
52  double gi=gammaInt ();
53  double gf=gammaFrac ();
54  return (gi+gf)/beta;
55  }
56  }
57  else
58  {
59  // an other generator has to be used in such a case
60  return -1.;
61  }
62 }
63 
65 {
66  /* This is exercise 16 from Knuth; see page 135, and the solution is
67  on page 551. */
68 
69  double p, q, x, u, v;
70  p = M_E / (frac + M_E);
71  do
72  {
73  u = random->flatShoot();
74  v = random->flatShoot();
75 
76  if (u < p)
77  {
78  x = exp ((1 / frac) * log (v));
79  q = exp (-x);
80  }
81  else
82  {
83  x = 1 - log (v);
84  q = exp ((frac - 1) * log (x));
85  }
86  }
87  while (random->flatShoot() >= q);
88 
89  return x;
90 }
91 
93 {
94  // Exponential distribution : no approximation
95  if(na==1)
96  {
97  return xmin-log(random->flatShoot());
98  }
99 
100  unsigned gn=na-1;
101 
102  // are we sure to be in the tail
103  if(coreProba==0.)
104  return xmin-coreCoeff[gn]*log(random->flatShoot());
105 
106  // core-tail interval
107  if(random->flatShoot()<coreProba)
108  {
109  return theGammas[gn].gamma_lin();
110  }
111  // std::cout << " Tail called " << std::endl;
112  return approxLimit[gn]-coreCoeff[gn]*log(random->flatShoot());
113 }
114 
115 void GammaFunctionGenerator::setParameters(double a,double b, double xm)
116 {
117  // std::cout << "Setting parameters " << std::endl;
118  alpha=a;
119  beta=b;
120  xmin=xm*beta;
121  if(xm>xmax)
122  {
123  badRange=true;
124  return;
125  }
126  badRange=false;
127  na=0;
128 
129  if(alpha>0.&&alpha<12)
130  na=(unsigned)floor(alpha);
131 
132  frac=alpha-na;
133  // Now calculate the probability to shoot between approxLimit and xmax
134  // The Incomplete gamma is normalized to 1
135  if(na<=1) return;
136 
137  myIncompleteGamma.a().setValue((double)na);
138 
139  unsigned gn=na-1;
140  // std::cout << " na " << na << " xm " << xm << " beta " << beta << " xmin " << xmin << " approxLimit " << approxLimit[gn] << std::endl;
141  if(xmin>approxLimit[gn])
142  {
143  coreProba=0.;
144  }
145  else
146  {
147  double tmp=(xmin!=0.) ?myIncompleteGamma(xmin) : 0.;
148  coreProba=(integralToApproxLimit[gn]-tmp)/(1.-tmp);
149  theGammas[gn].setSubInterval(xmin,approxLimit[gn]);
150  }
151  // std::cout << " Proba " << coreProba << std::endl;
152 }
int i
Definition: DBlmapReader.cc:9
std::vector< GammaNumericalGenerator > theGammas
const RandomEngine * random
TRandom random
Definition: MVATrainer.cc:138
std::vector< double > approxLimit
std::vector< double > coreCoeff
Genfun::IncompleteGamma myIncompleteGamma
double gammaFrac() const
values 0&lt;a&lt;1.
void setParameters(double a, double b, double xm)
The parameters must be set before shooting.
GammaFunctionGenerator(const RandomEngine *engine)
Constructor.
double b
Definition: hdecay.h:120
double flatShoot(double xmin=0.0, double xmax=1.0) const
Definition: RandomEngine.h:30
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
double a
Definition: hdecay.h:121
double gf
Definition: hdecay.h:34
Definition: DDAxes.h:10
double gammaInt() const
integer values
virtual ~GammaFunctionGenerator()
Destructor.
std::vector< double > integralToApproxLimit