Plotting.h
Go to the documentation of this file.
2 
3 #include "TH1.h"
4 #include "TH2.h"
5 #include "TH3.h"
6 #include "TCanvas.h"
7 #include "TLegend.h"
8 namespace ana {
9 
10  /// General plotting function that plots the vector of hists
11  /// to a multi-page pdf, where the first page can be an overlay of
12  /// all of the hists
13  void PlotAll(TString outfile,
14  TString title,
15  std::vector<TH1*> hists,
16  std::vector<TString> labels,
17  TString draw_option,
18  bool overlay,
19  bool logx = false)
20  {
21  TLegend * leg = new TLegend();
22  TCanvas * c = new TCanvas();
23  c->Print(outfile+"["); //opens pdf for printing
24  c->cd();
25  double maxy = -1;
26  for(int i = 0; i < (int) hists.size(); i++) {
27  hists[i]->GetXaxis()->CenterTitle();
28  hists[i]->GetYaxis()->CenterTitle();
29  if(hists[i]->GetMaximum() > maxy) maxy = hists[i]->GetMaximum();
30  }
31 
32  // leave some headroom
33  maxy = 1.2 * maxy;
34 
35  if(overlay) {
36  hists[0]->SetTitle(TString::Format("%s: All",
37  title.Data()));
38  hists[0]->GetYaxis()->SetRangeUser(0, maxy);
39  hists[0]->Draw(draw_option);
40  leg->AddEntry(hists[0], labels[0]);
41  for(int i = 1; i < (int) hists.size(); i++) {
42  hists[i]->Draw(draw_option + "same");
43  leg->AddEntry(hists[i], labels[i]);
44  }
45  leg->Draw();
46  if(logx) c->SetLogx();
47  c->Print(outfile);
48  }
49  for(int i = 0; i < (int) hists.size(); i++) {
50  hists[i]->SetTitle(TString::Format("%s: %s",
51  title.Data(),
52  labels[i].Data()));
53  hists[i]->GetYaxis()->SetRangeUser(0, maxy);
54  hists[i]->Draw(draw_option);
55  if(logx) c->SetLogx();
56  c->Print(outfile);
57  }
58  c->Print(outfile+"]");
59  }
60 
62  ICrossSectionAnalysis * xsec_ana,
63  std::vector<XSecSysts::Syst_t> systs,
64  std::vector<TString> systs_labels,
65  std::vector<TString> bkgd_labels,
66  std::vector<Int_t> bkgd_colors)
67  {
68  TCanvas * c = new TCanvas();
69  c->Print(outfile+"["); //opens pdf for printing
70  c->cd();
71  for(int isyst = 0; isyst < (int) systs.size(); isyst++) {
72  int ncomponents = xsec_ana->GetCrossSectionSpectra(systs[isyst])->GetSignalEstimator()->GetNBkgdComponents();
73  std::vector<TH1*> bkgds(ncomponents);
74  TLegend * leg = new TLegend();
75 
76  TH1 * selected = xsec_ana->GetCrossSectionSpectra(systs[isyst])->GetSelectedAllRecoSpace()->ToTH1(xsec_ana->GetCrossSectionSpectra(systs[isyst])->GetSelectedAllRecoSpace()->POT());
77  selected->SetTitle("Selection decomposition: " + systs_labels[isyst]);
78  selected->Draw("hist");
79  leg->AddEntry(selected, "Selected", "l");
80  for(int ibkgd = 0; ibkgd < ncomponents; ibkgd++) {
81  bkgds[ibkgd] = (TH1*) xsec_ana->GetCrossSectionSpectra(systs[isyst])->GetSignalEstimator()->BackgroundComponent(ibkgd)->ToTH1(xsec_ana->GetCrossSectionSpectra(systs[isyst])->GetSignalEstimator()->BackgroundComponent(ibkgd)->POT())->Clone();
82  bkgds[ibkgd]->SetLineColor(bkgd_colors[ibkgd]);
83  bkgds[ibkgd]->Draw("hist same");
84  leg->AddEntry(bkgds[ibkgd], bkgd_labels[ibkgd], "l");
85  }
86  leg->Draw();
87  c->Print(outfile);
88  }
89  c->Print(outfile+"]");
90  }
91 
92  /// Plot all efficiencies
93  void PlotAllEfficiency(TString outfile,
94  ICrossSectionAnalysis * xsec_ana,
95  std::vector<XSecSysts::Syst_t> systs,
96  std::vector<TString> labels,
97  std::vector<Int_t> colors)
98  {
99  std::vector<TH1*> eff;
100  for(int isyst = 0; isyst < (int) systs.size(); isyst++) {
101  eff.push_back((TH1*) xsec_ana->Efficiency(systs[isyst])->Clone());
102  eff.back()->SetLineColor(colors[isyst]);
103  eff.back()->GetYaxis()->SetTitle("#epsilon");
104  eff.back()->GetYaxis()->CenterTitle();
105  }
106  PlotAll(outfile,
107  "Efficiency",
108  eff,
109  labels,
110  "hist",
111  true);
112  }
113 
114  /// Plot all flux spectra
115  void PlotAllFlux(TString outfile,
116  ICrossSectionAnalysis * xsec_ana,
117  std::vector<XSecSysts::Syst_t> systs,
118  std::vector<TString> labels,
119  std::vector<Int_t> colors)
120  {
121  std::vector<TH1*> flux;
122  for(int isyst = 0; isyst < (int) systs.size(); isyst++) {
123  flux.push_back(xsec_ana->Flux(systs[isyst]));
124  flux.back()->SetLineColor(colors[isyst]);
125  }
126  PlotAll(outfile,
127  "Flux",
128  flux,
129  labels,
130  "hist",
131  true);
132  }
133 
134  /// Plot all reco to true matrices
135  void PlotAllRecoTrue(TString outfile,
136  ICrossSectionAnalysis * xsec_ana,
137  std::vector<XSecSysts::Syst_t> systs,
138  std::vector<TString> labels)
139 
140  {
141  std::vector<TH1*> recotrue;
142  for(int isyst = 0; isyst< (int) systs.size(); isyst++) {
143  double POT = xsec_ana->GetCrossSectionSpectra(systs[isyst])->GetRecoTrue()->POT();
144  recotrue.push_back(xsec_ana->GetCrossSectionSpectra(systs[isyst])->GetRecoTrue()->ToTH2(POT));
145  }
146 
147  PlotAll(outfile,
148  "Unfolding Matrix",
149  recotrue,
150  labels,
151  "colz",
152  false);
153  }
154 
155  /// Plot all of the signal estimates in a ICrossSectionAnalysis object
157  ICrossSectionAnalysis * xsec_ana,
158  std::vector<XSecSysts::Syst_t> systs,
159  std::vector<TString> labels,
160  std::vector<Int_t> colors)
161  {
162  std::vector<TH1*> signalest;
163  for(int isyst = 0; isyst < (int) systs.size(); isyst++) {
164  double POT = xsec_ana->SignalEstimate(systs[isyst])->POT();
165  signalest.push_back(xsec_ana->SignalEstimate(systs[isyst])->ToTH1(POT));
166  signalest.back()->SetLineColor(colors[isyst]);
167  signalest.back()->SetTitle(TString::Format("Signal Esimate: %s",
168  labels[isyst].Data()));
169  }
170 
171  PlotAll(outfile,
172  "Signal Estimate",
173  signalest,
174  labels,
175  "hist",
176  true);
177  }
178 
179  /// Plot all unfolded signal estimates in an ICrossSectionAnalysis object
181  ICrossSectionAnalysis * xsec_ana,
182  std::vector<XSecSysts::Syst_t> systs,
183  std::vector<TString> labels,
184  std::vector<Int_t> colors)
185  {
186  std::vector<TH1*> signal;
187  for(int isyst = 0; isyst < (int) systs.size(); isyst++) {
188  signal.push_back((TH1*) xsec_ana->UnfoldedSignal(systs[isyst])->Clone());
189  signal.back()->SetLineColor(colors[isyst]);
190  }
191 
192  PlotAll(outfile,
193  "Unfolded Signal",
194  signal,
195  labels,
196  "hist",
197  true);
198  }
199 
200  /// Plot all purity distributions in an ICrossSectionAnalysis object
201  void PlotAllPurity(TString outfile,
202  ICrossSectionAnalysis * xsec_ana,
203  std::vector<XSecSysts::Syst_t> systs,
204  std::vector<TString> labels,
205  std::vector<Int_t> colors)
206  {
207  std::vector<TH1*> purity;
208  for(int isyst = 0; isyst < (int) systs.size(); isyst++) {
209  purity.push_back((TH1*) xsec_ana->Purity(systs[isyst])->Clone());
210  purity.back()->SetLineColor(colors[isyst]);
211  }
212 
213  PlotAll(outfile,
214  "Purity",
215  purity,
216  labels,
217  "hist",
218  true);
219  }
220 
221  /// Plot cross section over Enu for each systematic shift
223  ICrossSectionAnalysis * xsec_ana,
224  std::vector<XSecSysts::Syst_t> systs,
225  std::vector<TString> syst_labels,
226  std::vector<Int_t> colors,
227  bool logx = true)
228  {
229  std::vector<TH1*> xsec_over_e;
230  for(int isyst = 0; isyst < (int) systs.size(); isyst++) {
231  xsec_over_e.push_back((TH1*) xsec_ana->GetCrossSection(systs[isyst])->Clone());
232  xsec_over_e.back()->SetLineColor(colors[isyst]);
233  xsec_over_e.back()->GetXaxis()->SetTitle("Neutrino Energy (GeV)");
234  xsec_over_e.back()->GetYaxis()->SetTitle("#sigma / E_{#nu} (10^{-38} cm^{2} / GeV)");
235  xsec_over_e.back()->GetXaxis()->CenterTitle();
236  xsec_over_e.back()->GetYaxis()->CenterTitle();
237  for(int ibin = 1; ibin <= xsec_over_e.back()->GetNbinsX(); ibin++) {
238  double e = xsec_over_e.back()->GetBinCenter(ibin);
239  double xsec = xsec_over_e.back()->GetBinContent(ibin);
240  xsec_over_e.back()->SetBinContent(ibin, xsec / e);
241  }
242  xsec_over_e.back()->Scale(1/1e-38);
243  }
244  PlotAll(outfile,
245  "#sigma / E_{#nu}",
246  xsec_over_e,
247  syst_labels,
248  "hist",
249  true,
250  logx);
251  }
252 
253  /// Plot cross section result of each systematic shift
255  TString xtitle,
256  TString ytitle,
257  ICrossSectionAnalysis * xsec_ana,
258  std::vector<XSecSysts::Syst_t> systs,
259  std::vector<TString> syst_labels,
260  std::vector<Int_t> colors,
261  double scale = 1e-38)
262  {
263  std::vector<TH1*> result;
264  for(int isyst = 0; isyst < (int) systs.size(); isyst++) {
265  result.push_back((TH1*) xsec_ana->GetCrossSection(systs[isyst])->Clone());
266  result.back()->SetLineColor(colors[isyst]);
267  if(scale > 0) result.back()->Scale(1 / scale);
268  result.back()->GetXaxis()->SetTitle(xtitle.Data());
269  result.back()->GetYaxis()->SetTitle(ytitle.Data());
270  }
271  PlotAll(outfile,
272  "Cross Section Result",
273  result,
274  syst_labels,
275  "hist",
276  true);
277  }
278 
279  TGraphAsymmErrors * GetAsymmErrorBars(TH1D * hnom, TH1D * hup, TH1D * hdown)
280  {
281  TGraphAsymmErrors * g = new TGraphAsymmErrors();
282  for(int ibin = 1; ibin <= hnom->GetNbinsX(); ibin++) {
283  double y = hnom->GetBinContent(ibin);
284  double x = hnom->GetBinCenter(ibin);
285  double w = hnom->GetBinWidth(ibin);
286  double err_up = hup->GetBinContent(ibin);
287  double err_down = hdown->GetBinContent(ibin);
288  g->SetPoint(ibin, x, y);
289  g->SetPointError(ibin, w/2, w/2, err_up, err_down);
290  }
291  return g;
292  }
293 
294 
295 }
void PlotAllSignalEstimates(TString outfile, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > labels, std::vector< Int_t > colors)
Plot all of the signal estimates in a ICrossSectionAnalysis object.
Definition: Plotting.h:156
virtual TH1 * Flux(XSecSysts::Syst_t)=0
TH1 * GetCrossSection(XSecSysts::Syst_t)
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
TGraphAsymmErrors * GetAsymmErrorBars(TH1D *hnom, TH1D *hup, TH1D *hdown)
Definition: Plotting.h:279
std::vector< SystGroupDef > systs
Definition: syst_header.h:385
TH1D * ToTH1(double exposure, Color_t col=kBlack, Style_t style=kSolid, EExposureType expotype=kPOT, EBinType bintype=kBinContent) const
Histogram made from this Spectrum, scaled to some exposure.
Definition: Spectrum.cxx:209
double maxy
const Spectrum * GetSelectedAllRecoSpace() const
const ISignalEstimator * GetSignalEstimator() const
void PlotAllSelectionDecomposition(TString outfile, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > systs_labels, std::vector< TString > bkgd_labels, std::vector< Int_t > bkgd_colors)
Definition: Plotting.h:61
void PlotAllPurity(TString outfile, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > labels, std::vector< Int_t > colors)
Plot all purity distributions in an ICrossSectionAnalysis object.
Definition: Plotting.h:201
TString hists[nhists]
Definition: bdt_com.C:3
Loaders::FluxType flux
void PlotAllRecoTrue(TString outfile, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > labels)
Plot all reco to true matrices.
Definition: Plotting.h:135
const TH1 * UnfoldedSignal(XSecSysts::Syst_t)
void PlotAllCrossSectionOverENu(TString outfile, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > syst_labels, std::vector< Int_t > colors, bool logx=true)
Plot cross section over Enu for each systematic shift.
Definition: Plotting.h:222
virtual TH1 * Efficiency(XSecSysts::Syst_t)=0
void PlotAllEfficiency(TString outfile, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > labels, std::vector< Int_t > colors)
Plot all efficiencies.
Definition: Plotting.h:93
virtual TH1 * Purity(XSecSysts::Syst_t)=0
Double_t scale
Definition: plot.C:25
int colors[6]
Definition: tools.h:1
void PlotAllCrossSectionResult(TString outfile, TString xtitle, TString ytitle, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > syst_labels, std::vector< Int_t > colors, double scale=1e-38)
Plot cross section result of each systematic shift.
Definition: Plotting.h:254
void PlotAllFlux(TString outfile, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > labels, std::vector< Int_t > colors)
Plot all flux spectra.
Definition: Plotting.h:115
double POT() const
Definition: Spectrum.h:231
Double_t xsec[nknots]
Definition: testXsec.C:47
std::vector< double > POT
const ReweightableSpectrum * GetRecoTrue() const
bool logx
void PlotAllUnfoldedSignalEstimates(TString outfile, ICrossSectionAnalysis *xsec_ana, std::vector< XSecSysts::Syst_t > systs, std::vector< TString > labels, std::vector< Int_t > colors)
Plot all unfolded signal estimates in an ICrossSectionAnalysis object.
Definition: Plotting.h:180
virtual int GetNBkgdComponents() const =0
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
virtual const Spectrum * BackgroundComponent(int) const =0
const CrossSectionSpectra * GetCrossSectionSpectra(XSecSysts::Syst_t)
Float_t e
Definition: plot.C:35
Float_t w
Definition: plot.C:20
FILE * outfile
Definition: dump_event.C:13
const Spectrum * SignalEstimate(XSecSysts::Syst_t)
void PlotAll(TString outfile, TString title, std::vector< TH1 * > hists, std::vector< TString > labels, TString draw_option, bool overlay, bool logx=false)
Definition: Plotting.h:13
TH2D * ToTH2(double pot) const