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

9  : //myHisto(histo),
10  //theXaxis(histo->GetXaxis()),
11  nBins(histo->GetXaxis()->GetNbins()),
12  xMin(histo->GetXaxis()->GetXmin()),
13  xMax(histo->GetXaxis()->GetXmax()),
14  binWidth((xMax - xMin) / (float)nBins) {
15  integral.reserve(nBins + 2);
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 }

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

◆ ~SimpleHistogramGenerator()

virtual SimpleHistogramGenerator::~SimpleHistogramGenerator ( )
inlinevirtual

Default destructor.

Definition at line 29 of file SimpleHistogramGenerator.h.

29 {}

Member Function Documentation

◆ binarySearch()

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

Definition at line 37 of file SimpleHistogramGenerator.cc.

37  {
38  // Binary search in an array of n values to locate value.
39  //
40  // Array is supposed to be sorted prior to this call.
41  // If match is found, function returns position of element.
42  // If no match found, function gives nearest element smaller than value.
43 
44  int nabove, nbelow, middle;
45  nabove = n + 1;
46  nbelow = 0;
47  while (nabove - nbelow > 1) {
48  middle = (nabove + nbelow) / 2;
49  if (value == array[middle - 1])
50  return middle - 1;
51  if (value < array[middle - 1])
52  nabove = middle;
53  else
54  nbelow = middle;
55  }
56  return nbelow - 1;
57 }

References mps_check::array, and dqmiodumpmetadata::n.

Referenced by generate().

◆ generate()

double SimpleHistogramGenerator::generate ( RandomEngineAndDistribution const *  random) const

The random generation.

Definition at line 25 of file SimpleHistogramGenerator.cc.

25  {
26  // return a random number distributed according the histogram bin contents.
27  // NB Only valid for 1-d histograms, with fixed bin width.
28 
29  double r1 = random->flatShoot();
30  int ibin = binarySearch(nBins, integral, r1);
31  double x = xMin + (double)(ibin)*binWidth;
32  if (r1 > integral[ibin])
33  x += binWidth * (r1 - integral[ibin]) / (integral[ibin + 1] - integral[ibin]);
34  return x;
35 }

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

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

Member Data Documentation

◆ binWidth

double SimpleHistogramGenerator::binWidth
private

Definition at line 50 of file SimpleHistogramGenerator.h.

Referenced by generate().

◆ integral

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

Integral.

Definition at line 53 of file SimpleHistogramGenerator.h.

Referenced by generate(), and SimpleHistogramGenerator().

◆ nBins

int SimpleHistogramGenerator::nBins
private

Pointer to the histogram.

the axis Number of bins

Definition at line 44 of file SimpleHistogramGenerator.h.

Referenced by generate(), and SimpleHistogramGenerator().

◆ nEntries

double SimpleHistogramGenerator::nEntries
private

Number of entries.

Definition at line 56 of file SimpleHistogramGenerator.h.

Referenced by SimpleHistogramGenerator().

◆ xMax

double SimpleHistogramGenerator::xMax
private

Definition at line 47 of file SimpleHistogramGenerator.h.

◆ xMin

double SimpleHistogramGenerator::xMin
private

Definition at line 47 of file SimpleHistogramGenerator.h.

Referenced by generate().

mps_fire.i
i
Definition: mps_fire.py:428
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
SimpleHistogramGenerator::xMin
double xMin
Definition: SimpleHistogramGenerator.h:47
timingPdfMaker.histo
histo
Definition: timingPdfMaker.py:278
mps_check.array
array
Definition: mps_check.py:216
DDAxes::x
SimpleHistogramGenerator::binarySearch
int binarySearch(const int &n, const std::vector< float > &array, const double &value) const
Definition: SimpleHistogramGenerator.cc:37
SimpleHistogramGenerator::nBins
int nBins
Pointer to the histogram.
Definition: SimpleHistogramGenerator.h:44
SimpleHistogramGenerator::nEntries
double nEntries
Number of entries.
Definition: SimpleHistogramGenerator.h:56
value
Definition: value.py:1
SimpleHistogramGenerator::xMax
double xMax
Definition: SimpleHistogramGenerator.h:47
diffTwoXMLs.r1
r1
Definition: diffTwoXMLs.py:53
SimpleHistogramGenerator::integral
std::vector< float > integral
Integral.
Definition: SimpleHistogramGenerator.h:53
SimpleHistogramGenerator::binWidth
double binWidth
Definition: SimpleHistogramGenerator.h:50