CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BaseNumericalRandomGenerator.cc
Go to the documentation of this file.
3 
4 #include <cmath>
5 // #include <iostream>
6 
8  double xmin, double xmax, int n, int iter ) :
9  xmin(xmin), xmax(xmax), n(n), iter(iter)
10 {
11  f.resize(n);
12  sampling.resize(n);
13 }
14 
15 void
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 }
80 
81 double
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 }
91 
92 double
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 }
109 
110 double
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 }
127 
128 
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 }
int i
Definition: DBlmapReader.cc:9
double generate(RandomEngineAndDistribution const *) const
The random generation according to function()
double flatShoot(double xmin=0.0, double xmax=1.0) const
double generateLin(RandomEngineAndDistribution const *) const
BaseNumericalRandomGenerator(double xmin=0., double xmax=1., int n=1000, int iter=6)
TRandom random
Definition: MVATrainer.cc:138
void initialize()
The initialization (numerical integarion, inversion)
double generateExp(RandomEngineAndDistribution const *) const
T sqrt(T t)
Definition: SSEVec.h:48
double b
Definition: hdecay.h:120
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
double a
Definition: hdecay.h:121
bool setSubInterval(double x1, double x2)
To shoot in a given interval.