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 (RandomEngineAndDistribution const *) const
 The random generation. More...
 
 SimpleHistogramGenerator (TH1 *histo)
 
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...
 
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)

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  myHisto(histo),
10  theXaxis(histo->GetXaxis()),
11  nBins(theXaxis->GetNbins()),
12  xMin(theXaxis->GetXmin()),
13  xMax(theXaxis->GetXmax()),
14  binWidth((xMax-xMin)/(float)nBins)
15 {
16  integral.push_back(0.);
17  for ( int i=1; i<=nBins; ++i )
18  integral.push_back(integral[i-1]+histo->GetBinContent(i));
19  integral.push_back(integral[nBins]);
20  nEntries = integral[nBins+1];
21  for ( int i=1; i<=nBins; ++i )
22  integral[i] /= nEntries;
23 
24 }
int i
Definition: DBlmapReader.cc:9
double nEntries
Number of entries.
TH1 * myHisto
Pointer to the histogram.
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 43 of file SimpleHistogramGenerator.cc.

Referenced by generate().

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

The random generation.

Definition at line 28 of file SimpleHistogramGenerator.cc.

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

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

Member Data Documentation

double SimpleHistogramGenerator::binWidth
private

Definition at line 55 of file SimpleHistogramGenerator.h.

Referenced by generate().

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

Integral.

Definition at line 58 of file SimpleHistogramGenerator.h.

Referenced by generate(), and SimpleHistogramGenerator().

TH1* SimpleHistogramGenerator::myHisto
private

Pointer to the histogram.

Definition at line 43 of file SimpleHistogramGenerator.h.

int SimpleHistogramGenerator::nBins
private

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().

TAxis* SimpleHistogramGenerator::theXaxis
private

the axis

Definition at line 46 of file SimpleHistogramGenerator.h.

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().