CMS 3D CMS Logo

List of all members | Public Member Functions | Private Attributes
SimpleHistogramGenerator Class Reference

#include <SimpleHistogramGenerator.h>

Public Member Functions

int binarySearch (const int &n, const std::vector< float > &array, const double &value) const
 
double generate (RandomEngineAndDistribution const *) const
 The random generation. More...
 
 SimpleHistogramGenerator (TH1 *histo)
 
virtual ~SimpleHistogramGenerator ()
 Default destructor. More...
 

Private Attributes

double binWidth
 
std::vector< float > integral
 Integral. More...
 
int nBins
 Pointer to the histogram. More...
 
double nEntries
 Number of entries. More...
 
double xMax
 
double xMin
 

Detailed Description

Definition at line 21 of file SimpleHistogramGenerator.h.

Constructor & Destructor Documentation

SimpleHistogramGenerator::SimpleHistogramGenerator ( TH1 *  histo)

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 8 of file SimpleHistogramGenerator.cc.

References mps_fire::i, integral, nBins, and nEntries.

8  :
9  //myHisto(histo),
10  //theXaxis(histo->GetXaxis()),
11  nBins(histo->GetXaxis()->GetNbins()),
12  xMin(histo->GetXaxis()->GetXmin()),
13  xMax(histo->GetXaxis()->GetXmax()),
15 {
16  integral.reserve(nBins+2);
17  integral.push_back(0.);
18  for ( int i=1; i<=nBins; ++i )
19  integral.push_back(integral[i-1]+histo->GetBinContent(i));
20  integral.push_back(integral[nBins]);
21  nEntries = integral[nBins+1];
22  for ( int i=1; i<=nBins; ++i )
23  integral[i] /= nEntries;
24 
25 }
double nEntries
Number of entries.
std::vector< float > integral
Integral.
int nBins
Pointer to the histogram.
virtual SimpleHistogramGenerator::~SimpleHistogramGenerator ( )
inlinevirtual

Default destructor.

Definition at line 31 of file SimpleHistogramGenerator.h.

References mps_check::array, binarySearch(), generate(), and gen::n.

31 {}

Member Function Documentation

int SimpleHistogramGenerator::binarySearch ( const int &  n,
const std::vector< float > &  array,
const double &  value 
) const

Definition at line 44 of file SimpleHistogramGenerator.cc.

Referenced by generate(), and ~SimpleHistogramGenerator().

47 {
48  // Binary search in an array of n values to locate value.
49  //
50  // Array is supposed to be sorted prior to this call.
51  // If match is found, function returns position of element.
52  // If no match found, function gives nearest element smaller than value.
53 
54  int nabove, nbelow, middle;
55  nabove = n+1;
56  nbelow = 0;
57  while(nabove-nbelow > 1) {
58  middle = (nabove+nbelow)/2;
59  if (value == array[middle-1]) return middle-1;
60  if (value < array[middle-1]) nabove = middle;
61  else nbelow = middle;
62  }
63  return nbelow-1;
64 }
Definition: value.py:1
double SimpleHistogramGenerator::generate ( RandomEngineAndDistribution const *  random) const

The random generation.

Definition at line 29 of file SimpleHistogramGenerator.cc.

References binarySearch(), binWidth, RandomEngineAndDistribution::flatShoot(), integral, nBins, diffTwoXMLs::r1, x, and xMin.

Referenced by PixelTemplateSmearerBase::smearHit(), PixelTemplateSmearerBase::smearMergeGroup(), and ~SimpleHistogramGenerator().

29  {
30 
31  // return a random number distributed according the histogram bin contents.
32  // NB Only valid for 1-d histograms, with fixed bin width.
33 
34  double r1 = random->flatShoot();
35  int ibin = binarySearch(nBins,integral,r1);
36  double x = xMin + (double)(ibin) * binWidth;
37  if (r1 > integral[ibin]) x +=
38  binWidth*(r1-integral[ibin])/(integral[ibin+1] - integral[ibin]);
39  return x;
40 
41 }
std::vector< float > integral
Integral.
TRandom random
Definition: MVATrainer.cc:138
int nBins
Pointer to the histogram.
int binarySearch(const int &n, const std::vector< float > &array, const double &value) const

Member Data Documentation

double SimpleHistogramGenerator::binWidth
private

Definition at line 55 of file SimpleHistogramGenerator.h.

Referenced by generate().

std::vector<float> SimpleHistogramGenerator::integral
private

Integral.

Definition at line 58 of file SimpleHistogramGenerator.h.

Referenced by generate(), and SimpleHistogramGenerator().

int SimpleHistogramGenerator::nBins
private

Pointer to the histogram.

the axis Number of bins

Definition at line 49 of file SimpleHistogramGenerator.h.

Referenced by generate(), and SimpleHistogramGenerator().

double SimpleHistogramGenerator::nEntries
private

Number of entries.

Definition at line 61 of file SimpleHistogramGenerator.h.

Referenced by SimpleHistogramGenerator().

double SimpleHistogramGenerator::xMax
private

Definition at line 52 of file SimpleHistogramGenerator.h.

double SimpleHistogramGenerator::xMin
private

Definition at line 52 of file SimpleHistogramGenerator.h.

Referenced by generate().