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() [1/2]

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

Definition at line 12 of file RandArrayFunction.cc.

13  : fNBins(theProbSize), fInterpolationType(intType) {
14  PrepareTable(aProbFunc);
15 }

References PrepareTable().

◆ RandArrayFunction() [2/2]

RandArrayFunction::RandArrayFunction ( int  probSize,
int  interpolationType = 0 
)

Definition at line 17 of file RandArrayFunction.cc.

17 : fNBins(theProbSize), fInterpolationType(intType) {}

Member Function Documentation

◆ Fire()

double RandArrayFunction::Fire ( ) const
inline

Definition at line 100 of file RandArrayFunction.h.

100 { return MapRandom(StandardRand()); }

References MapRandom(), and StandardRand().

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

◆ FireArray()

void RandArrayFunction::FireArray ( int  size,
double *  array 
) const

Definition at line 110 of file RandArrayFunction.cc.

110  {
111  for (int i = 0; i < size; ++i)
112  vect[i] = Fire();
113 }

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

Referenced by ShootArray().

◆ MapRandom()

double RandArrayFunction::MapRandom ( double  rand) const
private

Definition at line 71 of file RandArrayFunction.cc.

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 }

References fIntegralPdf, fInterpolationType, fNBins, and fOneOverNbins.

Referenced by Fire().

◆ operator()()

double RandArrayFunction::operator() ( ) const
inline

Definition at line 104 of file RandArrayFunction.h.

104 { return Fire(); }

References Fire().

◆ PrepareTable()

void RandArrayFunction::PrepareTable ( const double *  aProbFunc)

Definition at line 19 of file RandArrayFunction.cc.

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)
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 }

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

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

◆ Shoot()

double RandArrayFunction::Shoot ( ) const
inline

Definition at line 102 of file RandArrayFunction.h.

102 { return Fire(); }

References Fire().

◆ ShootArray()

void RandArrayFunction::ShootArray ( int  size,
double *  array 
) const
inline

Definition at line 106 of file RandArrayFunction.h.

106 { FireArray(size, array); }

References mps_check::array, FireArray(), and findQualityFiles::size.

◆ StandardRand()

double RandArrayFunction::StandardRand ( ) const
inlineprivate

Definition at line 98 of file RandArrayFunction.h.

98 { return CLHEP::RandFlat::shoot(hjRandomEngine); }

References hjRandomEngine.

Referenced by Fire().

◆ UseFlatDistribution()

void RandArrayFunction::UseFlatDistribution ( )
private

Definition at line 62 of file RandArrayFunction.cc.

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 }

References fIntegralPdf, fNBins, and fOneOverNbins.

Referenced by PrepareTable().

Member Data Documentation

◆ fIntegralPdf

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

Definition at line 75 of file RandArrayFunction.h.

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

◆ fInterpolationType

int RandArrayFunction::fInterpolationType
private

Definition at line 78 of file RandArrayFunction.h.

Referenced by MapRandom(), and PrepareTable().

◆ fNBins

int RandArrayFunction::fNBins
private

Definition at line 76 of file RandArrayFunction.h.

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

◆ fOneOverNbins

double RandArrayFunction::fOneOverNbins
private

Definition at line 77 of file RandArrayFunction.h.

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

mps_fire.i
i
Definition: mps_fire.py:428
mps_merge.weight
weight
Definition: mps_merge.py:88
RandArrayFunction::fNBins
int fNBins
Definition: RandArrayFunction.h:76
mps_check.array
array
Definition: mps_check.py:216
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
RandArrayFunction::fIntegralPdf
std::vector< double > fIntegralPdf
Definition: RandArrayFunction.h:75
RandArrayFunction::Fire
double Fire() const
Definition: RandArrayFunction.h:100
reco::method::intType
Definition: TypeCode.h:16
RandArrayFunction::StandardRand
double StandardRand() const
Definition: RandArrayFunction.h:98
RandArrayFunction::PrepareTable
void PrepareTable(const double *aProbFunc)
Definition: RandArrayFunction.cc:19
RandArrayFunction::fOneOverNbins
double fOneOverNbins
Definition: RandArrayFunction.h:77
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
RandArrayFunction::UseFlatDistribution
void UseFlatDistribution()
Definition: RandArrayFunction.cc:62
RandArrayFunction::MapRandom
double MapRandom(double rand) const
Definition: RandArrayFunction.cc:71
hjRandomEngine
CLHEP::HepRandomEngine * hjRandomEngine
Definition: Hydjet2Hadronizer.cc:79
RandArrayFunction::fInterpolationType
int fInterpolationType
Definition: RandArrayFunction.h:78
RandArrayFunction::FireArray
void FireArray(int size, double *array) const
Definition: RandArrayFunction.cc:110
weight
Definition: weight.py:1
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443