def doRatioHists(pspec,pmap,total,totalSyst,maxRange,fitRatio=False): numkey = "data" if "data" not in pmap: if len(pmap) == 4 and 'signal' in pmap and 'background' in pmap: # do this first total.GetXaxis().SetLabelOffset(999) ## send them away total.GetXaxis().SetTitleOffset(999) ## in outer space total.GetYaxis().SetLabelSize(0.05) # then we can overwrite total with background numkey = 'signal' total = pmap['background'] totalSyst = pmap['background'] else: return (None,None,None,None) ratio = None if hasattr(pmap[numkey], 'poissonGraph'): ratio = pmap[numkey].poissonGraph.Clone("data_div"); for i in xrange(ratio.GetN()): x = ratio.GetX()[i] div = total.GetBinContent(total.GetXaxis().FindBin(x)) ratio.SetPoint(i, x, ratio.GetY()[i]/div if div > 0 else 0) ratio.SetPointError(i, ratio.GetErrorXlow(i), ratio.GetErrorXhigh(i), ratio.GetErrorYlow(i)/div if div > 0 else 0, ratio.GetErrorYhigh(i)/div if div > 0 else 0) else: ratio = pmap[numkey].Clone("data_div"); ratio.Divide(total) unity = totalSyst.Clone("sim_div"); unity0 = total.Clone("sim_div"); rmin, rmax = 1,1 for b in xrange(1,unity.GetNbinsX()+1): e,e0,n = unity.GetBinError(b), unity0.GetBinError(b), unity.GetBinContent(b) unity.SetBinContent(b, 1 if n > 0 else 0) unity.SetBinError(b, e/n if n > 0 else 0) unity0.SetBinContent(b, 1 if n > 0 else 0) unity0.SetBinError(b, e0/n if n > 0 else 0) rmin = min([ rmin, 1-2*e/n if n > 0 else 1]) rmax = max([ rmax, 1+2*e/n if n > 0 else 1]) if ratio.ClassName() != "TGraphAsymmErrors": for b in xrange(1,unity.GetNbinsX()+1): if ratio.GetBinContent(b) == 0: continue rmin = min([ rmin, ratio.GetBinContent(b) - 2*ratio.GetBinError(b) ]) rmax = max([ rmax, ratio.GetBinContent(b) + 2*ratio.GetBinError(b) ]) else: for i in xrange(ratio.GetN()): rmin = min([ rmin, ratio.GetY()[i] - 2*ratio.GetErrorYlow(i) ]) rmax = max([ rmax, ratio.GetY()[i] + 2*ratio.GetErrorYhigh(i) ]) if rmin < maxRange[0]: rmin = maxRange[0]; if rmax > maxRange[1]: rmax = maxRange[1]; if (rmax > 3 and rmax <= 3.4): rmax = 3.4 if (rmax > 2 and rmax <= 2.4): rmax = 2.4 unity.SetFillStyle(1001); unity.SetFillColor(ROOT.kCyan); unity.SetMarkerStyle(1); unity.SetMarkerColor(ROOT.kCyan); unity0.SetFillStyle(1001); unity0.SetFillColor(53); unity0.SetMarkerStyle(1); unity0.SetMarkerColor(53); ROOT.gStyle.SetErrorX(0.5); unity.Draw("E2"); if fitRatio: from CMGTools.TTHAnalysis.tools.plotDecorations import fitTGraph fitTGraph(ratio,order=fitRatio) unity.SetFillStyle(3013); unity0.SetFillStyle(3013); unity.Draw("AXIS SAME"); unity0.Draw("E2 SAME"); else: if total != totalSyst: unity0.Draw("E2 SAME"); unity.GetYaxis().SetRangeUser(rmin,rmax); unity.GetXaxis().SetTitleSize(0.14) unity.GetYaxis().SetTitleSize(0.14) unity.GetXaxis().SetLabelSize(0.11) unity.GetYaxis().SetLabelSize(0.11) unity.GetYaxis().SetNdivisions(505) unity.GetYaxis().SetDecimals(True) unity.GetYaxis().SetTitle("Data/Pred.") unity.GetYaxis().SetTitleOffset(0.52); total.GetXaxis().SetLabelOffset(999) ## send them away total.GetXaxis().SetTitleOffset(999) ## in outer space total.GetYaxis().SetLabelSize(0.05) #ratio.SetMarkerSize(0.7*ratio.GetMarkerSize()) # no it is confusing #$ROOT.gStyle.SetErrorX(0.0); line = ROOT.TLine(unity.GetXaxis().GetXmin(),1,unity.GetXaxis().GetXmax(),1) line.SetLineWidth(2); line.SetLineColor(58); line.Draw("L") ratio.Draw("E SAME" if ratio.ClassName() != "TGraphAsymmErrors" else "PZ SAME"); return (ratio, unity, unity0, line)
def doStandaloneRatioHists(wplot,zplot,options,maxRange,fixRange=False,fitRatio=None,errorsOnRef=True,ratioNums="signal",ratioDen="background"): # do this first wplot.GetXaxis().SetLabelOffset(999) ## send them away wplot.GetXaxis().SetTitleOffset(999) ## in outer space wplot.GetYaxis().SetLabelSize(0.05) ratio = wplot.Clone("_".join(wplot.GetName().split("_")[:-1])+"Z_ratio") ratio.Divide(zplot) unity = zplot.Clone("z_div"); rmin, rmax = 1,1 for b in xrange(1,unity.GetNbinsX()+1): e,n = unity.GetBinError(b), unity.GetBinContent(b) unity.SetBinContent(b, 1 if n > 0 else 0) if errorsOnRef: unity.SetBinError(b, e/n if n > 0 else 0) else: unity.SetBinError(b, 0) rmin = min([ rmin, 1-2*e/n if n > 0 else 1]) rmax = max([ rmax, 1+2*e/n if n > 0 else 1]) if ratio.ClassName() != "TGraphAsymmErrors": for b in xrange(1,unity.GetNbinsX()+1): if ratio.GetBinContent(b) == 0: continue rmin = min([ rmin, ratio.GetBinContent(b) - 2*ratio.GetBinError(b) ]) rmax = max([ rmax, ratio.GetBinContent(b) + 2*ratio.GetBinError(b) ]) else: for i in xrange(ratio.GetN()): rmin = min([ rmin, ratio.GetY()[i] - 2*ratio.GetErrorYlow(i) ]) rmax = max([ rmax, ratio.GetY()[i] + 2*ratio.GetErrorYhigh(i) ]) if rmin < maxRange[0] or fixRange: rmin = maxRange[0]; if rmax > maxRange[1] or fixRange: rmax = maxRange[1]; if (rmax > 3 and rmax <= 3.4): rmax = 3.4 if (rmax > 2 and rmax <= 2.4): rmax = 2.4 unity.SetFillStyle(1001); unity.SetFillColor(ROOT.kCyan); unity.SetMarkerStyle(1); unity.SetMarkerColor(ROOT.kCyan); ROOT.gStyle.SetErrorX(0.5); if errorsOnRef: unity.Draw("E2"); else: unity.Draw("AXIS"); if fitRatio != None: from CMGTools.TTHAnalysis.tools.plotDecorations import fitTGraph fitTGraph(ratio,order=fitRatio) unity.SetFillStyle(3013); if errorsOnRef: unity.Draw("AXIS SAME"); unity.GetYaxis().SetRangeUser(rmin,rmax); unity.GetXaxis().SetTitleSize(0.14) unity.GetYaxis().SetTitleSize(0.14) unity.GetXaxis().SetLabelSize(0.11) unity.GetYaxis().SetLabelSize(0.11) unity.GetYaxis().SetNdivisions(505) unity.GetYaxis().SetDecimals(True) unity.GetYaxis().SetTitle("W/Z") unity.GetYaxis().SetTitleOffset(0.52); zplot.GetXaxis().SetLabelOffset(999) ## send them away zplot.GetXaxis().SetTitleOffset(999) ## in outer space zplot.GetYaxis().SetLabelSize(0.05) #ratio.SetMarkerSize(0.7*ratio.GetMarkerSize()) # no it is confusing #$ROOT.gStyle.SetErrorX(0.0); line = ROOT.TLine(unity.GetXaxis().GetXmin(),1,unity.GetXaxis().GetXmax(),1) line.SetLineWidth(2); line.SetLineColor(58); line.Draw("L") ratio.Draw("E SAME" if ratio.ClassName() != "TGraphAsymmErrors" else "PZ SAME"); return (ratio, unity, line)
def doRatioHists(pspec,pmap,total,totalSyst,maxRange,fitRatio=False): if "data" not in pmap: return (None,None,None,None) ratio = None if hasattr(pmap['data'], 'poissonGraph'): ratio = pmap["data"].poissonGraph.Clone("data_div"); for i in xrange(ratio.GetN()): x = ratio.GetX()[i] div = total.GetBinContent(total.GetXaxis().FindBin(x)) ratio.SetPoint(i, x, ratio.GetY()[i]/div if div > 0 else 0) ratio.SetPointError(i, ratio.GetErrorXlow(i), ratio.GetErrorXhigh(i), ratio.GetErrorYlow(i)/div if div > 0 else 0, ratio.GetErrorYhigh(i)/div if div > 0 else 0) else: ratio = pmap["data"].Clone("data_div"); ratio.Divide(total) unity = totalSyst.Clone("sim_div"); unity0 = total.Clone("sim_div"); rmin, rmax = 1,1 for b in xrange(1,unity.GetNbinsX()+1): e,e0,n = unity.GetBinError(b), unity0.GetBinError(b), unity.GetBinContent(b) unity.SetBinContent(b, 1 if n > 0 else 0) unity.SetBinError(b, e/n if n > 0 else 0) unity0.SetBinContent(b, 1 if n > 0 else 0) unity0.SetBinError(b, e0/n if n > 0 else 0) rmin = min([ rmin, 1-2*e/n if n > 0 else 1]) rmax = max([ rmax, 1+2*e/n if n > 0 else 1]) if ratio.ClassName() != "TGraphAsymmErrors": for b in xrange(1,unity.GetNbinsX()+1): if ratio.GetBinContent(b) == 0: continue rmin = min([ rmin, ratio.GetBinContent(b) - 2*ratio.GetBinError(b) ]) rmax = max([ rmax, ratio.GetBinContent(b) + 2*ratio.GetBinError(b) ]) else: for i in xrange(ratio.GetN()): rmin = min([ rmin, ratio.GetY()[i] - 2*ratio.GetErrorYlow(i) ]) rmax = max([ rmax, ratio.GetY()[i] + 2*ratio.GetErrorYhigh(i) ]) if rmin < maxRange[0]: rmin = maxRange[0]; if rmax > maxRange[1]: rmax = maxRange[1]; unity.SetFillStyle(1001); unity.SetFillColor(ROOT.kCyan); unity.SetMarkerStyle(1); unity.SetMarkerColor(ROOT.kCyan); unity0.SetFillStyle(1001); unity0.SetFillColor(53); unity0.SetMarkerStyle(1); unity0.SetMarkerColor(53); ROOT.gStyle.SetErrorX(0.5); unity.Draw("E2"); if fitRatio: from CMGTools.TTHAnalysis.tools.plotDecorations import fitTGraph fitTGraph(ratio,order=fitRatio) unity.SetFillStyle(3013); unity0.SetFillStyle(3013); unity.Draw("AXIS SAME"); unity0.Draw("E2 SAME"); else: if total != totalSyst: unity0.Draw("E2 SAME"); unity.GetYaxis().SetRangeUser(rmin,rmax); unity.GetXaxis().SetTitleSize(0.14) unity.GetYaxis().SetTitleSize(0.14) unity.GetXaxis().SetLabelSize(0.11) unity.GetYaxis().SetLabelSize(0.11) unity.GetYaxis().SetNdivisions(505) unity.GetYaxis().SetDecimals(True) unity.GetYaxis().SetTitle("Data/Sim.") unity.GetYaxis().SetTitleOffset(0.52); total.GetXaxis().SetLabelOffset(999) ## send them away total.GetXaxis().SetTitleOffset(999) ## in outer space total.GetYaxis().SetLabelSize(0.05) #ratio.SetMarkerSize(0.7*ratio.GetMarkerSize()) # no it is confusing #$ROOT.gStyle.SetErrorX(0.0); line = ROOT.TLine(unity.GetXaxis().GetXmin(),1,unity.GetXaxis().GetXmax(),1) line.SetLineWidth(2); line.SetLineColor(58); line.Draw("L") ratio.Draw("E SAME" if ratio.ClassName() != "TGraphAsymmErrors" else "PZ SAME"); return (ratio, unity, unity0, line)
def plotRatioWithFit(self, ratioHist, canvName, extraLabel, yMin, yMax, ytitle="Ratio", xtitle=""): # tdrStyle->SetPadTopMargin(0.05); # tdrStyle->SetPadBottomMargin(0.13); # tdrStyle->SetPadLeftMargin(0.16); # tdrStyle->SetPadRightMargin(0.02); # ROOT.gStyle.SetOptTitle(0) # ROOT.gStyle.SetOptStat(0) # ROOT.gStyle.SetPadTopMargin(0.075) # ROOT.gStyle.SetPadLeftMargin(0.05) # ROOT.gStyle.SetPadRightMargin(0.025) # ROOT.gStyle.SetPadBottomMargin(0.25) ROOT.gStyle.SetLegendBorderSize(0) ROOT.gStyle.SetOptStat(0) ROOT.gStyle.SetOptTitle(0) ROOT.gStyle.SetPadTopMargin(0.05) ROOT.gStyle.SetPadRightMargin(0.03) ROOT.gStyle.SetPadBottomMargin(0.15) ROOT.gStyle.SetPadLeftMargin(0.17) c1 = ROOT.TCanvas(canvName, canvName, 600, 600) # c1.SetTopMargin(c1.GetTopMargin()*options.topSpamSize); c1.Draw() p1, p2 = c1, None # high and low panes # set borders, if necessary create subpads # pspec.setOption('extralabel', options.extraLabel) # tmpMin = 0.1 #default log miminum # if pspec.hasOption('YMin'): # tmpMin = pspec.getOption('YMin',1.0) # total.SetMinimum(tmpMin) # tmpMax = total.GetMaximum()#total.GetBinContent(total.GetMaximumBin()) # relHistHeight = 1- (ROOT.gStyle.GetPadTopMargin() + ROOT.gStyle.GetPadBottomMargin() + 0.03*len(pspec.getOption('extralabel',"").split("\\n"))) # if islog: maximum = tmpMin * pow(tmpMax/tmpMin,1./relHistHeight); # else: maximum = (tmpMax-tmpMin)/relHistHeight + tmpMin # total.SetMaximum(maximum) ratioHist.Draw() ratioHist.GetYaxis().SetRangeUser(yMin, yMax) ratioHist.GetXaxis().SetRangeUser(3, 10) ratioHist.GetYaxis().SetTitle(ytitle) if xtitle != "": ratioHist.GetXaxis().SetTitle(xtitle) from CMGTools.TTHAnalysis.tools.plotDecorations import fitTGraph, histToGraph graph = histToGraph(ratioHist) fitTGraph(graph, order=1) decorrFitGraph = graph.Clone() ratioHist.Draw("same") ypoints = graph.band68.GetY() ypoints2 = graph.band68.GetEYhigh() print len(ypoints), len(ypoints2), graph.band68.GetN() sumw = 0 sumwtimesx = 0 for i in range(0, graph.GetN()): x, y, ex, ey = ROOT.Double(0), ROOT.Double(0), 0, 0 print i, graph.GetPoint(i, x, y) ex = graph.GetErrorX(i) ey = graph.GetErrorY(i) print x, y, ex, ey w = 1 / (ey * ey) sumw += w sumwtimesx += w * x wmean = sumwtimesx / sumw print "wmean", wmean orthonormLinear = TF1("orthonormLinear", "[0] +(x-{})*[1]".format(wmean), 0, 10) print "[0] +(x-{})*[1]".format(wmean) fitresult = decorrFitGraph.Fit(orthonormLinear, "SN0 EX0") orthonormLinear.SetLineColor(1) orthonormLinear.SetLineStyle(2) # orthonormLinear.Draw("same") # for i,y in enumerate(ypoints): # print y retUncUp = ROOT.TGraphErrors() retUncDn = ROOT.TGraphErrors() nPoints = graph.band68.GetN() # xmin = 0 # xmax = 10 # points = [ xmin + i*(xmax-xmin)/(nPoints-1) for i in xrange(nPoints) ] ywm = orthonormLinear.Eval(wmean) print "ywm", ywm nPoints = nPoints for i in range(0, nPoints): retUncUp.Set(retUncUp.GetN() + 1) retUncDn.Set(retUncDn.GetN() + 1) x, y, ex, ey = ROOT.Double(0), ROOT.Double(0), 0, 0 # print i, graph.band68.GetPoint(i, x, y) ex = graph.band68.GetErrorX(i) ey = graph.band68.GetErrorY(i) y = orthonormLinear.Eval(x) eyup = 0 if y > ywm: eyup = +ey eydn = -ey else: eyup = -ey eydn = +ey # print eyup retUncUp.SetPoint(i, x, eyup) retUncDn.SetPoint(i, x, eydn) # retUncUp.SetPoint(retUncUp.GetN()-1, x, yvalue) print "WeightDict = dict([", for i in range(3, 12): print "( {} , {:.4f}), ".format(i, 1 + retUncUp.Eval(i + 0.5)) print "])" retUncUp.SetLineColor(2) retUncDn.SetLineColor(1) # retUncUp.Draw("same") # retUncDn.Draw("same") # fitresult = "Fit: \\n const: %.2f \pm %.2f; \\n slope: %.2f \pm %.2f; \\n weighted mean %.2f " %(orthonormLinear.GetParameter(0),orthonormLinear.GetParError(0),orthonormLinear.GetParameter(1),orthonormLinear.GetParError(1), wmean) fitresult = "Fit: \\n const: %.2f +/- %.2f; \\n slope: %.2f +/- %.2f; \\n weighted mean %.2f " % ( orthonormLinear.GetParameter(0), orthonormLinear.GetParError(0), orthonormLinear.GetParameter(1), orthonormLinear.GetParError(1), wmean) # , " slope: ", orthonormLinear.GetParameter(1),"\pm", orthonormLinear.GetParError(1) , if extraLabel != "": printExtraLabel(extraLabel + "\\n" + fitresult, 'TR') else: printExtraLabel(fitresult, 'TR') # doCMSlumi(c1) ROOT.gPad.Update() ROOT.gPad.RedrawAxis() c1.Print(".png") # c1.Print(".eps") c1.Print(".pdf")