CMS 3D CMS Logo

compareTotals.py
Go to the documentation of this file.
1 import os,sys
2 import ROOT
3 import copy
4 
5 doNorm = True
6 
7 def getCanvasMainPad( logY ):
8  pad1 = ROOT.TPad("pad1", "pad1", 0, 0.2, 1, 1)
9  pad1.SetBottomMargin(0.15)
10  if( logY ):
11  pad1.SetLogy()
12  return pad1
13 
14 def getCanvasRatioPad( logY ):
15  pad2 = ROOT.TPad("pad2", "pad2", 0, 0, 1, 0.21)
16  pad2.SetTopMargin(0.05)
17  pad2.SetBottomMargin(0.1)
18  return pad2
19 
20 def getRatioAxes( xMin, xMax, yMin, yMax ):
21  h2_axes_ratio = ROOT.TH2D("axes_ratio", "", 10, xMin, xMax, 10, yMin, yMax )
22  h2_axes_ratio.SetStats(0)
23  h2_axes_ratio.GetXaxis().SetLabelSize(0.00)
24  h2_axes_ratio.GetXaxis().SetTickLength(0.09)
25  h2_axes_ratio.GetYaxis().SetNdivisions(5,5,0)
26  h2_axes_ratio.GetYaxis().SetTitleSize(0.13)
27  h2_axes_ratio.GetYaxis().SetTitleOffset(0.37)
28  h2_axes_ratio.GetYaxis().SetLabelSize(0.13)
29  h2_axes_ratio.GetYaxis().SetTitle("Ratio")
30  return h2_axes_ratio
31 
32 
33 dirnames = []
34 dirnames.append(sys.argv[1]) #original (1)
35 dirnames.append(sys.argv[2]) #updated (2)
36 
37 outdir = sys.argv[3]
38 if not os.path.exists(outdir):
39  os.makedirs(outdir)
40  os.system("cp web/index.php %s"%(outdir))
41 
42 names = ["Reference", "Other"]
43 if len(sys.argv)==5 and sys.argv[4]=="vsCMSSW":
44  names = ["CMSSW", "mkFit"]
45 
46 if len(sys.argv)==6:
47  names = [sys.argv[4], sys.argv[5]]
48 
49 colors = [1,2]
50 
51 fnames = []
52 fs = []
53 for d in range(0, len(dirnames)):
54  fnames.append(dirnames[d]+"/plots.root")
55  fs.append(ROOT.TFile.Open(fnames[d]))
56 
57 subdirs=[]
58 
59 eff_obj = []
60 eff_pass = []
61 eff_tot = []
62 eff_ratio = []
63 #
64 hist = []
65 #
66 hist_xratio = []
67 eff_xratio = []
68 
69 for d in range(0, len(fnames)):
70 
71  fs[d].cd()
72 
73  for dkey in ROOT.gDirectory.GetListOfKeys():
74  if not dkey.IsFolder():
75  continue
76  if d<=0:
77  subdirs.append(dkey.GetName())
78 
79  eff_obj_s = []
80  eff_pass_s = []
81  eff_tot_s = []
82  eff_ratio_s = []
83  #
84  hist_s = []
85 
86  subhist = []
87  subrate = []
88 
89  for subdir in subdirs:
90  print "In subdir %s:"%subdir
91  fs[d].cd(subdir)
92 
93  thiseff_obj = []
94  thiseff_pass = []
95  thiseff_tot = []
96  thiseff = []
97  #
98  thishist = []
99  #
100  nh=0
101  ne=0
102  #
103  for key in ROOT.gDirectory.GetListOfKeys():
104  obj = key.ReadObj()
105  if obj.IsA().InheritsFrom("TH1"):
106  h = obj
107  #print "Found TH1 %s"%h.GetName()
108 
109  thishist.append(h)
110  thishist[nh].Sumw2()
111  thishist[nh].SetLineColor(colors[d])
112  thishist[nh].SetMarkerColor(colors[d])
113  thishist[nh].SetMarkerSize(0.3)
114  thishist[nh].SetMarkerStyle(20)
115  thishist[nh].SetStats(0)
116 
117  nh=nh+1
118 
119  if obj.IsA().InheritsFrom("TEfficiency"):
120  e = obj
121  #print "Found TEfficiency %s"%e.GetName()
122 
123  thiseff_obj .append(e)
124  thiseff_obj[ne].SetLineColor(colors[d])
125  thiseff_obj[ne].SetMarkerColor(colors[d])
126  thiseff_obj[ne].SetMarkerSize(0.3)
127  thiseff_obj[ne].SetMarkerStyle(20)
128  thiseff_pass.append(e.GetPassedHistogram())
129  thiseff_tot .append(e.GetTotalHistogram())
130  thiseff_pass[ne].Sumw2()
131  thiseff_tot[ne] .Sumw2()
132  effname = "%s_rate"%(thiseff_pass[ne].GetName())
133  auxeff = thiseff_pass[ne].Clone(effname)
134  auxeff.Divide(thiseff_pass[ne],thiseff_tot[ne],1.0,1.0,"B")
135  thiseff.append(auxeff)
136  thiseff[ne].SetLineColor(colors[d])
137  thiseff[ne].SetMarkerColor(colors[d])
138  thiseff[ne].SetMarkerSize(0.3)
139  thiseff[ne].SetMarkerStyle(20)
140  thiseff[ne].SetStats(0)
141 
142  ne=ne+1
143 
144  hist_s .append(thishist)
145  #
146  eff_ratio_s.append(thiseff)
147  eff_pass_s .append(thiseff_pass)
148  eff_tot_s .append(thiseff_tot)
149  eff_obj_s .append(thiseff_obj)
150 
151  hist .append(hist_s)
152  hist_xratio.append(hist_s)
153  #
154  eff_ratio .append(eff_ratio_s)
155  eff_xratio.append(eff_ratio_s)
156  eff_pass .append(eff_pass_s)
157  eff_tot .append(eff_tot_s)
158  eff_obj .append(eff_obj_s)
159 
160 ratios_hist = []
161 ratios_eff = []
162 for dd in range(len(subdirs)):
163 
164  thisratio = []
165  for r in range(len(hist_xratio[0][dd])):
166  auxratio = hist_xratio[1][dd][r].Clone("num")
167  auxden = hist_xratio[0][dd][r].Clone("den")
168  intnum = auxratio.Integral(0,-1)
169  intden = auxden .Integral(0,-1)
170  if intnum>0:
171  auxratio.Scale(1.0/intnum)
172  if intden>0:
173  auxden.Scale(1.0/intden)
174  auxratio.Divide(auxden)
175  auxratio.SetName("ratio")
176  thisratio.append(auxratio)
177  thisratio[r].GetYaxis().SetTitle("Ratio")
178  thisratio[r].SetLineColor(colors[1])
179  thisratio[r].SetMarkerColor(colors[1])
180  thisratio[r].SetMarkerSize(0)
181  thisratio[r].SetStats(0)
182  ratios_hist.append(thisratio)
183 
184  thisratio = []
185  for r in range(len(eff_xratio[0][dd])):
186  auxratio = eff_xratio[1][dd][r].Clone( "numerator")
187  auxden = eff_xratio[0][dd][r].Clone("denominator")
188  auxratio.Divide(auxden)
189  auxratio.SetName("ratio")
190  thisratio.append(auxratio)
191  thisratio[r].GetYaxis().SetTitle("Ratio")
192  thisratio[r].SetLineColor(colors[1])
193  thisratio[r].SetMarkerColor(colors[1])
194  thisratio[r].SetMarkerSize(0)
195  thisratio[r].SetStats(0)
196  ratios_eff.append(thisratio)
197 
198 
199 ROOT.gStyle.SetOptStat(0)
200 
201 outsubdir = []
202 for ns,subdir in enumerate(subdirs):
203  thisdir = "%s/%s"%(outdir,subdir)
204  outsubdir.append(thisdir)
205  if not os.path.exists(thisdir):
206  os.mkdir(thisdir)
207  os.system("cp web/index.php %s"%(thisdir))
208 
209  for r in range(len(eff_xratio[0][ns])):
210 
211  outname = eff_obj[0][ns][r].GetName()
212 
213  can = ROOT.TCanvas("can_%s"%outname, "", 600, 600)
214  can.cd()
215 
217  pad1.SetTickx()
218  pad1.SetTicky()
219 
221  pad2.SetTickx()
222  pad2.SetTicky()
223 
224  can.cd()
225  pad1.Draw()
226  pad1.cd()
227 
228  ttitle = eff_obj[0][ns][r].GetTitle()
229  xmin = ratios_eff[ns][r].GetXaxis().GetBinLowEdge(1)
230  xmax = ratios_eff[ns][r].GetXaxis().GetBinUpEdge(ratios_eff[ns][r].GetNbinsX())
231  yminM = 0.0
232  ymaxM = 1.2
233  #if "dr" in outname or "ineff" in outname:
234  # ymaxM = 0.50
235  xtitle = ratios_eff[ns][r].GetXaxis().GetTitle()
236  ytitle = "Efficiency"
237  if "dr" in outname:
238  ytitle = "Duplicate rate"
239  elif "fr" in outname:
240  ytitle = "Fake rate"
241  elif "ineff" in outname:
242  ytitle = "Inefficiency"
243 
244  haxisMain = ROOT.TH2D("haxisMain" ,ttitle,1,xmin ,xmax,1,yminM,ymaxM)
245 
246  haxisMain.GetXaxis().SetTitle(xtitle)
247  haxisMain.GetXaxis().SetTitleOffset(1.2)
248  haxisMain.GetYaxis().SetTitle(ytitle)
249  haxisMain.GetYaxis().SetTitleOffset(1.4)
250 
251  haxisMain.Draw()
252  eff_obj[0][ns][r].Draw("PE,same")
253  eff_obj[1][ns][r].Draw("PE,same")
254 
255  legend = ROOT.TLegend(0.7,0.7, 0.87, 0.87);
256  legend.SetLineColor(0)
257  legend.SetFillColor(0)
258  legend.AddEntry(eff_obj[0][ns][r], names[0], "PL")
259  legend.AddEntry(eff_obj[1][ns][r], names[1], "PL")
260  legend.Draw("same")
261 
262  can.cd()
263  pad2.Draw()
264  pad2.cd()
265 
266  ymin = 0.9*ratios_eff[ns][r].GetMinimum()
267  ymax = 1.1*ratios_eff[ns][r].GetMaximum()
268 
269  if ymin==0:
270  ymin=0.75
271  if ymax<=ymin:
272  ymin=0.75*ymax
273  ymax=1.25*ymax
274 
275  if ymax<=ymin:
276  ymin=0.0
277  ymax=2.0
278 
279  hraxes = getRatioAxes(xmin,xmax,ymin,ymax)
280 
281  line = ROOT.TLine(xmin,1.0,xmax,1.0)
282  line.SetLineColor(1)
283  line.SetLineStyle(2)
284 
285  hraxes.Draw("")
286  ratios_eff[ns][r].Draw("PE,same")
287  line.Draw("same")
288 
289  can.cd()
290  pad1.Draw()
291  pad2.Draw()
292 
293  can.SaveAs("%s/%s.png"%(thisdir,outname));
294  can.SaveAs("%s/%s.pdf"%(thisdir,outname));
295 
296  can.Update()
297  can.Clear()
298 
299  tot = [eff_tot[0][ns][r].Integral(), eff_tot[1][ns][r].Integral()]
300  passing = [eff_pass[0][ns][r].Integral(), eff_pass[1][ns][r].Integral()]
301  efficiency = []
302  reldiff = []
303  for d in range(0,len(tot)):
304  if tot[d]>0:
305  efficiency.append(passing[d]/tot[d])
306  else:
307  efficiency.append(0.0)
308  if efficiency[0]>0:
309  reldiff.append(efficiency[d]/efficiency[0])
310  else:
311  reldiff.append(0.0)
312 
313  fo = open("%s/%s.log"%(thisdir,outname),"w+")
314  fo.write( "Totals:" )
315  for d in range(0,len(tot)):
316  fo.write( " %d " % int(tot[d]) ),
317  fo.write( "\nPassing:" )
318  for d in range(0,len(tot)):
319  fo.write( " %d " % int(passing[d]) ),
320  fo.write( "\nRate:" )
321  for d in range(0,len(tot)):
322  fo.write( " %0.4f " % efficiency[d] ),
323  fo.write( "\nRatio(/reference):" )
324  for d in range(0,len(tot)):
325  fo.write( " %0.4f " % reldiff[d] ),
326  fo.write( "\n" )
327 
328 
329  if "_pt_" in outname:
330  outname = outname+"_logx"
331 
332  can = ROOT.TCanvas("can_%s"%outname, "", 600, 600)
333  can.cd()
334 
335  pad1 = getCanvasMainPad(0)
336  pad1.SetTickx()
337  pad1.SetTicky()
338  pad1.SetLogx()
339 
340  pad2 = getCanvasRatioPad(0)
341  pad2.SetTickx()
342  pad2.SetTicky()
343  pad2.SetLogx()
344 
345  can.cd()
346  pad1.Draw()
347  pad1.cd()
348 
349  ttitle = eff_obj[0][ns][r].GetTitle()
350  xmin = 0.1
351  xmax = ratios_eff[ns][r].GetXaxis().GetBinUpEdge(ratios_eff[ns][r].GetNbinsX())
352  yminM = 0.0
353  ymaxM = 1.2
354  #if "dr" in outname or "ineff" in outname:
355  # ymaxM = 0.50
356  xtitle = ratios_eff[ns][r].GetXaxis().GetTitle()
357  ytitle = "Efficiency"
358  if "dr" in outname:
359  ytitle = "Duplicate rate"
360  elif "fr" in outname:
361  ytitle = "Fake rate"
362  elif "ineff" in outname:
363  ytitle = "Inefficiency"
364 
365  haxisMain = ROOT.TH2D("haxisMain" ,ttitle,1,xmin,xmax,1,yminM,ymaxM)
366 
367  haxisMain.GetXaxis().SetTitle(xtitle)
368  haxisMain.GetXaxis().SetTitleOffset(1.2)
369  haxisMain.GetYaxis().SetTitle(ytitle)
370  haxisMain.GetYaxis().SetTitleOffset(1.4)
371 
372  haxisMain.Draw()
373  eff_obj[0][ns][r].Draw("PE,same")
374  eff_obj[1][ns][r].Draw("PE,same")
375 
376  legend = ROOT.TLegend(0.7, 0.7, 0.87, 0.87);
377  legend.SetLineColor(0)
378  legend.SetFillColor(0)
379  legend.AddEntry(eff_obj[0][ns][r], names[0], "PL")
380  legend.AddEntry(eff_obj[1][ns][r], names[1], "PL")
381  legend.Draw("same")
382 
383  can.cd()
384  pad2.Draw()
385  pad2.cd()
386 
387  ymin = 0.9*ratios_eff[ns][r].GetMinimum()
388  ymax = 1.1*ratios_eff[ns][r].GetMaximum()
389 
390  if ymin==0:
391  ymin=0.75
392  if ymax<=ymin:
393  ymin=0.75*ymax
394  ymax=1.25*ymax
395 
396  if ymax<=ymin:
397  ymin=0.0
398  ymax=2.0
399 
400  hraxes = getRatioAxes(xmin,xmax,ymin,ymax)
401 
402  line = ROOT.TLine(xmin,1.0,xmax,1.0)
403  line.SetLineColor(1)
404  line.SetLineStyle(2)
405 
406  hraxes.Draw("")
407  ratios_eff[ns][r].Draw("PE,same")
408  line.Draw("same")
409 
410  can.cd()
411  pad1.Draw()
412  pad2.Draw()
413 
414  can.SaveAs("%s/%s.png"%(thisdir,outname));
415  can.SaveAs("%s/%s.pdf"%(thisdir,outname));
416 
417  can.Update()
418  can.Clear()
419 
420  del haxisMain
421  del hraxes
422  del pad1
423  del pad2
424  del can
425 
426 
427 
428  for r in range(len(hist_xratio[0][ns])):
429 
430  outname = hist[0][ns][r].GetName()
431 
432  can = ROOT.TCanvas("can_%s"%outname, "", 600, 600)
433  can.cd()
434 
435  pad1 = getCanvasMainPad(0)
436  pad1.SetTickx()
437  pad1.SetTicky()
438 
439  pad2 = getCanvasRatioPad(0)
440  pad2.SetTickx()
441  pad2.SetTicky()
442 
443  can.cd()
444  pad1.Draw()
445  pad1.cd()
446 
447  int0 = hist[0][ns][r].Integral(0,-1)
448  int1 = hist[1][ns][r].Integral(0,-1)
449  if int0>0 and doNorm:
450  hist[0][ns][r].Scale(1.0/int0)
451  if int1>0 and doNorm:
452  hist[1][ns][r].Scale(1.0/int1)
453 
454  means = [hist[0][ns][r].GetMean(),hist[1][ns][r].GetMean()]
455 
456  ttitle = hist[0][ns][r].GetTitle()
457  xmin = ratios_hist[ns][r].GetXaxis().GetBinLowEdge(1)
458  xmax = ratios_hist[ns][r].GetXaxis().GetBinUpEdge(ratios_hist[ns][r].GetNbinsX())
459  yminM = 0.0
460  ymaxM = hist[0][ns][r].GetMaximum()
461  if hist[1][ns][r].GetMaximum() > ymaxM:
462  ymaxM = hist[1][ns][r].GetMaximum()
463  ymaxM=1.5*ymaxM
464  if ymaxM<=yminM:
465  ymaxM = 1.0
466  xtitle = hist[0][ns][r].GetXaxis().GetTitle()
467  ytitle = "Fraction of tracks"
468  if not doNorm:
469  ytitle = "Number of tracks"
470 
471  haxisMain = ROOT.TH2D("haxisMain" ,ttitle,1,xmin ,xmax,1,yminM,ymaxM)
472 
473  haxisMain.GetXaxis().SetTitle(xtitle)
474  haxisMain.GetXaxis().SetTitleOffset(1.2)
475  haxisMain.GetYaxis().SetTitle(ytitle)
476  haxisMain.GetYaxis().SetTitleOffset(1.4)
477 
478  haxisMain.Draw()
479  hist[0][ns][r].Draw("PE,same")
480  hist[1][ns][r].Draw("PE,same")
481 
482  legend = ROOT.TLegend(0.6, 0.7, 0.87, 0.87);
483  legend.SetLineColor(0)
484  legend.SetFillColor(0)
485  legend.AddEntry(hist[0][ns][r], "%s [#mu=%.2f]"%(names[0],means[0]), "PL")
486  legend.AddEntry(hist[1][ns][r], "%s [#mu=%.2f]"%(names[1],means[1]), "PL")
487  legend.Draw("same")
488 
489  can.cd()
490  pad2.Draw()
491  pad2.cd()
492 
493  ymin = 0.9*ratios_hist[ns][r].GetMinimum()
494  ymax = 1.1*ratios_hist[ns][r].GetMaximum()
495 
496  if ymin==0:
497  ymin=0.75
498  if ymax<=ymin:
499  ymin=0.75*ymax
500  ymax=1.25*ymax
501 
502  if ymax<=ymin:
503  ymin=0.0
504  ymax=2.0
505 
506  hraxes = getRatioAxes(xmin,xmax,ymin,ymax)
507 
508  line = ROOT.TLine(xmin,1.0,xmax,1.0)
509  line.SetLineColor(1)
510  line.SetLineStyle(2)
511 
512  hraxes.Draw("")
513  ratios_hist[ns][r].Draw("PE,same")
514  line.Draw("same")
515 
516  can.cd()
517  pad1.Draw()
518  pad2.Draw()
519 
520  can.SaveAs("%s/%s.png"%(thisdir,outname));
521  can.SaveAs("%s/%s.pdf"%(thisdir,outname));
522 
523  can.Update()
524  can.Clear()
525 
526  del haxisMain
527  del hraxes
528  del pad1
529  del pad2
530  del can
531 
def getRatioAxes(xMin, xMax, yMin, yMax)
def getCanvasMainPad(logY)
Definition: compareTotals.py:7
def getCanvasRatioPad(logY)
if(threadIdxLocalY==0 &&threadIdxLocalX==0)