CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 (const RandomEngine *engine, double xmin=0., double xmax=1., int n=1000, int iter=6)
 
virtual double function (double x)=0
 
double generate () const
 The random generation according to function() More...
 
double generateExp () const
 
double generateLin () 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
 
const RandomEnginerandom
 
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 ( const RandomEngine engine,
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 41 of file BaseNumericalRandomGenerator.h.

41 {}

Member Function Documentation

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

The random generation according to function()

Definition at line 84 of file BaseNumericalRandomGenerator.cc.

References deltar, RandomEngine::flatShoot(), i, alignCSCRings::r, random, rmin, alignCSCRings::s, and sampling.

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

84  {
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 }
int i
Definition: DBlmapReader.cc:9
double flatShoot(double xmin=0.0, double xmax=1.0) const
Definition: RandomEngine.h:30
double BaseNumericalRandomGenerator::generateExp ( ) const

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

Definition at line 95 of file BaseNumericalRandomGenerator.cc.

References a, b, deltar, create_public_lumi_plots::exp, f, RandomEngine::flatShoot(), i, create_public_lumi_plots::log, alignCSCRings::r, random, rmin, and sampling.

Referenced by GammaNumericalGenerator::gamma_exp().

95  {
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 }
int i
Definition: DBlmapReader.cc:9
double b
Definition: hdecay.h:120
double flatShoot(double xmin=0.0, double xmax=1.0) const
Definition: RandomEngine.h:30
double a
Definition: hdecay.h:121
double BaseNumericalRandomGenerator::generateLin ( ) const

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

Definition at line 113 of file BaseNumericalRandomGenerator.cc.

References a, b, deltar, f, RandomEngine::flatShoot(), i, alignCSCRings::r, random, rmin, sampling, and mathSSE::sqrt().

Referenced by GammaNumericalGenerator::gamma_lin().

113  {
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 }
int i
Definition: DBlmapReader.cc:9
T sqrt(T t)
Definition: SSEVec.h:48
double b
Definition: hdecay.h:120
double flatShoot(double xmin=0.0, double xmax=1.0) const
Definition: RandomEngine.h:30
double a
Definition: hdecay.h:121
void BaseNumericalRandomGenerator::initialize ( )

The initialization (numerical integarion, inversion)

Definition at line 18 of file BaseNumericalRandomGenerator.cc.

References a, deltar, f, i, iter, gen::k, m, n, alignCSCRings::r, rmin, sampling, xmax, xmin, detailsBasic3DVector::y, and detailsBasic3DVector::z.

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

18  {
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 }
int i
Definition: DBlmapReader.cc:9
float float float z
int k[5][pyjets_maxn]
double a
Definition: hdecay.h:121
bool BaseNumericalRandomGenerator::setSubInterval ( double  x1,
double  x2 
)

To shoot in a given interval.

Definition at line 131 of file BaseNumericalRandomGenerator.cc.

References deltar, rmin, sampling, tmp, and xmax.

131  {
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 }
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100

Member Data Documentation

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

Definition at line 70 of file BaseNumericalRandomGenerator.h.

Referenced by initialize().

int BaseNumericalRandomGenerator::m
private

Definition at line 75 of file BaseNumericalRandomGenerator.h.

Referenced by initialize().

int BaseNumericalRandomGenerator::n
protected

Definition at line 70 of file BaseNumericalRandomGenerator.h.

Referenced by initialize().

const RandomEngine* BaseNumericalRandomGenerator::random
protected

Definition at line 65 of file BaseNumericalRandomGenerator.h.

Referenced by generate(), generateExp(), and generateLin().

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