remakeresultsplots_nue.C
Go to the documentation of this file.
1 // Authors : Anna Holin
2 // Modified by Prabhjot Singh (prabhjot@fnal.gov) on March 06, 2018 for nue analysis pretty plots
3 //
4 // Purpose : root macro to make results plots for the FNEX results
5 //
6 // run it by doing this:
7 // root -l
8 // .L remakeresultsplots_nue.C+
9 // remakeresultsplots_nue("fnex_fitpoint_hist.root") or remakeresultsplots_nue("fnex_fitpoint_hist.root","whateveryouwanttocalltheoutputplots")
10 
11 #include "TFile.h"
12 #include "TH1D.h"
13 #include "TSpline.h"
14 #include "TCanvas.h"
15 #include "TPad.h"
16 #include "TLegend.h"
17 #include "THStack.h"
18 #include "TLine.h"
19 #include "TText.h"
20 #include "TKey.h"
21 #include "TROOT.h"
22 #include <iostream>
23 #include "TClass.h"
24 #include "TString.h"
25 
26 using namespace std;
27 
28 bool findNames(string qnames[4], TFile *f, string det);
29 
30 void remakeresultsplots_nue(char *fname, string name="prettyfitresults"){
31 
32  //first is the results file to read in, this should be something like fnex_fitpoint_hist.root, and the second thing is a string for the output plot names
33  TFile *fil = new TFile(fname, "read");
34  string qnamesND[4];
35  string qnamesFD[4];
36 
37  string det[2];
38  det[0] = "ND";
39  det[1] = "FD";
40 
41  bool hasSplineND = findNames(qnamesND, fil, det[0]);
42  bool hasSplineFD = findNames(qnamesFD, fil, det[1]);
43 
44  TH1D *data[2][4];
45  TH1D *rat[2][4];
46  TH1D *ghost[2];
47  THStack *stack[2][4];
48  TSpline3 *spline[2][4];
49  TCanvas *CC[2][4]; //canvases in the root file
50  TCanvas *CCParallel;
51  TPad *left[2][4];
52  TPad *right[2][4];
53  TPad *leftup[2][4];
54  TPad *leftdown[2][4];
55  TPad *rightosc[2];
56  TPad *rightsys[2];
57  TLegend *leg[2];
58 
59  std::cout << "test " << qnamesND[0].c_str() << std::endl;
60  std::cout << "test " << qnamesND[1].c_str() << std::endl;
61  std::cout << "test " << qnamesND[2].c_str() << std::endl;
62  std::cout << "test " << qnamesND[3].c_str() << std::endl;
63 
64  std::string PID = " ";
65  std::string pid = " ";
66  std::string fit_or_plot;
67 
68  std::string f_fnex = (std::string)fil->GetName();
69  if(f_fnex.find("plotpoint") != std::string::npos) fit_or_plot = "plot" ; else fit_or_plot = "fit/Fit";
70  std::cout << "File type is " << fit_or_plot << std::endl;
71 
72  //Get the parallel canvas to grab the oscillation parameter values
73  CCParallel = (TCanvas*)fil->Get(Form("%s/MultiExperimentParallel/NOvAExperimentContributions_FD", fit_or_plot.c_str()) );
74 
75  for(int iq = 0; iq < 4; iq++){
76 
77  if(iq == 0) PID = "LowPID";
78  else if(iq == 1) PID = "MidPID";
79  else if(iq == 2) PID = "HighPID";
80  else PID = "Peripheral";
81 
82  //looping over 2 detectors and 4 quantiles to get all the correct objects
83  std::cout << "Getting stuff for PID " << PID << std::endl;
84 
85  CC[0][iq] = (TCanvas *)fil ->Get(Form("%s/MultiExperiment/NOvA_NuESel_%s_NuMuSelQ1_NuMuSelQ2_NuMuSelQ3_NuMuSelQ4/CorrectedStacks/Nu_RecoE_Corrected_Stacks_%s", fit_or_plot.c_str(), PID.c_str(), qnamesND[iq].c_str()));
86  left[0][iq] = (TPad*)CC[0][iq] ->GetPrimitive(("Nu_RecoE_Corrected_Stacks_"+qnamesND[iq]+"_1").c_str());
87  leftup[0][iq] = (TPad*)left[0][iq] ->GetPrimitive("Upper");
88  leftdown[0][iq] = (TPad*)left[0][iq] ->GetPrimitive("Lower");
89  right[0][iq] = (TPad*)CC[0][iq] ->GetPrimitive(("Nu_RecoE_Corrected_Stacks_"+qnamesND[iq]+"_2").c_str());
90  data[0][iq] = (TH1D *)leftup[0][iq] ->GetPrimitive("NDDataCorrected");
91  stack[0][iq] = (THStack *)leftup[0][iq] ->GetPrimitive("NDStacksCorrected");
92  rat[0][iq] = (TH1D *)leftdown[0][iq] ->GetPrimitive("ND_Uncorr_PropDecomp_Data_Over_MC_Nu_RecoE");
93 
94  if(hasSplineND==true) spline[0][iq] = (TSpline3 *)leftup[0][iq]->GetPrimitive("Spline3");
95 
96  if(iq == 0){
97  rightosc[0] = (TPad*)CCParallel->GetPrimitive("NOvAExperimentContributions_FD_5");
98  rightsys[0] = (TPad*)right[0][iq] ->GetPrimitive("sysPad");
99  leg[0] = (TLegend *)leftup[0][iq] ->GetPrimitive("TPave");
100  }
101 
102  CC[1][iq] = (TCanvas *)fil ->Get(Form("%s/MultiExperiment/NOvA_NuESel_%s_NuMuSelQ1_NuMuSelQ2_NuMuSelQ3_NuMuSelQ4/CorrectedStacks/Nu_RecoE_Corrected_Stacks_%s", fit_or_plot.c_str(), PID.c_str(), qnamesFD[iq].c_str()));
103  left[1][iq] = (TPad*)CC[1][iq] ->GetPrimitive(("Nu_RecoE_Corrected_Stacks_"+qnamesFD[iq]+"_1").c_str());
104  leftup[1][iq] = (TPad*)left[1][iq] ->GetPrimitive("Upper");
105  leftdown[1][iq] = (TPad*)left[1][iq] ->GetPrimitive("Lower");
106  right[1][iq] = (TPad*)CC[1][iq] ->GetPrimitive(("Nu_RecoE_Corrected_Stacks_"+qnamesFD[iq]+"_2").c_str());
107  data[1][iq] = (TH1D *)leftup[1][iq] ->GetPrimitive("FDDataCorrected");
108  stack[1][iq] = (THStack *)leftup[1][iq] ->GetPrimitive("FDStacksCorrected");
109  rat[1][iq] = (TH1D *)leftdown[1][iq] ->GetPrimitive("FD_Uncorr_PropDecomp_Data_Over_MC_Nu_RecoE");
110 
111  if(hasSplineFD==true) spline[1][iq] = (TSpline3 *)leftup[1][iq]->GetPrimitive("Spline3");
112 
113  if(iq == 0){
114  rightosc[1] = (TPad*)CCParallel->GetPrimitive("NOvAExperimentContributions_FD_6");
115  rightsys[1] = (TPad*)right[1][iq] ->GetPrimitive("sysPad");
116  leg[1] = (TLegend *)leftup[1][iq] ->GetPrimitive("TPave");
117  }
118 
119  } //ok should have the correct objects now
120 
121  std::cout << "I am here" << std::endl;
122 
123  TCanvas *AA[2];
124  TPad *pad1[2][4];
125  TPad *pad2[2][4];
126  TPad *legpad[2];
127 
128  AA[0] = new TCanvas("AA_ND", "AA_ND", 1200, 900);
129  AA[1] = new TCanvas("AA_FD", "AA_FD", 1200, 900);
130 
131  pad1[0][0] = new TPad("pad1_ND_1", "pad1_ND_1", 0.00, 0.65, 0.35, 1.00);
132  pad2[0][0] = new TPad("pad2_ND_1", "pad2_ND_1", 0.00, 0.50, 0.35, 0.64);
133  pad1[0][1] = new TPad("pad1_ND_2", "pad1_ND_2", 0.35, 0.65, 0.70, 1.00);
134  pad2[0][1] = new TPad("pad2_ND_2", "pad2_ND_2", 0.35, 0.50, 0.70, 0.64);
135  pad1[0][2] = new TPad("pad1_ND_3", "pad1_ND_3", 0.00, 0.15, 0.35, 0.50);
136  pad2[0][2] = new TPad("pad2_ND_3", "pad2_ND_3", 0.00, 0.00, 0.35, 0.14);
137  pad1[0][3] = new TPad("pad1_ND_4", "pad1_ND_4", 0.35, 0.15, 0.70, 0.50);
138  pad2[0][3] = new TPad("pad2_ND_4", "pad2_ND_4", 0.35, 0.00, 0.70, 0.14);
139 
140  pad1[1][0] = new TPad("pad1_FD_1", "pad1_FD_1", 0.00, 0.65, 0.35, 1.00);
141  pad2[1][0] = new TPad("pad2_FD_1", "pad2_FD_1", 0.00, 0.50, 0.35, 0.65);
142  pad1[1][1] = new TPad("pad1_FD_2", "pad1_FD_2", 0.35, 0.65, 0.70, 1.00);
143  pad2[1][1] = new TPad("pad2_FD_2", "pad2_FD_2", 0.35, 0.50, 0.70, 0.65);
144  pad1[1][2] = new TPad("pad1_FD_3", "pad1_FD_3", 0.00, 0.15, 0.35, 0.50);
145  pad2[1][2] = new TPad("pad2_FD_3", "pad2_FD_3", 0.00, 0.00, 0.35, 0.15);
146  pad1[1][3] = new TPad("pad1_FD_4", "pad1_FD_4", 0.35, 0.15, 0.70, 0.50);
147  pad2[1][3] = new TPad("pad2_FD_4", "pad2_FD_4", 0.35, 0.00, 0.70, 0.15);
148 
149  ghost[0] = new TH1D("ghost0", "", 1, 0,1);
150  ghost[1] = new TH1D("ghost1", "", 1, 0,1);
151 
152  double datalow = data[0][0]->GetBinLowEdge(1);
153  double datahigh = 5.;
154 
155  if(hasSplineFD == true) datahigh = 5.05;
156 
157  TLine *line = new TLine(datalow, 1.0, datahigh, 1.0);
158  line ->SetLineWidth(2);
159 
160  TText *quant[2][4];
161 
162  for(int id =0; id < 2; id++){
163  for(int iq = 0; iq < 4; iq++){
164  pad1[id][iq] ->SetBottomMargin(0.02);
165  pad2[id][iq] ->SetBottomMargin(0.2);
166  pad1[id][iq] ->SetTopMargin(0.085);
167  pad2[id][iq] ->SetTopMargin(0.01);
168  pad1[id][iq] ->SetRightMargin(0.04);
169  pad2[id][iq] ->SetRightMargin(0.04);
170  pad1[id][iq] ->SetLeftMargin(0.12);
171  pad2[id][iq] ->SetLeftMargin(0.12);
172  AA[id] ->cd();
173  pad1[id][iq] ->Draw();
174  AA[id] ->cd();
175  pad2[id][iq] ->Draw();
176  }
177  }
178 
179 
180  for(int id = 0; id < 2; id++){
181  for(int iq = 0; iq < 4; iq++){
182 
183  std::cout << " data int = " << data[id][iq]->Integral() << std::endl;
184 
185  AA[id] ->cd();
186  pad1[id][iq]->cd();
187  pad1[id][iq]->SetLeftMargin(0.20);
188 
189  if(id == 0 && hasSplineFD == true) data[id][iq]->GetXaxis()->SetRangeUser(datalow, 5);
190  if(id == 0 && hasSplineFD == false) data[id][iq]->GetXaxis()->SetRangeUser(datalow, 5);
191  if(id == 1 && hasSplineFD == true) data[id][iq]->GetXaxis()->SetRangeUser(datalow, 5);
192  if(id == 1 && hasSplineFD == false) data[id][iq]->GetXaxis()->SetRangeUser(datalow, 5);
193 
194  if(iq == 0) PID = "LowPID";
195  else if(iq == 1) PID = "MidPID";
196  else if(iq == 2) PID = "HighPID";
197  else PID = "Peripheral";
198 
199  if(iq == 0) pid = "Low CVN";
200  else if(iq == 1) pid = "Mid CVN";
201  else if(iq == 2) pid = "High CVN";
202  else pid = "Peripheral";
203 
204  double yrange = data[id][iq]->GetMaximum()*1.5;
205  data[id][iq] ->GetYaxis()->SetRangeUser(0.0, yrange);
206  data[id][iq] ->GetYaxis()->CenterTitle();
207  data[id][iq] ->GetYaxis()->SetTitleSize(0.07);
208  data[0][iq] ->GetYaxis()->SetTitleOffset(1.2 );
209  data[1][iq] ->GetYaxis()->SetTitleOffset(1. );
210  data[id][iq] ->GetYaxis()->SetLabelSize(0.06);
211  data[id][iq] ->GetXaxis()->SetLabelSize(0.0);
212  data[id][iq] ->SetMarkerStyle(20);
213  data[id][iq] ->SetLineWidth(2);
214  data[id][iq] ->SetStats(0);
215  data[id][iq] ->Draw();
216  stack[id][iq] ->Draw("same");
217 
218  if(id == 0 && hasSplineND == true) spline[id][iq]->Draw("same");
219  if(id == 1 && hasSplineFD == true) spline[id][iq]->Draw("same");
220 
221  data[id][iq] ->Draw("same e1");
222  double ycoord = data[id][iq]->GetMaximum()*0.8;
223  quant[id][iq] = new TText(2.5, ycoord, Form("%s - %s", det[id].c_str(), pid.c_str() ));
224  quant[id][iq] ->SetTextSize(0.06);
225  quant[id][iq] ->Draw();
226  pad2[id][iq] ->cd();
227  pad2[id][iq] ->SetBottomMargin(0.40);
228  pad2[id][iq] ->SetLeftMargin(0.20);
229  rat[id][iq] ->SetStats(0);
230  rat[id][iq] ->GetYaxis()->CenterTitle();
231  rat[id][iq] ->GetYaxis()->SetTitle("Data/MC");
232  rat[id][iq] ->GetYaxis()->SetTitleSize(18.0);
233  rat[id][iq] ->GetYaxis()->SetLabelSize(18.0);
234  rat[id][iq] ->GetYaxis()->SetDecimals();
235  rat[0][iq] ->GetYaxis()->SetTitleOffset(3.0);
236  rat[1][iq] ->GetYaxis()->SetTitleOffset(2.0);
237  rat[id][iq] ->GetXaxis()->SetTitle("Reco E_{#nu} (GeV)");
238  rat[id][iq] ->GetXaxis()->CenterTitle();
239  rat[id][iq] ->GetXaxis()->SetDecimals();
240  rat[id][iq] ->GetXaxis()->SetTitleSize(20.0);
241  rat[id][iq] ->GetXaxis()->SetLabelSize(20.);
242  rat[id][iq] ->GetXaxis()->SetTitleOffset(6.0);
243  rat[id][iq] ->GetXaxis()->SetNdivisions(509);
244  rat[id][iq] ->SetMarkerStyle(20);
245  rat[id][iq] ->SetLineWidth(2);
246 
247  if(id == 0 && hasSplineFD == true) rat[id][iq]->GetXaxis()->SetRangeUser(datalow, 5);
248  if(id == 0 && hasSplineFD == false) rat[id][iq]->GetXaxis()->SetRangeUser(datalow, 5);
249  if(id == 1 && hasSplineFD == true) rat[id][iq]->GetXaxis()->SetRangeUser(datalow, 5);
250  if(id == 1 && hasSplineFD == false) rat[id][iq]->GetXaxis()->SetRangeUser(datalow, 5);
251 
252  if(id == 0) rat[id][iq]->GetYaxis()->SetRangeUser(0.41, 1.59);
253  if(id == 1 && hasSplineFD == true) {double max = rat[id][iq]->GetMaximum()*1.2; rat[id][iq]->GetYaxis()->SetRangeUser(0., max);}
254  if(id == 1 && hasSplineFD == false) rat[id][iq] ->GetYaxis()->SetRangeUser(0., 4.9);
255 
256  rat[id][iq] ->Draw();
257  if(id == 0 || id == 1) line->Draw();
258  }
259 
260  AA[id] ->cd();
261  legpad[id] = new TPad(Form("legpad%d", id),"", 0.7, 0, 1.0, 0.4);
262  legpad[id] ->SetTopMargin(0.05);
263  legpad[id] ->SetBottomMargin(0.073);
264  legpad[id] ->Draw();
265  legpad[id] ->cd();
266  ghost[id] ->SetStats(0);
267  ghost[id] ->GetYaxis()->SetTickLength(0);
268  ghost[id] ->GetXaxis()->SetTickLength(0);
269  ghost[id] ->GetYaxis()->SetTitleSize(0.0);
270  ghost[id] ->GetXaxis()->SetTitleSize(0.0);
271  ghost[id] ->GetXaxis()->SetLabelSize(0.00);
272  ghost[id] ->GetYaxis()->SetLabelSize(0.0);
273  ghost[id] ->SetMarkerSize(0);
274  ghost[id] ->SetLineWidth(0);
275  ghost[id] ->SetLineColor(0);
276  ghost[id] ->Draw();
277 
278  if(id == 0){
279  leg[id]->SetX1NDC(0.25);
280  leg[id]->SetX2NDC(0.75);
281  leg[id]->SetY1NDC(0.2);
282  leg[id]->SetY2NDC(0.8);
283  }
284  if(id == 1){
285  leg[id]->SetX1NDC(0.3);
286  leg[id]->SetX2NDC(0.8);
287  leg[id]->SetY1NDC(0.12);
288  leg[id]->SetY2NDC(0.88);
289  }
290 
291  leg[id] ->Draw();
292  AA[id] ->cd();
293  rightosc[id] ->SetPad(0.7, 0.70, 1.0, 1.0);
294  rightosc[id] ->Draw();
295  AA[id] ->cd();
296  rightsys[id] ->SetPad(0.7, 0.40, 1.0, 0.7);
297  rightsys[id] ->Draw();
298  }
299 
300  //finished plotting
301  //now save the two plots:
302 // AA[0]->Print(Form("%s_ND.png", name.c_str()));
303 // AA[1]->Print(Form("%s_FD.png", name.c_str()));
304  AA[0]->Print(Form("%s_ND.pdf", name.c_str()));
305  AA[1]->Print(Form("%s_FD.pdf", name.c_str()));
306 
307 
308  //saved
309  std::cout << "DONE! " << std::endl;
310 
311 }
312 
313 bool findNames(string qnames[4], TFile *f, string det){
314 
315  std::string PID = " ";
316  std::string fit_or_plot;
317 
318  std::string f_fnex = (std::string)f->GetName();
319  if(f_fnex.find("plotpoint") != std::string::npos) fit_or_plot = "plot" ; else fit_or_plot = "fit/Fit";
320 
321  bool hasSpline = false;
322 
323  for (int iq = 0; iq < 4; iq++){
324 
325  if(iq == 0) PID = "LowPID";
326  else if(iq == 1) PID = "MidPID";
327  else if(iq == 2) PID = "HighPID";
328  else PID = "Peripheral";
329 
330  f->cd(Form("%s/MultiExperiment/NOvA_NuESel_%s_NuMuSelQ1_NuMuSelQ2_NuMuSelQ3_NuMuSelQ4/CorrectedStacks/", fit_or_plot.c_str(), PID.c_str()));
331 
332  TDirectory *source = gDirectory;
333 
334  //loop on all entries of this directory
335  TKey *key;
336  TIter nextkey(source->GetListOfKeys());
337 
338  while ((key = (TKey*)nextkey())) {
339 
340  const char *classname = key->GetClassName();
341  TClass *cl = gROOT->GetClass(classname);
342 
343  if (!cl) continue;
344 
345  if (cl->InheritsFrom(TCanvas::Class())) {
346 
347  string name = key->GetName();
348 
349  if (name.find(det) != std::string::npos) {
350  std::cout << "found!" << '\n';
351  std::cout << "here " << key->GetName() << std::endl;
352  size_t pos;
353  pos = name.find(det); // position of det in name
354  qnames[iq] += name.substr (pos); // get from "live" to the end
355 
356 
357  // Check if spline exhists
358  if (iq == 0){
359  TCanvas *CC1 = (TCanvas *)f ->Get(Form("%s/MultiExperiment/NOvA_NuESel_%s_NuMuSelQ1_NuMuSelQ2_NuMuSelQ3_NuMuSelQ4/CorrectedStacks/Nu_RecoE_Corrected_Stacks_%s", fit_or_plot.c_str(), PID.c_str(), qnames[iq].c_str()));
360  TPad *left1 = (TPad*)CC1 ->GetPrimitive(("Nu_RecoE_Corrected_Stacks_"+qnames[iq]+"_1").c_str());
361  TPad *leftup1 = (TPad*)left1 ->GetPrimitive("Upper");
362 
363  TObject *obj;
364  TIter next(leftup1->GetListOfPrimitives());
365  while ((obj=next())) {
366  if (obj->InheritsFrom("TSpline3")) {
367  hasSpline = true;
368  }
369  }
370 
371  delete leftup1;
372  delete left1;
373  delete CC1;
374  }
375  }
376 
377  }
378  }
379  }
380 
381  return hasSpline;
382 }
383 
::xsd::cxx::tree::id< char, ncname > id
Definition: Database.h:165
T max(const caf::Proxy< T > &a, T b)
const XML_Char * name
Definition: expat.h:151
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:112
bool findNames(string qnames[4], TFile *f, string det)
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
const XML_Char const XML_Char * data
Definition: expat.h:268
void remakeresultsplots_nue(char *fname, string name="prettyfitresults")
const char * source
Definition: lz4.h:436
OStream cout
Definition: OStream.cxx:6
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
TPad * pad2
Definition: analysis.C:13
void next()
Definition: show_event.C:84
TPad * pad1
Definition: analysis.C:13
enum BeamMode string