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 }
DDAxes::y
mps_fire.i
i
Definition: mps_fire.py:355
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
BaseNumericalRandomGenerator::setSubInterval
bool setSubInterval(double x1, double x2)
To shoot in a given interval.
Definition: BaseNumericalRandomGenerator.cc:113
BaseNumericalRandomGenerator::f
std::vector< double > f
Definition: BaseNumericalRandomGenerator.h:59
BaseNumericalRandomGenerator::generateExp
double generateExp(RandomEngineAndDistribution const *) const
Definition: BaseNumericalRandomGenerator.cc:83
testProducerWithPsetDescEmpty_cfi.x2
x2
Definition: testProducerWithPsetDescEmpty_cfi.py:28
BaseNumericalRandomGenerator::generateLin
double generateLin(RandomEngineAndDistribution const *) const
Definition: BaseNumericalRandomGenerator.cc:98
deltar
Definition: deltar.py:1
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
RandomEngineAndDistribution.h
testProducerWithPsetDescEmpty_cfi.x1
x1
Definition: testProducerWithPsetDescEmpty_cfi.py:33
alignCSCRings.s
s
Definition: alignCSCRings.py:92
BaseNumericalRandomGenerator::initialize
void initialize()
The initialization (numerical integarion, inversion)
Definition: BaseNumericalRandomGenerator.cc:13
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
BaseNumericalRandomGenerator::generate
double generate(RandomEngineAndDistribution const *) const
The random generation according to function()
Definition: BaseNumericalRandomGenerator.cc:75
DDAxes::z
dqmdumpme.k
k
Definition: dqmdumpme.py:60
b
double b
Definition: hdecay.h:118
a
double a
Definition: hdecay.h:119
BaseNumericalRandomGenerator::xmin
double xmin
Definition: BaseNumericalRandomGenerator.h:60
createfilelist.int
int
Definition: createfilelist.py:10
BaseNumericalRandomGenerator::rmin
double rmin
Definition: BaseNumericalRandomGenerator.h:62
BaseNumericalRandomGenerator.h
BaseNumericalRandomGenerator::n
int n
Definition: BaseNumericalRandomGenerator.h:61
RandomEngineAndDistribution::flatShoot
double flatShoot(double xmin=0.0, double xmax=1.0) const
Definition: RandomEngineAndDistribution.h:27
BaseNumericalRandomGenerator::sampling
std::vector< double > sampling
Definition: BaseNumericalRandomGenerator.h:58
alignCSCRings.r
r
Definition: alignCSCRings.py:93
BaseNumericalRandomGenerator::iter
int iter
Definition: BaseNumericalRandomGenerator.h:61
BaseNumericalRandomGenerator::xmax
double xmax
Definition: BaseNumericalRandomGenerator.h:60
BaseNumericalRandomGenerator::m
int m
Definition: BaseNumericalRandomGenerator.h:65
PVValHelper::dz
Definition: PVValidationHelpers.h:50
TrackerOfflineValidation_Dqm_cff.xmax
xmax
Definition: TrackerOfflineValidation_Dqm_cff.py:11
BaseNumericalRandomGenerator::BaseNumericalRandomGenerator
BaseNumericalRandomGenerator(double xmin=0., double xmax=1., int n=1000, int iter=6)
Definition: BaseNumericalRandomGenerator.cc:7
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
TrackerOfflineValidation_Dqm_cff.xmin
xmin
Definition: TrackerOfflineValidation_Dqm_cff.py:10
JetChargeProducer_cfi.exp
exp
Definition: JetChargeProducer_cfi.py:6
RandomEngineAndDistribution
Definition: RandomEngineAndDistribution.h:18