CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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< double > &array, const double &value) const
 
double generate () const
 The random generation. More...
 
 SimpleHistogramGenerator (TH1 *histo, const RandomEngine *engine)
 
virtual ~SimpleHistogramGenerator ()
 Default destructor. More...
 

Private Attributes

double binWidth
 
std::vector< double > integral
 Integral. More...
 
TH1 * myHisto
 Pointer to the histogram. More...
 
int nBins
 Number of bins. More...
 
double nEntries
 Number of entries. More...
 
const RandomEnginerandom
 
TAxis * theXaxis
 the axis More...
 
double xMax
 
double xMin
 

Detailed Description

Definition at line 21 of file SimpleHistogramGenerator.h.

Constructor & Destructor Documentation

SimpleHistogramGenerator::SimpleHistogramGenerator ( TH1 *  histo,
const RandomEngine engine 
)

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 i, integral, nBins, and nEntries.

8  :
9  random(engine),
10  myHisto(histo),
11  theXaxis(histo->GetXaxis()),
12  nBins(theXaxis->GetNbins()),
13  xMin(theXaxis->GetXmin()),
14  xMax(theXaxis->GetXmax()),
15  binWidth((xMax-xMin)/(float)nBins)
16 {
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 }
int i
Definition: DBlmapReader.cc:9
double nEntries
Number of entries.
TH1 * myHisto
Pointer to the histogram.
tuple histo
Definition: trackerHits.py:12
std::vector< double > integral
Integral.
virtual SimpleHistogramGenerator::~SimpleHistogramGenerator ( )
inlinevirtual

Default destructor.

Definition at line 31 of file SimpleHistogramGenerator.h.

31 {}

Member Function Documentation

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

Definition at line 44 of file SimpleHistogramGenerator.cc.

Referenced by generate().

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 }
double SimpleHistogramGenerator::generate ( ) const

The random generation.

Definition at line 29 of file SimpleHistogramGenerator.cc.

References binarySearch(), binWidth, RandomEngine::flatShoot(), integral, nBins, random, x, and xMin.

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< double > integral
Integral.
int binarySearch(const int &n, const std::vector< double > &array, const double &value) const
double flatShoot(double xmin=0.0, double xmax=1.0) const
Definition: RandomEngine.h:30
Definition: DDAxes.h:10

Member Data Documentation

double SimpleHistogramGenerator::binWidth
private

Definition at line 57 of file SimpleHistogramGenerator.h.

Referenced by generate().

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

Integral.

Definition at line 60 of file SimpleHistogramGenerator.h.

Referenced by generate(), and SimpleHistogramGenerator().

TH1* SimpleHistogramGenerator::myHisto
private

Pointer to the histogram.

Definition at line 45 of file SimpleHistogramGenerator.h.

int SimpleHistogramGenerator::nBins
private

Number of bins.

Definition at line 51 of file SimpleHistogramGenerator.h.

Referenced by generate(), and SimpleHistogramGenerator().

double SimpleHistogramGenerator::nEntries
private

Number of entries.

Definition at line 63 of file SimpleHistogramGenerator.h.

Referenced by SimpleHistogramGenerator().

const RandomEngine* SimpleHistogramGenerator::random
private

Definition at line 42 of file SimpleHistogramGenerator.h.

Referenced by generate().

TAxis* SimpleHistogramGenerator::theXaxis
private

the axis

Definition at line 48 of file SimpleHistogramGenerator.h.

double SimpleHistogramGenerator::xMax
private

Definition at line 54 of file SimpleHistogramGenerator.h.

double SimpleHistogramGenerator::xMin
private

Definition at line 54 of file SimpleHistogramGenerator.h.

Referenced by generate().