CMS 3D CMS Logo

Public Member Functions | Private Attributes

SimpleHistogramGenerator Class Reference

#include <SimpleHistogramGenerator.h>

List of all members.

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 RandomEnginerandom
TAxis * theXaxis
 the axis
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.

                                                                                          :
  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]

Default destructor.

Definition at line 31 of file SimpleHistogramGenerator.h.

{}

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

{
   // 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;

}

Member Data Documentation

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

Pointer to the histogram.

Definition at line 45 of file SimpleHistogramGenerator.h.

Number of bins.

Definition at line 51 of file SimpleHistogramGenerator.h.

Referenced by generate(), and SimpleHistogramGenerator().

Number of entries.

Definition at line 63 of file SimpleHistogramGenerator.h.

Referenced by SimpleHistogramGenerator().

Definition at line 42 of file SimpleHistogramGenerator.h.

Referenced by generate().

the axis

Definition at line 48 of file SimpleHistogramGenerator.h.

Definition at line 54 of file SimpleHistogramGenerator.h.

Definition at line 54 of file SimpleHistogramGenerator.h.

Referenced by generate().