CMS 3D CMS Logo

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),
15 {
16  PrepareTable(aProbFunc);
17 }
void PrepareTable(const double *aProbFunc)
RandArrayFunction::RandArrayFunction ( int  probSize,
int  interpolationType = 0 
)

Definition at line 19 of file RandArrayFunction.cc.

Member Function Documentation

double RandArrayFunction::Fire ( ) const
inline

Definition at line 102 of file RandArrayFunction.h.

References MapRandom(), and StandardRand().

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

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

Definition at line 113 of file RandArrayFunction.cc.

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

Referenced by ShootArray().

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

Definition at line 73 of file RandArrayFunction.cc.

References fIntegralPdf, fInterpolationType, fNBins, and fOneOverNbins.

Referenced by Fire().

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

Definition at line 110 of file RandArrayFunction.h.

References Fire().

110  {
111  return Fire();
112 }
double Fire() const
void RandArrayFunction::PrepareTable ( const double *  aProbFunc)

Definition at line 24 of file RandArrayFunction.cc.

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

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

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

Definition at line 106 of file RandArrayFunction.h.

References Fire().

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

Definition at line 114 of file RandArrayFunction.h.

References FireArray().

114  {
115  FireArray(size, array);
116 }
size
Write out results.
void FireArray(int size, double *array) const
double RandArrayFunction::StandardRand ( ) const
inlineprivate

Definition at line 98 of file RandArrayFunction.h.

References hjRandomEngine.

Referenced by Fire().

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

Definition at line 64 of file RandArrayFunction.cc.

References fIntegralPdf, fNBins, and fOneOverNbins.

Referenced by PrepareTable().

64  {
65  //Called only by PrepareTable in case of user error.
66  fNBins = 1;
67  fIntegralPdf.resize(2);
68  fIntegralPdf[0] = 0;
69  fIntegralPdf[1] = 1;
70  fOneOverNbins = 1.0;
71 }
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().