BAT  0.9.4
The Bayesian analysis toolkit
 All Classes Namespaces Functions Variables Enumerations
BCMTFComparisonTool.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 <TH1D.h>
14 #include <TH2D.h>
15 #include <TCanvas.h>
16 #include <TGraphAsymmErrors.h>
17 #include <TLatex.h>
18 
19 #include "BCMTFComparisonTool.h"
20 
21 // ---------------------------------------------------------
23 {
24  fName = name;
25 }
26 
27 // ---------------------------------------------------------
29 {
30  for (int i = 0; i < GetNContributions(); ++i) {
31  if (fHistogramContainer.at(i))
32  delete fHistogramContainer.at(i);
33  }
34 }
35 
36 // ---------------------------------------------------------
37 void BCMTFComparisonTool::AddContribution(const char * name, TH1D hist)
38 {
39  // add name to container
40  fNameContainer.push_back(name);
41 
42  // add histogram to container
43  fHistogramContainer.push_back(new TH1D(hist));
44 
45  // add central value to container
46  fCentralValueContainer.push_back(hist.GetMean());
47 
48  // add uncertainty to container
49  fUncertaintyContainer.push_back(hist.GetRMS());
50 }
51 
52 // ---------------------------------------------------------
53 void BCMTFComparisonTool::AddContribution(const char * name, double centralvalue, double uncertainty)
54 {
55  // add name to container
56  fNameContainer.push_back(name);
57 
58  // add 0 to container
59  fHistogramContainer.push_back(0);
60 
61  // add central value to container
62  fCentralValueContainer.push_back(centralvalue);
63 
64  // add uncertainty to container
65  fUncertaintyContainer.push_back(uncertainty);
66 }
67 
68 // ---------------------------------------------------------
69 void BCMTFComparisonTool::PrintHistograms(const char * filename)
70 {
71  // get number of histograms
72  int nhistograms = (int) fHistogramContainer.size();
73 
74  // create canvas
75  TCanvas * c1 = new TCanvas();
76  c1->cd();
77 
78  // loop over all histograms
79  for (int i = 0; i < nhistograms; ++i) {
80  // get histogram
81  TH1D * hist = fHistogramContainer.at(i);
82 
83  // set color
84  hist->SetLineColor(2+i);
85 
86  // draw histogram
87  if (i == 0) {
88  hist->Draw("HIST");
89  std::cout << " here as well." << std::endl;
90  }
91  else {
92  hist->Draw("SAMEHIST");
93  std::cout << " here as well 2." << std::endl;
94  }
95  }
96 
97  // print canvas
98  c1->Print(filename);
99 
100  // free memory
101  delete c1;
102 }
103 
104 // ---------------------------------------------------------
106 {
107  // get number of contributions
108  int ncontributions = GetNContributions();
109 
110  // create graph
111  TGraphAsymmErrors * graph_contributions = new TGraphAsymmErrors(ncontributions);
112  graph_contributions->SetMarkerStyle(20);
113  graph_contributions->SetMarkerSize(1);
114 
115  // coordinate system
116  double xmin = 0.0;
117  double xmax = 0.0;
118  double xwidth = 0.0;
119  double ymin = -0.5;
120  double ymax = double(ncontributions)-0.5;
121 
122  // ---- fill the graph ---- //
123 
124  // loop over all contributions
125  for (int i = 0; i < ncontributions; ++i) {
126 
127  // get summary information
128  double centralvalue = fCentralValueContainer.at(i);
129  double uncertainty = fUncertaintyContainer.at(i);
130 
131  // update coordinate system
132  if ((centralvalue-uncertainty) < xmin || i == 0)
133  xmin = centralvalue-uncertainty;
134  if ((centralvalue+uncertainty) > xmax || i == 0)
135  xmax = centralvalue+uncertainty;
136  xwidth = xmax - xmin;
137 
138  // set point and error
139  graph_contributions->SetPoint(i, centralvalue, double(ncontributions-i-1));
140  graph_contributions->SetPointError(i, uncertainty, uncertainty, 0, 0);
141  }
142 
143  // ---- do the plotting ---- //
144 
145  // create histogram for axes
146  TH2D * hist_axes = new TH2D("", Form(";%s;", GetName().c_str()), 1, xmin - 0.25 *xwidth, xmax + 1.75 * xwidth, ncontributions, ymin, ymax);
147  hist_axes->SetStats(kFALSE);
148  hist_axes->GetYaxis()->SetNdivisions(0);
149  hist_axes->GetYaxis()->SetTitleOffset(1.0);
150 
151  // create latex
152  TLatex * latex = new TLatex();
153  latex->SetTextSize(0.04);
154  if (ncontributions>=10)
155  latex->SetTextSize(0.02);
156  latex->SetTextAlign(12);
157 
158  // draw
159  hist_axes->Draw();
160  graph_contributions->Draw("SAMEPZ");
161 
162  // loop over all contributions and draw labels
163  for (int i = 0; i < ncontributions; ++i) {
164  latex->DrawLatex(xmax + 0.25 *xwidth, double(ncontributions-i-1), fNameContainer.at(i).c_str());
165  }
166  hist_axes->Draw("SAMEAXIS");
167 
168  // todo: delete objects properly
169 }
170 
171 // ---------------------------------------------------------
172 void BCMTFComparisonTool::PrintOverview(const char * filename)
173 {
174  // create canvas
175  TCanvas * c1 = new TCanvas();
176  c1->cd();
177 
178  // draw the overview
179  DrawOverview();
180 
181  // print to file
182  c1->Print(filename);
183 }
184 
185 // ---------------------------------------------------------
BCMTFComparisonTool(const char *name)
void PrintOverview(const char *filename)
void AddContribution(const char *name, TH1D hist)
void PrintHistograms(const char *filename)