CMS 3D CMS Logo

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