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