BAT  0.9.4
The Bayesian analysis toolkit
 All Classes Namespaces Functions Variables Enumerations
BCMTFChannel.cxx
1 /*
2  * Copyright (C) 2007-2014, 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 <iostream>
12 
13 #include <TCanvas.h>
14 #include <TH1D.h>
15 #include <TH2D.h>
16 #include <TLatex.h>
17 #include <TMath.h>
18 
19 #include "BCMTFTemplate.h"
20 #include "BCMTFSystematicVariation.h"
21 
22 #include "BCMTFChannel.h"
23 
24 // ---------------------------------------------------------
25 BCMTFChannel::BCMTFChannel(const char * name)
26  : fData(0)
27  , fFlagChannelActive(true)
28  , fHistUncertaintyBandExpectation(0)
29  , fHistUncertaintyBandPoisson(0)
30 {
31  fName = name;
32 }
33 
34 // ---------------------------------------------------------
36 {
37  if (fData)
38  delete fData;
39 
40  for (unsigned int i = 0; i < fTemplateContainer.size(); ++i)
41  delete (fTemplateContainer.at(i));
42 
43  for (unsigned int i = 0; i < fSystematicVariationContainer.size(); ++i)
44  delete (fSystematicVariationContainer.at(i));
45 
46  /*
47  if (fHistUncertaintyBandExpectation)
48  delete fHistUncertaintyBandExpectation;
49 
50  if (fHistUncertaintyBandPoisson)
51  delete fHistUncertaintyBandPoisson;
52  */
53 }
54 
55 // ---------------------------------------------------------
56 void BCMTFChannel::PrintTemplates(std::string filename)
57 {
58  // check if file extension is pdf
59  if ( (filename.find_last_of(".") != std::string::npos) &&
60  (filename.substr(filename.find_last_of(".")+1) == "pdf") ) {
61  ; // it's a PDF file
62 
63  }
64  else if ( (filename.find_last_of(".") != std::string::npos) &&
65  (filename.substr(filename.find_last_of(".")+1) == "ps") ) {
66  ; // it's a PS file
67  }
68  else {
69  ; // make it a PDF file
70  filename += ".pdf";
71  }
72 
73  // create new canvas
74  TCanvas * c1 = new TCanvas();
75  c1->cd();
76 
77  // get number of templates
78  int ntemplates = int(fTemplateContainer.size());
79 
80  int first_hist = -1;
81  int last_hist = 0;
82 
83  // calculate first and last existing histogram
84  for (int i = 0; i < ntemplates; ++i) {
85  // get histogram
86  TH1D * temphist = GetTemplate(i)->GetHistogram();
87 
88  if (first_hist < 0 && temphist)
89  first_hist = i;
90 
91  if (temphist)
92  last_hist = i;
93  }
94 
95  // Don't print if there are no histograms
96  if ( first_hist < 0 ) {
97  // free memory
98  delete c1;
99 
100  return;
101  }
102 
103  // loop over templates
104  for (int i = 0; i < ntemplates; ++i) {
105 
106  // get histogram
107  TH1D * temphist = GetTemplate(i)->GetHistogram();
108 
109  TLatex* l = new TLatex();
110 
111  // draw
112  if (temphist) {
113  temphist->Draw();
114  l->DrawTextNDC(0.2, 0.9, Form("%s - %s", fName.c_str(), GetTemplate(i)->GetProcessName().c_str()));
115  }
116 
117  // print
118  if (i == first_hist && (first_hist != last_hist))
119  c1->Print(std::string( filename + "(").c_str());
120  else if (i == last_hist && (first_hist != last_hist))
121  c1->Print(std::string( filename + ")").c_str());
122  else {
123  if (temphist)
124  c1->Print(filename.c_str());
125  }
126 
127  // free memory
128  delete l;
129  }
130 
131  // free memory
132  delete c1;
133 }
134 
135 // ---------------------------------------------------------
136 void BCMTFChannel::PrintTemplate(int index, const char * filename)
137 {
138  // create new canvas
139  TCanvas * c1 = new TCanvas();
140  c1->cd();
141 
142  // get number of systematics
143  unsigned int nsystematics = fSystematicVariationContainer.size();
144 
145  // draw template
146  GetTemplate(index)->GetHistogram()->Draw();
147 
148  // print to file
149  if (nsystematics == 0)
150  c1->Print(filename);
151  else
152  c1->Print( (std::string(filename)+std::string("(")).c_str() );
153 
154  // loop over systematics
155  for (unsigned int isystematic = 0; isystematic < nsystematics; ++isystematic) {
156  c1->cd();
157 
158  // check that histogram exists
159  if (!GetSystematicVariation(isystematic)->GetHistogramUp(index) ||
160  !GetSystematicVariation(isystematic)->GetHistogramDown(index))
161  continue;
162 
163  // get histogram
164  TH1D hist = TH1D(*GetTemplate(index)->GetHistogram());
165  TH1D hist_up(hist);
166  TH1D hist_down(hist);
167 
168  // set style
169  hist.SetFillStyle(0);
170  hist_up.SetFillStyle(0);
171  hist_up.SetLineStyle(2);
172  hist_down.SetFillStyle(0);
173  hist_down.SetLineStyle(3);
174 
175  // get efficiency
176  double efficiency = GetTemplate(index)->GetEfficiency();
177 
178  // scale histogram
179  hist.Scale(efficiency/ hist.Integral());
180 
181  // loop over all bins
182  for (int i = 1; i <= hist.GetNbinsX(); ++i) {
183  hist.SetBinContent(i, GetTemplate(index)->GetHistogram()->GetBinContent(i));
184  hist_up.SetBinContent(i, hist.GetBinContent(i) * (1.0 + GetSystematicVariation(isystematic)->GetHistogramUp(index)->GetBinContent(i)));
185  hist_down.SetBinContent(i, hist.GetBinContent(i) * (1.0 - GetSystematicVariation(isystematic)->GetHistogramDown(index)->GetBinContent(i)));
186  }
187 
188  // draw histogram
189  hist_up.Draw("HIST");
190  hist.Draw("HISTSAME");
191  hist_down.Draw("HISTSAME");
192 
193  if (isystematic < nsystematics-1)
194  c1->Print(filename);
195  else
196  c1->Print( (std::string(filename)+std::string(")")).c_str() );
197  }
198 
199  // free memory
200  delete c1;
201 }
202 
203 // ---------------------------------------------------------
205 {
206  // create new canvas
207  TCanvas * c1 = new TCanvas();
208  c1->cd();
209 
210  // draw histogram
211  fHistUncertaintyBandExpectation->Draw("COLZ");
212  c1->Draw();
213 
214  // print
215  c1->Print(filename);
216 
217  // free memory
218  delete c1;
219 }
220 
221 // ---------------------------------------------------------
223 {
224  // calculate histogram
225  int nbinsy_exp = fHistUncertaintyBandExpectation->GetNbinsY();
226  int nbinsx_poisson = fHistUncertaintyBandPoisson->GetNbinsX();
227  int nbinsy_poisson = fHistUncertaintyBandPoisson->GetNbinsY();
228 
229  // loop over x-axis of observation
230  for (int ix = 1; ix <= nbinsx_poisson; ++ix) {
231  double sum_w = 0;
232  // loop over y-axis of expectation and calculate sum of weights
233  for (int iy = 1; iy <= nbinsy_exp; ++iy) {
234  double w = fHistUncertaintyBandExpectation->GetBinContent(ix, iy);
235  sum_w += w;
236  }
237  // loop over y-axis of expectation
238  for (int iy = 1; iy <= nbinsy_exp; ++iy) {
239  double w = fHistUncertaintyBandExpectation->GetBinContent(ix, iy)/sum_w;
240  double expectation = fHistUncertaintyBandExpectation->GetYaxis()->GetBinCenter(iy);
241  // loop over y-axis of observation
242  for (int jbin = 1; jbin <= nbinsy_poisson; ++jbin) {
243  double p = TMath::Poisson(double(jbin-1), expectation);
244  double bincontent = 0;
245  if (iy>1)
246  bincontent=fHistUncertaintyBandPoisson->GetBinContent(ix, jbin);
247  fHistUncertaintyBandPoisson->SetBinContent(ix, jbin, bincontent+p*w);
248  }
249  }
250  }
251 
252 }
253 
254 // ---------------------------------------------------------
255 TH1D* BCMTFChannel::CalculateUncertaintyBandPoisson(double minimum, double maximum, int color)
256 {
257  TH1D* hist = new TH1D(*(fData->GetHistogram()));
258  hist->SetMarkerSize(0);
259  hist->SetFillColor(color);
260  hist->SetFillStyle(1001);
261 
262  int nbinsx_poisson = fHistUncertaintyBandPoisson->GetNbinsX();
263  int nbinsy_poisson = fHistUncertaintyBandPoisson->GetNbinsY();
264 
265  // loop over x-axis of observation
266  for (int ix = 1; ix <= nbinsx_poisson; ++ix) {
267  double sum_p = 0; // sum of all probabilities inside the interval
268  int limit_min = 0;
269  int limit_max = nbinsx_poisson-1;
270 
271  // loop over y-axis of observation
272  for (int jbin = 1; jbin <= nbinsy_poisson; ++jbin) {
273  double p = fHistUncertaintyBandPoisson->GetBinContent(ix, jbin);
274  sum_p+=p;
275  if (sum_p < minimum)
276  limit_min=jbin;
277  if (sum_p > maximum && (sum_p - p) < maximum )
278  limit_max=jbin-1;
279  }
280  // hist->SetBinContent(ix, 0.5*double(limit_min+limit_max));
281  // hist->SetBinError(ix, 0.5*double(limit_max-limit_min));
282  double ylimit_min = fHistUncertaintyBandPoisson->GetYaxis()->GetBinCenter(limit_min);
283  double ylimit_max = fHistUncertaintyBandPoisson->GetYaxis()->GetBinCenter(limit_max);
284  hist->SetBinContent(ix, 0.5*double(ylimit_min+ylimit_max));
285  hist->SetBinError(ix, 0.5*double(ylimit_max-ylimit_min));
286  }
287 
288  return hist;
289 }
290 
291 // ---------------------------------------------------------
293 {
294  // create new canvas
295  TCanvas * c1 = new TCanvas();
296  c1->cd();
297 
298  // calculate error band
300 
301  TH2D hist(*fHistUncertaintyBandPoisson);
302 
303  int nbinsx_poisson = hist.GetNbinsX();
304  int nbinsy_poisson = hist.GetNbinsY();
305 
306  // loop over x-axis of observation
307  for (int ix = 1; ix <= nbinsx_poisson; ++ix) {
308  double sum_p = 0; // sum of all probabilities inside the interval
309 
310  // loop over y-axis of observation
311  for (int jbin = 1; jbin <= nbinsy_poisson; ++jbin) {
312  double p = hist.GetBinContent(ix, jbin);
313  sum_p+=p;
314  hist.SetBinContent(ix, jbin, sum_p);
315  }
316  }
317 
318  // draw histogram
319  hist.Draw("COLZ");
320  c1->Draw();
321 
322  // print
323  c1->Print(filename);
324 
325  // free memory
326  delete c1;
327 }
328 
329 // ---------------------------------------------------------
330 void BCMTFChannel::PrintHistUncertaintyBandPoisson(const char* filename, const char * options)
331 {
332  // create new canvas
333  TCanvas * c1 = new TCanvas();
334  c1->cd();
335 
336  // calculate error band
338 
339  // draw histogram
340  fHistUncertaintyBandPoisson->Draw(options);
341  c1->Draw();
342 
343  // print
344  c1->Print(filename);
345 
346  // free memory
347  delete c1;
348 }
349 
350 // ---------------------------------------------------------
351 void BCMTFChannel::PrintUncertaintyBandPoisson(const char* filename, double minimum, double maximum, int color)
352 {
353  // create new canvas
354  TCanvas * c1 = new TCanvas();
355  c1->cd();
356 
357  // calculate error band
358  TH1D* hist=CalculateUncertaintyBandPoisson(minimum, maximum, color);
359 
360  // draw histogram
361  hist->Draw("E2");
362  c1->Draw();
363 
364  // print
365  c1->Print(filename);
366 
367  // free memory
368  delete c1;
369 }
370 
371 // ---------------------------------------------------------
TH1D * CalculateUncertaintyBandPoisson(double minimum, double maximumm, int color)
void PrintHistUncertaintyBandExpectation(const char *filename)
void PrintTemplates(std::string filename)
double GetEfficiency()
Definition: BCMTFTemplate.h:66
BCMTFChannel(const char *name)
BCMTFTemplate * GetTemplate(int index)
Definition: BCMTFChannel.h:68
BCMTFSystematicVariation * GetSystematicVariation(int index)
Definition: BCMTFChannel.h:75
TH1D * GetHistogram()
Definition: BCMTFTemplate.h:71
void PrintHistCumulativeUncertaintyBandPoisson(const char *filename)
std::string GetProcessName()
Definition: BCMTFTemplate.h:61
void CalculateHistUncertaintyBandPoisson()
void PrintUncertaintyBandPoisson(const char *filename, double minimum, double maximum, int color)
void PrintHistUncertaintyBandPoisson(const char *filename, const char *options="COLZ")
void PrintTemplate(int index, const char *filename)