CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BaseNumericalRandomGenerator.cc
Go to the documentation of this file.
3 
4 #include <cmath>
5 // #include <iostream>
6 
8  const RandomEngine* engine,
9  double xmin, double xmax, int n, int iter ) :
10  random(engine),
11  xmin(xmin), xmax(xmax), n(n), iter(iter)
12 {
13  f.resize(n);
14  sampling.resize(n);
15 }
16 
17 void
19 
20  m = n-1;
21  rmin = 0.;
22  deltar = (double)m-rmin;
23 
24  std::vector<double> a,y,z,xnew;
25  a.resize(n);
26  y.resize(n);
27  z.resize(n);
28  xnew.resize(n);
29 
30  double sig1 = 0.;
31 
32  // Initialize sampling
33  double du = (xmax-xmin)/(float)m;
34  sampling[0] = xmin;
35  for (int i=1; i<n; ++i)
36  sampling[i] = sampling[i-1] + du;
37 
38  // Starting point for iterations
39  for (int it=0; it<iter; ++it) {
40 
41  // Calculate function values
42  for (int i=0; i<n; ++i )
43  f[i] = function(sampling[i]);
44 
45  // Calculate bin areas
46  for (int i=0; i<m; ++i )
47  a[i] = (sampling[i+1]-sampling[i]) * (f[i+1]+f[i]) / 2.;
48 
49  // Calculate cumulative spectrum Y values
50  y[0]=0.;
51  for (int i=1; i<n; ++i )
52  y[i] = y[i-1] + a[i-1];
53 
54  // Put equidistant points on y scale
55  double dz = y[n-1]/(float)m;
56  z[0]=0;
57  for (int i=1; i<n; ++i )
58  z[i] = z[i-1] + dz;
59 
60  // Determine spacinf of Z points in between Y points
61  // From this, determine new X values and finally replace old values
62  xnew[0] = sampling[0];
63  xnew[n-1] = sampling[n-1];
64  int k=0;
65  for ( int i=1; i<m; ++i ) {
66  while ( y[k+1] < z[i] ) ++k;
67  double r = (z[i]-y[k]) / (y[k+1]-y[k]);
68  xnew[i] = sampling[k] + (sampling[k+1]-sampling[k])*r;
69  }
70 
71  for ( int i=0; i<n; ++i )
72  sampling[i] = xnew[i];
73 
74  sig1 = sig1 + y[m];
75  // std::cout << "BaseNumericalRandomGenerator::Iteration # " << it+1
76  // << " Integral = " << sig1/(float)(it+1)
77  // << std::endl;
78 
79  }
80 
81 }
82 
83 double
85 
86  double r=rmin+deltar*random->flatShoot();
87  int i=(int)r;
88  double s=r-(double)i;
89  // cout << " i,r,s = " << i << " " << r << " " << s << endl;
90  return sampling[i]+s*(sampling[i+1]-sampling[i]);
91 
92 }
93 
94 double
96 
97  double r=rmin+deltar*random->flatShoot();
98  int i=(int)r;
99  // double s=r-(double)i;
100  // cout << " i,r,s = " << i << " " << r << " " << s << endl;
101  double b = -std::log(f[i+1]/f[i]) / (sampling[i+1]-sampling[i]);
102  double a = f[i] * std::exp(b*sampling[i]);
103 
104  double umin = -a/b*std::exp(-b*sampling[i]);
105  double umax = -a/b*std::exp(-b*sampling[i+1]);
106  double u= (umax-umin) * random->flatShoot() + umin;
107 
108  return -std::log(-b/a*u) / b;
109 
110 }
111 
112 double
114 
115  double r=rmin+deltar*random->flatShoot();
116  int i=(int)r;
117  // double s=r-(double)i;
118  // cout << " i,r,s = " << i << " " << r << " " << s << endl;
119  double a = (f[i+1]-f[i]) / (sampling[i+1]-sampling[i]);
120  double b = f[i] - a*sampling[i];
121 
122  double umin = a*sampling[i]*sampling[i]/2. + b*sampling[i];
123  double umax = a*sampling[i+1]*sampling[i+1]/2. + b*sampling[i+1];
124  double u= (umax-umin) * random->flatShoot() + umin;
125 
126  return (-b+std::sqrt(b*b+2.*a*u))/a;
127 
128 }
129 
130 
132  if(x1<xmin||x2>xmax) return false;
133  if(x1>x2)
134  {
135  double tmp=x1;
136  x1=x2;
137  x2=tmp;
138  }
139 
140  unsigned ibin=1;
141  for(;ibin<(unsigned)n&&x1>sampling[ibin];++ibin);
142  unsigned ibin1=ibin;
143  for(;ibin<(unsigned)n&&x2>sampling[ibin];++ibin);
144  unsigned ibin2=ibin;
145 
146  // cout << sampling[ibin1-1] << " " << x1 << " " << sampling[ibin1] << endl;
147  // cout << sampling[ibin2-1] << " " << x2 << " " << sampling[ibin2] << endl;
148 
149  rmin = ibin1+(x1-sampling[ibin1])/(sampling[ibin1]-sampling[ibin1-1]);
150  deltar= ibin2+(x2-sampling[ibin2])/(sampling[ibin2]-sampling[ibin2-1]) - rmin;
151  // cout << rmin << " " << deltar << endl;
152  return true;
153 }
int i
Definition: DBlmapReader.cc:9
TRandom random
Definition: MVATrainer.cc:138
void initialize()
The initialization (numerical integarion, inversion)
double double double z
T sqrt(T t)
Definition: SSEVec.h:46
int k[5][pyjets_maxn]
double generate() const
The random generation according to function()
BaseNumericalRandomGenerator(const RandomEngine *engine, double xmin=0., double xmax=1., int n=1000, int iter=6)
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
bool setSubInterval(double x1, double x2)
To shoot in a given interval.