CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
RandArrayFunction Class Reference

#include <RandArrayFunction.h>

Public Member Functions

double Fire () const
 
void FireArray (int size, double *array) const
 
double operator() () const
 
void PrepareTable (const double *aProbFunc)
 
 RandArrayFunction (const double *aProbFunc, int theProbSize, int interpolationType=0)
 
 RandArrayFunction (int probSize, int interpolationType=0)
 
double Shoot () const
 
void ShootArray (int size, double *array) const
 

Private Member Functions

double MapRandom (double rand) const
 
double StandardRand () const
 
void UseFlatDistribution ()
 

Private Attributes

std::vector< double > fIntegralPdf
 
int fInterpolationType
 
int fNBins
 
double fOneOverNbins
 

Detailed Description

Definition at line 73 of file RandArrayFunction.h.

Constructor & Destructor Documentation

RandArrayFunction::RandArrayFunction ( const double *  aProbFunc,
int  theProbSize,
int  interpolationType = 0 
)

Definition at line 12 of file RandArrayFunction.cc.

References PrepareTable().

13  : fNBins(theProbSize), fInterpolationType(intType) {
14  PrepareTable(aProbFunc);
15 }
void PrepareTable(const double *aProbFunc)
RandArrayFunction::RandArrayFunction ( int  probSize,
int  interpolationType = 0 
)

Definition at line 17 of file RandArrayFunction.cc.

Member Function Documentation

double RandArrayFunction::Fire ( ) const
inline

Definition at line 100 of file RandArrayFunction.h.

References MapRandom(), and StandardRand().

Referenced by FireArray(), operator()(), and Shoot().

100 { return MapRandom(StandardRand()); }
double StandardRand() const
double MapRandom(double rand) const
void RandArrayFunction::FireArray ( int  size,
double *  array 
) const

Definition at line 110 of file RandArrayFunction.cc.

References Fire(), mps_fire::i, and findQualityFiles::size.

Referenced by ShootArray().

110  {
111  for (int i = 0; i < size; ++i)
112  vect[i] = Fire();
113 }
tuple size
Write out results.
double Fire() const
double RandArrayFunction::MapRandom ( double  rand) const
private

Definition at line 71 of file RandArrayFunction.cc.

References fIntegralPdf, fInterpolationType, fNBins, and fOneOverNbins.

Referenced by Fire().

71  {
72  // Private method to take the random (however it is created) and map it
73  // according to the distribution.
74 
75  int nBelow = 0; // largest k such that I[k] is known to be <= rand
76  int nAbove = fNBins; // largest k such that I[k] is known to be > rand
77  int middle;
78 
79  while (nAbove > nBelow + 1) {
80  middle = (nAbove + nBelow + 1) >> 1;
81  rand >= fIntegralPdf[middle] ? nBelow = middle : nAbove = middle;
82  } // after this loop, nAbove is always nBelow+1 and they straddle rad:
83 
84  /*assert ( nAbove = nBelow+1 );
85  assert ( fIntegralPdf[nBelow] <= rand );
86  assert ( fIntegralPdf[nAbove] >= rand );*/
87  // If a defective engine produces rand=1, that will
88  // still give sensible results so we relax the > rand assertion
89 
90  if (fInterpolationType == 1) {
91  return nBelow * fOneOverNbins;
92  } else {
93  double binMeasure = fIntegralPdf[nAbove] - fIntegralPdf[nBelow];
94  // binMeasure is always aProbFunc[nBelow],
95  // but we don't have aProbFunc any more so we subtract.
96 
97  if (!binMeasure) {
98  // rand lies right in a bin of measure 0. Simply return the center
99  // of the range of that bin. (Any value between k/N and (k+1)/N is
100  // equally good, in this rare case.)
101  return (nBelow + .5) * fOneOverNbins;
102  }
103 
104  double binFraction = (rand - fIntegralPdf[nBelow]) / binMeasure;
105 
106  return (nBelow + binFraction) * fOneOverNbins;
107  }
108 }
std::vector< double > fIntegralPdf
double RandArrayFunction::operator() ( ) const
inline

Definition at line 104 of file RandArrayFunction.h.

References Fire().

104 { return Fire(); }
double Fire() const
void RandArrayFunction::PrepareTable ( const double *  aProbFunc)

Definition at line 19 of file RandArrayFunction.cc.

References fIntegralPdf, fInterpolationType, fNBins, fOneOverNbins, UseFlatDistribution(), and histoStyle::weight.

Referenced by gen::Hydjet2Hadronizer::generatePartonsAndHadronize(), and RandArrayFunction().

19  {
20  //Prepares fIntegralPdf.
21  if (fNBins < 1) {
22  edm::LogError("RandArrayFunction") << "RandArrayFunction constructed with no bins - will use flat distribution";
24  return;
25  }
26 
27  fIntegralPdf.resize(fNBins + 1);
28  fIntegralPdf[0] = 0;
29  int ptn;
30  for (ptn = 0; ptn < fNBins; ++ptn) {
31  double weight = aProbFunc[ptn];
32  if (weight < 0.) {
33  // We can't stomach negative bin contents, they invalidate the
34  // search algorithm when the distribution is fired.
35  edm::LogWarning("RandArrayFunction") << "RandArrayFunction constructed with negative-weight bin " << ptn
36  << " == " << weight << " -- will substitute 0 weight";
37  weight = 0.;
38  }
39  fIntegralPdf[ptn + 1] = fIntegralPdf[ptn] + weight;
40  }
41 
42  if (fIntegralPdf[fNBins] <= 0.) {
43  edm::LogWarning("RandArrayFunction")
44  << "RandArrayFunction constructed with nothing in bins - will use flat distribution";
46  return;
47  }
48 
49  for (ptn = 0; ptn < fNBins + 1; ++ptn)
50  fIntegralPdf[ptn] /= fIntegralPdf[fNBins];
51 
52  // And another useful variable is ...
53  fOneOverNbins = 1.0 / fNBins;
54  // One last chore:
56  edm::LogInfo("RandArrayFunction") << "RandArrayFunction does not recognize fInterpolationType "
57  << fInterpolationType << " Will use type 0 (continuous linear interpolation)";
59  }
60 }
std::vector< double > fIntegralPdf
Log< level::Error, false > LogError
Log< level::Info, false > LogInfo
Log< level::Warning, false > LogWarning
int weight
Definition: histoStyle.py:51
double RandArrayFunction::Shoot ( ) const
inline

Definition at line 102 of file RandArrayFunction.h.

References Fire().

102 { return Fire(); }
double Fire() const
void RandArrayFunction::ShootArray ( int  size,
double *  array 
) const
inline

Definition at line 106 of file RandArrayFunction.h.

References FireArray().

106 { FireArray(size, array); }
tuple array
Definition: mps_check.py:216
void FireArray(int size, double *array) const
tuple size
Write out results.
double RandArrayFunction::StandardRand ( ) const
inlineprivate

Definition at line 98 of file RandArrayFunction.h.

References hjRandomEngine.

Referenced by Fire().

98 { return CLHEP::RandFlat::shoot(hjRandomEngine); }
CLHEP::HepRandomEngine * hjRandomEngine
void RandArrayFunction::UseFlatDistribution ( )
private

Definition at line 62 of file RandArrayFunction.cc.

References fIntegralPdf, fNBins, and fOneOverNbins.

Referenced by PrepareTable().

62  {
63  //Called only by PrepareTable in case of user error.
64  fNBins = 1;
65  fIntegralPdf.resize(2);
66  fIntegralPdf[0] = 0;
67  fIntegralPdf[1] = 1;
68  fOneOverNbins = 1.0;
69 }
std::vector< double > fIntegralPdf

Member Data Documentation

std::vector<double> RandArrayFunction::fIntegralPdf
private

Definition at line 75 of file RandArrayFunction.h.

Referenced by MapRandom(), PrepareTable(), and UseFlatDistribution().

int RandArrayFunction::fInterpolationType
private

Definition at line 78 of file RandArrayFunction.h.

Referenced by MapRandom(), and PrepareTable().

int RandArrayFunction::fNBins
private

Definition at line 76 of file RandArrayFunction.h.

Referenced by MapRandom(), PrepareTable(), and UseFlatDistribution().

double RandArrayFunction::fOneOverNbins
private

Definition at line 77 of file RandArrayFunction.h.

Referenced by MapRandom(), PrepareTable(), and UseFlatDistribution().