def makeEffFromFiles(var,dirs,inFileNum,inFileDen,binning): #offLF = getHist(inFile,"caloJets_matchedL", var,binning,ROOT.kBlack) numHist = getHist(inFileNum,dirs, var,binning,color=ROOT.kBlue) denHist = getHist(inFileDen,dirs, var,binning,color=ROOT.kBlue) #effHist = makeBayesRatio(num = numHist.Clone(), den = denHist.Clone()) #effHist = makeBayesLikeRatio(num = numHist.Clone(), den = denHist.Clone()) effHist = makeRatio(num = numHist.Clone(), den = denHist.Clone()) #print numHist.GetXaxis().GetTitle() effHist.GetXaxis().SetTitle(numHist.GetXaxis().GetTitle()) return effHist
def drawStackCompRatio(outName, dataInfo, MCInfo, yTitle, xTitle, rTitle, outDir, min=1, setLogy=1, x_min=None, x_max=None, cmsText="", lumiText=""): from ROOTHelp.Plotting import makeRatio histData = dataInfo[0].Clone() stacksum = MCInfo[0][0].Clone("tmp") stacksum.Reset() for h in MCInfo: stacksum.Add(h[0]) stacksum.Integral() scaleFactor = histData.Integral() / stacksum.Integral() stack = ROOT.THStack("TestStack", outName) for hMC in MCInfo: hMC[0].SetFillColor(hMC[2]) hMC[0].Scale(scaleFactor) stack.Add(hMC[0], 'sames') stacksum.Scale(scaleFactor) # hist2 = histInfo[1][0].Clone() # hist2.SetFillColor(ROOT.kYellow) maxY = max(histData.GetMaximum(), stack.GetMaximum()) if setLogy: histData.SetMaximum(4e0 * maxY) histData.SetMinimum(min) stack.SetMaximum(4e0 * maxY) stack.SetMinimum(min) else: stack.SetMaximum(1.4 * maxY) histData.SetMaximum(1.4 * maxY) histData.GetYaxis().SetTitle(yTitle) histData.GetXaxis().SetTitle(xTitle) xpos = 0.5 ypos = 0.69 xwidth = 0.3 ywidth = 0.05 * (len(MCInfo) + 1) leg = ROOT.TLegend(xpos, ypos, xpos + xwidth, ypos + ywidth) leg.AddEntry(histData, dataInfo[1], "PEL") for hMC in MCInfo: leg.AddEntry(hMC[0], hMC[1], "F") #leg.AddEntry(offLF,"Offline tracks light-flavor jets","L") #leg.AddEntry(hltLF,"HLT tracks light-flavor jets" ,"PEL") canvas = makeCanvas(outName, outName, width=600, height=600) split = 0.3 top_pad = ROOT.TPad("pad1", "The pad 80% of the height", 0, split, 1, 1, 0) bottom_pad = ROOT.TPad("pad2", "The pad 20% of the height", 0, 0, 1, split, 0) top_pad.Draw() bottom_pad.Draw() axissep = 0.02 top_pad.cd() top_pad.SetLogy(setLogy) top_pad.SetTopMargin(canvas.GetTopMargin() * 1.0 / (1.0 - split)) top_pad.SetBottomMargin(0.5 * axissep) top_pad.SetRightMargin(canvas.GetRightMargin()) top_pad.SetLeftMargin(canvas.GetLeftMargin()) top_pad.SetFillStyle(0) # transparent top_pad.SetBorderSize(0) stack.Draw() if x_max is not None and x_min is not None: stack.GetXaxis().SetRangeUser(x_min, x_max) histData.GetXaxis().SetRangeUser(x_min, x_max) stack.GetYaxis().SetTitle(yTitle) stack.GetXaxis().SetTitle(xTitle) stack.Draw("hist") #hltLF.SetMarkerSize(0.75) #hltLF.SetMarkerStyle(21) histData.Draw("same pe") #offBQ.Draw("hist same") #hltBQ.SetMarkerSize(0.75) #hltBQ.SetMarkerStyle(21) #hltBQ.Draw("same pe") leg.Draw("same") histRatio = makeRatio(num=histData.Clone(), den=stacksum.Clone()) cmsLines = getCMSText(xStart=0.225, yStart=0.85, subtext=cmsText, lumiText=lumiText) for cmsl in cmsLines: cmsl.Draw("same") bottom_pad.cd() bottom_pad.SetTopMargin(2 * axissep) bottom_pad.SetBottomMargin(canvas.GetBottomMargin() * 1.0 / split) bottom_pad.SetRightMargin(canvas.GetRightMargin()) bottom_pad.SetLeftMargin(canvas.GetLeftMargin()) bottom_pad.SetFillStyle(0) # transparent bottom_pad.SetBorderSize(0) ratio_axis = histData.Clone() #ratio_axis.GetYaxis().SetTitle("PF to Calo") ratio_axis.GetYaxis().SetTitle("Ratio") ratio_axis.GetXaxis().SetTitle(histData.GetXaxis().GetTitle()) ratio_axis.GetYaxis().SetNdivisions(507) rMin = 0 rMax = 2 ratio_axis.GetYaxis().SetRangeUser(rMin, rMax) histRatio.GetYaxis().SetRangeUser(rMin, rMax) histRatio.GetYaxis().SetTitle(rTitle) histRatio.Draw("PE") histRatio.Draw("PE same") oldSize = histRatio.GetMarkerSize() histRatio.SetMarkerSize(0) histRatio.DrawCopy("same e0") histRatio.SetMarkerSize(oldSize) histRatio.Draw("PE same") line = ROOT.TLine() if x_max is not None and x_min is not None: line.DrawLine(x_min, 1.0, x_max, 1.0) else: line.DrawLine(histData.GetXaxis().GetXmin(), 1.0, histData.GetXaxis().GetXmax(), 1.0) ndivs = [505, 503] pads = [top_pad, bottom_pad] factors = [0.8 / (1.0 - split), 0.7 / split] for i_pad, pad in enumerate(pads): factor = factors[i_pad] ndiv = ndivs[i_pad] prims = [p.GetName() for p in pad.GetListOfPrimitives()] # # Protection for scaling hists multiple times # procedHist = [] for name in prims: if name in procedHist: continue procedHist.append(name) h = pad.GetPrimitive(name) if isinstance( h, ROOT.TH1) or isinstance(h, ROOT.THStack) or isinstance( h, ROOT.TGraph) or isinstance( h, ROOT.TGraphErrors) or isinstance( h, ROOT.TGraphAsymmErrors): if isinstance(h, ROOT.TGraph) or isinstance( h, ROOT.THStack) or isinstance( h, ROOT.TGraphErrors) or isinstance( h, ROOT.TGraphAsymmErrors): h = h.GetHistogram() #print "factor is",factor,h.GetName(),split if i_pad == 1: h.SetLabelSize(h.GetLabelSize('Y') * factor, 'Y') h.SetTitleSize(h.GetTitleSize('X') * factor, 'X') h.SetTitleSize(h.GetTitleSize('Y') * factor, 'Y') h.SetTitleOffset(h.GetTitleOffset('Y') / factor, 'Y') if i_pad == 1: h.GetYaxis().SetNdivisions(ndiv) h.GetXaxis().SetNdivisions() if i_pad == 0: h.SetLabelSize(0.0, 'X') h.GetXaxis().SetTitle("") else: h.SetLabelSize(h.GetLabelSize('X') * factor, 'X') ## Trying to remove overlapping y-axis labels. Doesn't work. # h.GetYaxis().Set(4, h.GetYaxis().GetXmin(), h.GetYaxis().GetXmax()) # h.GetYaxis().SetBinLabel( h.GetYaxis().GetLast(), '') canvas.SaveAs(outDir + "/" + outName + ".pdf")
def drawCompRatio(outName,histInfo,yTitle,xTitle,rTitle,outDir,setLogy=1,yMax=None,leg="",cmsText="", lumiText=""): hist1 = histInfo[0][0].Clone() hist2 = histInfo[1][0].Clone() hist2.SetFillColor(ROOT.kYellow) if yMax: maxY = yMax else: maxY = max(hist1.GetMaximum(),hist2.GetMaximum()) if setLogy: hist2.SetMaximum(4e0*maxY) hist2.SetMinimum(1e-6) hist1.SetMaximum(4e0*maxY) hist1.SetMinimum(1e-6) else: hist2.SetMaximum(1.2*maxY) hist1.SetMaximum(1.2*maxY) hist2.GetYaxis().SetTitle(yTitle) hist2.GetXaxis().SetTitle(xTitle) hist1.GetYaxis().SetTitle(yTitle) hist1.GetXaxis().SetTitle(xTitle) histRatio = makeRatio(num = hist1.Clone(), den = hist2.Clone()) xpos = 0.5 ypos = 0.79 xwidth = 0.3 ywidth = 0.1 leg = ROOT.TLegend(xpos, ypos, xpos+xwidth, ypos+ywidth) leg.AddEntry(hist1,histInfo[0][1],"PEL") leg.AddEntry(hist2,histInfo[1][1] ,"F") #leg.AddEntry(offLF,"Offline tracks light-flavor jets","L") #leg.AddEntry(hltLF,"HLT tracks light-flavor jets" ,"PEL") canvas = makeCanvas(outName, outName, width=600, height=600) split=0.3 top_pad = ROOT.TPad("pad1", "The pad 80% of the height",0,split,1,1,0) bottom_pad = ROOT.TPad("pad2", "The pad 20% of the height",0,0,1,split,0) top_pad.Draw() bottom_pad.Draw() axissep = 0.02 top_pad.cd() top_pad.SetLogy(setLogy) top_pad.SetTopMargin(canvas.GetTopMargin()*1.0/(1.0-split)) top_pad.SetBottomMargin(0.5*axissep) top_pad.SetRightMargin(canvas.GetRightMargin()) top_pad.SetLeftMargin(canvas.GetLeftMargin()); top_pad.SetFillStyle(0) # transparent top_pad.SetBorderSize(0) hist2.Draw("hist") #hltLF.SetMarkerSize(0.75) # hltLF.SetMarkerSize(0.5) #hltLF.SetMarkerStyle(21) hist1.Draw("same pe") #offBQ.Draw("hist same") # hltBQ.SetMarkerSize(0.5) #hltBQ.SetMarkerStyle(21) #hltBQ.Draw("same pe") leg.Draw("same") cmsLines = getCMSText(xStart=0.225,yStart=0.85,subtext=cmsText,lumiText=lumiText) for cmsl in cmsLines: cmsl.Draw("same") bottom_pad.cd() bottom_pad.SetTopMargin(2*axissep) bottom_pad.SetBottomMargin(canvas.GetBottomMargin()*1.0/split) bottom_pad.SetRightMargin(canvas.GetRightMargin()) bottom_pad.SetLeftMargin(canvas.GetLeftMargin()); bottom_pad.SetFillStyle(0) # transparent bottom_pad.SetBorderSize(0) ratio_axis = hist1.Clone() #ratio_axis.GetYaxis().SetTitle("PF to Calo") ratio_axis.GetYaxis().SetTitle("Ratio") ratio_axis.GetXaxis().SetTitle(hist1.GetXaxis().GetTitle()) ratio_axis.GetYaxis().SetNdivisions(507) rMin = 0 rMax = 2 ratio_axis.GetYaxis().SetRangeUser(rMin, rMax) histRatio.GetYaxis().SetRangeUser(rMin, rMax) histRatio.GetYaxis().SetTitle(rTitle) histRatio.Draw("PE") histRatio.Draw("PE same") oldSize = histRatio.GetMarkerSize() histRatio.SetMarkerSize(0) histRatio.DrawCopy("same e0") # histRatio.SetMarkerSize(oldSize) histRatio.SetMarkerSize(0.5) histRatio.Draw("PE same") line = ROOT.TLine() line.DrawLine(hist1.GetXaxis().GetXmin(), 1.0, hist1.GetXaxis().GetXmax(), 1.0) ndivs=[505,503] pads = [top_pad, bottom_pad] factors = [0.8/(1.0-split), 0.7/split] for i_pad, pad in enumerate(pads): factor = factors[i_pad] ndiv = ndivs[i_pad] prims = [ p.GetName() for p in pad.GetListOfPrimitives() ] # # Protection for scaling hists multiple times # procedHist = [] for name in prims: if name in procedHist: continue procedHist.append(name) h = pad.GetPrimitive(name) if isinstance(h, ROOT.TH1) or isinstance(h, ROOT.THStack) or isinstance(h, ROOT.TGraph) or isinstance(h, ROOT.TGraphErrors) or isinstance(h, ROOT.TGraphAsymmErrors): if isinstance(h, ROOT.TGraph) or isinstance(h, ROOT.THStack) or isinstance(h, ROOT.TGraphErrors) or isinstance(h, ROOT.TGraphAsymmErrors): h = h.GetHistogram() #print "factor is",factor,h.GetName(),split if i_pad == 1: h.SetLabelSize(h.GetLabelSize('Y')*factor, 'Y') h.SetTitleSize(h.GetTitleSize('X')*factor, 'X') h.SetTitleSize(h.GetTitleSize('Y')*factor, 'Y') h.SetTitleOffset(h.GetTitleOffset('Y')/factor, 'Y') if i_pad == 1: h.GetYaxis().SetNdivisions(ndiv) h.GetXaxis().SetNdivisions() if i_pad == 0: h.SetLabelSize(0.0, 'X') h.GetXaxis().SetTitle("") else: h.SetLabelSize(h.GetLabelSize('X')*factor, 'X') ## Trying to remove overlapping y-axis labels. Doesn't work. # h.GetYaxis().Set(4, h.GetYaxis().GetXmin(), h.GetYaxis().GetXmax()) # h.GetYaxis().SetBinLabel( h.GetYaxis().GetLast(), '') canvas.SaveAs(outDir+"/"+outName+".pdf") canvas.SaveAs(outDir+"/"+outName+".png")
def doVarRatio(var, binning, xTitle, setLogy=1, minX=None, maxX=None, minY=None, etaRange=None): if o.doCaloJets: offLF = getHist(inFile, "offJets_matchedCalo_L", var, binning, ROOT.kBlack) hltLF = getHist(inFile, "offJets_matchedCaloJet_L", var, binning, ROOT.kBlack) offBQ = getHist(inFile, "offJets_matchedCalo_B", var, binning, ROOT.kRed) hltBQ = getHist(inFile, "offJets_matchedCaloJet_B", var, binning, ROOT.kRed) elif o.doPuppiJets: if etaRange: offLF = getHist(inFile, "offJets_matchedPuppi_L" + etaRange, var, binning, ROOT.kBlack) hltLF = getHist(inFile, "offJets_matchedPuppiJet_L" + etaRange, var, binning, ROOT.kBlack) offBQ = getHist(inFile, "offJets_matchedPuppi_B" + etaRange, var, binning, ROOT.kRed) hltBQ = getHist(inFile, "offJets_matchedPuppiJet_B" + etaRange, var, binning, ROOT.kRed) else: offLF = getHist(inFile, "offJets_matchedPuppi_L", var, binning, ROOT.kBlack) hltLF = getHist(inFile, "offJets_matchedPuppiJet_L", var, binning, ROOT.kBlack) offBQ = getHist(inFile, "offJets_matchedPuppi_B", var, binning, ROOT.kRed) hltBQ = getHist(inFile, "offJets_matchedPuppiJet_B", var, binning, ROOT.kRed) else: if etaRange: offLF = getHist(inFile, "offJets_matched_L" + etaRange, var, binning, ROOT.kBlack) hltLF = getHist(inFile, "offJets_matchedJet_L" + etaRange, var, binning, ROOT.kBlack) offBQ = getHist(inFile, "offJets_matched_B" + etaRange, var, binning, ROOT.kRed) hltBQ = getHist(inFile, "offJets_matchedJet_B" + etaRange, var, binning, ROOT.kRed) else: offLF = getHist(inFile, "offJets_matched_L", var, binning, ROOT.kBlack) hltLF = getHist(inFile, "offJets_matchedJet_L", var, binning, ROOT.kBlack) offBQ = getHist(inFile, "offJets_matched_B", var, binning, ROOT.kRed) hltBQ = getHist(inFile, "offJets_matchedJet_B", var, binning, ROOT.kRed) maxY = max(offLF.GetMaximum(), offBQ.GetMaximum(), hltLF.GetMaximum(), hltBQ.GetMaximum()) if setLogy: offLF.SetMaximum(4e0 * maxY) offLF.SetMinimum(1.01e-5) else: offLF.SetMaximum(1.2 * maxY) if not minY == None: offLF.SetMinimum(minY) offLF.GetYaxis().SetTitle("Simulated Tracks") offLF.GetXaxis().SetTitle(xTitle) if maxX: offLF.GetXaxis().SetRangeUser(minX, maxX) offBQ.GetXaxis().SetRangeUser(minX, maxX) hltLF.GetXaxis().SetRangeUser(minX, maxX) hltBQ.GetXaxis().SetRangeUser(minX, maxX) LFRatio = makeRatio(num=hltLF.Clone(), den=offLF.Clone()) BQRatio = makeRatio(num=hltBQ.Clone(), den=offBQ.Clone()) xpos = 0.2 ypos = 0.07 xwidth = 0.7 ywidth = 0.1 leg = ROOT.TLegend(xpos, ypos, xpos + xwidth, ypos + ywidth) leg.SetNColumns(2) leg.AddEntry(offBQ, "Offline tracks b-quark jets", "L") leg.AddEntry(offLF, "Offline tracks light-flavor jets", "L") leg.AddEntry(hltBQ, "HLT tracks b-quark jets", "PEL") leg.AddEntry(hltLF, "HLT tracks light-flavor jets", "PEL") canvas = makeCanvas(var, var, width=600, height=600) split = 0.3 top_pad = ROOT.TPad("pad1", "The pad 80% of the height", 0, split, 1, 1, 0) bottom_pad = ROOT.TPad("pad2", "The pad 20% of the height", 0, 0, 1, split, 0) top_pad.Draw() bottom_pad.Draw() axissep = 0.02 top_pad.cd() top_pad.SetLogy(setLogy) top_pad.SetTopMargin(canvas.GetTopMargin() * 1.0 / (1.0 - split)) top_pad.SetBottomMargin(0.5 * axissep) top_pad.SetRightMargin(canvas.GetRightMargin()) top_pad.SetLeftMargin(canvas.GetLeftMargin()) top_pad.SetFillStyle(0) # transparent top_pad.SetBorderSize(0) if var in maxDict.keys(): offLF.GetXaxis().SetRangeUser(offLF.GetXaxis().GetXmin(), maxDict[var]) offLF.Draw("hist") # hltLF.SetMarkerSize(0.75) hltLF.SetMarkerSize(0.5) #hltLF.SetMarkerStyle(21) hltLF.Draw("same pe") offBQ.Draw("hist same") hltBQ.SetMarkerSize(0.5) #hltBQ.SetMarkerStyle(21) hltBQ.Draw("same pe") leg.Draw("same") cmsLines = getCMSText(xStart=0.2, yStart=0.85, subtext=o.cmsText) for cmsl in cmsLines: cmsl.Draw("same") bottom_pad.cd() bottom_pad.SetTopMargin(2 * axissep) bottom_pad.SetBottomMargin(canvas.GetBottomMargin() * 1.0 / split) bottom_pad.SetRightMargin(canvas.GetRightMargin()) bottom_pad.SetLeftMargin(canvas.GetLeftMargin()) bottom_pad.SetFillStyle(0) # transparent bottom_pad.SetBorderSize(0) ratio_axis = offLF.Clone() #ratio_axis.GetYaxis().SetTitle("PF to Calo") ratio_axis.GetYaxis().SetTitle("HLT to Offline") ratio_axis.GetXaxis().SetTitle(offLF.GetXaxis().GetTitle()) ratio_axis.GetYaxis().SetNdivisions(507) rMin = 0 rMax = 2 if var in maxDict.keys(): LFRatio.GetXaxis().SetRangeUser(LFRatio.GetXaxis().GetXmin(), maxDict[var]) ratio_axis.GetYaxis().SetRangeUser(rMin, rMax) LFRatio.GetYaxis().SetRangeUser(rMin, rMax) #LFRatio.GetYaxis().SetTitle("Calo to Offline") #LFRatio.GetYaxis().SetTitle("PF to Calo") LFRatio.GetYaxis().SetTitle("HLT to Offline") LFRatio.Draw("PE") LFRatio.Draw("PE same") # oldSize = LFRatio.GetMarkerSize() oldSize = hltLF.GetMarkerSize() LFRatio.SetMarkerSize(0) LFRatio.DrawCopy("same e0") LFRatio.SetMarkerSize(oldSize) LFRatio.Draw("PE same") BQRatio.Draw("PE same") BQRatio.Draw("PE same") # oldSize = BQRatio.GetMarkerSize() oldSize = hltBQ.GetMarkerSize() BQRatio.SetMarkerSize(0) BQRatio.DrawCopy("same e0") BQRatio.SetMarkerSize(oldSize) BQRatio.Draw("PE same") line = ROOT.TLine() line.DrawLine(offLF.GetXaxis().GetXmin(), 1.0, offLF.GetXaxis().GetXmax(), 1.0) ndivs = [505, 503] pads = [top_pad, bottom_pad] factors = [0.8 / (1.0 - split), 0.7 / split] for i_pad, pad in enumerate(pads): factor = factors[i_pad] ndiv = ndivs[i_pad] prims = [p.GetName() for p in pad.GetListOfPrimitives()] # # Protection for scaling hists multiple times # procedHist = [] for name in prims: if name in procedHist: continue procedHist.append(name) h = pad.GetPrimitive(name) if isinstance( h, ROOT.TH1) or isinstance(h, ROOT.THStack) or isinstance( h, ROOT.TGraph) or isinstance( h, ROOT.TGraphErrors) or isinstance( h, ROOT.TGraphAsymmErrors): if isinstance(h, ROOT.TGraph) or isinstance( h, ROOT.THStack) or isinstance( h, ROOT.TGraphErrors) or isinstance( h, ROOT.TGraphAsymmErrors): h = h.GetHistogram() #print "factor is",factor,h.GetName(),split if i_pad == 1: h.SetLabelSize(h.GetLabelSize('Y') * factor, 'Y') h.SetTitleSize(h.GetTitleSize('X') * factor, 'X') h.SetTitleSize(h.GetTitleSize('Y') * factor, 'Y') h.SetTitleOffset(h.GetTitleOffset('Y') / factor, 'Y') if i_pad == 1: h.GetYaxis().SetNdivisions(ndiv) h.GetXaxis().SetNdivisions() if i_pad == 0: h.SetLabelSize(0.0, 'X') h.GetXaxis().SetTitle("") else: h.SetLabelSize(h.GetLabelSize('X') * factor, 'X') ## Trying to remove overlapping y-axis labels. Doesn't work. # h.GetYaxis().Set(4, h.GetYaxis().GetXmin(), h.GetYaxis().GetXmax()) # h.GetYaxis().SetBinLabel( h.GetYaxis().GetLast(), '') ##xatlas, yatlas = 0.18, 0.88 ##atlas = ROOT.TLatex(xatlas+0.01, yatlas, "ATLAS") ###simulation = ROOT.TLatex(xatlas+0.11, yatlas, "Simulation Internal") ##simulation = ROOT.TLatex(xatlas+0.11, yatlas, "Simulation Preliminary") ##lumi = ROOT.TLatex(xatlas+0.01, yatlas-0.05, "#sqrt{s}=13 TeV, t#bar{t}") ##jetText = ROOT.TLatex(xatlas+0.02, yatlas-0.1, "p_{T}^{jet} > 40 GeV, |#eta^{jet}| < 2.5" ) ##wm = [atlas, simulation, lumi, jetText] #watermarks = drawWaterMarks(wm) #varName = var.replace("tracks/","").replace("btags/","btags_") varName = var.replace("tracks/", "track_").replace("btags/", "btag_").replace( "btags_noV0/", "btag_noV0_") if etaRange: canvas.SaveAs(o.outDir + etaRange + "/" + varName + ".pdf") canvas.SaveAs(o.outDir + etaRange + "/" + varName + ".png") else: canvas.SaveAs(o.outDir + "/" + varName + ".pdf") canvas.SaveAs(o.outDir + "/" + varName + ".png")