CMS 3D CMS Logo

SimpleHistogramGenerator.cc
Go to the documentation of this file.
3 
4 #include <cmath>
5 #include "TH1.h"
6 // #include <iostream>
7 
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 }
24 
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 }
36 
37 int SimpleHistogramGenerator::binarySearch(const int& n, const std::vector<float>& array, const double& value) const {
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 }
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:279
mps_check.array
array
Definition: mps_check.py:216
SimpleHistogramGenerator::generate
double generate(RandomEngineAndDistribution const *) const
The random generation.
Definition: SimpleHistogramGenerator.cc:25
DDAxes::x
RandomEngineAndDistribution.h
photonAnalyzer_cfi.xMin
xMin
Definition: photonAnalyzer_cfi.py:82
SimpleHistogramGenerator::binarySearch
int binarySearch(const int &n, const std::vector< float > &array, const double &value) const
Definition: SimpleHistogramGenerator.cc:37
seedmultiplicitymonitor_newtracking_cfi.nBins
nBins
Definition: seedmultiplicitymonitor_newtracking_cfi.py:8
SimpleHistogramGenerator::nBins
int nBins
Pointer to the histogram.
Definition: SimpleHistogramGenerator.h:44
SimpleHistogramGenerator.h
SimpleHistogramGenerator::nEntries
double nEntries
Number of entries.
Definition: SimpleHistogramGenerator.h:56
value
Definition: value.py:1
RandomEngineAndDistribution::flatShoot
double flatShoot(double xmin=0.0, double xmax=1.0) const
Definition: RandomEngineAndDistribution.h:27
SimpleHistogramGenerator::SimpleHistogramGenerator
SimpleHistogramGenerator(TH1 *histo)
Definition: SimpleHistogramGenerator.cc:8
diffTwoXMLs.r1
r1
Definition: diffTwoXMLs.py:53
multiplicitycorr_cfi.xMax
xMax
Definition: multiplicitycorr_cfi.py:5
SimpleHistogramGenerator::integral
std::vector< float > integral
Integral.
Definition: SimpleHistogramGenerator.h:53
SimpleHistogramGenerator::binWidth
double binWidth
Definition: SimpleHistogramGenerator.h:50
RandomEngineAndDistribution
Definition: RandomEngineAndDistribution.h:18