fiducial_optimization_plots_2d.C
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "TCanvas.h"
9 #include "TH1.h"
10 #include "TH1D.h"
11 #include "TH2D.h"
12 #include "TGraph.h"
13 #include "TArrow.h"
14 #include "TSystem.h"
15 #include "TFile.h"
16 #include "TPaletteAxis.h"
17 #include "Utilities/rootlogon.C"
18 using namespace ana;
19 
20 void DrawArrow(TCanvas * c, double x, TString arrow_opt, int line_color = kBlack, int line_style = 1, int line_width = 3);
21 void DrawBox(double lo, double hi, int line_color = kBlack, int line_style = 7, int line_width = 3);
22 void MovePalette(TH1 * hist);
23 void PlotSysts(std::vector<TH1*> hists, std::vector<SystematicDef*> systs, double arrow_down, double arrow_up, std::string name, double limit=0);
24 
26  std::string input_file_name = "/nova/ana/users/ddoyle/NuebarFiducialOptimization/fiducial_optimization_nocvn_all.root",
27  std::string plot_dump = "/nova/ana/users/ddoyle/NuebarFiducialOptimization/plots_2d_nocvn/")
28 {
29  std::string rootdir = "fiducial_vtx_";
30  double arrow_up = 0;
31  double arrow_down = 0;
32  switch(axis) {
33  case 0:
34  plot_dump += "x/";
35  rootdir += "x";
36  arrow_down = nuebarccinc::vNueCCIncFiducialMin.X();
37  arrow_up = nuebarccinc::vNueCCIncFiducialMax.X();
38  break;
39  case 1:
40  plot_dump += "y/";
41  rootdir += "y";
42  arrow_down = nuebarccinc::vNueCCIncFiducialMin.Y();
43  arrow_up = nuebarccinc::vNueCCIncFiducialMax.Y();
44  break;
45  case 2:
46  plot_dump += "z/";
47  rootdir += "z";
48  arrow_down = nuebarccinc::vNueCCIncFiducialMin.Z();
49  arrow_up = nuebarccinc::vNueCCIncFiducialMax.Z();
50  break;
51  }
52 
53  std::cout << "Loading CutOptimization found in " << input_file_name << std::endl;
54 
55  std::string results_file_name = input_file_name;
56  results_file_name.insert(results_file_name.find(".root"), "_results");
57  std::cout << "Saving results to " << results_file_name << std::endl;
58  std::cout << "Saving plots to " << plot_dump << std::endl;
59 
60  TFile * input = TFile::Open(input_file_name.c_str(), "read");
61  CutOptimization * optimize = CutOptimization::LoadFrom(input, rootdir.c_str())).release(;
62  input->Close();
63 
64  TFile * results = new TFile(results_file_name.c_str(), "recreate");
68  results->mkdir(rootdir.c_str()),
69  plot_dump,
70  true); //debug
71 
72  TH2D * frac_uncert_xsec = (TH2D*) results->Get((rootdir + "/frac_uncert_xsec" ).c_str());
73  TH2D * frac_uncert_sel = (TH2D*) results->Get((rootdir + "/frac_uncert_sel" ).c_str());
74  TH2D * frac_uncert_bkgd_stat = (TH2D*) results->Get((rootdir + "/frac_uncert_bkgd_stat").c_str());
75  TH2D * frac_uncert_bkgd_syst = (TH2D*) results->Get((rootdir + "/frac_uncert_bkgd_syst").c_str());
76  TH2D * frac_uncert_eff = (TH2D*) results->Get((rootdir + "/frac_uncert_eff" ).c_str());
77 
78  double maxy = 1.2;
79 
80  TCanvas * c = new TCanvas();
81  c->SetRightMargin(0.2);
82 
83  CenterTitles(frac_uncert_xsec);
84  frac_uncert_xsec->GetZaxis()->SetRangeUser(0,6);
85 
86  CenterTitles(frac_uncert_sel);
87  frac_uncert_sel->GetZaxis()->SetRangeUser(0, 1.2);
88 
89  CenterTitles(frac_uncert_bkgd_stat);
90  frac_uncert_bkgd_stat->GetZaxis()->SetRangeUser(0, 1.2);
91 
92  CenterTitles(frac_uncert_bkgd_syst);
93  frac_uncert_bkgd_syst->GetZaxis()->SetRangeUser(0, 6);
94 
95  CenterTitles(frac_uncert_eff);
96  frac_uncert_eff->GetZaxis()->SetRangeUser(0, 1.2);
97 
98 
99  frac_uncert_xsec->Draw("colz");
100  MovePalette(frac_uncert_xsec);
101  c->Update();
102  DrawBox(arrow_down, arrow_up);
103  c->Print((plot_dump + "/" + rootdir + "_frac_uncert_xsec.pdf").c_str());
104 
105  frac_uncert_sel->Draw("colz");
106  MovePalette(frac_uncert_sel);
107  c->Update();
108  DrawBox(arrow_down, arrow_up);
109  c->Print((plot_dump + "/" + rootdir + "_frac_uncert_sel.pdf").c_str());
110 
111  frac_uncert_bkgd_stat->Draw("colz");
112  MovePalette(frac_uncert_bkgd_stat);
113  c->Update();
114  DrawBox(arrow_down, arrow_up);
115  c->Print((plot_dump + "/" + rootdir + "_frac_uncert_bkgd_stat.pdf").c_str());
116 
117  frac_uncert_bkgd_syst->Draw("colz");
118  MovePalette(frac_uncert_bkgd_syst);
119  c->Update();
120  DrawBox(arrow_down, arrow_up);
121  c->Print((plot_dump + "/" + rootdir + "_frac_uncert_bkgd_syst.pdf").c_str());
122 
123  frac_uncert_eff->Draw("colz");
124  MovePalette(frac_uncert_eff);
125  c->Update();
126  DrawBox(arrow_down, arrow_up);
127  c->Print((plot_dump + "/" + rootdir + "_frac_uncert_eff.pdf").c_str());
128 
129 
130  // make debug plots
131  TFile * fdebug = TFile::Open((plot_dump + "/debug.root").c_str());
132  auto syst_defs = optimize->GetSystDefs();
133  auto mvsyst_defs = optimize->GetMVSystDefs();
134  syst_defs.insert(syst_defs.end(), mvsyst_defs.begin(), mvsyst_defs.end());
135  std::vector<TH1*> vabs_uncert_eff_syst;
136  std::vector<TH1*> vabs_uncert_bkgd_syst;
137  std::vector<TH1*> vfrac_uncert_eff_syst;
138  std::vector<TH1*> vfrac_uncert_bkgd_syst;
139  for(auto syst : syst_defs) {
140  vabs_uncert_eff_syst .push_back((TH1*) fdebug->Get((syst->GetName() + "_abs_uncert_eff_syst" ).c_str()));
141  vabs_uncert_bkgd_syst .push_back((TH1*) fdebug->Get((syst->GetName() + "_abs_uncert_bkgd_syst" ).c_str()));
142  vfrac_uncert_eff_syst .push_back((TH1*) fdebug->Get((syst->GetName() + "_frac_uncert_eff_syst" ).c_str()));
143  vfrac_uncert_bkgd_syst.push_back((TH1*) fdebug->Get((syst->GetName() + "_frac_uncert_bkgd_syst").c_str()));
144  }
145 
146  PlotSysts(vabs_uncert_eff_syst , syst_defs, arrow_down, arrow_up, plot_dump + "/vabs_uncert_eff_syst" );
147  PlotSysts(vabs_uncert_bkgd_syst , syst_defs, arrow_down, arrow_up, plot_dump + "/vabs_uncert_bkgd_syst" );
148  PlotSysts(vfrac_uncert_eff_syst , syst_defs, arrow_down, arrow_up, plot_dump + "/vfrac_uncert_eff_syst" , 1);
149  PlotSysts(vfrac_uncert_bkgd_syst, syst_defs, arrow_down, arrow_up, plot_dump + "/vfrac_uncert_bkgd_syst", 1);
150 
151 
152 }
153 
154 void PlotSysts(std::vector<TH1*> hists, std::vector<SystematicDef*> systs, double arrow_down, double arrow_up, std::string name, double limit)
155 {
156  TCanvas * c = new TCanvas(UniqueName().c_str(), "");
157  c->SetRightMargin(0.2);
158  for(auto ihist = 0u; ihist < hists.size(); ihist++) {
159  hists[ihist]->SetTitle(systs[ihist]->GetName().c_str());
160  if(limit) hists[ihist]->GetZaxis()->SetRangeUser(0, limit);
161  hists[ihist]->Draw("colz");
162  c->Update();
163  MovePalette(hists[ihist]);
164  c->Update();
165  DrawBox(arrow_down, arrow_up);
166 
167  c->Print((name + "_" +systs[ihist]->GetName() + ".pdf").c_str());
168  }
169 }
170 
171 void MovePalette(TH1 * hist)
172 {
173  auto palette = (TPaletteAxis*) hist->GetListOfFunctions()->FindObject("palette");
174  palette->SetX1NDC(0.81);
175  palette->SetX2NDC(0.85);
176  hist->Draw("colz");
177 }
178 
179 void DrawBox(double lo, double hi, int line_color, int line_style, int line_width)
180 {
181  TGraph * glo = new TGraph();
182  TGraph * ghi = new TGraph();
183  glo->SetPoint(0, lo, -1e6);
184  glo->SetPoint(1, lo, 1e6);
185 
186  ghi->SetPoint(0, -1e6, hi);
187  ghi->SetPoint(1, 1e6, hi);
188 
189  glo->SetLineStyle(line_style);
190  glo->SetLineWidth(line_width);
191  glo->SetLineColor(line_color);
192  ghi->SetLineStyle(line_style);
193  ghi->SetLineWidth(line_width);
194  ghi->SetLineColor(line_color);
195 
196  glo->Draw("same");
197  ghi->Draw("same");
198 
199 }
200 
201 void DrawArrow(TCanvas * c, double x, TString arrow_opt, int line_color, int line_style, int line_width)
202 {
203 
204  bool left = arrow_opt.Contains("<");
205  double offset = 0.07 * (c->GetX2() - c->GetX1());
206  if(left) offset *= -1;
207 
208  double y = c->GetY1() + 0.7 * (c->GetY2() - c->GetY1());
209 
210  TLine * l1 = new TLine(x, 0, x, y);
211  TArrow * arrow = new TArrow(x, y, x+offset, y, 0.04, arrow_opt.Data());
212  if(left) arrow = new TArrow(x+offset, y, x, y, 0.04, arrow_opt.Data());
213 
214  arrow->SetLineColor(line_color);
215  l1 ->SetLineColor(line_color);
216 
217  arrow->SetLineStyle(line_style);
218  l1 ->SetLineStyle(line_style);
219 
220  arrow->SetLineWidth(line_width);
221  l1 ->SetLineWidth(line_width);
222 
223  arrow->Draw();
224  l1->Draw();
225  c->Update();
226 }
227 
const XML_Char * name
Definition: expat.h:151
TSpline3 lo("lo", xlo, ylo, 12,"0")
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
const TVector3 vNueCCIncFiducialMin(-130,-140, 150)
std::vector< SystGroupDef > systs
Definition: syst_header.h:385
void DrawArrow(TCanvas *c, double x, TString arrow_opt, int line_color=kBlack, int line_style=1, int line_width=3)
double maxy
static std::unique_ptr< CutOptimization > LoadFrom(TDirectory *dir, const std::string &name)
void fiducial_optimization_plots_2d(int axis, std::string input_file_name="/nova/ana/users/ddoyle/NuebarFiducialOptimization/fiducial_optimization_nocvn_all.root", std::string plot_dump="/nova/ana/users/ddoyle/NuebarFiducialOptimization/plots_2d_nocvn/")
void CenterTitles(TH1 *histo)
Definition: Plots.cxx:1481
TString hists[nhists]
Definition: bdt_com.C:3
std::string GetName(int i)
TSpline3 hi("hi", xhi, yhi, 18,"0")
OStream cout
Definition: OStream.cxx:6
const TVector3 vNueCCIncFiducialMax(150, 140, 800)
const double kAna2019RHCPOT
Definition: Exposures.h:224
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void MovePalette(TH1 *hist)
void PlotSysts(std::vector< TH1 * > hists, std::vector< SystematicDef * > systs, double arrow_down, double arrow_up, std::string name, double limit=0)
void DrawBox(double lo, double hi, int line_color=kBlack, int line_style=7, int line_width=3)
std::string UniqueName()
Return a different string each time, for creating histograms.
Definition: Utilities.cxx:30