#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. | |
SimpleHistogramGenerator (TH1 *histo, const RandomEngine *engine) | |
virtual | ~SimpleHistogramGenerator () |
Default destructor. | |
Private Attributes | |
double | binWidth |
std::vector< double > | integral |
Integral. | |
TH1 * | myHisto |
Pointer to the histogram. | |
int | nBins |
Number of bins. | |
double | nEntries |
Number of entries. | |
const RandomEngine * | random |
TAxis * | theXaxis |
the axis | |
double | xMax |
double | xMin |
Definition at line 21 of file SimpleHistogramGenerator.h.
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.
: random(engine), myHisto(histo), theXaxis(histo->GetXaxis()), nBins(theXaxis->GetNbins()), xMin(theXaxis->GetXmin()), xMax(theXaxis->GetXmax()), binWidth((xMax-xMin)/(float)nBins) { integral.push_back(0.); for ( int i=1; i<=nBins; ++i ) integral.push_back(integral[i-1]+histo->GetBinContent(i)); integral.push_back(integral[nBins]); nEntries = integral[nBins+1]; for ( int i=1; i<=nBins; ++i ) integral[i] /= nEntries; }
virtual SimpleHistogramGenerator::~SimpleHistogramGenerator | ( | ) | [inline, virtual] |
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().
{ // Binary search in an array of n values to locate value. // // Array is supposed to be sorted prior to this call. // If match is found, function returns position of element. // If no match found, function gives nearest element smaller than value. int nabove, nbelow, middle; nabove = n+1; nbelow = 0; while(nabove-nbelow > 1) { middle = (nabove+nbelow)/2; if (value == array[middle-1]) return middle-1; if (value < array[middle-1]) nabove = middle; else nbelow = middle; } return nbelow-1; }
double SimpleHistogramGenerator::generate | ( | ) | const |
The random generation.
Definition at line 29 of file SimpleHistogramGenerator.cc.
References binarySearch(), binWidth, RandomEngine::flatShoot(), integral, nBins, random, ExpressReco_HICollisions_FallBack::x, and xMin.
{ // return a random number distributed according the histogram bin contents. // NB Only valid for 1-d histograms, with fixed bin width. double r1 = random->flatShoot(); int ibin = binarySearch(nBins,integral,r1); double x = xMin + (double)(ibin) * binWidth; if (r1 > integral[ibin]) x += binWidth*(r1-integral[ibin])/(integral[ibin+1] - integral[ibin]); return x; }
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().