PlotEfficiency.C
Go to the documentation of this file.
1 void SetLeg(TLegend *leg){
2 
3  leg->SetFillStyle(0);
4  leg->SetBorderSize(0);
5  leg->SetTextSize(0.04);
6  leg->SetTextFont(42);
7  leg->SetY1(leg->GetY2()-leg->GetNRows()*0.05);
8 
9  return;
10 
11 }
12 
13 TLatex *MiscText(float x, float y, float size, TString text){
14 
15  TLatex *l = new TLatex(x,y,text);
16  l->SetNDC();
17  l->SetTextSize(size);
18  l->SetTextColor(kBlack);
19  l->Draw();
20 
21  return l;
22 
23 }
24 
25 void UpdateStamp(int ptype, int maxt){
26  TDatime tnow(maxt);
27  TString lasttime = TString::Format("Last updated on: %s", tnow.AsString());
28  TLatex *ltx = MiscText(0.6,0.96,0.03,lasttime);
29  // ltx->SetTextAngle(-90);
30 }
31 
32 void PlotEfficiency(int ptype=0, int run_lowlimit =0, int run_highlimit=0){
33  //TFile* f=new TFile("test.root","recreate");
34  TDatime t;
35  int GMToffset = (t.Convert(true) - t.Convert());
36  gSystem->Setenv("TZ","UTC");
37  TString stdP1 = "run>11495&&par==1";
38  t.Set();
39  int maxt = t.Convert();
40  int mint = TDatime(20130729,0).Convert();
41  // TDatime(maxt).Print();
42  int dt = 3600*24;
43 
44  if(ptype==1) dt *= 7;
45 
46  if(ptype==0) dt *= 30;
47 
48  TString tsel = TString::Format("&& lastsec > %d && lastsec < %d",maxt - dt + 2*GMToffset, maxt + 2*GMToffset);
49  stdP1 += tsel;
50  int tdiv = 706;
51  int tbin = 3600*24;
52  TString tlab = "Date (dd/mm/yy - Central Time)";
53  TString tform = "%d/%m/%y";
54  TString suf = "Month";
55 
56  if(ptype==1){
57 
58  tbin = 3600*6;
59  tdiv = 408;
60  tlab = "Date (dd/mm - Central Time)";
61  tform = "%d/%m";
62  suf = "Week";
63 
64  }
65 
66  if(ptype==2){
67 
68  tbin = 3600;
69  tdiv = 309;
70  tlab = "Central Time (hour:min)";
71  tform = "%H:%M";
72  suf = "Day";
73 
74  }
75 
76  TString metricstr = TString::Format("lastsec-%d",2*GMToffset);
77  TString sel =stdP1;
78  TString title = tlab;
79  int nbins = -tbin;
80  double xmin = maxt-dt;
81  double xmax = maxt;
82 
83  TString filenames = "MetricsTree*.root";
84  gStyle->SetTimeOffset(0);
85 
86  if(sel=="") sel = "1";
87 
88  if(title=="") title = metricstr;
89 
90  TString selpre = "((" + sel + ") && preliminary)";
91  // TString selpre = "((" + sel + ") && 0)";
92  TChain *mytree = new TChain("mytree");
93  mytree->Add(filenames);
94  // MakeLongCanvas();
95 
96  //my: Option to plot a run subset: fix xmin and xmax
97  //The axes format are still given with ptype
98  bool gotRunLimits = run_highlimit != run_lowlimit;
99  TString runlabel ="";
100  if(gotRunLimits){
101  //Get lower time
102  mytree->Draw("firstsec",
103  TString::Format("run>=%d && run<=%d && firstsec>=%d"
104  , run_lowlimit, run_highlimit, mint));
105  xmin = mytree->GetHistogram()->GetXaxis()->GetXmin();
106  //Get Higher time
107  mytree->Draw("lastsec",
108  TString::Format("run>=%d && run<=%d && firstsec>=%d"
109  , run_lowlimit, run_highlimit, mint));
110  xmax = mytree->GetHistogram()->GetXaxis()->GetXmax();
111  //Now fix the run subset into the selection strings
112  TString stdP1 = "run>11495&&par==1"; //Before it gets the time cuts
113  sel = stdP1 + TString::Format(
114  "&& run>=%d && run<=%d",run_lowlimit, run_highlimit);
115  selpre = "((" + sel + ") && preliminary)";
116  //Set the suffix for saving
117  suf = "CustomRunRange";
118  //Set a label for the plot title
119  runlabel = TString::Format("%d < Run < %d",run_lowlimit, run_highlimit);
120  }
121  //endmy
122 
123  TCanvas *c1 = new TCanvas("c1","c1",1000,600);
124  c1->SetBottomMargin(0.12);
125  c1->SetLeftMargin(0.12);
126  c1->SetRightMargin(0.07);
127  c1->SetTopMargin(0.07);
128  c1->cd();
129  TLegend *leg = new TLegend(0.15,0.65,0.65,0.9);
130  SetLeg(leg);
131  TLegend *legpre = new TLegend(0.16,0.50,0.93,0.68);
132  SetLeg(legpre);
133  TH1F* hAll = 0;
134  mytree->Draw(metricstr,sel);
135  hAll = (TH1F*)(mytree->GetHistogram())->Clone("");
136 
137  if(xmin == -9999 || xmax == -9999){
138 
139  xmin = hAll->GetXaxis()->GetXmin();
140  xmax = hAll->GetXaxis()->GetXmax();
141 
142  }
143  // cout << xmin << " " << xmax << endl;
144  if(nbins<0){
145 
146  xmin = floor(xmin);
147  xmax = ceil(xmax);
148  nbins *= -1;
149 
150  if(int(xmax-xmin)%nbins!=0){
151 
152  xmax += (nbins - int(xmax-xmin)%nbins)/2;
153  xmin -= (nbins - int(xmax-xmin)%nbins);
154 
155  }
156 
157  nbins = (xmax - xmin) / nbins;
158 
159  }
160  // cout << xmin << " " << xmax << endl;
161 
162  delete hAll;
163 
164  hAll = new TH1F("hAll","hAll",nbins,xmin,xmax);
165  mytree->Draw(metricstr+">>hAll",sel,"hist");
166  hAll->Clear();
167  hAll->SetLineColor(1);
168  hAll->SetLineWidth(2);
169  hAll->SetTitle(";"+title+";Percentage of Subruns");
170  hAll->GetXaxis()->SetTitleOffset(1);
171  hAll->GetYaxis()->SetTitleOffset(1);
172  hAll->GetXaxis()->CenterTitle();
173  hAll->GetYaxis()->CenterTitle();
174  // hAll->SetMinimum(0);
175  mytree->Draw(metricstr,"!(pass_all && ngoodcdb==14 && !preliminary) && "+sel,"hist same");
176  TH1F* h0 = (TH1F*)(mytree->GetHistogram())->Clone("h0");
177  h0->SetLineColor(1);
178  h0->SetLineWidth(2);
179  h0->SetFillColor(kRed-4);
180 
181  mytree->Draw(metricstr,"!(pass_all && !preliminary) && "+sel,"hist same");
182  TH1F* h1 = (TH1F*)(mytree->GetHistogram())->Clone("h1");
183  h1->SetLineColor(1);
184  h1->SetLineWidth(2);
185  h1->SetFillColor(kOrange+7);
186 
187  mytree->Draw(metricstr,"!(pass_time && pass_runlen && pass_hits && pass_db && pass_empty && !preliminary) && "+sel,"hist same");
188  TH1F* h2r = (TH1F*)(mytree->GetHistogram())->Clone("h2r");
189  h2r->SetLineColor(1);
190  h2r->SetLineWidth(2);
191  h2r->SetFillColor(kGreen+1);
192 
193  mytree->Draw(metricstr,"!(pass_time && pass_runlen && pass_hits && pass_empty && !preliminary) && "+sel,"hist same");
194  TH1F* h2 = (TH1F*)(mytree->GetHistogram())->Clone("h2");
195  h2->SetLineColor(1);
196  h2->SetLineWidth(2);
197  h2->SetFillColor(4);
198 
199  mytree->Draw(metricstr,"!(pass_time && pass_runlen && pass_empty && !preliminary) && "+sel,"hist same");
200  TH1F* h3 = (TH1F*)(mytree->GetHistogram())->Clone("h3");
201  h3->SetLineColor(1);
202  h3->SetLineWidth(2);
203  h3->SetFillColor(kMagenta+2);
204 
205  mytree->Draw(metricstr,"!(pass_time && pass_empty && !preliminary) && "+sel,"hist same");
206  TH1F* h4 = (TH1F*)(mytree->GetHistogram())->Clone("h4");
207  h4->SetLineColor(1);
208  h4->SetLineWidth(2);
209  h4->SetFillColor(kBlack);
210 
211  mytree->Draw(metricstr,selpre,"hist same");
212  TH1F* hGp = (TH1F*)(mytree->GetHistogram())->Clone("hGp");
213  hGp->SetLineColor(1);
214  hGp->SetLineWidth(2);
215  hGp->SetFillColor(18);
216 
217  mytree->Draw(metricstr,"!(pass_all && ngoodcdb==14) && "+selpre,"hist same");
218  TH1F* h0p = (TH1F*)(mytree->GetHistogram())->Clone("h0p");
219  h0p->SetLineColor(1);
220  h0p->SetLineWidth(2);
221  h0p->SetFillColor(kPink-4);
222 
223  mytree->Draw(metricstr,"!pass_all && "+selpre,"hist same");
224  TH1F* h1p = (TH1F*)(mytree->GetHistogram())->Clone("h1p");
225  h1p->SetLineColor(1);
226  h1p->SetLineWidth(2);
227  h1p->SetFillColor(kOrange-9);
228 
229  mytree->Draw(metricstr,"!(pass_time && pass_runlen && pass_hits && pass_db && pass_empty) && "+selpre,"hist same");
230  TH1F* h2rp = (TH1F*)(mytree->GetHistogram())->Clone("h2rp");
231  h2rp->SetLineColor(1);
232  h2rp->SetLineWidth(2);
233  h2rp->SetFillColor(kGreen-9);
234 
235  mytree->Draw(metricstr,"!(pass_time && pass_runlen && pass_hits && pass_empty) && "+selpre,"hist same");
236  TH1F* h2p = (TH1F*)(mytree->GetHistogram())->Clone("h2p");
237  h2p->SetLineColor(1);
238  h2p->SetLineWidth(2);
239  h2p->SetFillColor(kBlue-9);
240 
241  mytree->Draw(metricstr,"!(pass_time && pass_runlen && pass_empty) && "+selpre,"hist same");
242  TH1F* h3p = (TH1F*)(mytree->GetHistogram())->Clone("h3p");
243  h3p->SetLineColor(1);
244  h3p->SetLineWidth(2);
245  h3p->SetFillColor(kMagenta-7);
246 
247  mytree->Draw(metricstr,"!(pass_time && pass_empty) && "+selpre,"hist same");
248  TH1F* h4p = (TH1F*)(mytree->GetHistogram())->Clone("h4p");
249  h4p->SetLineColor(1);
250  h4p->SetLineWidth(2);
251  h4p->SetFillColor(kGray+2);
252  hAll->Scale(0.01);
253  h0->Divide(hAll);
254  h1->Divide(hAll);
255  h2r->Divide(hAll);
256  h2->Divide(hAll);
257  h3->Divide(hAll);
258  h4->Divide(hAll);
259  hGp->Divide(hAll);
260  h0p->Divide(hAll);
261  h1p->Divide(hAll);
262  h2rp->Divide(hAll);
263  h2p->Divide(hAll);
264  h3p->Divide(hAll);
265  h4p->Divide(hAll);
266 
267  hAll->Divide(hAll);
268  hAll->Scale(100);
269 
270  leg->AddEntry(hAll,"Good Subruns","f");
271  leg->AddEntry(h0,"Partial Detector","f");
272  leg->AddEntry(h1,"Failed Reco","f");
273  leg->AddEntry(h2r,"Failed Diblock","f");
274  leg->AddEntry(h2,"Failed Hit Rate","f");
275  leg->AddEntry(h3,"Failed Live Time","f");
276  leg->AddEntry(h4,"Failed Other","f");
277 
278  legpre->AddEntry(hGp," ","f");
279  legpre->AddEntry(h0p," ","f");
280  legpre->AddEntry(h1p," ","f");
281  legpre->AddEntry(h2rp," ","f");
282  legpre->AddEntry(h2p," ","f");
283  legpre->AddEntry(h3p," ","f");
284  legpre->AddEntry(h4p," ","f");
285  // hAll->GetYaxis()->SetRangeUser(0.001,10);
286  hAll->GetYaxis()->SetRangeUser(0,250);
287  /*
288  hAll->Rebin(rebin);
289  h1->Rebin(rebin);
290  h2->Rebin(rebin);
291  h3->Rebin(rebin);
292  h4->Rebin(rebin);
293  */
294  gStyle->SetPaintTextFormat("2.1f%%");
295 
296  h0->SetMarkerSize(2);
297  hAll->Draw("hist");
298  h0->Draw("hist text same");
299  h1->Draw("hist same");
300  h2r->Draw("hist same");
301  h2->Draw("hist same");
302  h3->Draw("hist same");
303  h4->Draw("hist same");
304  hGp->Draw("hist same");
305  h0p->Draw("hist same");
306  h1p->Draw("hist same");
307  h2rp->Draw("hist same");
308  h2p->Draw("hist same");
309  h3p->Draw("hist same");
310  h4p->Draw("hist same");
311 
312  leg->SetNColumns(2);
313  leg->SetY1(0.9-leg->GetNRows()*0.05);
314  leg->Draw();
315  legpre->SetNColumns(7);
316  // legpre->SetY1(0.9-leg->GetNRows()*0.01);
317  SetLeg(legpre);
318  legpre->SetColumnSeparation(-4.3);
319  legpre->Draw();
320  // c1->SetLogy();
321  // MiscText(0.35,0.94,0.06,"NDOS Cosmics");
322  MiscText(0.15,0.94,0.055,"Far Detector");
323  double eff = 100.0*mytree->GetEntries(sel+" && pass_all")/mytree->GetEntries(sel);
324  TString effstr = TString::Format("%2.1f%% Efficiency",eff);
325  MiscText(0.675,0.850,0.055,effstr.Data());
326  MiscText(0.380,0.638,0.055,"Preliminary");
327  hAll->GetXaxis()->SetTimeFormat(tform);
328  hAll->GetXaxis()->SetTimeDisplay(1);
329  hAll->GetXaxis()->SetNdivisions(tdiv);
330  hAll->SetStats(kFALSE);
331  gPad->SetGridx();
332  //Add label if got runlimits
333  if(gotRunLimits) MiscText(0.7,0.96,0.04,runlabel);
334  else UpdateStamp(ptype,maxt);
335  c1->Update();
336  c1->SaveAs("pngfiles/FarDet-t02-P1GoodDataSelDateEff"+suf+".png");
337  c1->Close();
338 
339 }
std::map< std::string, double > xmax
TH1F * h3
Definition: berger.C:36
Definition: Cand.cxx:23
const int nbins
Definition: cellShifts.C:15
void PlotEfficiency(int ptype=0, int run_lowlimit=0, int run_highlimit=0)
void SetLeg(TLegend *leg)
Definition: PlotEfficiency.C:1
TTree * mytree
Definition: SimpleIterate.C:18
TLatex * MiscText(float x, float y, float size, TString text)
TH1F * h2
Definition: plot.C:45
void UpdateStamp(int ptype, int maxt)
TH1F * h1
Int_t GMToffset
Definition: AnaPlotMaker.h:43
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:11
c1
Definition: demo5.py:24
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
fvar< T > ceil(const fvar< T > &x)
Definition: ceil.hpp:11