示例#1
0
def parameterization(name,cmsLumi,data, mclist, x_min, x_max, mean, meanerr, gamma, gammaerr,doLog=True,doRatio=True):
    canv=makeCanvas("c",doRatio)
    pads=[canv]
    pads = rootplotcore.divide_canvas(canv, 0.3)
    pads[0].cd()
    pads[0].SetGridy()

    fp = ROOT.TF1("fp",fParameterization,x_min,x_max,5)
    fp.SetNpx(x_max-x_min)

    leg = ROOT.TLegend(0.71,0.60,0.90,0.80)
    leg.SetBorderSize(0)
    #leg.SetNColumns(2)
    leg.SetTextSize(0.029)
    leg.SetTextFont(42)
    leg.SetLineColor(0)
    leg.SetFillColor(0)
    leg.AddEntry(data,"Data","lp")
    hs1 = ROOT.THStack("hs_%s_mc"%(name), "hs_%s_mc"%(name))
    hs2 = ROOT.THStack("hs_%s_mc"%(name), "hs_%s_mc"%(name))
    leghist = []
    for i, mc in enumerate(mclist):
        hnew = mc.Clone("hnew"+mc.GetName())
        print mc.GetTitle()
        hnew.Sumw2(False)
        if ("GG_H" in mc.GetTitle()) or ("VBF_H" in mc.GetTitle()):
            hs2.Add(hnew)
        else:
            hs1.Add(hnew)
        inversed = mclist[len(mclist)-1-i]
        if not any(inversed.GetTitle() == s for s in leghist):
            #leg.AddEntry(inversed, inversed.GetTitle(), "f")
            leghist.append(inversed.GetTitle())
    hsum = hs1.GetStack().Last()
    nmc = hsum.Integral(x_min,x_max)
    print nmc

    # fit error
    fp_up=ROOT.TF1("fp_up",fParameterization,x_min,x_max,5)
    fp_dn=ROOT.TF1("fp_dn",fParameterization,x_min,x_max,5)
    fp_up.SetObjectStat(0)
    fp_dn.SetObjectStat(0)
    fp_up.SetNpx(x_max-x_min)
    fp_dn.SetNpx(x_max-x_min)

    fp.SetLineColor(ROOT.kAzure)
    fp.SetFillColor(ROOT.kAzure+10)
    #fp.SetParLimits(0,nmc,nmc)
    fp.SetParLimits(1,mean-meanerr,mean+meanerr)
    fp.SetParLimits(2,gamma-gammaerr,gamma+gammaerr)
    fp.SetLineWidth(2)
    fp.SetParNames("N_{bg}","m_{Z}","#gamma","#Lambda","#beta")
    from time import localtime, strftime
    #leg.SetHeader(strftime("%Y-%m-%d %H:%M", localtime()))
    #legheader=leg.GetListOfPrimitives().First()
    #legheader.SetTextAlign(22)
    #legheader.SetTextSize(0.04)
    hs2sum = hs2.GetStack().Last()
    hs2sum.SetLineColor(ROOT.kOrange)
    hs2sum.SetLineWidth(3)
    hs2sum.SetFillColor(0)
    leg.AddEntry(fp,"fit","lf")
    leg.AddEntry(hs2sum,"Higgs x 30","l")

    #data.Draw("AXIS")
    data.Fit("fp","R0+")
    data.Draw("ex0")

    par = ""
    parerr = ""
    for i in range(5):
        par = fp.GetParameter(i)
        parerr =fp.GetParError(i)
        print "fp_up.par[%d] : %f"%(i,par+parerr)
        print "fp_dn.par[%d] : %f"%(i,par-parerr)
        fp_up.FixParameter(i,par+parerr)
        fp_dn.FixParameter(i,par-parerr)
        
    fp_up.SetLineColor(ROOT.kOrange)
    fp_dn.SetLineColor(ROOT.kOrange)
    fp_up.SetFillColor(ROOT.kAzure+10)
    fp_up.SetLineWidth(2)
    fp_dn.SetLineWidth(2)

    data.Fit("fp_up","R0+")
    data.Fit("fp_dn","R0+")

    #fp.Draw("same")
    #fp_up.Draw("same")
    #fp_dn.Draw("same")
    hs2sum.Draw("lsame")
    #hs1.Draw("same")
    leg.Draw("same")
    pads[0].Update()

    x, y_up, y_dn, y = shade(pads[0],fp_up,fp_dn,fp)
    data.Draw("e same x0")
    hs2sum.Draw("lsame")
    #hs1.Draw("same")
    leg.Draw("same")
    

    print "fp : %f"%fp.Integral(120,130)
    print "fp_up : %f"%fp_up.Integral(120,130)
    print "fp_dn : %f"%fp_dn.Integral(120,130)

    print "fp : %f"%fp.Integral(120,130)
    print "fp_up percent : %f%%"%((fp_up.Integral(120,130)-fp.Integral(120,130))/fp.Integral(120,130)*100)
    print "fp_dn percent : %f%%"%((fp_dn.Integral(120,130)-fp.Integral(120,130))/fp.Integral(120,130)*100)

    ROOT.gStyle.SetOptStat(0)
    ROOT.gStyle.SetOptFit(0)
    if doLog:
        #data.SetMinimum(10**-2)
        pads[0].SetLogy()
    data.GetXaxis().SetLabelSize(0)
    data.GetYaxis().SetLabelSize(0.03)
    data.GetYaxis().SetTitle("Events/ 1 GeV/c^{2}")
    data.SetAxisRange(110,160,"X")
    
    #pads[0].Modified()
    #setNameIntoCanvas(pads[0],name)

    pads[1].cd()
    pads[1].SetGridy()
    ratiorange=0.3
    lhratio=[]
    yratio_up=[]
    yratio_dn=[]
    for i in range(len(x)):
        #print i,x[i],y_dn[i],y[i],y_up[i]
        yratio_up.append(y_up[i]/y[i])
        yratio_dn.append(y_dn[i]/y[i])
      
    #print len(x)
    from array import array
    gr=ROOT.TGraph(len(x),array('d',x),array('d',y))
    gr_ratio_up=ROOT.TGraph(len(x),array('d',x),array('d',yratio_up))
    gr_ratio_dn=ROOT.TGraph(len(x),array('d',x),array('d',yratio_dn))
    hfit=copy.deepcopy(fp.GetHistogram())
    #if hfit.GetSumw2N() == 0:
    #    hfit.Sumw2()
    hfit_up=copy.deepcopy(fp_up.GetHistogram())
    hfit_dn=copy.deepcopy(fp_dn.GetHistogram())

    lh = [hfit,copy.deepcopy(data),hfit_up,hfit_dn]
    lh[1].GetXaxis().SetLabelSize(0.03)
    if lh[0].GetSumw2N() == 0:
        print "lh0 has no sumw2!!!"
        lh[0].Sumw2(1)
    #lh[0].GetXaxis().SetLimits(110,160)
    #for i in range(len(lh)):
        #lh[i].SetAxisRange(110,160,"X")
        #lh[1].GetXaxis().SetLimits(lh[1].FindBin(110),lh[1].FindBin(300))
        #lh[i]=lh[i].Clone()
        #lh[i].SetBins(50, 110, 160)
    htmp = ROOT.TH1F("tmp","tmp",50,110,160)
    k=0
    for i in range(110,300):
        k+=1
        if i>120 and i<130:continue
        htmp.SetBinContent(k,lh[1].GetBinContent(lh[1].FindBin(i)))
        print k,lh[1].GetBinContent(lh[1].FindBin(i))
    x,y,exl,eyl,exh,eyh=[],[],[],[],[],[]
    for i in range(110,300):
        y1,y2,y3,y4,y5 = lh[0].GetBinContent(lh[0].FindBin(i)), lh[1].GetBinContent(lh[1].FindBin(i)), lh[2].GetBinContent(lh[2].FindBin(i)), lh[3].GetBinContent(lh[3].FindBin(i)), htmp.GetBinContent(htmp.FindBin(i))
        print """ 
    lh[0].FindBin(i) = %d, lh[1].FindBin(i) = %d, lh[2].FindBin(i) = %d, lh[3].FindBin(i) = %d, htmp.FindBin(i) = %d
        """%(lh[0].FindBin(i), lh[1].FindBin(i), lh[2].FindBin(i), lh[3].FindBin(i),htmp.FindBin(i))
        print """ 
    lh[0].GetBinContent(lh[0].FindBin(i)) = %d, lh[1].GetBinContent(lh[1].FindBin(i)) = %d, lh[2].GetBinContent(lh[2].FindBin(i)) = %d, lh[3].GetBinContent(lh[3].FindBin(i)) = %d,  htmp.GetBinContent(htmp.FindBin(i)) = %d
        """%(y1,y2,y3,y4,y5)
        x.append(ROOT.Double(i))
        y.append(ROOT.Double(1))
        exl.append(ROOT.Double(0))
        exh.append(ROOT.Double(0))
        eyl.append(ROOT.Double(abs((y1-y4)/y1)))
        eyh.append(ROOT.Double(abs((y1-y3)/y1)))        
        #eyl.append(ROOT.Double(y4/y1))
        #eyh.append(ROOT.Double(y3/y1))        
    print x,y,exl,exh,eyl,eyh
    print len(x),len(y),len(exl),len(exh),len(eyl),len(eyh)
    band = ROOT.TGraphAsymmErrors(len(x),array('d',x),array('d',y),array('d',exl),array('d',exh),array('d',eyl),array('d',eyh))
    xline = ROOT.TGraph(len(x),array('d',x),array('d',y))
    band.SetFillColor(fp.GetFillColor())
    band.SetLineColor(fp.GetLineColor())
    band.SetLineWidth(fp.GetLineWidth()) 
    xline.SetLineColor(fp.GetLineColor())
    xline.SetLineWidth(fp.GetLineWidth()) 
    #band.SetFillColor(fp_up.GetFillColor())
    lh[1] = htmp.Clone()    
    lh[1].Sumw2(1)
    for i in range(len(lh)):
        lh[i].SetAxisRange(110,160,"X")
    #lh = [gr.GetHistogram(),data,gr_ratio_up.GetHistogram(),gr_ratio_dn.GetHistogram()]
    lcolor = [2,1,ROOT.kBlue-9,ROOT.kBlue-9]
    #lfcolor = [0,0,ROOT.kBlue-9,ROOT.kBlue-9]
    lfcolor = [0,0,ROOT.kAzure+10,0]
    ldrawopt = ["","pesame","hist same","hist same"]
    lhratio=[]
    dataratio=False
    for i in range(len(lh)):
        hratio=lh[1].Clone("hratio_%d"%i)
        hratio.Reset()
        if i == 1:
            dataratio=True
            hratio.Sumw2(0)
            pratio=ROOT.TH1F("tempratio","tempratio",300,0,300)
        else:
            dataratio=False
        htmp1=lh[1].Clone("htmp1_%d"%i)
        htmp2=lh[1].Clone("htmp2_%d"%i)
        htmp1.Reset()
        htmp2.Reset()
        #hratio.Sumw2(1)
        #hratio.Divide(lh[0],lh[i],1.,1.,"B")
        for j in range(110,161):
            valtmp1=lh[0].GetBinContent(lh[0].FindBin(j))
            valtmp2=lh[i].GetBinContent(lh[i].FindBin(j))
            htmp1.Fill(j,valtmp1)
            htmp2.Fill(j,valtmp2)
            errtmp1=lh[0].GetBinError(lh[0].FindBin(j))
            errtmp2=lh[i].GetBinError(lh[0].FindBin(j))
            reerrtmp1=errtmp1/valtmp1
            if valtmp2==0:
                reerrtmp2=0
            else:
                reerrtmp2=errtmp2/valtmp2
            print "mass : %d , htmp1 : %d , htmp2 : %d , errtmp1 : %.4f, errtmp2 : %.4f, reerrtmp1 : %.4f, reerrtmp2 : %.4f"%(j,lh[0].GetBinContent(lh[0].FindBin(j)),lh[1].GetBinContent(lh[1].FindBin(j)),errtmp1,errtmp2,reerrtmp1,reerrtmp2)
            print "valtmp2/valtmp1 = %.4f , errtmp2/valtmp1 = %.4f"%(valtmp2/valtmp1,errtmp2/valtmp1)
            if dataratio:
                hratio.SetBinContent(j,float(valtmp2/valtmp1))
                hratio.SetBinError(j,float(errtmp2/valtmp1))
                pratio.SetBinContent(j,float(valtmp2/valtmp1))
                pratio.SetBinError(j,float(errtmp2/valtmp1))
            else:
                dataratio=False

        if dataratio:dataratio=True
        else:
            hratio.Divide(htmp1,htmp2,1.,1.,"B")
            
        hratio.SetLineColor(lcolor[i])
        hratio.SetFillColor(lfcolor[i])
        lhratio.append(copy.deepcopy(hratio))
    pads[1].cd()
    hratio.Reset()
    hratio.Draw("")
    ROOT.gDirectory.Add(band)
    band.Draw("4")
    xline.Draw("l")
    #lhratio[1].Draw("%s"%ldrawopt[1])
    pratio.Draw("epsame")
    for i in range(len(lh)+2):
        canvas=ROOT.TCanvas()
        canvas.cd()
        if i<len(lh):lh[i].Draw()
        elif i==len(lh):
            band.Draw("a4")
            xline.Draw("l")
            #band.Draw("L")
        elif i==len(lh)+1:
            lhratio[1].Draw()
        canvas.SaveAs("hratio_%d.png"%i)
        pads[1].cd()
        
    print """
    data.GetNbinsX() = %d
    hfit.GetNbinsX() = %d
    hfit_up.GetNbinsX() = %d
    hfit_dn.GetNbinsX() = %d
    hratio.GetNbinsX() = %d
    """%(lh[1].GetNbinsX(),lh[0].GetNbinsX(),lh[2].GetNbinsX(),lh[3].GetNbinsX(),hratio.GetNbinsX()) 

    hratio.SetLabelSize(0.03,"Y")
    hratio.SetLabelSize(0.04,"X")
    hratio.GetYaxis().SetTitle("#frac{data}{fit}")
    hratio.GetYaxis().SetNdivisions(9)
    hratio.GetXaxis().SetTitle("M_{#mu#mu} [GeV/c^{2}]")
    #hratio[0].SetAxisRange(110,160,"X")
    hratio.SetMaximum(1.+ratiorange)
    hratio.SetMinimum(1.-ratiorange)

    for p in pads:
        p.RedrawAxis()
        p.SetGridx()
        p.Modified()
        p.Update()

    iPos = 0
    if( iPos==0 ):
        cmsLumi.relPosX = 0.1
    cmsLumi.CMS_lumi(pads[0], 0, iPos)

    canv.cd()
    canv.Modified()
    canv.Update()

    canv.SaveAs(name)
示例#2
0
def drawTH1(name,
            cmsLumi,
            mclist,
            data,
            x_name,
            y_name,
            doLog=False,
            doRatio=True,
            ratioRange=0.45,
            siglist=None,
            legx=0.68,
            legfontsize=0.030):
    leg = ROOT.TLegend(legx, 0.68, legx + 0.2, 0.91)
    leg.SetBorderSize(0)
    #leg.SetNColumns(2)
    leg.SetTextSize(legfontsize)
    leg.SetTextFont(42)
    leg.SetLineColor(0)
    leg.SetFillColor(0)
    leg.SetFillStyle(0)
    leg.AddEntry(data, "Data", "lp")

    leghist = []

    if siglist is not None:
        #leg.AddEntry(sig, sig.GetTitle(), "l")
        #leghist.append(sig.GetTitle())
        for i, sig in enumerate(siglist):
            leg.AddEntry(sig, sig.GetTitle(), "l")
            leghist.append(sig.GetTitle())

    hs = ROOT.THStack("hs_%s_mc" % (name), "hs_%s_mc" % (name))
    hratio = mclist[0].Clone("hratio")
    hratio.Reset()

    for i, mc in enumerate(mclist):
        hnew = mc.Clone("hnew" + mc.GetName())
        hnew.Sumw2(False)
        hs.Add(hnew)
        hratio.Add(mc)
        inversed = mclist[len(mclist) - 1 - i]
        if not any(inversed.GetTitle() == s for s in leghist):
            leg.AddEntry(inversed, inversed.GetTitle(), "f")
            leghist.append(inversed.GetTitle())

    hratio.Divide(data, hratio, 1., 1., "B")

    tdrstyle.setTDRStyle()

    setDefTH1Style(data, x_name, y_name)
    data.SetMaximum(data.GetMaximum() * 1.8)
    if doLog:
        #data.SetMaximum(10**7)
        #data.SetMinimum(10**-3)
        data.SetMaximum(data.GetMaximum() * 100)
    else:
        data.GetYaxis().SetTitleSize(0.04)
        data.GetYaxis().SetLabelSize(0.024)
        data.GetYaxis().SetTitleOffset(1.5)

    ratio_fraction = 0
    if doRatio:
        ratio_fraction = 0.3
        data.GetXaxis().SetLabelSize(0)
        data.GetXaxis().SetTitleSize(0)
        setDefTH1Style(hratio, x_name, "Data/MC")
        hratio.GetYaxis().CenterTitle()
        hratio.GetYaxis().SetNdivisions(5)

    canv = makeCanvas(name, doRatio)
    pads = [canv]
    pads = rootplotcore.divide_canvas(canv, ratio_fraction)
    pads[0].cd()

    setMargins(pads[0], doRatio)
    if doLog:
        pads[0].SetLogy()

    data.Draw()
    hs.Draw("same")

    if siglist is not None:
        for i, sig in enumerate(siglist):
            sig.Draw("samehist")

    data.Draw("esamex0")
    leg.Draw("same")

    tex = ROOT.TLatex()
    tex.SetNDC()
    tex.SetTextFont(42)
    tex.SetTextSize(0.04)
    tex.DrawLatex(0.25, 0.85, name.split('_')[0])
    #tex.DrawLatex(canv.GetLeftMargin()*1.4, 1-canv.GetTopMargin()*2.8, name.split('_')[0])

    pads[0].Update()

    if doRatio:
        pads[1].cd()
        pads[1].SetGridy()
        setMargins(pads[1], doRatio)
        hratio.SetLineColor(1)
        hratio.Draw("e")
        hratio.SetMaximum(1. + ratioRange)
        hratio.SetMinimum(1. - ratioRange)

    for p in pads:
        p.RedrawAxis()
        p.Modified()
        p.Update()

    canv.cd()
    iPos = 0
    if (iPos == 0):
        cmsLumi.relPosX = 0.16
    cmsLumi.CMS_lumi(pads[0], 4, iPos)

    canv.Modified()
    canv.Update()
    canv.SaveAs(name + ".png")
    canv.SaveAs(name + ".pdf")
示例#3
0
def drawTH1(name, cmsLumi, mclist, data, x_name, y_name, doLog=False, doRatio=True, ratioRange=0.45):
    #leg = ROOT.TLegend(0.58,0.78,0.8,0.9)
    leg = ROOT.TLegend(0.71,0.68,0.88,0.91)
    leg.SetBorderSize(0)
    #leg.SetNColumns(2)
    leg.SetTextSize(0.029)
    leg.SetTextFont(42)
    leg.SetLineColor(0)
    leg.SetFillColor(0)
    leg.AddEntry(data,"Data","lp")

    hs = ROOT.THStack("hs_%s_mc"%(name), "hs_%s_mc"%(name))
    hratio = mclist[0].Clone("hratio")
    hratio.Reset()
    leghist = []
    for i, mc in enumerate(mclist):
        hnew = mc.Clone("hnew"+mc.GetName())
        hnew.Sumw2(False)
        hs.Add(hnew)
        hratio.Add(mc)
        inversed = mclist[len(mclist)-1-i]
        if not any(inversed.GetTitle() == s for s in leghist):
            leg.AddEntry(inversed, inversed.GetTitle(), "f")
            leghist.append(inversed.GetTitle())
                        
    hratio.Divide(data,hratio,1.,1.,"B")

    tdrstyle.setTDRStyle()

    setDefTH1Style(data, x_name, y_name)
    data.SetMaximum(data.GetMaximum()*1.8)
    if doLog:
        #data.SetMaximum(10**7)
        data.SetMaximum(data.GetMaximum()*100)
        
    ratio_fraction = 0
    if doRatio:
        ratio_fraction = 0.3        
        data.GetXaxis().SetLabelSize(0)
        data.GetXaxis().SetTitleSize(0)
        data.GetYaxis().CenterTitle()
        setDefTH1Style(hratio, x_name, "Data/MC")
        hratio.GetYaxis().SetNdivisions(5)
            
    canv = makeCanvas(name, doRatio)
    pads=[canv]
    pads = rootplotcore.divide_canvas(canv, ratio_fraction)
    pads[0].cd()
    
    setMargins(pads[0],doRatio)
    if doLog:
        pads[0].SetLogy()

    data.Draw()
    hs.Draw("same")
    data.Draw("same")
    data.Draw("esamex0")
    leg.Draw("same")
    pads[0].Update()

    if doRatio:
        pads[1].cd()
        pads[1].SetGridy()
        setMargins(pads[1],doRatio)
        hratio.SetLineColor(1)
        hratio.SetMarkerStyle(10)
        hratio.Draw("ep")
        hratio.SetMaximum(1.+ratioRange)
        hratio.SetMinimum(1.-ratioRange)

    for p in pads:
        p.RedrawAxis()
        p.Modified()
        p.Update()

    canv.cd()    
    iPos = 11
    if( iPos==0 ):
        cmsLumi.relPosX = 0.12    
    cmsLumi.CMS_lumi(canv, 0, iPos)
    
    canv.Modified()
    canv.Update()
    canv.SaveAs(name)
示例#4
0
def drawTH1(name,
            cmsLumi,
            mclist,
            data,
            x_name,
            y_name,
            doLog=False,
            doRatio=True,
            ratioRange=0.45,
            legx=0.68,
            legfontsize=0.030):
    leg = ROOT.TLegend(legx, 0.68, legx + 0.2, 0.91)
    leg.SetBorderSize(0)
    #leg.SetNColumns(2)
    leg.SetTextSize(legfontsize)
    leg.SetTextFont(42)
    leg.SetLineColor(0)
    leg.SetFillColor(0)
    leg.SetFillStyle(0)
    leg.AddEntry(data, "Data", "lp")

    hs = ROOT.THStack("mcstack", "mcstack")
    hratio = mclist[0].Clone("hratio")
    hratio.Reset()

    leghist = []
    for i, mc in enumerate(mclist):
        hnew = mc.Clone("hnew" + mc.GetName())
        hnew.Sumw2(False)
        hs.Add(hnew)
        hratio.Add(mc)
        inversed = mclist[len(mclist) - 1 - i]
        if not any(inversed.GetTitle() == s for s in leghist):
            leg.AddEntry(inversed, inversed.GetTitle(), "f")
            leghist.append(inversed.GetTitle())

    #hratio = hs.GetStack().Last()
    hratio.Divide(data, hratio, 1., 1., "B")

    tdrstyle.setTDRStyle()

    setDefTH1Style(data, x_name, y_name)
    data.SetName('data')
    data.SetMaximum(data.GetMaximum() * 1.8)
    if doLog:
        data.SetMaximum(data.GetMaximum() * 100)
        #data.SetMinimum(10**-3)
    else:
        data.GetYaxis().SetTitleSize(0.04)
        data.GetYaxis().SetLabelSize(0.024)
        data.GetYaxis().SetTitleOffset(1.35)

    ratio_fraction = 0
    if doRatio:
        ratio_fraction = 0.3
        data.GetXaxis().SetLabelSize(0)
        data.GetXaxis().SetTitleSize(0)
        setDefTH1Style(hratio, x_name, "Data/MC")
        hratio.GetYaxis().CenterTitle()
        hratio.GetYaxis().SetNdivisions(5)

    canv = makeCanvas(name, doRatio)
    pads = [canv]
    pads = rootplotcore.divide_canvas(canv, ratio_fraction)

    pads[0].cd()
    setMargins(pads[0], doRatio)
    if doLog:
        pads[0].SetLogy()

    data.Draw()
    hs.Draw("same")
    data.Draw("esamex0")
    leg.Draw("same")

    pads[0].Update()

    if doRatio:
        pads[1].cd()
        pads[1].SetGridy()
        setMargins(pads[1], doRatio)
        hratio.SetLineColor(1)
        hratio.Draw("e")
        hratio.SetMaximum(1. + ratioRange)
        hratio.SetMinimum(1. - ratioRange)

    for p in pads:
        p.RedrawAxis()
        p.Modified()
        p.Update()

    canv.cd()

    #iPos = 0 # in frame
    iPos = 11  # out frame
    if (iPos == 0):
        cmsLumi.relPosX = 0.1
    cmsLumi.CMS_lumi(pads[0], 0, iPos)

    canv.Modified()
    canv.Update()
    return copy.deepcopy(canv)
示例#5
0
def drawTH1(name,
            cmsLumi,
            mclist,
            data,
            x_name,
            y_name,
            doLog=False,
            doRatio=True,
            ratioRange=0.45):
    #leg = ROOT.TLegend(0.58,0.78,0.8,0.9)
    leg = ROOT.TLegend(0.71, 0.68, 0.88, 0.91)
    leg.SetBorderSize(0)
    #leg.SetNColumns(2)
    leg.SetTextSize(0.029)
    leg.SetTextFont(42)
    leg.SetLineColor(0)
    leg.SetFillColor(0)
    leg.AddEntry(data, "Data", "lp")

    hs = ROOT.THStack("hs_%s_mc" % (name), "hs_%s_mc" % (name))
    hratio = mclist[0].Clone("hratio")
    hratio.Reset()
    leghist = []
    for i, mc in enumerate(mclist):
        hnew = mc.Clone("hnew" + mc.GetName())
        hnew.Sumw2(False)
        hs.Add(hnew)
        hratio.Add(mc)
        inversed = mclist[len(mclist) - 1 - i]
        if not any(inversed.GetTitle() == s for s in leghist):
            leg.AddEntry(inversed, inversed.GetTitle(), "f")
            leghist.append(inversed.GetTitle())

    #hratio.Divide(data,mclist[0],1.,1.,"B")
    hratio.Divide(data, hratio, 1., 1., "B")

    tdrstyle.setTDRStyle()

    setDefTH1Style(data, x_name, y_name)
    data.SetMaximum(data.GetMaximum() * 1.8)
    if doLog:
        #data.SetMaximum(10**7)
        data.SetMaximum(data.GetMaximum() * 100)

    ratio_fraction = 0
    if doRatio:
        ratio_fraction = 0.3
        data.GetXaxis().SetLabelSize(0)
        data.GetXaxis().SetTitleSize(0)
        data.GetYaxis().CenterTitle()
        setDefTH1Style(hratio, x_name, "Data/MC")
        hratio.GetYaxis().SetNdivisions(5)

    canv = makeCanvas(name, doRatio)
    pads = [canv]
    pads = rootplotcore.divide_canvas(canv, ratio_fraction)
    pads[0].cd()

    setMargins(pads[0], doRatio)
    if doLog:
        pads[0].SetLogy()

    data.Draw()
    hs.Draw("same")
    #hs.Draw("samenostack")
    data.Draw("esamex0")
    leg.Draw("same")
    pads[0].Update()

    if doRatio:
        pads[1].cd()
        pads[1].SetGridy()
        setMargins(pads[1], doRatio)
        hratio.SetLineColor(1)
        hratio.Draw("e")
        hratio.SetMaximum(1. + ratioRange)
        hratio.SetMinimum(1. - ratioRange)

    for p in pads:
        p.RedrawAxis()
        p.Modified()
        p.Update()

    canv.cd()
    iPos = 11
    if (iPos == 0):
        cmsLumi.relPosX = 0.12
    cmsLumi.CMS_lumi(canv, 0, iPos)

    canv.Modified()
    canv.Update()
    canv.SaveAs(name)