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 ( 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, and sampling.

virtual BaseNumericalRandomGenerator::~BaseNumericalRandomGenerator ( )
inlinevirtual

Default destructor.

Definition at line 40 of file BaseNumericalRandomGenerator.h.

References generate(), generateExp(), generateLin(), initialize(), setSubInterval(), x, globals_cff::x1, and globals_cff::x2.

40 {}

Member Function Documentation

virtual double BaseNumericalRandomGenerator::function ( double  x)
pure virtual
double BaseNumericalRandomGenerator::generate ( RandomEngineAndDistribution const *  random) const

The random generation according to function()

Definition at line 82 of file BaseNumericalRandomGenerator.cc.

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

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

82  {
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  return sampling[i]+s*(sampling[i+1]-sampling[i]);
89 
90 }
TRandom random
Definition: MVATrainer.cc:138
Definition: deltar.py:1
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 93 of file BaseNumericalRandomGenerator.cc.

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

Referenced by GammaNumericalGenerator::gamma_exp(), and ~BaseNumericalRandomGenerator().

93  {
94 
95  double r=rmin+deltar*random->flatShoot();
96  int i=(int)r;
97  // double s=r-(double)i;
98  // cout << " i,r,s = " << i << " " << r << " " << s << endl;
99  double b = -std::log(f[i+1]/f[i]) / (sampling[i+1]-sampling[i]);
100  double a = f[i] * std::exp(b*sampling[i]);
101 
102  double umin = -a/b*std::exp(-b*sampling[i]);
103  double umax = -a/b*std::exp(-b*sampling[i+1]);
104  double u= (umax-umin) * random->flatShoot() + umin;
105 
106  return -std::log(-b/a*u) / b;
107 
108 }
TRandom random
Definition: MVATrainer.cc:138
Definition: deltar.py:1
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
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 111 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(), and ~BaseNumericalRandomGenerator().

111  {
112 
113  double r=rmin+deltar*random->flatShoot();
114  int i=(int)r;
115  // double s=r-(double)i;
116  // cout << " i,r,s = " << i << " " << r << " " << s << endl;
117  double a = (f[i+1]-f[i]) / (sampling[i+1]-sampling[i]);
118  double b = f[i] - a*sampling[i];
119 
120  double umin = a*sampling[i]*sampling[i]/2. + b*sampling[i];
121  double umax = a*sampling[i+1]*sampling[i+1]/2. + b*sampling[i+1];
122  double u= (umax-umin) * random->flatShoot() + umin;
123 
124  return (-b+std::sqrt(b*b+2.*a*u))/a;
125 
126 }
TRandom random
Definition: MVATrainer.cc:138
T sqrt(T t)
Definition: SSEVec.h:18
Definition: deltar.py:1
double b
Definition: hdecay.h:120
double a
Definition: hdecay.h:121
void BaseNumericalRandomGenerator::initialize ( )

The initialization (numerical integarion, inversion)

Definition at line 16 of file BaseNumericalRandomGenerator.cc.

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

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

16  {
17 
18  m = n-1;
19  rmin = 0.;
20  deltar = (double)m-rmin;
21 
22  std::vector<double> a,y,z,xnew;
23  a.resize(n);
24  y.resize(n);
25  z.resize(n);
26  xnew.resize(n);
27 
28  double sig1 = 0.;
29 
30  // Initialize sampling
31  double du = (xmax-xmin)/(float)m;
32  sampling[0] = xmin;
33  for (int i=1; i<n; ++i)
34  sampling[i] = sampling[i-1] + du;
35 
36  // Starting point for iterations
37  for (int it=0; it<iter; ++it) {
38 
39  // Calculate function values
40  for (int i=0; i<n; ++i )
41  f[i] = function(sampling[i]);
42 
43  // Calculate bin areas
44  for (int i=0; i<m; ++i )
45  a[i] = (sampling[i+1]-sampling[i]) * (f[i+1]+f[i]) / 2.;
46 
47  // Calculate cumulative spectrum Y values
48  y[0]=0.;
49  for (int i=1; i<n; ++i )
50  y[i] = y[i-1] + a[i-1];
51 
52  // Put equidistant points on y scale
53  double dz = y[n-1]/(float)m;
54  z[0]=0;
55  for (int i=1; i<n; ++i )
56  z[i] = z[i-1] + dz;
57 
58  // Determine spacinf of Z points in between Y points
59  // From this, determine new X values and finally replace old values
60  xnew[0] = sampling[0];
61  xnew[n-1] = sampling[n-1];
62  int k=0;
63  for ( int i=1; i<m; ++i ) {
64  while ( y[k+1] < z[i] ) ++k;
65  double r = (z[i]-y[k]) / (y[k+1]-y[k]);
66  xnew[i] = sampling[k] + (sampling[k+1]-sampling[k])*r;
67  }
68 
69  for ( int i=0; i<n; ++i )
70  sampling[i] = xnew[i];
71 
72  sig1 = sig1 + y[m];
73  // std::cout << "BaseNumericalRandomGenerator::Iteration # " << it+1
74  // << " Integral = " << sig1/(float)(it+1)
75  // << std::endl;
76 
77  }
78 
79 }
int k[5][pyjets_maxn]
Definition: deltar.py:1
double a
Definition: hdecay.h:121
bool BaseNumericalRandomGenerator::setSubInterval ( double  x1,
double  x2 
)

To shoot in a given interval.

Definition at line 129 of file BaseNumericalRandomGenerator.cc.

References rmin, sampling, tmp, globals_cff::x1, globals_cff::x2, and xmax.

Referenced by ~BaseNumericalRandomGenerator().

129  {
130  if(x1<xmin||x2>xmax) return false;
131  if(x1>x2)
132  {
133  double tmp=x1;
134  x1=x2;
135  x2=tmp;
136  }
137 
138  unsigned ibin=1;
139  for(;ibin<(unsigned)n&&x1>sampling[ibin];++ibin);
140  unsigned ibin1=ibin;
141  for(;ibin<(unsigned)n&&x2>sampling[ibin];++ibin);
142  unsigned ibin2=ibin;
143 
144  // cout << sampling[ibin1-1] << " " << x1 << " " << sampling[ibin1] << endl;
145  // cout << sampling[ibin2-1] << " " << x2 << " " << sampling[ibin2] << endl;
146 
147  rmin = ibin1+(x1-sampling[ibin1])/(sampling[ibin1]-sampling[ibin1-1]);
148  deltar= ibin2+(x2-sampling[ibin2])/(sampling[ibin2]-sampling[ibin2-1]) - rmin;
149  // cout << rmin << " " << deltar << endl;
150  return true;
151 }
Definition: deltar.py:1
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100

Member Data Documentation

double BaseNumericalRandomGenerator::deltar
protected

Definition at line 68 of file BaseNumericalRandomGenerator.h.

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

Definition at line 67 of file BaseNumericalRandomGenerator.h.

Referenced by initialize().

int BaseNumericalRandomGenerator::m
private

Definition at line 72 of file BaseNumericalRandomGenerator.h.

Referenced by initialize().

int BaseNumericalRandomGenerator::n
protected

Definition at line 67 of file BaseNumericalRandomGenerator.h.

Referenced by initialize().

double BaseNumericalRandomGenerator::rmin
protected
std::vector<double> BaseNumericalRandomGenerator::sampling
protected
double BaseNumericalRandomGenerator::xmax
protected
double BaseNumericalRandomGenerator::xmin
protected