PlotUtilities.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include "TCanvas.h"
8 #include "TH1.h"
9 #include "TH2.h"
10 #include "THStack.h"
11 #include "TLegend.h"
12 #include "TLine.h"
13 #include "TPaletteAxis.h"
14 #include "TText.h"
15 
16 /// Function to make NOvA-style spectra
17 void DrawSpectrum(covmx::Sample sample, Spectrum* cosmic, osc::IOscCalc* calc,
19 
20  // Create canvas
21  TCanvas c("c", "c", 1600, 900);
22  TLegend l(0.6, 0.6, 0.88, 0.88);
23 
24  // NC spectrum
25  TH1* hNC = sample.GetPrediction()->PredictComponent(calc,
26  Flavors::kAll, Current::kNC, Sign::kBoth).ToTH1(sample.GetPOT());
27  for (int iBin = 1; iBin <= hNC->GetNbinsX(); ++iBin) {
28  hNC->SetBinContent(iBin, 0.25*hNC->GetBinContent(iBin)/hNC->GetBinWidth(iBin));
29  hNC->SetBinError(iBin, 0.25*sqrt(error(iBin-1,iBin-1))/hNC->GetBinWidth(iBin));
30  }
31  TH1D* hTotal = (TH1D*)hNC->Clone("hTotal");
32  hTotal->SetLineColor(kAzure+2);
33 
34  // Beam backgrounds
35  TH1* hBeamBkg = sample.GetPrediction()->PredictComponent(calc,
36  Flavors::kAll, Current::kCC, Sign::kBoth).ToTH1(sample.GetPOT());
37  for (int iBin = 1; iBin <= hBeamBkg->GetNbinsX(); ++iBin) {
38  hBeamBkg->SetBinContent(iBin, 0.25*hBeamBkg->GetBinContent(iBin)/hBeamBkg->GetBinWidth(iBin));
39  }
40  hBeamBkg->SetLineColor(kNumuBackgroundColor);
41  hBeamBkg->SetFillColorAlpha(kNumuBackgroundColor, 0.35);
42  hTotal->Add(hBeamBkg);
43 
44  // Cosmic spectrum
45  TH1D* hCosmic = nullptr;
46  if (cosmic) {
47  hCosmic = cosmic->ToTH1(sample.GetLivetime(), kLivetime);
48  for (int iBin = 1; iBin <= hCosmic->GetNbinsX(); ++iBin) {
49  hCosmic->SetBinContent(iBin, 0.25*hCosmic->GetBinContent(iBin)/hCosmic->GetBinWidth(iBin));
50  }
51  hCosmic->SetLineColor(kOrange-3);
52  hCosmic->SetFillColorAlpha(kOrange-3, 0.35);
53  hTotal->Add(hCosmic);
54  }
55 
56  // Make legend
57  TH1D* hTotalError = (TH1D*)hTotal->Clone("hTotalError");
58  hTotalError->SetFillColorAlpha(kAzure+2, 0.35);
59  l.AddEntry(hTotalError, "NC beam signal", "lf");
60  l.AddEntry(hBeamBkg, "CC beam background", "lf");
61  if (hCosmic) l.AddEntry(hCosmic, "Cosmic background", "lf");
62 
63  // Create stacked histogram
64  hTotalError->Draw("e2");
65  hTotal->Draw("hist same");
66  THStack hStack("hStack", "");
67  if (hCosmic) hStack.Add(hCosmic);
68  hStack.Add(hBeamBkg);
69  hStack.Draw("hist same");
70  hTotalError->GetXaxis()->SetTitle("Reconstructed #nu energy (GeV)");
71  hTotalError->GetYaxis()->SetTitle(Form("Events / 250MeV / %.2e POT", sample.GetPOT()));
72  l.Draw();
73 
74  // Draw the plot and clean up
75  c.SaveAs(path.c_str());
76  delete hTotal;
77  delete hNC;
78  delete hBeamBkg;
79  if (hCosmic) delete hCosmic;
80 
81 } // function DrawSpectrum
82 
83 /// Function to draw a two-detector covariance matrix for the NC selection
84 /*void DrawCovMx(covmx::CovarianceMatrix* mx, std::vector<double> pred, std::string path) {
85 
86  // New canvas and histogram
87  TCanvas c("c", "c", 1600, 900);
88  TH2D* h = mx->GetCovMxRelativeTH2(pred);
89  h->Draw("colz");
90 
91  // Draw lines to separate out the matrix quadrants
92  TLine lx(0, 19.5, 39, 19.5);
93  lx.Draw();
94  TLine ly(19.5, 0, 19.5, 39);
95  ly.Draw();
96  c.Update();
97 
98  // Move over the palette so Z axis tick labels are no longer cut off
99  TPaletteAxis *palette = (TPaletteAxis*)h->GetListOfFunctions()->FindObject("palette");
100  palette->SetX2NDC(0.93);
101  h->GetXaxis()->SetTitle("Reconstructed energy bin");
102  h->GetYaxis()->SetTitle("Reconstructed energy bin");
103 
104  // Add text labelling the two detectors
105  TText tndx(0.24, 0.94, "Near Detector");
106  tndx.SetNDC(true);
107  tndx.SetTextSize(0.04);
108  tndx.Draw();
109  TText tfdx(0.64, 0.94, "Far Detector");
110  tfdx.SetNDC(true);
111  tfdx.SetTextSize(0.04);
112  tfdx.Draw();
113  TText tndy(0.06, 0.2, "Near Detector");
114  tndy.SetNDC(true);
115  tndy.SetTextSize(0.04);
116  tndy.SetTextAngle(90);
117  tndy.Draw();
118  TText tfdy(0.06, 0.6, "Far Detector");
119  tfdy.SetNDC(true);
120  tfdy.SetTextSize(0.04);
121  tfdy.SetTextAngle(90);
122  tfdy.Draw();
123 
124  // Save the plot and clean up
125  c.SaveAs(path.c_str());
126  delete h;
127 
128 } // function DrawCovMx
129 */
130 /// Function to draw a two-detector correlation matrix for the NC selection
131 void DrawCorrMx(covmx::CovarianceMatrix* mx, std::string path) {
132 
133  // New canvas and histogram
134  TCanvas c("c", "c", 1600, 900);
135  TH2D* h = mx->GetCorrMxTH2();
136  h->Draw("colz");
137 
138  // Draw lines to separate out the matrix quadrants
139  TLine lx(0, 19.5, 39, 19.5);
140  lx.Draw();
141  TLine ly(19.5, 0, 19.5, 39);
142  ly.Draw();
143  c.Update();
144 
145  // Move over the palette so Z axis tick labels are no longer cut off
146  TPaletteAxis *palette = (TPaletteAxis*)h->GetListOfFunctions()->FindObject("palette");
147  palette->SetX2NDC(0.93);
148  h->GetXaxis()->SetTitle("Reconstructed energy bin");
149  h->GetYaxis()->SetTitle("Reconstructed energy bin");
150 
151  // Add text labelling the two detectors
152  TText tndx(0.24, 0.94, "Near Detector");
153  tndx.SetNDC(true);
154  tndx.SetTextSize(0.04);
155  tndx.Draw();
156  TText tfdx(0.64, 0.94, "Far Detector");
157  tfdx.SetNDC(true);
158  tfdx.SetTextSize(0.04);
159  tfdx.Draw();
160  TText tndy(0.06, 0.2, "Near Detector");
161  tndy.SetNDC(true);
162  tndy.SetTextSize(0.04);
163  tndy.SetTextAngle(90);
164  tndy.Draw();
165  TText tfdy(0.06, 0.6, "Far Detector");
166  tfdy.SetNDC(true);
167  tfdy.SetTextSize(0.04);
168  tfdy.SetTextAngle(90);
169  tfdy.Draw();
170 
171  // Save the plot and clean up
172  c.SaveAs(path.c_str());
173  delete h;
174 
175 } // function DrawCorrMx
176 
178 
179  double width = 19.5;
180  double ndOffset = 5 * width;
181  double size = 18 * width;
182 
183  for (unsigned int i = 1; i < 18; ++i) {
184  double pos = (double)i * width;
185  TLine* lx = new TLine(0, pos, size, pos);
186  TLine* ly = new TLine(pos, 0, pos, size);
187  if (pos == ndOffset) {
188  lx->SetLineWidth(3);
189  ly->SetLineWidth(3);
190  }
191  lx->Draw();
192  ly->Draw();
193  }
194 }
195 
196 void DrawFullCovMx(covmx::CovarianceMatrix* mx, std::string path) {
197 
198  // Draw covariance matrix
199  TCanvas c("c", "c", 1600, 900);
200  TH2D* h = mx->GetFullCovMxTH2();
201  h->Draw("colz");
202 
203  DrawFullMxLines();
204  c.Update();
205 
206  // Move over the palette so Z axis tick labels are no longer cut off
207  TPaletteAxis *palette = (TPaletteAxis*)h->GetListOfFunctions()->FindObject("palette");
208  palette->SetX2NDC(0.93);
209  h->GetXaxis()->SetTitle("Reconstructed energy bin");
210  h->GetYaxis()->SetTitle("Reconstructed energy bin");
211 
212  // Save the plot and clean up
213  c.SaveAs(path.c_str());
214  delete h;
215 
216 } // function DrawFullCovMx
217 
218 void DrawFullCorrMx(covmx::CovarianceMatrix* mx, std::string path) {
219 
220  // Draw correlation matrix
221  TCanvas c("c", "c", 1600, 900);
222  TH2D* h = mx->GetFullCorrMxTH2();
223  h->Draw("colz");
224 
225  DrawFullMxLines();
226  c.Update();
227 
228  // Move over the palette so Z axis tick labels are no longer cut off
229  TPaletteAxis *palette = (TPaletteAxis*)h->GetListOfFunctions()->FindObject("palette");
230  palette->SetX2NDC(0.93);
231  h->GetXaxis()->SetTitle("Reconstructed energy bin");
232  h->GetYaxis()->SetTitle("Reconstructed energy bin");
233 
234  // Save the plot and clean up
235  c.SaveAs(path.c_str());
236  delete h;
237 
238 } // function DrawFullCorrMx
239 
240 // Format the histogram owned by a contour canvas
241 void FormatCanvas(TCanvas* c) {
242  TIter next(c->GetListOfPrimitives());
243  TH1D* hSurf = nullptr;
244  while (TObject* obj = next()) {
245  TString name(obj->GetName());
246  if (name.Contains("cafana")) hSurf = (TH1D*)obj;
247  break;
248  }
249  hSurf->GetXaxis()->SetTitle("sin^{2}#theta_{24}");
250  hSurf->GetXaxis()->CenterTitle();
251  hSurf->GetYaxis()->SetTitle("#Deltam_{41}^{2} (eV^{2})");
252  hSurf->GetYaxis()->CenterTitle();
253  hSurf->GetYaxis()->SetTitleOffset(0.8);
254  hSurf->SetTitle("");
255 }
256 
const XML_Char * name
Definition: expat.h:151
enum BeamMode kOrange
T sqrt(T number)
Definition: d0nt_math.hpp:156
std::vector< double > Spectrum
Definition: Constants.h:746
osc::OscCalcDumb calc
const Color_t kNumuBackgroundColor
Definition: Style.h:30
void FormatCanvas(TCanvas *c)
Charged-current interactions.
Definition: IPrediction.h:39
Sum up livetimes from individual cosmic triggers.
void DrawFullCovMx(covmx::CovarianceMatrix *mx, std::string path)
void DrawFullCorrMx(covmx::CovarianceMatrix *mx, std::string path)
const std::string path
Definition: plot_BEN.C:43
void DrawFullMxLines()
void DrawSpectrum(covmx::Sample sample, Spectrum *cosmic, osc::IOscCalc *calc, TMatrixD error, std::string path)
Function to make NOvA-style spectra.
Definition: PlotUtilities.h:17
All neutrinos, any flavor.
Definition: IPrediction.h:26
void next()
Definition: show_event.C:84
void DrawCorrMx(covmx::CovarianceMatrix *mx, std::string path)
Function to draw a two-detector covariance matrix for the NC selection.
enum BeamMode string