BCMTFTemplate.cxx
1 /*
2  * Copyright (C) 2007-2018, the BAT core developer team
3  * All rights reserved.
4  *
5  * For the licensing terms see doc/COPYING.
6  * For documentation see http://mpp.mpg.de/bat
7  */
8 
9 // ---------------------------------------------------------
10 
11 #include <TH1D.h>
12 #include <TRandom.h>
13 
14 #include <iostream>
15 
16 #include "BCMTFTemplate.h"
17 
18 // ---------------------------------------------------------
19 BCMTFTemplate::BCMTFTemplate(const std::string& channelname, const std::string& processname)
20  : fEfficiency(0)
21  , fHistogram(0)
22  , fNBins(0)
23  , fNormalization(0)
24  , fOriginalNormalization(0)
25 {
26  fChannelName = channelname;
27  fProcessName = processname;
28  fFunctionContainer = new std::vector<TF1*>(0);
29  fRandom = new TRandom3(0);
30 }
31 
32 // ---------------------------------------------------------
34 {
35 }
36 
37 // ---------------------------------------------------------
38 void BCMTFTemplate::SetHistogram(TH1D* hist, double norm)
39 {
40  // set histogram
41  fHistogram = hist;
42 
43  // check if histogram exists
44  if (!hist)
45  return;
46 
47  // get number of bins
48  fNBins = fHistogram->GetNbinsX();
49 
50  // set original normalization
51  double orignorm = fHistogram->Integral();
52  SetOrignialNormalization(orignorm);
53 
54  // normalize histogram
55  if (orignorm && norm)
56  fHistogram->Scale(norm / orignorm);
57 
58  // set normalization
59  if (norm)
60  fNormalization = norm;
61 }
62 
63 // ---------------------------------------------------------
64 void BCMTFTemplate::SetFunctionContainer(std::vector<TF1*>* funccont, int nbins)
65 {
66  fFunctionContainer = funccont;
67  fNBins = nbins;
68 }
69 
70 // ---------------------------------------------------------
71 TH1D BCMTFTemplate::FluctuateHistogram(const std::string& options, double norm)
72 {
73  // option flags
74  bool flag_p = false;
75  bool flag_g = false;
76  bool flag_z = false;
77 
78  // check content of options string
79  if (options.find("P") < options.size()) {
80  flag_p = true;
81  }
82 
83  if (options.find("G") < options.size()) {
84  flag_g = true;
85  }
86 
87  if (options.find("Z") < options.size()) {
88  flag_z = true;
89  }
90 
91  if (flag_p && flag_g) {
92  flag_g = false;
93  }
94 
95  TH1D hist_temp = TH1D(*fHistogram);
96 
97  for (int i = 1; i <= fNBins; ++i) {
98  double expectation = fOriginalNormalization * hist_temp.GetBinContent(i);
99  double error = fOriginalNormalization * hist_temp.GetBinError(i);
100  double n = 0;
101 
102  // throw random number according to Poisson distribution
103  if (flag_p) {
104  n = (double) fRandom->Poisson(expectation);
105  }
106 
107  // throw random number according to Gauss distribution
108  else if (flag_g) {
109  double dn = fRandom->Gaus(expectation, error);
110 
111  // make it a truncated Gaussian
112  if (flag_z) {
113  while (n + dn < 0)
114  dn = fRandom->Gaus(expectation, error);
115  }
116  n += dn;
117  }
118 
119  // set the number of events in the template
120  hist_temp.SetBinContent(i, n);
121  }
122 
123  // normalize histogram
124  double orignorm = hist_temp.Integral();
125 
126  if (orignorm)
127  hist_temp.Scale(norm / orignorm);
128 
129  return hist_temp;
130 }
131 
132 // ---------------------------------------------------------
void SetFunctionContainer(std::vector< TF1 * > *funccont, int nbins)
Set a function container funccont The function container nbins The number of bins (and functions) ...
void SetOrignialNormalization(double norm)
Set the original normalization.
BCMTFTemplate(const std::string &channelname, const std::string &processname)
The default constructor.
void SetHistogram(TH1D *hist, double norm=1)
Set the histogram.
TH1D FluctuateHistogram(const std::string &options="GZ", double norm=1)
Fluctuate the original template histogram by the uncertainty on the bin content.
~BCMTFTemplate()
The default destructor.