muonid_optimization_plots.C
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include "TCanvas.h"
8 #include "TFile.h"
9 #include "TH1.h"
10 #include "TH1D.h"
11 #include "TArrow.h"
12 #include "TSystem.h"
13 
14 using namespace ana;
15 
16 void DrawArrow(TCanvas * c, double x, TString arrow_opt, int line_color = kBlack, int line_style = 1, int line_width = 3);
17 
18 void muonid_optimization_plots(std::string input_file_name, std::string plot_dump)
19 {
20  std::cout << "Loading CutOptimization found in " << input_file_name << std::endl;
21 
22  std::string results_file_name = input_file_name;
23  results_file_name.insert(results_file_name.find(".root"), "_results");
24  std::cout << "Saving results to " << results_file_name << std::endl;
25  std::cout << "Saving plots to " << plot_dump << std::endl;
26 
27 
28  TFile * input = TFile::Open(input_file_name.c_str(), "read");
29  CutOptimization * optimize = CutOptimization::LoadFrom(input, "muonid_cut_optimize")).release(;
30  input->Close();
31 
32  TFile * results = new TFile(results_file_name.c_str(), "recreate");
33  optimize->Optimize(CutOptimization::kIntegratedDown,
36  results->mkdir("muonid_cut_optimize"),
37  plot_dump,
38  true);
39 
40  TH1D * frac_uncert_xsec = (TH1D*) results->Get("muonid_cut_optimize/frac_uncert_xsec" );
41  TH1D * frac_uncert_sel = (TH1D*) results->Get("muonid_cut_optimize/frac_uncert_sel" );
42  TH1D * frac_uncert_bkgd_stat = (TH1D*) results->Get("muonid_cut_optimize/frac_uncert_bkgd_stat");
43  TH1D * frac_uncert_bkgd_syst = (TH1D*) results->Get("muonid_cut_optimize/frac_uncert_bkgd_syst");
44  TH1D * frac_uncert_eff = (TH1D*) results->Get("muonid_cut_optimize/frac_uncert_eff" );
45 
46  double maxy = 1.2;
47 
48  TCanvas * c = new TCanvas();
49  c->SetLeftMargin(0.12);
50 
51  frac_uncert_xsec->GetXaxis()->CenterTitle();
52  frac_uncert_xsec->GetYaxis()->CenterTitle();
53  frac_uncert_xsec->GetYaxis()->SetRangeUser(0, 12);
54  frac_uncert_xsec->GetYaxis()->SetTitleOffset(0.8);
55  frac_uncert_xsec->SetLineColor(kRed);
56 
57  frac_uncert_sel->GetXaxis()->CenterTitle();
58  frac_uncert_sel->GetYaxis()->CenterTitle();
59  frac_uncert_sel->GetYaxis()->SetRangeUser(0, 1.2);
60  frac_uncert_sel->GetYaxis()->SetTitleOffset(0.8);
61  frac_uncert_sel->SetLineColor(kRed);
62 
63  frac_uncert_bkgd_stat->GetXaxis()->CenterTitle();
64  frac_uncert_bkgd_stat->GetYaxis()->CenterTitle();
65  frac_uncert_bkgd_stat->GetYaxis()->SetRangeUser(0, 1.2);
66  frac_uncert_bkgd_stat->GetYaxis()->SetTitleOffset(0.8);
67  frac_uncert_bkgd_stat->SetLineColor(kRed);
68 
69  frac_uncert_bkgd_syst->GetXaxis()->CenterTitle();
70  frac_uncert_bkgd_syst->GetYaxis()->CenterTitle();
71  frac_uncert_bkgd_syst->GetYaxis()->SetRangeUser(0, 12);
72  frac_uncert_bkgd_syst->GetYaxis()->SetTitleOffset(0.8);
73  frac_uncert_bkgd_syst->SetLineColor(kRed);
74 
75  frac_uncert_eff->GetXaxis()->CenterTitle();
76  frac_uncert_eff->GetYaxis()->CenterTitle();
77  frac_uncert_eff->GetYaxis()->SetRangeUser(0, 1.2);
78  frac_uncert_eff->GetYaxis()->SetTitleOffset(0.8);
79  frac_uncert_eff->SetLineColor(kRed);
80 
81  frac_uncert_xsec->Draw("hist");
82  c->Update();
83  DrawArrow(c, 0, "<");
84  c->Print((plot_dump + "/muonid_frac_uncert_xsec.pdf").c_str());
85 
86  frac_uncert_sel->Draw("hist");
87  c->Update();
88  DrawArrow(c, 0, "<");
89  c->Print((plot_dump + "/muonid_frac_uncert_sel.pdf").c_str());
90 
91  frac_uncert_bkgd_stat->Draw("hist");
92  c->Update();
93  DrawArrow(c, 0, "<");
94  c->Print((plot_dump + "/muonid_frac_uncert_bkgd_stat.pdf").c_str());
95 
96  frac_uncert_bkgd_syst->Draw("hist");
97  c->Update();
98  DrawArrow(c, 0, "<");
99  c->Print((plot_dump + "/muonid_frac_uncert_bkgd_syst.pdf").c_str());
100 
101  frac_uncert_eff->Draw("hist");
102  c->Update();
103  DrawArrow(c, 0, "<");
104  c->Print((plot_dump + "/muonid_frac_uncert_eff.pdf").c_str());
105 
106  // debug plots
107  TFile * fdebug = TFile::Open((plot_dump + "/debug.root").c_str());
108  auto syst_defs = optimize->GetSystDefs();
109  auto mvsyst_defs = optimize->GetMVSystDefs();
110  syst_defs.insert(syst_defs.end(), mvsyst_defs.begin(), mvsyst_defs.end());
111  std::vector<TH1*> vabs_uncert_eff_syst;
112  std::vector<TH1*> vabs_uncert_bkgd_syst;
113  std::vector<TH1*> vfrac_uncert_eff_syst;
114  std::vector<TH1*> vfrac_uncert_bkgd_syst;
115  std::vector<std::string> syst_labels;
116  for(auto syst : syst_defs) {
117  syst_labels.push_back(syst->GetName());
118  vabs_uncert_eff_syst .push_back((TH1*) fdebug->Get((syst->GetName() + "_abs_uncert_eff_syst" ).c_str()));
119  vabs_uncert_bkgd_syst .push_back((TH1*) fdebug->Get((syst->GetName() + "_abs_uncert_bkgd_syst" ).c_str()));
120  vfrac_uncert_eff_syst .push_back((TH1*) fdebug->Get((syst->GetName() + "_frac_uncert_eff_syst" ).c_str()));
121  vfrac_uncert_bkgd_syst.push_back((TH1*) fdebug->Get((syst->GetName() + "_frac_uncert_bkgd_syst").c_str()));
122  vfrac_uncert_eff_syst .back()->GetYaxis()->SetRangeUser(0, 1.2);
123  vfrac_uncert_bkgd_syst.back()->GetYaxis()->SetRangeUser(0, 1.2);
124 
125  }
126 
127  CutOptimization::PlotDebug(vabs_uncert_eff_syst , syst_labels, "hist", "Abs. Syst. Uncert Efficiency", plot_dump + "/debug_syst_abs_uncert_eff.pdf");
128  CutOptimization::PlotDebug(vabs_uncert_bkgd_syst , syst_labels, "hist", "Abs. Syst. Uncert Background", plot_dump + "/debug_syst_abs_uncert_bkgd.pdf");
129  CutOptimization::PlotDebug(vfrac_uncert_eff_syst , syst_labels, "hist", "Frac. Syst. Uncert Efficiency", plot_dump + "/debug_syst_frac_uncert_eff.pdf");
130  CutOptimization::PlotDebug(vfrac_uncert_bkgd_syst, syst_labels, "hist", "Frac. Syst. Uncert Background", plot_dump + "/debug_syst_frac_uncert_bkgd.pdf");
131 
132 
133 }
134 
135 void DrawArrow(TCanvas * c, double x, TString arrow_opt, int line_color, int line_style, int line_width)
136 {
137 
138  bool left = arrow_opt.Contains("<");
139  double offset = 0.07 * (c->GetX2() - c->GetX1());
140  if(left) offset *= -1;
141 
142  double y = c->GetY1() + 0.7 * (c->GetY2() - c->GetY1());
143 
144  TLine * l1 = new TLine(x, 0, x, y);
145  TArrow * arrow = new TArrow(x, y, x+offset, y, 0.04, arrow_opt.Data());
146  if(left) arrow = new TArrow(x+offset, y, x, y, 0.04, arrow_opt.Data());
147 
148  arrow->SetLineColor(line_color);
149  l1 ->SetLineColor(line_color);
150 
151  arrow->SetLineStyle(line_style);
152  l1 ->SetLineStyle(line_style);
153 
154  arrow->SetLineWidth(line_width);
155  l1 ->SetLineWidth(line_width);
156 
157  arrow->Draw();
158  l1->Draw();
159  c->Update();
160 }
161 
enum BeamMode kRed
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
double maxy
static std::unique_ptr< CutOptimization > LoadFrom(TDirectory *dir, const std::string &name)
void DrawArrow(TCanvas *c, double x, TString arrow_opt, int line_color=kBlack, int line_style=1, int line_width=3)
static void PlotDebug(TH1 *hist, std::string draw_option, std::string title, std::string name)
OStream cout
Definition: OStream.cxx:6
void muonid_optimization_plots(std::string input_file_name, std::string plot_dump)
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
enum BeamMode string