CMS 3D CMS Logo

List of all members | Public Member Functions | Protected Attributes | Private Attributes
BaseNumericalRandomGenerator Class Referenceabstract

#include <BaseNumericalRandomGenerator.h>

Inheritance diagram for BaseNumericalRandomGenerator:
GammaNumericalGenerator HistogramGenerator LandauFluctuationGenerator

Public Member Functions

 BaseNumericalRandomGenerator (double xmin=0., double xmax=1., int n=1000, int iter=6)
 
virtual double function (double x)=0
 
double generate (RandomEngineAndDistribution const *) const
 The random generation according to function() More...
 
double generateExp (RandomEngineAndDistribution const *) const
 
double generateLin (RandomEngineAndDistribution const *) const
 
void initialize ()
 The initialization (numerical integarion, inversion) More...
 
bool setSubInterval (double x1, double x2)
 To shoot in a given interval. More...
 
virtual ~BaseNumericalRandomGenerator ()
 Default destructor. More...
 

Protected Attributes

double deltar
 
std::vector< double > f
 
int iter
 
int n
 
double rmin
 
std::vector< double > sampling
 
double xmax
 
double xmin
 

Private Attributes

int m
 

Detailed Description

Definition at line 27 of file BaseNumericalRandomGenerator.h.

Constructor & Destructor Documentation

◆ BaseNumericalRandomGenerator()

BaseNumericalRandomGenerator::BaseNumericalRandomGenerator ( double  xmin = 0.,
double  xmax = 1.,
int  n = 1000,
int  iter = 6 
)

Constructor that perform the necessary integration and inversion steps xmin and xmax are the generation bounds, n is the internal table size and iter is the number of iterations for the numerical part.

Definition at line 7 of file BaseNumericalRandomGenerator.cc.

References f, n, and sampling.

◆ ~BaseNumericalRandomGenerator()

virtual BaseNumericalRandomGenerator::~BaseNumericalRandomGenerator ( )
inlinevirtual

Default destructor.

Definition at line 35 of file BaseNumericalRandomGenerator.h.

35 {}

Member Function Documentation

◆ function()

virtual double BaseNumericalRandomGenerator::function ( double  x)
pure virtual

◆ generate()

double BaseNumericalRandomGenerator::generate ( RandomEngineAndDistribution const *  random) const

The random generation according to function()

Definition at line 75 of file BaseNumericalRandomGenerator.cc.

References RandomEngineAndDistribution::flatShoot(), mps_fire::i, createfilelist::int, alignCSCRings::r, rmin, alignCSCRings::s, and sampling.

Referenced by GammaNumericalGenerator::gamma(), and LandauFluctuationGenerator::landau().

75  {
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 }
Definition: deltar.py:1

◆ generateExp()

double BaseNumericalRandomGenerator::generateExp ( RandomEngineAndDistribution const *  random) const

The random generation according to function(), refined to generate as an exponential in each of the intervals

Definition at line 83 of file BaseNumericalRandomGenerator.cc.

References a, b, JetChargeProducer_cfi::exp, f, RandomEngineAndDistribution::flatShoot(), mps_fire::i, createfilelist::int, dqm-mbProfile::log, alignCSCRings::r, rmin, and sampling.

Referenced by GammaNumericalGenerator::gamma_exp().

83  {
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 }
Definition: deltar.py:1
double b
Definition: hdecay.h:118
double a
Definition: hdecay.h:119

◆ generateLin()

double BaseNumericalRandomGenerator::generateLin ( RandomEngineAndDistribution const *  random) const

The random generation according to function(), refined to generate as a linear function in each of the intervals

Definition at line 98 of file BaseNumericalRandomGenerator.cc.

References a, b, f, RandomEngineAndDistribution::flatShoot(), mps_fire::i, createfilelist::int, alignCSCRings::r, rmin, sampling, and mathSSE::sqrt().

Referenced by GammaNumericalGenerator::gamma_lin().

98  {
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 }
T sqrt(T t)
Definition: SSEVec.h:19
Definition: deltar.py:1
double b
Definition: hdecay.h:118
double a
Definition: hdecay.h:119

◆ initialize()

void BaseNumericalRandomGenerator::initialize ( )

The initialization (numerical integarion, inversion)

Definition at line 13 of file BaseNumericalRandomGenerator.cc.

References a, PVValHelper::dz, f, dqmMemoryStats::float, mps_fire::i, iter, dqmdumpme::k, m, n, alignCSCRings::r, rmin, sampling, xmax, xmin, y, and z.

Referenced by GammaNumericalGenerator::GammaNumericalGenerator(), HistogramGenerator::HistogramGenerator(), and LandauFluctuationGenerator::LandauFluctuationGenerator().

13  {
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 }
Definition: deltar.py:1
double a
Definition: hdecay.h:119

◆ setSubInterval()

bool BaseNumericalRandomGenerator::setSubInterval ( double  x1,
double  x2 
)

To shoot in a given interval.

Definition at line 113 of file BaseNumericalRandomGenerator.cc.

References rmin, sampling, createJobs::tmp, testProducerWithPsetDescEmpty_cfi::x1, testProducerWithPsetDescEmpty_cfi::x2, and xmax.

113  {
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 }
Definition: deltar.py:1
tmp
align.sh
Definition: createJobs.py:716

Member Data Documentation

◆ deltar

double BaseNumericalRandomGenerator::deltar
protected

Definition at line 62 of file BaseNumericalRandomGenerator.h.

◆ f

std::vector<double> BaseNumericalRandomGenerator::f
protected

◆ iter

int BaseNumericalRandomGenerator::iter
protected

Definition at line 61 of file BaseNumericalRandomGenerator.h.

Referenced by initialize().

◆ m

int BaseNumericalRandomGenerator::m
private

Definition at line 65 of file BaseNumericalRandomGenerator.h.

Referenced by initialize().

◆ n

int BaseNumericalRandomGenerator::n
protected

Definition at line 61 of file BaseNumericalRandomGenerator.h.

Referenced by BaseNumericalRandomGenerator(), and initialize().

◆ rmin

double BaseNumericalRandomGenerator::rmin
protected

◆ sampling

std::vector<double> BaseNumericalRandomGenerator::sampling
protected

◆ xmax

double BaseNumericalRandomGenerator::xmax
protected

◆ xmin

double BaseNumericalRandomGenerator::xmin
protected