genie_plot_utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "TH1D.h"
4 #include "TCanvas.h"
5 #include "TFile.h"
6 #include "TStyle.h"
7 #include "TLine.h"
8 #include "TLegend.h"
9 #include "TLatex.h"
10 
11 #include <iostream>
12 
13 std::vector<int> axis_widths = {6,8,6,8};
14 std::vector<std::string> axis_labels_fv = {
15  "N_{#mu}#nu_{#muQE}",
16  "N_{#mu}#nu_{#mu}",
17  "N_{#mu}NC",
18 
19  "N_{e}#nu_{eQE}",
20  "N_{e}#nu_{e}",
21  "N_{e}#nu_{#mu}",
22  "N_{e}NC",
23 
24  "F_{#mu}#nu_{#muQE}",
25  "F_{#mu}#nu_{#mu}",
26  "F_{#mu}NC",
27 
28  "F_{e}#nu_{eQE}",
29  "F_{e}#nu_{e}",
30  "F_{e}#nu_{#mu}",
31  "F_{e}NC"
32 };
33 
34 std::vector<std::string> axis_labels = {
35  "N_{#mu}#nu_{#muQE}",
36  "N_{#mu}#nu_{#mu}",
37  "N_{#mu}NC",
38 
39  "N_{e}#nu_{eQE}",
40  "N_{e}#nu_{e}",
41  "N_{e}#nu_{#mu}",
42  "N_{e}NC",
43 
44  "F_{#mu}#nu_{#muQE}/N_{#mu}#nu_{#muQE}",
45  "F_{#mu}#nu_{#mu}/N_{#mu}#nu_{#mu}",
46  "F_{#mu}NC",
47 
48  "F_{e}#nu_{eQE}/N_{#mu}#nu_{#muQE}",
49  "F_{e}#nu_{e}/N_{#mu}#nu_{#mu}",
50  "F_{e}#nu_{#mu}/N_{e}#nu_{#mu}",
51  "F_{e}NC/N_{e}NC"
52 };
53 
55  std::vector<TH1D*> topHistos,
56  std::vector<TString> topOption,
57  std::vector<TH1D*> bottomHistos,
58  std::vector<TString> bottomOption,
59  double min = 0.3,double max = 1.7,
60  std::vector<double> ls = {0.8,1,1.2})
61 {
62  gStyle->SetTitleStyle(0);
63  TCanvas *c = new TCanvas(name.c_str(), "canvas", 1000, 700);
64  //define top and bottom pads
65  TPad* p1 = new TPad("p1", "p1", 0, 0, 1, 1);
66  TPad* p2 = new TPad("p2", "p2", 0, 0, 1, 1);
67  p1->SetNumber(1);
68  p2->SetNumber(2);
69  p1->SetBottomMargin(.55);
70  p2->SetTopMargin(.45);
71  p2->SetBottomMargin(.35);
72 
73  for(auto p:{p1,p2}){
74  p->SetFillStyle(0);
75  p->Draw();
76  }
77 
78  // format histograms
79  auto h1 = (TH1D*) topHistos[0]->Clone();
80  auto h3 = (TH1D*) bottomHistos[0]->Clone();
81 
82  for(auto & h:{h1,h3}){
83  h->SetStats(0);
84 
85  h->GetYaxis()->SetTitleSize(26);
86  h->GetYaxis()->SetTitleFont(43);
87  h->GetYaxis()->SetTitleOffset(1.3);
88  h->GetYaxis()->SetLabelFont(43);
89  h->GetYaxis()->CenterTitle();
90 
91  h->GetXaxis()->SetTitleOffset(1.2);
92  h->GetXaxis()->SetTitleSize(28);
93  h->GetXaxis()->SetTitleFont(43);
94  h->GetXaxis()->SetLabelFont(43);
95  h->GetXaxis()->CenterTitle();
96  }
97 
98  h1->GetYaxis()->SetLabelSize(18);
99 
100  h1->GetXaxis()->SetLabelSize(0);
101  h1->GetXaxis()->SetTitleSize(0);
102 
103  h3->SetTitle("");
104  h3->GetYaxis()->SetTitle("Ratio");
105  h3->GetYaxis()->SetRangeUser(min,max);
106  h3->GetYaxis()->SetLabelSize(15);
107  h3->GetYaxis()->SetDecimals();
108  h3->GetYaxis()->CenterTitle();
109 
110  h3->GetXaxis()->SetTitle(h1->GetXaxis()->GetTitle());
111  h3->GetXaxis()->SetLabelSize(0);
112 
113  p1->cd();
114 
115  h1->Draw(topOption[0]);
116 
117  for(unsigned int ii = 1; ii < topHistos.size(); ++ii) {
118  topHistos[ii]->Draw("same "+topOption[ii]);
119  }
120  h1->Draw("same "+topOption[0]);
121 
122  p1->RedrawAxis(); //white histograms cover axes
123 
124  p2->cd();
125  h3->Draw(bottomOption[0]);
126  for(unsigned int ii = 1; ii < bottomHistos.size(); ++ii){
127  bottomHistos[ii]->Draw("same " + bottomOption[ii]);
128  }
129  //h3->Draw("same " + bottomOption[0]);
130 
131  TLine *lone = new TLine();
132  lone->SetLineStyle(3);
133  lone->SetLineColor(kGray+3);
134  p2->Update();
135  for(unsigned int i = 0;i < ls.size();++i) lone->DrawLine(p2->GetUxmin(),ls[i],p2->GetUxmax(),ls[i]);
136  p2->RedrawAxis();
137 
138  return c;
139 }
140 
141 void zeroRatio(TH1D* h){
142  for(int i = 1;i <= h->GetNbinsX();++i){
143  if(h->GetBinContent(i) == 0) h->SetBinContent(i,1);
144  }
145 }
146 
147 TCanvas* QuickUnivRatioPlot(std::string name,TH1D* hNom,std::vector<TH1D*> others){
148  std::vector<TH1D*> h0;
149  std::vector<TString> h0opt;
150  std::vector<TH1D*> hRat;
151  std::vector<TString> hRatopt;
152 
153  h0.push_back(hNom);
154  h0opt.push_back("hist");
155  for(unsigned int i = 0;i < others.size();++i){
156  //others[i]->SetLineColor(kGray);
157  h0.push_back(others[i]);
158  h0opt.push_back("hist");
159  TH1D* h = (TH1D*)others[i]->Clone("h");
160  h->Divide(hNom);
161  zeroRatio(h);
162  hRat.push_back(h);
163  hRatopt.push_back("hist");
164  }
165 
166  return RatioPlot(name,h0,h0opt,hRat,hRatopt);
167 }
168 
169 void drawCoverLines(TH1D* hist){
170  TLine *lone = new TLine();
171 
172  std::vector<double> coverage = {0.9,0.95,0.99,0.999,0.9999};
173  std::vector<int> coverageBins;
174  for(unsigned int i = 0;i < coverage.size();++i) coverageBins.push_back(hist->FindFirstBinAbove(coverage[i]));
175 
176  std::cout<<"Coverage & PCs \\\\"<<std::endl;
177  for(unsigned int i = 0;i < coverage.size();++i){
178  double x = hist->GetBinCenter(coverageBins[i]);
179  lone->DrawLine(x,gPad->GetUymin(),x,gPad->GetUymax());
180 
181  std::cout<<100*coverage[i]<<"\\% & "<<(int)(x+0.5)<<" \\\\"<<std::endl;
182  }
183 
184 }
185 
186 void drawBinLines(std::vector<int> width){
187  TLine *lone = new TLine();
188  const int bins = 20;
189 
190  double loc = 0;
191  for(unsigned int i = 0;i < width.size();++i){
192  for(int j = 0;j < width[i];++j){
193  loc+=bins;
194  if(j == width[i]-1 && i != width.size()-1){
195  lone->SetLineWidth(3);
196  lone->DrawLine(loc,gPad->GetUymin(),loc,gPad->GetUymax());
197  continue;
198  }
199  lone->SetLineWidth(1);
200  lone->DrawLine(loc,gPad->GetUymin(),loc,gPad->GetUymax());
201  }
202  }
203 }
204 
205 void drawLabels(std::vector<std::string> labels)
206 {
207  double left = gPad->GetLeftMargin();
208  double right = 1-gPad->GetRightMargin();
209  double top = 1-gPad->GetTopMargin();
210  double bottom = gPad->GetBottomMargin();
211 
212  TLatex *tLabel = new TLatex();
213  tLabel->SetTextSize(0.025);
214  tLabel->SetTextAlign(12);
215  tLabel->SetTextSize(1./30);
216  tLabel->SetNDC();
217  tLabel->SetTextAngle(-90);
218 
219  double center=0;
220  for(unsigned int i = 0;i < labels.size();++i){
221  center = left+(right-left)*(1+2*i)/(labels.size()*2);
222  tLabel->DrawLatex(center, 0.95*bottom, labels[i].c_str());
223  }
224 }
225 
226 TCanvas* SplitPlot (std::string name,
227  std::vector<TH1D*> topHistos,
228  std::vector<TH1D*> bottomHistos)
229 {
230  gStyle->SetTitleStyle(0);
231  TCanvas *c = new TCanvas(name.c_str(), "canvas", 1000, 700);
232  //define top and bottom pads
233  TPad* p1 = new TPad("p1", "p1", 0, 0, 1, 1);
234  TPad* p2 = new TPad("p2", "p2", 0, 0, 1, 1);
235  p1->SetNumber(1);
236  p2->SetNumber(2);
237  p1->SetBottomMargin(.5);
238  p2->SetTopMargin(.5);
239 
240  for(auto p:{p1,p2}){
241  p->SetFillStyle(0);
242  p->Draw();
243  }
244 
245  // format histograms
246  auto h1 = (TH1D*) topHistos[0]->Clone();
247  auto h3 = (TH1D*) bottomHistos[0]->Clone();
248 
249  for(auto & h:{h1,h3}){
250  h->SetStats(0);
251 
252  h->GetYaxis()->SetTitleSize(26);
253  h->GetYaxis()->SetTitleFont(43);
254  h->GetYaxis()->SetTitleOffset(1.3);
255  h->GetYaxis()->SetLabelFont(43);
256  h->GetYaxis()->CenterTitle();
257  h->GetYaxis()->SetRangeUser(0.75,1.25);
258 
259  h->GetXaxis()->SetTitleOffset(1.2);
260  h->GetXaxis()->SetTitleSize(28);
261  h->GetXaxis()->SetTitleFont(43);
262  h->GetXaxis()->SetLabelFont(43);
263  h->GetXaxis()->CenterTitle();
264  }
265 
266  h1->GetYaxis()->SetTitle("Shift");
267  h1->GetYaxis()->SetLabelSize(18);
268 
269  h1->GetXaxis()->SetLabelSize(0);
270  h1->GetXaxis()->SetTitleSize(0);
271 
272  h3->SetTitle("");
273  h3->GetYaxis()->SetTitle("Shift");
274  h3->GetYaxis()->SetLabelSize(15);
275  h3->GetYaxis()->SetDecimals();
276  h3->GetYaxis()->CenterTitle();
277 
278  h3->GetXaxis()->SetTitle(h1->GetXaxis()->GetTitle());
279  h3->GetXaxis()->SetLabelSize(0);
280 
281  p1->cd();
282 
283  h1->Draw("hist");
284 
285  for(unsigned int ii = 1; ii < topHistos.size(); ++ii) {
286  topHistos[ii]->Draw("hist same");
287  }
288  h1->Draw("hist same");
289 
290  p1->RedrawAxis(); //white histograms cover axes
291 
292  p2->cd();
293  h3->Draw("hist");
294  for(unsigned int ii = 1; ii < bottomHistos.size(); ++ii){
295  bottomHistos[ii]->Draw("hist same");
296  }
297  h3->Draw("hist same");
298 
299  TLine *lone = new TLine();
300  lone->SetLineStyle(3);
301  lone->SetLineColor(kGray+3);
302  p2->Update();
303  //for(unsigned int i = 0;i < ls.size();++i) lone->DrawLine(p2->GetUxmin(),ls[i],p2->GetUxmax(),ls[i]);
304  p2->RedrawAxis();
305 
306  return c;
307 }
const XML_Char * name
Definition: expat.h:151
TCanvas * QuickUnivRatioPlot(std::string name, TH1D *hNom, std::vector< TH1D * > others)
std::vector< int > axis_widths
TCanvas * RatioPlot(std::string name, std::vector< TH1D * > topHistos, std::vector< TString > topOption, std::vector< TH1D * > bottomHistos, std::vector< TString > bottomOption, double min=0.3, double max=1.7, std::vector< double > ls={0.8, 1, 1.2})
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:112
TH1F * h3
Definition: berger.C:36
void drawBinLines(std::vector< int > width)
const char * p
Definition: xmltok.h:285
TCanvas * SplitPlot(std::string name, std::vector< TH1D * > topHistos, std::vector< TH1D * > bottomHistos)
void drawLabels(std::vector< std::string > labels)
const double j
Definition: BetheBloch.cxx:29
const Binning bins
OStream cout
Definition: OStream.cxx:6
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
void zeroRatio(TH1D *h)
TH1F * h1
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
std::vector< std::string > axis_labels_fv
def ls(target="")
Definition: g4zmq.py:69
std::vector< std::string > axis_labels
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
void drawCoverLines(TH1D *hist)
TGeoVolume * top
Definition: make_fe_box.C:9
enum BeamMode string