Plotting_OverlayStudies.C
Go to the documentation of this file.
2 
3 #include "CAFAna/Core/Binning.h"
4 #include "CAFAna/Core/Spectrum.h"
5 #include "CAFAna/Core/Spectrum.h"
6 #include "CAFAna/Core/Ratio.h"
8 
9 #include <fstream>
10 #include "TCanvas.h"
11 #include "TFile.h"
12 #include "TGraph.h"
13 #include "TH1.h"
14 #include "TH2.h"
15 #include "TH2D.h"
16 #include "TMath.h"
17 #include "TGaxis.h"
18 #include "TMultiGraph.h"
19 #include "TLegend.h"
20 #include "TLatex.h"
21 #include "TStyle.h"
22 #include "THStack.h"
23 #include "TPaveText.h"
24 #include "TLine.h"
25 #include <TIterator.h>
26 #include <TKey.h>
27 
28 
29 using namespace ana;
30 
31 
32 void drawBox()
33 {
34  TBox *box = new TBox(0.955, 0.1, 1.0, 0.2);
35  box->SetFillColor(kWhite);
36  box->Draw();
37 
38  return;
39 }
40 
41 
42 TCanvas* makeCanvas(TH1 *h_MCData, TH1 *h_MCMC, TString s_Obj, std::string s_FileAppend, bool drawCutsOnSpecialPlots, bool loosePresel, bool dataData = false)
43 {
44  Helper help;
45 
46  h_MCData->SetLineColor(kBlack);
47  h_MCData->SetLineWidth(2);
48  h_MCData->GetXaxis()->SetTitle("");
49  if(s_Obj.Contains("_Sp")){
50  h_MCData->GetYaxis()->SetTitle("Area Norm. Spills");
51  }
52  else{
53  h_MCData->GetYaxis()->SetTitle("Area Norm. Events");
54  }
55  h_MCData->GetYaxis()->SetTitleSize(0.05);
56  h_MCData->GetYaxis()->SetTitleOffset(1.2);
57  h_MCData->GetYaxis()->CenterTitle();
58  h_MCMC->SetLineColor(kRed);
59  h_MCMC->SetLineWidth(2);
60 
61  if(s_Obj.Contains("kSpillPOT_Sp")){
62  h_MCData->Rebin(3);
63  h_MCMC ->Rebin(3);
64  }
65 
66  h_MCData->Scale(1./h_MCData->Integral());
67  h_MCMC ->Scale(1./h_MCMC->Integral());
68 
69  double ymax = std::max(h_MCMC->GetMaximum(), h_MCData->GetMaximum());
70 
71  TCanvas *c = new TCanvas("c", "c", 800, 800);
72  TPad *p1 = new TPad("p1", "p1", 0, 0, 1, 1);
73  p1->SetFillStyle(0);
74  p1->SetBottomMargin(0.3);
75  p1->SetTopMargin(0.01);
76  p1->SetRightMargin(0.05);
77  p1->SetLeftMargin(0.13);
78  TPad *p2 = new TPad("p2", "p2", 0, 0, 1, 1);
79  p2->SetFillStyle(0);
80  p2->SetTopMargin(0.7);
81  p2->SetRightMargin(0.05);
82  p2->SetBottomMargin(0.1);
83  p2->SetLeftMargin(0.13);
84 
85  p1->cd();
86 
87  h_MCData->Draw();
88  h_MCMC->Draw("SAME");
89  h_MCData->GetYaxis()->SetRangeUser(0, ymax + ymax*0.3);
90 
91  if((drawCutsOnSpecialPlots) && help.IsSpecial(s_Obj)){
92  std::vector<TLine*> vec_CutLines = help.PlotSpecial(s_Obj, s_FileAppend, 0., ymax, loosePresel);
93  for(auto line : vec_CutLines){
94  line->Draw();
95  }
96  }
97 
98  p2->cd();
99 
100  TH1D *h_Rat = (TH1D*)h_MCData->Clone();
101  h_MCData->GetXaxis()->SetLabelSize(0);
102  h_Rat->Divide(h_MCMC);
103  double ratMin, ratMax;
104  if(s_Obj.Contains("_Sp")){
105  ratMin = 0.74; ratMax = 1.26;
106  }
107  else{
108  ratMin = 0.89; ratMax = 1.11;
109  }
110  h_Rat->SetAxisRange(ratMin, ratMax,"Y");
111  h_Rat->SetLineColor(kBlue);
112  h_Rat->GetXaxis()->SetTitle(help.VarLabel((std::string)s_Obj, true).c_str());
113  if(!dataData) h_Rat->GetYaxis()->SetTitle("MCData/MCMC");
114  else h_Rat->GetYaxis()->SetTitle("NoWeight/Weight");
115  h_Rat->GetYaxis()->SetTitleSize(0.05);
116  h_Rat->GetXaxis()->CenterTitle();
117  h_Rat->GetYaxis()->CenterTitle();
118  h_Rat->Draw();
119 
120  double x[2] = {h_Rat->GetXaxis()->GetXmin(), h_Rat->GetXaxis()->GetXmax()};
121  double y[2] = {1.0, 1.0};
122  TGraph* l_Xaxis = new TGraph(2, x, y);
123  l_Xaxis->SetLineColor(kGray);
124  l_Xaxis->SetLineWidth(3);
125  l_Xaxis->SetLineStyle(2);
126  l_Xaxis->Draw("L SAME");
127 
128  double x1_leg; double x2_leg;
129  s_Obj.Contains("kPDG") ? x1_leg = 0.4 : x1_leg = 0.45;
130  s_Obj.Contains("kPDG") ? x2_leg = 0.6 : x2_leg = 0.89;
131  TLegend *leg = new TLegend(x1_leg, 0.84, x2_leg, 0.98);
132  leg->AddEntry((TObject*)0, ("Cut: " + help.CutLabel((std::string)s_Obj)).c_str(), "");
133  leg->AddEntry(h_MCData, "MC singles in Data", "L");
134  leg->AddEntry(h_MCMC, "MC singles in MC", "L");
135 
136  c->cd();
137  p1->Draw();
138  p2->Draw();
139 
140  if(s_Obj.Contains("kSpillPOT")) drawBox();
141 
142  if(!dataData) leg->Draw();
143 
144  return c;
145 }
146 
147 void Plotting_OverlayStudies(std::string s_OutDir = "", std::string s_FileAppend = "", std::string s_InFileMCData = "", std::string s_InFileMCMC = "", bool drawCutsOnSpecialPlots = true, bool loosePresel = false)
148 {
149  gStyle->SetOptStat(0);
150 
151  Helper help;
152 
153  TFile f_InFileMCData(s_InFileMCData.c_str());
154  TFile f_InFileMCMC (s_InFileMCMC .c_str());
155  TIter iter(f_InFileMCData.GetListOfKeys());
156  TKey *key;
157  while((key=(TKey*)iter())){
158  TString s_Obj = ((TObject*)key->ReadObj())->GetName();
159 
160  if(s_Obj=="SpillHists") continue;
161 
162  std::unique_ptr<Spectrum> spec_MCData = Spectrum::LoadFrom(&f_InFileMCData, s_Obj);
163  std::unique_ptr<Spectrum> spec_MCMC = Spectrum::LoadFrom(&f_InFileMCMC, s_Obj);
164 
165  TH1 *h_MCData = spec_MCData->ToTH1(spec_MCData->POT());
166  TH1 *h_MCMC = spec_MCMC ->ToTH1(spec_MCMC ->POT());
167 
168  TCanvas *c = makeCanvas(h_MCData, h_MCMC, s_Obj, s_FileAppend, drawCutsOnSpecialPlots, loosePresel);
169 
170  std::string s_OutName = s_OutDir + "/OverlayStudies_" + help.VarLabel((std::string)s_Obj, false) + "_" + help.CutLabel((std::string)s_Obj) + "_" + s_FileAppend + ".pdf";
171  c->SaveAs(s_OutName.c_str());
172 
173  delete c;
174  }
175 
176 
177  TDirectory* dir_SpillHist_MCData = (TDirectory*)f_InFileMCData.Get("SpillHists");
178  TDirectory* dir_SpillHist_MCMC = (TDirectory*)f_InFileMCMC .Get("SpillHists");
179  TIter iter_Dir_MCData(dir_SpillHist_MCData->GetListOfKeys());
180  TKey *key_Dir_MCData;
181  while((key_Dir_MCData=(TKey*)iter_Dir_MCData())){
182  TString s_Obj = ((TObject*)key_Dir_MCData->ReadObj())->GetName();
183 
184  TH1D *h_MCData = (TH1D*)dir_SpillHist_MCData->Get(s_Obj);
185  TH1D *h_MCMC = (TH1D*)dir_SpillHist_MCMC ->Get(s_Obj);
186 
187  TCanvas *c = makeCanvas(h_MCData, h_MCMC, s_Obj, s_FileAppend, drawCutsOnSpecialPlots, loosePresel);
188 
189  std::string s_OutName = s_OutDir + "/OverlayStudies_" + help.VarLabel((std::string)s_Obj, false) + "_" + help.CutLabel((std::string)s_Obj) + "_" + s_FileAppend + ".pdf";
190  c->SaveAs(s_OutName.c_str());
191 
192  delete c;
193  }
194 
195  //SPECIAL PLOTS:
196  std::unique_ptr<Spectrum> spec_DataNW = Spectrum::LoadFrom(&f_InFileMCData, "dir_-CUT-special_noweight_notover_rec_sel-VAR-kSpillPOT");
197  std::unique_ptr<Spectrum> spec_Data = Spectrum::LoadFrom(&f_InFileMCData, "dir_-CUT-over_fid_sig_rec_sel-VAR-kSpillPOT");
198 
199  TH1 *h_DataNW = spec_DataNW->ToTH1(spec_DataNW->POT());
200  TH1 *h_Data = spec_Data ->ToTH1(spec_Data ->POT());
201 
202  TCanvas *c = makeCanvas(h_DataNW, h_Data, "dir_-CUT-over_fid_sig_rec_sel-VAR-kSpillPOT", s_FileAppend, drawCutsOnSpecialPlots, loosePresel, true);
203 
204  c->cd();
205 
206  TLegend *leg = new TLegend(0.45, 0.84, 0.89, 0.98);
207  leg->AddEntry(h_DataNW, "Selected Data (No Weight)", "L");
208  leg->AddEntry(h_Data, "MC singles in Data", "L");
209  leg->Draw();
210 
211  std::string s_OutName = s_OutDir + "/OverlayStudies_special_weightVsNoWeight_kSpillPOT_" + s_FileAppend + ".pdf";
212  c->SaveAs(s_OutName.c_str());
213 
214  std::cout << "\n\n\nFINISHED MAKING PLOTS!\n\n\n";
215 
216 }
T max(const caf::Proxy< T > &a, T b)
enum BeamMode kRed
Cuts and Vars for the 2020 FD DiF Study.
Definition: vars.h:6
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
std::vector< TLine * > PlotSpecial(TString const &s_Obj, std::string const &s_Selection, double const &yMin, double const &yMax, bool loosePresel)
Definition: Helper.h:141
TCanvas * makeCanvas(TH1 *h_MCData, TH1 *h_MCMC, TString s_Obj, std::string s_FileAppend, bool drawCutsOnSpecialPlots, bool loosePresel, bool dataData=false)
Double_t ymax
Definition: plot.C:25
void drawBox()
std::string GetName(int i)
static std::unique_ptr< Spectrum > LoadFrom(TDirectory *dir, const std::string &name)
Definition: Spectrum.cxx:535
std::string VarLabel(std::string s_DirName, bool isNice)
Definition: Helper.h:222
OStream cout
Definition: OStream.cxx:6
std::vector< double > POT
bool IsSpecial(TString const &s_Obj)
Definition: Helper.h:128
void Plotting_OverlayStudies(std::string s_OutDir="", std::string s_FileAppend="", std::string s_InFileMCData="", std::string s_InFileMCMC="", bool drawCutsOnSpecialPlots=true, bool loosePresel=false)
Definition: Helper.h:14
std::string CutLabel(std::string s_DirName)
Definition: Helper.h:246
enum BeamMode kBlue
enum BeamMode string