All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tools_nuesel_icarus.h
Go to the documentation of this file.
3 
4 #include "TCanvas.h"
5 #include "TColor.h"
6 #include "TFile.h"
7 #include "TGraph.h"
8 #include "TH1.h"
9 #include "TLegend.h"
10 #include "TPaveText.h"
11 #include "TROOT.h"
12 #include "TText.h"
13 
14 #include <iostream>
15 #include <fstream>
16 #include <iomanip>
17 
18 ofstream output("/outputdir_path/table_output.txt");
19 
20 namespace ana{
21 
22  // ----------------------------------------------------------------------
23  // Tables
24 
25  // Highlight cell depending on range
26  TString thisCellColor(double weird){
27  TString mystring = "";
28  double thisval = abs(weird);
29  if( thisval>=0.05 && thisval<0.10) mystring = "\\cellcolor{green!25}";
30  else if( thisval>=0.10 && thisval<0.15) mystring = "\\cellcolor{yellow!25}";
31  else if( thisval>=0.15) mystring = "\\cellcolor{red!25}";
32  return mystring;
33  }
34 
35  TString fixLatexName(TString mystring){
36  // latex doesnt like underscores
37  std::vector<TString> in = {"#"," ",".","_"};
38  std::vector<TString> out = {"","","","\\_"};
39 
40  for(unsigned int i=0;i<in.size();i++)
41  mystring.ReplaceAll(in[i],out[i]);
42  return mystring;
43  }
44 
45  void printTableHeader(int quantId=0)
46  {
47  std::setprecision(3);
48  output << "\\begin{table}[H]\n";
49  output << "\\centering\n";
50  output << "\\resizebox{\\textwidth}{!}{\n";
51  // output << "\\begin{tabular}{|l||c|c|c|c|c||c|c|}\n";
52  output << "\\begin{tabular}{|l||c|c|c|c|c||c|}\n";
53  output << "\\hline \n";
54  // output << "\\multicolumn{1}{|c||}{} & \\multicolumn{5}{c||}{Number of interactions (\\% total)} & \\multicolumn{2}{c|}{Integrated} \\\\ \\hline \n \
55  // \\multicolumn{1}{|c||}{Cut} & $\\nu_{e}$ CC & $\\nu_{\\mu}$ CC & NC & Cosmic & Other bkg & Efficiency & Purity \\\\ \\hline \n";
56  output << "\\multicolumn{1}{|c||}{} & \\multicolumn{4}{c||}{Number of interactions (\\% total)} & \\multicolumn{2}{c|}{Integrated} \\\\ \\hline \n \
57  \\multicolumn{1}{|c||}{Cut} & $\\nu_{e}$ CC & $\\nu_{\\mu}$ CC & NC & Cosmic & Efficiency & Purity \\\\ \\hline \n";
58  }// printTableHeader
59 
61  output << "\\end{tabular}}\n";
62  output << "\\end{table}";
63  output << "\n\n\n";
64  //output.close();
65  }
66 
67  void printEventsLine(std::string cutname, float nue, float numu, float nc, float cos, float other, float eff, float pur){
68  float total = nue+numu+nc+cos+other;
69  float percnue = 100*nue/total;
70  float percnumu = 100*numu/total;
71  float percnc = 100*nc/total;
72  float perccos = 100*cos/total;
73  float percother = 100*other/total;
74  float perctotbkg = 100*(numu+nc+cos+other)/total;
75  // output << std::fixed << std::setw(6) << std::setprecision(3) << cutname << "&" << nue <<"("<<percnue<<")"<< "&" << numu<<"("<<percnumu<<")"<< "&" << nc<<"("<<percnc<<")"<< "&" << cos<<"("<<perccos<<")"<< "&" << other<<"("<<percother<<")"<< "&" << eff << "&" << pur << "\\\\ \\hline \n";
76  output << std::fixed << std::setw(6) << std::setprecision(3) << cutname << "&" << nue <<"("<<percnue<<")"<< "&" << numu<<"("<<percnumu<<")"<< "&" << nc<<"("<<percnc<<")"<< "&" << cos<<"("<<perccos<<")"<< "&" << eff << "&" << pur << "\\\\ \\hline \n";
77  }
78 
79 
80  // ----------------------------------------------------------------------
81  // Canvases
82  // split canvas in 2
83  void SplitCanvas2(TCanvas *& c1, TPad *& pad1, TPad *& pad2){
84 
85  c1 = new TCanvas("c1","",700,800);
86  c1->cd();
87 
88  pad1 = new TPad("pad1","pad1",0,0,1,1);
89  pad1->SetTopMargin(0.1);
90  pad1->SetBottomMargin(0.4);
91  pad1->SetLeftMargin(0.12);
92  pad1->SetRightMargin(0.03);
93  pad1->SetFillStyle(0);
94  pad1->Draw();
95  c1->cd();
96 
97  pad2 = new TPad("pad2","pad2",0,0,1,1);// x1 y1 x2 y2
98  pad2->SetTopMargin(0.6);
99  pad2->SetBottomMargin(0.1);
100  pad2->SetLeftMargin(0.12);
101  pad2->SetRightMargin(0.03);
102  pad2->SetFillStyle(0);
103  pad2->Draw();
104  c1->cd();
105 
106  }
107 
108  float GetHistMax(std::vector<TH1*> histos){
109 
110  float hmax = 0.;
111  for(unsigned int hId=0; hId<histos.size(); hId++){
112  float thismax = histos[hId]->GetMaximum();
113  if(thismax>hmax) hmax=thismax;
114  }
115  return hmax;
116  }
117 
118 
119  void PimpHist(TH1* histo, Color_t color, Style_t linestyle, int linewidth, Style_t markerstyle=8, double markersize=8){
120 
121  histo->SetLineColor(color);
122  histo->SetLineStyle(linestyle);
123  histo->SetLineWidth(linewidth);
124  histo->SetMarkerColor(color);
125  histo->SetMarkerStyle(markerstyle);
126  histo->SetMarkerSize(markersize);
127 
128  }
129 
130  void FillWithDimColor(TH1* h, bool usealpha=false, float dim=0.8)
131  {
132  if ( usealpha ){
133  h->SetFillColorAlpha(h->GetLineColor(),dim);
134  return;
135  }
136  TColor *color = gROOT->GetColor(h->GetLineColor());
137  float R,G,B,hR,hG,hB,hHue,hSat,hVal;
138  color->GetRGB(hR,hG,hB);
139  color->RGB2HSV(hR,hG,hB,hHue,hSat,hVal);
140  color->HSV2RGB(hHue,dim*hSat,hVal,R,G,B);
141  h->SetFillColor(color->GetColor(R,G,B));
142  }
143 
144  // Legends and Texts
145  //--------------------------------------------------
146  void DrawComponentsLegend(TH1* hnue, TH1* hnumu, TH1* hnc, TH1* hcos, TH1* hother){
147  // TLegend *l = new TLegend(0.60, 0.65, 0.85, 0.85, NULL,"brNDC");
148  TLegend *l = new TLegend(0.70, 0.70, 0.85, 0.85, NULL,"brNDC");
149  l->SetFillStyle(0);
150  l->SetTextSize(0.035);
151  // l->SetHeader(pot_tag.c_str());
152  // l->SetHeader("Integral");
153  // l->AddEntry(hnue, Form("#nu_{e} CC: %.2f", hnue->Integral()), "l");
154  // l->AddEntry(hnumu, Form("#nu_{#mu} CC: %.2f", hnumu->Integral()), "l");
155  // l->AddEntry(hnc, Form("NC: %.2f", hnc->Integral()), "l");
156  // l->AddEntry(hcos, Form("Cosmics: %.2f", hcos->Integral()), "l");
157  // l->AddEntry(hother, Form("Other bkg: %.2f", hother->Integral()), "l");
158  l->AddEntry(hnue, "#nu_{e} CC", "l");
159  l->AddEntry(hnumu, "#nu_{#mu} CC", "l");
160  l->AddEntry(hnc, "NC", "l");
161  l->AddEntry(hcos, "Cosmics", "l");
162  l->AddEntry(hother, "Other bkg", "l");
163  l->Draw("");
164  }
165 
166  void DrawSigBkgLegend(TH1* h1, char *name1, TH1* h2, char *name2){
167 
168  TLegend *leg = new TLegend(.60,.60,.8,.8);
169  leg->AddEntry(h1, name1,"l");
170  leg->AddEntry(h2, name2,"l");
171  leg->SetBorderSize(0); //no border for legend
172  leg->SetFillColor(0); //fill colour is white
173  leg->SetFillStyle(0); //fill colour is white
174  leg->SetTextSize(0.04);
175  leg->Draw();
176 
177  }
178 
179 
180  void DrawSigBkgIntLegend(TH1* h1, char *name1, double iSig, TH1* h2, char *name2, double iBkg){
181 
182  //double iRatio = iBac/iSig;
183 
184  TLegend *leg = new TLegend(.60,.60,.8,.8);
185  leg->AddEntry(h1, name1,"l");
186  leg->AddEntry(h1, TString::Format("%.2f",iSig),"");
187  leg->AddEntry(h2, name2,"l");
188  leg->AddEntry(h2, TString::Format("%.2f",iBkg),"");
189  //leg->AddEntry(h2, TString::Format("Ratio=%.2f",iRatio),"");
190  leg->SetBorderSize(0); //no border for legend
191  leg->SetFillColor(0); //fill colour is white
192  leg->SetFillStyle(0); //fill colour is white
193  leg->SetTextSize(0.04);
194  leg->Draw();
195 
196  }
197 
198  //--------------------------------------------------
199  void DrawIntEffPurLegend(TH1D* g1, char *name1, TH1D* g2, char *name2){
200 
201  float int1 = g1->Integral();
202  float int2 = g2->Integral();
203  TLegend *leg = new TLegend(.65,.2,.85,.3);
204  leg->AddEntry(g1, name1,"l");
205  leg->AddEntry(g2, name2,"l");
206  // leg->AddEntry(g1, Form("%s: %2.f", name1, int1),"l");
207  // leg->AddEntry(g2, Form("%s: %2.f", name2, int2),"l");
208  leg->SetBorderSize(0); //no border for legend
209  leg->SetFillColor(0); //fill colour is white
210  leg->SetFillStyle(0); //fill colour is white
211  leg->SetTextSize(0.03); // this goes in the split canvas so use smaller text
212  leg->Draw();
213 
214  }
215 
216  //--------------------------------------------------
217  void DrawPurLegend(TH1D* g1, char *name1){
218 
219  TLegend *leg = new TLegend(.65,.25,.85,.35);
220  leg->AddEntry(g1, name1,"l");
221  leg->SetBorderSize(0); //no border for legend
222  leg->SetFillColor(0); //fill colour is white
223  leg->SetFillStyle(0); //fill colour is white
224  leg->SetTextSize(0.03); // this goes in the split canvas so use smaller text
225  leg->Draw();
226 
227  }
228 
229  //--------------------------------------------------
230  void DrawEffPurLegend(TH1D* g1, char *name1, TH1D* g2, char *name2){
231 
232  TLegend *leg = new TLegend(.65,.2,.85,.3);
233  leg->AddEntry(g1, name1,"l");
234  leg->AddEntry(g2, name2,"l");
235  leg->SetBorderSize(0); //no border for legend
236  leg->SetFillColor(0); //fill colour is white
237  leg->SetFillStyle(0); //fill colour is white
238  leg->SetTextSize(0.03); // this goes in the split canvas so use smaller text
239  leg->Draw();
240 
241  }
242 
243  void DrawSigBkgIntText(TH1* hsig, TH1* hbkg, float textsize){
244 
245  float isig = hsig->Integral();
246  float ibkg = hbkg->Integral();
247  float psig = 100. * isig / (isig + ibkg);
248  float pbkg = 100. * ibkg / (isig + ibkg);
249 
250  TPaveText *pText1 = new TPaveText(0.15, 0.84, 0.30, 0.89, "brNDC");
251  TText *text1 = (pText1->AddText("6.6 #times 10^{20} POT"));
252  text1->SetTextSize(textsize);
253  pText1->SetBorderSize(0);
254  pText1->SetFillStyle(0);
255  pText1->Draw();
256  TPaveText *pText2 = new TPaveText(0.15, 0.72, 0.30, 0.80, "brNDC");
257  TText *text2 = pText2->AddText(Form("Sig: %2.f = %2.f %%", isig, psig));
258  text2->SetTextAlign(11);
259  text2->SetTextSize(textsize);
260  TText *text3 = pText2->AddText(Form("Bkg: %2.f = %2.f %%", ibkg, pbkg));
261  text3->SetTextAlign(11);
262  text3->SetTextSize(textsize);
263  pText2->SetBorderSize(0);
264  pText2->SetFillStyle(0);
265  pText2->Draw();
266  }
267 
268  // Efficiency and purity graphs
269  //-----------------------------------------------------------------
270 
271  TH1D* EffOrPurHistogram(TH1* hSelSignal, TH1* hSelBack, TH1* hSignal, bool geteff) {
272 
273  //
274  // Make a ROC TGraph for the given signal and background histos
275  //
276  TH1D* hTotal = (TH1D*)hSelSignal->Clone();
277  hTotal->Add(hSelBack);
278 
279  TH1D* hPurity = (TH1D*)hSelSignal->Clone();
280  hPurity->Divide(hTotal);
281 
282  TH1D* hEfficiency = (TH1D*)hSelSignal->Clone();
283  hEfficiency->Divide(hSignal);
284 
285  if ( geteff )
286  return hEfficiency;
287  else
288  return hPurity;
289 
290  }
291 
292  TGraph* EffOrPurGraph(TH1* hSelSignal, TH1* hSelBack, TH1* hSignal, bool geteff) {
293 
294  const int NBins = hSignal->GetNbinsX();
295 
296  TString xTitle = hSignal->GetXaxis()->GetTitle();
297  TString yTitle = hSignal->GetYaxis()->GetTitle();
298 
299  double eff[NBins], pur[NBins], val[NBins];
300  double sb[NBins], ssb[NBins];
301 
302  // loop over bins to calculate eff and pur as functions of the bin
303  for(unsigned int i = 1; i <= (unsigned int)NBins; ++i) {
304  double allsig = hSignal ->GetBinContent(i);
305  double selsig = hSelSignal->GetBinContent(i);
306  double selbac = hSelBack ->GetBinContent(i);
307 
308  val[i-1] = hSignal->GetBinCenter(i);
309  double EFF = selsig;
310  double PUR = selsig;
311 
312  if ( (selsig + selbac) > 0 ){
313  if ( allsig > 0 ) EFF = EFF/allsig;
314  PUR = PUR / (selsig + selbac);
315  }
316 
317  eff[i-1] = EFF;
318  pur[i-1] = PUR;
319  if ( PUR > 1 || EFF > 1 ){
320  std::cout << "\n\n\n" << " >>> EFF " << EFF << "\t PUR " << PUR << std::endl;
321  }
322  }
323 
324  TString n = hSignal->GetName();
325  TGraph *graphPur= new TGraph(NBins,val,pur);
326  graphPur->SetName("SelPur_"+n);
327  graphPur->GetXaxis()->SetTitle(xTitle);
328  graphPur->GetYaxis()->SetTitle("Pur.");
329  TGraph *graphEff= new TGraph(NBins,val,eff);
330  graphEff->SetName("SelEff_"+n);
331  graphEff->GetXaxis()->SetTitle(xTitle);
332  graphEff->GetYaxis()->SetTitle("Eff.");
333 
334  if ( geteff )
335  return graphEff;
336  else
337  return graphPur;
338 
339  }
340 
341 }
void printTableFooter()
void PimpHist(TH1 *histo, Color_t color, Style_t linestyle, int linewidth, Style_t markerstyle=8, double markersize=8)
TH1D * EffOrPurHistogram(TH1 *hSelSignal, TH1 *hSelBack, TH1 *hSignal, bool geteff)
void DrawSigBkgIntLegend(TH1 *h1, char *name1, double iSig, TH1 *h2, char *name2, double iBkg)
void DrawIntEffPurLegend(TH1D *g1, char *name1, TH1D *g2, char *name2)
void printEventsLine(std::string cutname, float nue, float numu, float nc, float cos, float other, float eff, float pur)
process_name opflashCryoW ana
void SplitCanvas2(TCanvas *&c1, TPad *&pad1, TPad *&pad2)
void DrawEffPurLegend(TH1D *g1, char *name1, TH1D *g2, char *name2)
void FillWithDimColor(TH1 *h, bool usealpha=false, float dim=0.8)
void DrawComponentsLegend(TH1 *hnue, TH1 *hnumu, TH1 *hnc, TH1 *hcos, TH1 *hother)
while getopts h
TString fixLatexName(TString mystring)
T abs(T value)
void DrawSigBkgIntText(TH1 *hsig, TH1 *hbkg, float textsize)
TGraph * EffOrPurGraph(TH1 *hSelSignal, TH1 *hSelBack, TH1 *hSignal, bool geteff)
void DrawPurLegend(TH1D *g1, char *name1)
if &&[-z"$BASH_VERSION"] then echo Attempting to switch to bash bash shellSwitch exit fi &&["$1"= 'shellSwitch'] shift declare a IncludeDirectives for Dir in
TString thisCellColor(double weird)
BEGIN_PROLOG sequence::SlidingWindowTriggerPatternsOppositeWindows END_PROLOG simSlidingORM6O6 effSlidingORW output
void DrawSigBkgLegend(TH1 *h1, char *name1, TH1 *h2, char *name2)
void printTableHeader(int quantId=0)
BEGIN_PROLOG could also be cout
float GetHistMax(std::vector< TH1 * > histos)