#include <FastSimulation/Utilities/interface/BaseNumericalRandomGenerator.h>
Public Member Functions | |
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. | |
virtual double | function (double x)=0 |
double | generate () const |
The random generation according to function(). | |
double | generateExp () const |
The random generation according to function(), refined to generate as an exponential in each of the intervals. | |
double | generateLin () const |
The random generation according to function(), refined to generate as a linear function in each of the intervals. | |
void | initialize () |
The initialization (numerical integarion, inversion). | |
bool | setSubInterval (double x1, double x2) |
To shoot in a given interval. | |
virtual | ~BaseNumericalRandomGenerator () |
Default destructor. | |
Protected Attributes | |
double | deltar |
std::vector< double > | f |
int | iter |
int | n |
const RandomEngine * | random |
double | rmin |
std::vector< double > | sampling |
double | xmax |
double | xmin |
Private Attributes | |
int | m |
Definition at line 27 of file BaseNumericalRandomGenerator.h.
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.
00009 : 00010 random(engine), 00011 xmin(xmin), xmax(xmax), n(n), iter(iter) 00012 { 00013 f.resize(n); 00014 sampling.resize(n); 00015 }
virtual BaseNumericalRandomGenerator::~BaseNumericalRandomGenerator | ( | ) | [inline, virtual] |
virtual double BaseNumericalRandomGenerator::function | ( | double | x | ) | [pure virtual] |
Implemented in GammaNumericalGenerator, HistogramGenerator, and LandauFluctuationGenerator.
Referenced by initialize().
double BaseNumericalRandomGenerator::generate | ( | ) | const |
The random generation according to function().
Definition at line 84 of file BaseNumericalRandomGenerator.cc.
References deltar, RandomEngine::flatShoot(), i, int, r, random, rmin, s, and sampling.
Referenced by GammaNumericalGenerator::gamma(), and LandauFluctuationGenerator::landau().
00084 { 00085 00086 double r=rmin+deltar*random->flatShoot(); 00087 int i=(int)r; 00088 double s=r-(double)i; 00089 // cout << " i,r,s = " << i << " " << r << " " << s << endl; 00090 return sampling[i]+s*(sampling[i+1]-sampling[i]); 00091 00092 }
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, funct::exp(), f, RandomEngine::flatShoot(), i, int, funct::log(), r, random, rmin, and sampling.
Referenced by GammaNumericalGenerator::gamma_exp().
00095 { 00096 00097 double r=rmin+deltar*random->flatShoot(); 00098 int i=(int)r; 00099 // double s=r-(double)i; 00100 // cout << " i,r,s = " << i << " " << r << " " << s << endl; 00101 double b = -std::log(f[i+1]/f[i]) / (sampling[i+1]-sampling[i]); 00102 double a = f[i] * std::exp(b*sampling[i]); 00103 00104 double umin = -a/b*std::exp(-b*sampling[i]); 00105 double umax = -a/b*std::exp(-b*sampling[i+1]); 00106 double u= (umax-umin) * random->flatShoot() + umin; 00107 00108 return -std::log(-b/a*u) / b; 00109 00110 }
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, int, r, random, rmin, sampling, and funct::sqrt().
Referenced by GammaNumericalGenerator::gamma_lin().
00113 { 00114 00115 double r=rmin+deltar*random->flatShoot(); 00116 int i=(int)r; 00117 // double s=r-(double)i; 00118 // cout << " i,r,s = " << i << " " << r << " " << s << endl; 00119 double a = (f[i+1]-f[i]) / (sampling[i+1]-sampling[i]); 00120 double b = f[i] - a*sampling[i]; 00121 00122 double umin = a*sampling[i]*sampling[i]/2. + b*sampling[i]; 00123 double umax = a*sampling[i+1]*sampling[i+1]/2. + b*sampling[i+1]; 00124 double u= (umax-umin) * random->flatShoot() + umin; 00125 00126 return (-b+std::sqrt(b*b+2.*a*u))/a; 00127 00128 }
void BaseNumericalRandomGenerator::initialize | ( | ) |
The initialization (numerical integarion, inversion).
Definition at line 18 of file BaseNumericalRandomGenerator.cc.
References a, deltar, f, function(), i, it, iter, k, m, n, r, rmin, sampling, xmax, xmin, y, and z.
Referenced by GammaNumericalGenerator::GammaNumericalGenerator(), HistogramGenerator::HistogramGenerator(), and LandauFluctuationGenerator::LandauFluctuationGenerator().
00018 { 00019 00020 m = n-1; 00021 rmin = 0.; 00022 deltar = (double)m-rmin; 00023 00024 std::vector<double> a,y,z,xnew; 00025 a.resize(n); 00026 y.resize(n); 00027 z.resize(n); 00028 xnew.resize(n); 00029 00030 double sig1 = 0.; 00031 00032 // Initialize sampling 00033 double du = (xmax-xmin)/(float)m; 00034 sampling[0] = xmin; 00035 for (int i=1; i<n; ++i) 00036 sampling[i] = sampling[i-1] + du; 00037 00038 // Starting point for iterations 00039 for (int it=0; it<iter; ++it) { 00040 00041 // Calculate function values 00042 for (int i=0; i<n; ++i ) 00043 f[i] = function(sampling[i]); 00044 00045 // Calculate bin areas 00046 for (int i=0; i<m; ++i ) 00047 a[i] = (sampling[i+1]-sampling[i]) * (f[i+1]+f[i]) / 2.; 00048 00049 // Calculate cumulative spectrum Y values 00050 y[0]=0.; 00051 for (int i=1; i<n; ++i ) 00052 y[i] = y[i-1] + a[i-1]; 00053 00054 // Put equidistant points on y scale 00055 double dz = y[n-1]/(float)m; 00056 z[0]=0; 00057 for (int i=1; i<n; ++i ) 00058 z[i] = z[i-1] + dz; 00059 00060 // Determine spacinf of Z points in between Y points 00061 // From this, determine new X values and finally replace old values 00062 xnew[0] = sampling[0]; 00063 xnew[n-1] = sampling[n-1]; 00064 int k=0; 00065 for ( int i=1; i<m; ++i ) { 00066 while ( y[k+1] < z[i] ) ++k; 00067 double r = (z[i]-y[k]) / (y[k+1]-y[k]); 00068 xnew[i] = sampling[k] + (sampling[k+1]-sampling[k])*r; 00069 } 00070 00071 for ( int i=0; i<n; ++i ) 00072 sampling[i] = xnew[i]; 00073 00074 sig1 = sig1 + y[m]; 00075 // std::cout << "BaseNumericalRandomGenerator::Iteration # " << it+1 00076 // << " Integral = " << sig1/(float)(it+1) 00077 // << std::endl; 00078 00079 } 00080 00081 }
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.
00131 { 00132 if(x1<xmin||x2>xmax) return false; 00133 if(x1>x2) 00134 { 00135 double tmp=x1; 00136 x1=x2; 00137 x2=tmp; 00138 } 00139 00140 unsigned ibin=1; 00141 for(;ibin<(unsigned)n&&x1>sampling[ibin];++ibin); 00142 unsigned ibin1=ibin; 00143 for(;ibin<(unsigned)n&&x2>sampling[ibin];++ibin); 00144 unsigned ibin2=ibin; 00145 00146 // cout << sampling[ibin1-1] << " " << x1 << " " << sampling[ibin1] << endl; 00147 // cout << sampling[ibin2-1] << " " << x2 << " " << sampling[ibin2] << endl; 00148 00149 rmin = ibin1+(x1-sampling[ibin1])/(sampling[ibin1]-sampling[ibin1-1]); 00150 deltar= ibin2+(x2-sampling[ibin2])/(sampling[ibin2]-sampling[ibin2-1]) - rmin; 00151 // cout << rmin << " " << deltar << endl; 00152 return true; 00153 }
double BaseNumericalRandomGenerator::deltar [protected] |
Definition at line 71 of file BaseNumericalRandomGenerator.h.
Referenced by generate(), generateExp(), generateLin(), initialize(), and setSubInterval().
std::vector<double> BaseNumericalRandomGenerator::f [protected] |
Definition at line 68 of file BaseNumericalRandomGenerator.h.
Referenced by BaseNumericalRandomGenerator(), generateExp(), generateLin(), and initialize().
int BaseNumericalRandomGenerator::iter [protected] |
int BaseNumericalRandomGenerator::m [private] |
int BaseNumericalRandomGenerator::n [protected] |
const RandomEngine* BaseNumericalRandomGenerator::random [protected] |
Definition at line 65 of file BaseNumericalRandomGenerator.h.
Referenced by generate(), generateExp(), and generateLin().
double BaseNumericalRandomGenerator::rmin [protected] |
Definition at line 71 of file BaseNumericalRandomGenerator.h.
Referenced by generate(), generateExp(), generateLin(), initialize(), and setSubInterval().
std::vector<double> BaseNumericalRandomGenerator::sampling [protected] |
Definition at line 67 of file BaseNumericalRandomGenerator.h.
Referenced by BaseNumericalRandomGenerator(), generate(), generateExp(), generateLin(), initialize(), and setSubInterval().
double BaseNumericalRandomGenerator::xmax [protected] |
Definition at line 69 of file BaseNumericalRandomGenerator.h.
Referenced by HistogramGenerator::HistogramGenerator(), initialize(), and setSubInterval().
double BaseNumericalRandomGenerator::xmin [protected] |
Definition at line 69 of file BaseNumericalRandomGenerator.h.
Referenced by HistogramGenerator::HistogramGenerator(), and initialize().