Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:28:23

0001 import os,sys
0002 import ROOT
0003 import copy
0004 
0005 doNorm = True
0006 
0007 def getCanvasMainPad( logY ):
0008   pad1 = ROOT.TPad("pad1", "pad1", 0, 0.2, 1, 1)
0009   pad1.SetBottomMargin(0.15)
0010   if( logY ):
0011     pad1.SetLogy()
0012   return pad1
0013 
0014 def getCanvasRatioPad( logY ):
0015   pad2 = ROOT.TPad("pad2", "pad2", 0, 0, 1, 0.21)
0016   pad2.SetTopMargin(0.05)
0017   pad2.SetBottomMargin(0.1)
0018   return pad2
0019 
0020 def getRatioAxes( xMin, xMax, yMin, yMax ):
0021   h2_axes_ratio = ROOT.TH2D("axes_ratio", "", 10, xMin, xMax, 10, yMin, yMax )
0022   h2_axes_ratio.SetStats(0)
0023   h2_axes_ratio.GetXaxis().SetLabelSize(0.00)
0024   h2_axes_ratio.GetXaxis().SetTickLength(0.09)
0025   h2_axes_ratio.GetYaxis().SetNdivisions(5,5,0)
0026   h2_axes_ratio.GetYaxis().SetTitleSize(0.13)
0027   h2_axes_ratio.GetYaxis().SetTitleOffset(0.37)
0028   h2_axes_ratio.GetYaxis().SetLabelSize(0.13)
0029   h2_axes_ratio.GetYaxis().SetTitle("Ratio")
0030   return h2_axes_ratio
0031 
0032 
0033 dirnames = []
0034 dirnames.append(sys.argv[1]) #original (1)
0035 dirnames.append(sys.argv[2]) #updated (2)
0036 
0037 outdir = sys.argv[3]
0038 if not os.path.exists(outdir):
0039   os.makedirs(outdir)
0040   os.system("cp web/index.php %s"%(outdir))
0041 
0042 names = ["Reference", "Other"]
0043 if len(sys.argv)==5 and sys.argv[4]=="vsCMSSW":
0044   names = ["CMSSW", "mkFit"]
0045 
0046 if len(sys.argv)==6:
0047   names = [sys.argv[4], sys.argv[5]]
0048 
0049 colors = [1,2]
0050 
0051 fnames = []
0052 fs = []
0053 for d in range(0, len(dirnames)):
0054   fnames.append(dirnames[d]+"/plots.root")
0055   fs.append(ROOT.TFile.Open(fnames[d]))
0056 
0057 subdirs=[]
0058 
0059 eff_obj   = []
0060 eff_pass  = []
0061 eff_tot   = []
0062 eff_ratio = []
0063 #
0064 hist      = []
0065 #
0066 hist_xratio = []
0067 eff_xratio  = []
0068 
0069 for d in range(0, len(fnames)):
0070   
0071   fs[d].cd()
0072 
0073   for dkey in ROOT.gDirectory.GetListOfKeys():
0074     if not dkey.IsFolder():
0075       continue
0076     if d<=0:
0077       subdirs.append(dkey.GetName())
0078 
0079   eff_obj_s   = []
0080   eff_pass_s  = []
0081   eff_tot_s   = []
0082   eff_ratio_s = []
0083   #
0084   hist_s      = []
0085   
0086   subhist = []
0087   subrate = []
0088   
0089   for subdir in subdirs:
0090     print "In subdir %s:"%subdir
0091     fs[d].cd(subdir)
0092 
0093     thiseff_obj  = []
0094     thiseff_pass = []
0095     thiseff_tot  = []
0096     thiseff      = []      
0097     #
0098     thishist = []
0099     #
0100     nh=0
0101     ne=0
0102     #
0103     for key in ROOT.gDirectory.GetListOfKeys():
0104       obj = key.ReadObj()
0105       if obj.IsA().InheritsFrom("TH1"):
0106         h = obj
0107         #print "Found TH1 %s"%h.GetName()
0108           
0109         thishist.append(h)
0110         thishist[nh].Sumw2()
0111         thishist[nh].SetLineColor(colors[d])
0112         thishist[nh].SetMarkerColor(colors[d])
0113         thishist[nh].SetMarkerSize(0.3)
0114         thishist[nh].SetMarkerStyle(20)
0115         thishist[nh].SetStats(0)
0116         
0117         nh=nh+1
0118 
0119       if obj.IsA().InheritsFrom("TEfficiency"):
0120         e = obj
0121         #print "Found TEfficiency %s"%e.GetName()
0122         
0123         thiseff_obj .append(e)
0124         thiseff_obj[ne].SetLineColor(colors[d])
0125         thiseff_obj[ne].SetMarkerColor(colors[d])
0126         thiseff_obj[ne].SetMarkerSize(0.3)
0127         thiseff_obj[ne].SetMarkerStyle(20)
0128         thiseff_pass.append(e.GetPassedHistogram())
0129         thiseff_tot .append(e.GetTotalHistogram())
0130         thiseff_pass[ne].Sumw2()
0131         thiseff_tot[ne] .Sumw2()
0132         effname = "%s_rate"%(thiseff_pass[ne].GetName())
0133         auxeff = thiseff_pass[ne].Clone(effname)
0134         auxeff.Divide(thiseff_pass[ne],thiseff_tot[ne],1.0,1.0,"B")
0135         thiseff.append(auxeff)
0136         thiseff[ne].SetLineColor(colors[d])
0137         thiseff[ne].SetMarkerColor(colors[d])
0138         thiseff[ne].SetMarkerSize(0.3)
0139         thiseff[ne].SetMarkerStyle(20)
0140         thiseff[ne].SetStats(0)
0141         
0142         ne=ne+1
0143 
0144     hist_s     .append(thishist)
0145     #
0146     eff_ratio_s.append(thiseff)
0147     eff_pass_s .append(thiseff_pass)
0148     eff_tot_s  .append(thiseff_tot)
0149     eff_obj_s  .append(thiseff_obj)
0150         
0151   hist       .append(hist_s)
0152   hist_xratio.append(hist_s)
0153   #
0154   eff_ratio .append(eff_ratio_s)
0155   eff_xratio.append(eff_ratio_s)
0156   eff_pass  .append(eff_pass_s)
0157   eff_tot   .append(eff_tot_s)
0158   eff_obj   .append(eff_obj_s)
0159   
0160 ratios_hist = []
0161 ratios_eff  = []
0162 for dd in range(len(subdirs)):
0163   
0164   thisratio = []
0165   for r in range(len(hist_xratio[0][dd])):
0166     auxratio = hist_xratio[1][dd][r].Clone("num")
0167     auxden   = hist_xratio[0][dd][r].Clone("den")
0168     intnum   = auxratio.Integral(0,-1)
0169     intden   = auxden  .Integral(0,-1)
0170     if intnum>0:
0171       auxratio.Scale(1.0/intnum)
0172     if intden>0:
0173       auxden.Scale(1.0/intden)
0174       auxratio.Divide(auxden)
0175     auxratio.SetName("ratio")
0176     thisratio.append(auxratio)
0177     thisratio[r].GetYaxis().SetTitle("Ratio")
0178     thisratio[r].SetLineColor(colors[1])
0179     thisratio[r].SetMarkerColor(colors[1])
0180     thisratio[r].SetMarkerSize(0)
0181     thisratio[r].SetStats(0)
0182   ratios_hist.append(thisratio)
0183   
0184   thisratio = []
0185   for r in range(len(eff_xratio[0][dd])):
0186     auxratio = eff_xratio[1][dd][r].Clone(  "numerator")
0187     auxden   = eff_xratio[0][dd][r].Clone("denominator")
0188     auxratio.Divide(auxden)
0189     auxratio.SetName("ratio")
0190     thisratio.append(auxratio)
0191     thisratio[r].GetYaxis().SetTitle("Ratio")
0192     thisratio[r].SetLineColor(colors[1])
0193     thisratio[r].SetMarkerColor(colors[1])
0194     thisratio[r].SetMarkerSize(0)
0195     thisratio[r].SetStats(0)
0196   ratios_eff.append(thisratio)
0197 
0198 ### Drawing
0199 ROOT.gStyle.SetOptStat(0)
0200 
0201 outsubdir = []
0202 for ns,subdir in enumerate(subdirs):
0203   thisdir = "%s/%s"%(outdir,subdir)
0204   outsubdir.append(thisdir)
0205   if not os.path.exists(thisdir):
0206     os.mkdir(thisdir)
0207     os.system("cp web/index.php %s"%(thisdir))
0208 
0209   for r in range(len(eff_xratio[0][ns])): 
0210       
0211     outname = eff_obj[0][ns][r].GetName()
0212     
0213     can = ROOT.TCanvas("can_%s"%outname, "", 600, 600)
0214     can.cd()
0215     
0216     pad1 = getCanvasMainPad(0)
0217     pad1.SetTickx()
0218     pad1.SetTicky()
0219     
0220     pad2 = getCanvasRatioPad(0)
0221     pad2.SetTickx()
0222     pad2.SetTicky()
0223     
0224     can.cd()
0225     pad1.Draw()
0226     pad1.cd()
0227     
0228     ttitle = eff_obj[0][ns][r].GetTitle()
0229     xmin = ratios_eff[ns][r].GetXaxis().GetBinLowEdge(1)
0230     xmax = ratios_eff[ns][r].GetXaxis().GetBinUpEdge(ratios_eff[ns][r].GetNbinsX())
0231     yminM = 0.0
0232     ymaxM = 1.2
0233     #if "dr" in outname or "ineff" in outname:
0234     #  ymaxM = 0.50
0235     xtitle = ratios_eff[ns][r].GetXaxis().GetTitle()
0236     ytitle = "Efficiency"
0237     if "dr" in outname:
0238       ytitle = "Duplicate rate"
0239     elif "fr" in outname:
0240       ytitle = "Fake rate"
0241     elif "ineff" in outname:
0242       ytitle = "Inefficiency"
0243       
0244     haxisMain  = ROOT.TH2D("haxisMain" ,ttitle,1,xmin ,xmax,1,yminM,ymaxM)
0245     
0246     haxisMain.GetXaxis().SetTitle(xtitle)
0247     haxisMain.GetXaxis().SetTitleOffset(1.2)
0248     haxisMain.GetYaxis().SetTitle(ytitle)
0249     haxisMain.GetYaxis().SetTitleOffset(1.4)
0250     
0251     haxisMain.Draw()
0252     eff_obj[0][ns][r].Draw("PE,same")
0253     eff_obj[1][ns][r].Draw("PE,same")
0254     
0255     legend = ROOT.TLegend(0.7,0.7, 0.87, 0.87);
0256     legend.SetLineColor(0)
0257     legend.SetFillColor(0)
0258     legend.AddEntry(eff_obj[0][ns][r], names[0], "PL")
0259     legend.AddEntry(eff_obj[1][ns][r], names[1], "PL")
0260     legend.Draw("same")
0261     
0262     can.cd()
0263     pad2.Draw()
0264     pad2.cd()
0265     
0266     ymin = 0.9*ratios_eff[ns][r].GetMinimum()
0267     ymax = 1.1*ratios_eff[ns][r].GetMaximum()
0268     
0269     if ymin==0:
0270       ymin=0.75
0271       if ymax<=ymin:
0272         ymin=0.75*ymax
0273         ymax=1.25*ymax
0274         
0275     if ymax<=ymin:
0276       ymin=0.0
0277       ymax=2.0
0278     
0279     hraxes = getRatioAxes(xmin,xmax,ymin,ymax)
0280     
0281     line = ROOT.TLine(xmin,1.0,xmax,1.0)
0282     line.SetLineColor(1)
0283     line.SetLineStyle(2)
0284     
0285     hraxes.Draw("")
0286     ratios_eff[ns][r].Draw("PE,same")
0287     line.Draw("same")
0288     
0289     can.cd()
0290     pad1.Draw()
0291     pad2.Draw()
0292     
0293     can.SaveAs("%s/%s.png"%(thisdir,outname));
0294     can.SaveAs("%s/%s.pdf"%(thisdir,outname));
0295     
0296     can.Update()
0297     can.Clear()
0298 
0299     tot        = [eff_tot[0][ns][r].Integral(), eff_tot[1][ns][r].Integral()]
0300     passing    = [eff_pass[0][ns][r].Integral(), eff_pass[1][ns][r].Integral()]
0301     efficiency = []
0302     reldiff    = []
0303     for d in range(0,len(tot)):
0304       if tot[d]>0:
0305         efficiency.append(passing[d]/tot[d])
0306       else:
0307         efficiency.append(0.0)
0308       if efficiency[0]>0:
0309         reldiff.append(efficiency[d]/efficiency[0])
0310       else:
0311         reldiff.append(0.0)
0312 
0313     fo = open("%s/%s.log"%(thisdir,outname),"w+")
0314     fo.write( "Totals:" )
0315     for d in range(0,len(tot)):
0316       fo.write( " %d " % int(tot[d]) ),
0317     fo.write( "\nPassing:" )
0318     for d in range(0,len(tot)):
0319       fo.write( " %d " % int(passing[d]) ),
0320     fo.write( "\nRate:" )
0321     for d in range(0,len(tot)):
0322       fo.write( " %0.4f " % efficiency[d] ),
0323     fo.write( "\nRatio(/reference):" )
0324     for d in range(0,len(tot)):
0325       fo.write( " %0.4f " % reldiff[d] ),
0326     fo.write( "\n" )
0327 
0328 
0329     if "_pt_" in outname:
0330       outname = outname+"_logx"
0331       
0332       can = ROOT.TCanvas("can_%s"%outname, "", 600, 600)
0333       can.cd()
0334       
0335       pad1 = getCanvasMainPad(0)
0336       pad1.SetTickx()
0337       pad1.SetTicky()
0338       pad1.SetLogx()
0339       
0340       pad2 = getCanvasRatioPad(0)
0341       pad2.SetTickx()
0342       pad2.SetTicky()
0343       pad2.SetLogx()
0344       
0345       can.cd()
0346       pad1.Draw()
0347       pad1.cd()
0348       
0349       ttitle = eff_obj[0][ns][r].GetTitle()
0350       xmin = 0.1
0351       xmax = ratios_eff[ns][r].GetXaxis().GetBinUpEdge(ratios_eff[ns][r].GetNbinsX())
0352       yminM = 0.0
0353       ymaxM = 1.2
0354       #if "dr" in outname or "ineff" in outname:
0355       #  ymaxM = 0.50
0356       xtitle = ratios_eff[ns][r].GetXaxis().GetTitle()
0357       ytitle = "Efficiency"
0358       if "dr" in outname:
0359         ytitle = "Duplicate rate"
0360       elif "fr" in outname:
0361         ytitle = "Fake rate"
0362       elif "ineff" in outname:
0363         ytitle = "Inefficiency"
0364         
0365       haxisMain  = ROOT.TH2D("haxisMain" ,ttitle,1,xmin,xmax,1,yminM,ymaxM)
0366       
0367       haxisMain.GetXaxis().SetTitle(xtitle)
0368       haxisMain.GetXaxis().SetTitleOffset(1.2)
0369       haxisMain.GetYaxis().SetTitle(ytitle)
0370       haxisMain.GetYaxis().SetTitleOffset(1.4)
0371       
0372       haxisMain.Draw()
0373       eff_obj[0][ns][r].Draw("PE,same")
0374       eff_obj[1][ns][r].Draw("PE,same")
0375       
0376       legend = ROOT.TLegend(0.7, 0.7, 0.87, 0.87);
0377       legend.SetLineColor(0)
0378       legend.SetFillColor(0)
0379       legend.AddEntry(eff_obj[0][ns][r], names[0], "PL")
0380       legend.AddEntry(eff_obj[1][ns][r], names[1], "PL")
0381       legend.Draw("same")
0382       
0383       can.cd()
0384       pad2.Draw()
0385       pad2.cd()
0386       
0387       ymin = 0.9*ratios_eff[ns][r].GetMinimum()
0388       ymax = 1.1*ratios_eff[ns][r].GetMaximum()
0389       
0390       if ymin==0:
0391         ymin=0.75
0392         if ymax<=ymin:
0393           ymin=0.75*ymax
0394           ymax=1.25*ymax
0395         
0396       if ymax<=ymin:
0397         ymin=0.0
0398         ymax=2.0
0399         
0400       hraxes = getRatioAxes(xmin,xmax,ymin,ymax)
0401       
0402       line = ROOT.TLine(xmin,1.0,xmax,1.0)
0403       line.SetLineColor(1)
0404       line.SetLineStyle(2)
0405       
0406       hraxes.Draw("")
0407       ratios_eff[ns][r].Draw("PE,same")
0408       line.Draw("same")
0409       
0410       can.cd()
0411       pad1.Draw()
0412       pad2.Draw()
0413       
0414       can.SaveAs("%s/%s.png"%(thisdir,outname));
0415       can.SaveAs("%s/%s.pdf"%(thisdir,outname));
0416 
0417       can.Update()
0418       can.Clear()
0419       
0420     del haxisMain
0421     del hraxes
0422     del pad1
0423     del pad2
0424     del can
0425 
0426   ###
0427 
0428   for r in range(len(hist_xratio[0][ns])): 
0429       
0430     outname = hist[0][ns][r].GetName()
0431     
0432     can = ROOT.TCanvas("can_%s"%outname, "", 600, 600)
0433     can.cd()
0434     
0435     pad1 = getCanvasMainPad(0)
0436     pad1.SetTickx()
0437     pad1.SetTicky()
0438     
0439     pad2 = getCanvasRatioPad(0)
0440     pad2.SetTickx()
0441     pad2.SetTicky()
0442     
0443     can.cd()
0444     pad1.Draw()
0445     pad1.cd()
0446     
0447     int0   = hist[0][ns][r].Integral(0,-1)
0448     int1   = hist[1][ns][r].Integral(0,-1)
0449     if int0>0 and doNorm:
0450       hist[0][ns][r].Scale(1.0/int0)
0451     if int1>0 and doNorm:
0452       hist[1][ns][r].Scale(1.0/int1)
0453     
0454     means = [hist[0][ns][r].GetMean(),hist[1][ns][r].GetMean()]
0455 
0456     ttitle = hist[0][ns][r].GetTitle()
0457     xmin = ratios_hist[ns][r].GetXaxis().GetBinLowEdge(1)
0458     xmax = ratios_hist[ns][r].GetXaxis().GetBinUpEdge(ratios_hist[ns][r].GetNbinsX())
0459     yminM = 0.0
0460     ymaxM = hist[0][ns][r].GetMaximum()
0461     if hist[1][ns][r].GetMaximum() > ymaxM:
0462       ymaxM = hist[1][ns][r].GetMaximum()
0463     ymaxM=1.5*ymaxM
0464     if ymaxM<=yminM:
0465       ymaxM = 1.0
0466     xtitle = hist[0][ns][r].GetXaxis().GetTitle()
0467     ytitle = "Fraction of tracks"
0468     if not doNorm:
0469       ytitle = "Number of tracks"
0470 
0471     haxisMain  = ROOT.TH2D("haxisMain" ,ttitle,1,xmin ,xmax,1,yminM,ymaxM)
0472     
0473     haxisMain.GetXaxis().SetTitle(xtitle)
0474     haxisMain.GetXaxis().SetTitleOffset(1.2)
0475     haxisMain.GetYaxis().SetTitle(ytitle)
0476     haxisMain.GetYaxis().SetTitleOffset(1.4)
0477 
0478     haxisMain.Draw()
0479     hist[0][ns][r].Draw("PE,same")
0480     hist[1][ns][r].Draw("PE,same")
0481     
0482     legend = ROOT.TLegend(0.6, 0.7, 0.87, 0.87);
0483     legend.SetLineColor(0)
0484     legend.SetFillColor(0)
0485     legend.AddEntry(hist[0][ns][r], "%s [#mu=%.2f]"%(names[0],means[0]), "PL")
0486     legend.AddEntry(hist[1][ns][r], "%s [#mu=%.2f]"%(names[1],means[1]), "PL")
0487     legend.Draw("same")
0488     
0489     can.cd()
0490     pad2.Draw()
0491     pad2.cd()
0492     
0493     ymin = 0.9*ratios_hist[ns][r].GetMinimum()
0494     ymax = 1.1*ratios_hist[ns][r].GetMaximum()
0495     
0496     if ymin==0:
0497       ymin=0.75
0498       if ymax<=ymin:
0499         ymin=0.75*ymax
0500         ymax=1.25*ymax
0501         
0502     if ymax<=ymin:
0503       ymin=0.0
0504       ymax=2.0
0505     
0506     hraxes = getRatioAxes(xmin,xmax,ymin,ymax)
0507     
0508     line = ROOT.TLine(xmin,1.0,xmax,1.0)
0509     line.SetLineColor(1)
0510     line.SetLineStyle(2)
0511     
0512     hraxes.Draw("")
0513     ratios_hist[ns][r].Draw("PE,same")
0514     line.Draw("same")
0515     
0516     can.cd()
0517     pad1.Draw()
0518     pad2.Draw()
0519     
0520     can.SaveAs("%s/%s.png"%(thisdir,outname));
0521     can.SaveAs("%s/%s.pdf"%(thisdir,outname));
0522     
0523     can.Update()
0524     can.Clear()
0525 
0526     del haxisMain
0527     del hraxes
0528     del pad1
0529     del pad2
0530     del can
0531