示例#1
0
def draw_ratio(c_title, h_top=[], h_bottom=[], draw_top=[], draw_bottom=[]):
    c = TCanvas(c_title)
    c.cd()
    pad_bottom = TPad("pad_bottom", "", 0, 0.02105263, 1, 0.4)
    pad_bottom.SetFrameBorderMode(0)
    pad_bottom.SetFrameBorderMode(0)
    pad_bottom.SetBorderMode(0)
    pad_bottom.SetTopMargin(0)
    pad_bottom.SetBottomMargin(0.3)

    pad_bottom.Draw()
    pad_bottom.cd()
    h_bottom[0].SetStats(0)
    pad_bottom.SetGridy()

    h_bottom[0].Draw(draw_bottom[0])
    h_bottom[0].GetYaxis().SetRangeUser(0.91, 1.09)
    h_bottom[0].GetYaxis().SetNdivisions(5, 0, 5)
    h_bottom[0].GetYaxis().SetLabelSize(0.10)
    h_bottom[0].GetYaxis().SetTitleSize(0.13)
    h_bottom[0].GetYaxis().SetTitleOffset(0.35)
    h_bottom[0].GetXaxis().SetLabelSize(0.10)
    h_bottom[0].GetXaxis().SetTitleSize(0.13)
    h_bottom[0].GetXaxis().SetTitleOffset(0.85)
    h_bottom[0].GetYaxis().SetTitle("Data/MC")
    h_bottom[0].GetYaxis().CenterTitle()

    h_bottom[0].SetMarkerStyle(20)
    h_bottom[0].SetLineColor(1)
    for i, h in enumerate(h_bottom[1:]):
        h.Draw("same" + draw_bottom[i + 1])

    c.cd()
    pad_top = TPad("pad_top", "", 0, 0.4, 1, 1)
    pad_top.SetBottomMargin(0)
    pad_top.SetTopMargin(0.15)
    pad_top.Draw()
    pad_top.cd()
    h_top[0].SetStats(0)
    h_top[0].DrawCopy(draw_top[0])
    h_top[0].GetYaxis().SetTitleOffset(0.59)
    h_top[0].GetYaxis().SetTitleSize(0.07)

    h_top[0].SetFillStyle(3001)
    h_top[0].Draw("e2same")

    for i, h in enumerate(h_top[1:]):
        h.Draw("same" + draw_top[i + 1])

    c.Update()
    return c
def PlotFakeRatio(ratio):
    c = TCanvas("c", "canvas", 800, 800)
    gStyle.SetOptStat(0)
    gStyle.SetLegendBorderSize(0)

    pad = TPad("pad", "pad", 0.01, 0.01, 0.99, 0.99)
    pad.Draw()
    pad.cd()
    pad.SetGrid()
    pad.SetFillColor(0)
    pad.SetFrameBorderMode(0)
    pad.SetBorderMode(0)

    xaxis_title = "Photon P_{T} [GeV]"

    leg = getLegend(xmin=0.5, ymin=0.7, scale=0.75)

    fake_style(ratio)
    ratio.SetTitle("")
    ratio.GetYaxis().SetTitle("Fake Ratio")
    # ratio.GetYaxis().SetTitleOffset(0.5)
    ratio.GetXaxis().SetTitle(xaxis_title)
    # ratio.GetYaxis().SetRangeUser(0.75,1.2)
    print SetBounds([ratio], scale=1)

    ratio.Draw("p same")

    leg.AddEntry(ratio, "#frac{Data_{ISO}}{Data_{Non-ISO}}", "lp")
    leg.Draw()

    lumi_label = '%s' % float('%.3g' %
                              (max(config.lumi.values()) / 1000.)) + " fb^{-1}"
    texLumi, texCMS = getCMSText(lumi_label, config.version, scale=0.8)

    SaveAs(c,
           "fake_ratio_%s" % parser.args.variable,
           year=config.version,
           sub="GammaPurity/FakeRatio/")
        c.SetTickx(0)
        c.SetTicky(0)
        c.Draw()
        c.cd()

        pad1 = TPad("zxc_p1", "zxc_p1", 0, padRatio - padOverlap, 1, 1)
        pad2 = TPad("qwe_p2", "qwe_p2", 0, 0, 1, padRatio + padOverlap)
        pad1.SetLeftMargin(L / W)
        pad1.SetRightMargin(R / W)
        pad1.SetTopMargin(T / H / (1 - padRatio + padOverlap))
        pad1.SetBottomMargin(
            (padOverlap + padGap) / (1 - padRatio + padOverlap))
        pad1.SetFillColor(0)
        pad1.SetBorderMode(0)
        pad1.SetFrameFillStyle(0)
        pad1.SetFrameBorderMode(0)
        pad1.SetTickx(0)
        pad1.SetTicky(0)

        pad2.SetLeftMargin(L / W)
        pad2.SetRightMargin(R / W)
        pad2.SetTopMargin((padOverlap) / (padRatio + padOverlap))
        pad2.SetBottomMargin(B / H / (padRatio + padOverlap))
        pad2.SetFillColor(0)
        pad2.SetFillStyle(4000)
        pad2.SetBorderMode(0)
        pad2.SetFrameFillStyle(0)
        pad2.SetFrameBorderMode(0)
        pad2.SetTickx(0)
        pad2.SetTicky(0)
示例#4
0
def CanvasPartition(C,
                    Nx,
                    Ny,
                    lMargin=0.15,
                    rMargin=0.05,
                    bMargin=0.15,
                    tMargin=0.05):
    if (not C):
        return (0)
    #Setup Pad layout:
    vSpacing = 0.0
    vStep = (1. - bMargin - tMargin - (Ny - 1) * vSpacing) / Ny
    hSpacing = 0.0
    hStep = (1. - lMargin - rMargin - (Nx - 1) * hSpacing) / Nx
    vposd = 0
    vposu = 0
    vmard = 0
    vmaru = 0
    vfactor = 0
    hposl = 0
    hposr = 0
    hmarl = 0
    hmarr = 0
    hfactor = 0
    for i in range(Nx):
        if (i == 0):
            hposl = 0.0
            hposr = lMargin + hStep
            hfactor = hposr - hposl
            hmarl = lMargin / hfactor
            hmarr = 0.0
        elif (i == Nx - 1):
            hposl = hposr + hSpacing
            hposr = hposl + hStep + rMargin
            hfactor = hposr - hposl
            hmarl = 0.0
            hmarr = rMargin / (hposr - hposl)
        else:
            hposl = hposr + hSpacing
            hposr = hposl + hStep
            hfactor = hposr - hposl
            hmarl = 0.0
            hmarr = 0.0

            for j in range(Ny):
                if (j == 0):
                    vposd = 0.0
                    vposu = bMargin + vStep
                    vfactor = vposu - vposd
                    vmard = bMargin / vfactor
                    vmaru = 0.0
                elif (j == Ny - 1):
                    vposd = vposu + vSpacing
                    vposu = vposd + vStep + tMargin
                    vfactor = vposu - vposd
                    vmard = 0.0
                    vmaru = tMargin / (vposu - vposd)
                else:
                    vposd = vposu + vSpacing
                    vposu = vposd + vStep
                    vfactor = vposu - vposd
                    vmard = 0.0
                    vmaru = 0.0

                C.cd(0)
                name = "pad_" + str(i) + "_" + str(j)
                #pad = gROOT.FindObject(name)
                pad = TPad(name, "", hposl, vposd, hposr, vposu)
                pad.SetLeftMargin(hmarl)
                pad.SetRightMargin(hmarr)
                pad.SetBottomMargin(vmard)
                pad.SetTopMargin(vmaru)
                pad.SetFrameBorderMode(0)
                pad.SetBorderMode(0)
                pad.SetBorderSize(0)
                pad.Draw()
示例#5
0
def DrawOverlap(fileVec,
                histVec,
                titleVec,
                legendtext,
                pngname,
                logstatus=[0, 0],
                xRange=[-99999, 99999, 1]):

    gStyle.SetOptTitle(0)
    gStyle.SetOptStat(0)
    gStyle.SetTitleOffset(1.1, "Y")
    gStyle.SetTitleOffset(0.9, "X")
    gStyle.SetLineWidth(3)
    gStyle.SetFrameLineWidth(3)

    i = 0

    histList_ = []
    histList = []
    histList1 = []
    maximum = []

    ## Legend
    leg = TLegend(0.65, 0.650, 0.89, 0.85)  #,NULL,"brNDC");
    leg.SetBorderSize(0)
    #    leg.SetNColumns(3)
    leg.SetLineColor(1)
    leg.SetLineStyle(1)
    leg.SetLineWidth(1)
    leg.SetFillColor(0)
    leg.SetFillStyle(0)
    leg.SetTextFont(22)
    leg.SetTextSize(0.045)

    c = TCanvas("c1", "c1", 0, 0, 500, 500)
    #c.SetBottomMargin(0.15)
    #c.SetLeftMargin(0.15)
    #c.SetLogy(0)
    #c.SetLogx(0)
    c1_2 = TPad("c1_2", "newpad", 0., 0.30, 1, 0.994)
    c1_2.Draw()
    c1_1 = TPad("c1_1", "newpad1", 0, 0.02, 1, 0.30)
    c1_1.Draw()

    print("you have provided " + str(len(fileVec)) + " files and " +
          str(len(histVec)) + " histograms to make a overlapping plot")
    print "opening rootfiles"
    c.cd()
    c1_2.SetBottomMargin(0.013)
    c1_2.SetLogy(logstatus[1])
    c1_2.SetLogx(logstatus[0])

    c1_2.cd()
    ii = 0
    inputfile = {}
    print str(fileVec[(len(fileVec) - 1)])
    for ifile_ in range(len(fileVec)):
        print("opening file  " + fileVec[ifile_])
        inputfile[ifile_] = TFile(fileVec[ifile_])
        print "fetching histograms"
        for ihisto_ in range(len(histVec)):
            print("printing histo " + str(histVec[ihisto_]))
            histo = inputfile[ifile_].Get(histVec[ihisto_])
            #status_ = type(histo) is TGraphAsymmErrors
            histList.append(histo)
            # for ratio plot as they should nt be normalize
            histList1.append(histo)
            print histList[ii].Integral()
            histList[ii].Rebin(xRange[2])
            print('after', histList[ii].Integral(0, -1))
            histList[ii].Scale(1.0 / histList[ii].Integral(0, -1))
            maximum.append(histList[ii].GetMaximum())
            maximum.sort()
            ii = ii + 1

    print histList
    for ih in range(len(histList)):
        tt = type(histList[ih])
        if logstatus[1] is 1:
            histList[ih].SetMaximum(maximum[(len(maximum) - 1)] *
                                    25)  #1.4 for log
            histList[ih].SetMinimum(0.001)  #1.4 for log
        if logstatus[1] is 0:
            histList[ih].SetMaximum(maximum[(len(maximum) - 1)] *
                                    1.2)  #1.4 for log
            histList[ih].SetMinimum(0)  #1.4 for log


#        print "graph_status =" ,(tt is TGraphAsymmErrors)
#        print "hist status =", (tt is TH1D) or (tt is TH1F)
        if ih == 0:
            if tt is TGraphAsymmErrors:
                histList[ih].Draw("AP")
            if (tt is TH1D) or (tt is TH1F) or (tt is TH1) or (tt is TH1I):
                histList[ih].Draw("hist")
        if ih > 0:
            #histList[ih].SetLineWidth(2)
            if tt is TGraphAsymmErrors:
                histList[ih].Draw("P same")
            if (tt is TH1D) or (tt is TH1F) or (tt is TH1) or (tt is TH1I):
                histList[ih].Draw("hist same")

        if tt is TGraphAsymmErrors:
            histList[ih].SetMaximum(1.4)
            histList[ih].SetMarkerColor(colors[ih])
            histList[ih].SetLineColor(colors[ih])
            histList[ih].SetMarkerStyle(markerStyle[ih])
            histList[ih].SetMarkerSize(1)
            leg.AddEntry(histList[ih], legendtext[ih], "PL")
        if (tt is TH1D) or (tt is TH1F) or (tt is TH1) or (tt is TH1I):
            histList[ih].SetLineStyle(linestyle[ih])
            histList[ih].SetLineColor(colors[ih])
            histList[ih].SetLineWidth(3)
            leg.AddEntry(histList[ih], legendtext[ih], "L")
        histList[ih].GetYaxis().SetTitle(titleVec[1])
        histList[ih].GetYaxis().SetTitleSize(0.062)
        histList[ih].GetYaxis().SetTitleOffset(0.78)
        histList[ih].GetYaxis().SetTitleFont(22)
        histList[ih].GetYaxis().SetLabelFont(22)
        histList[ih].GetYaxis().SetLabelSize(.062)
        histList[ih].GetXaxis().SetRangeUser(xRange[0], xRange[1])
        histList[ih].GetXaxis().SetLabelSize(0.0000)
        histList[ih].GetXaxis().SetTitle(titleVec[0])
        histList[ih].GetXaxis().SetLabelSize(0.052)
        histList[ih].GetXaxis().SetTitleSize(0.052)
        histList[ih].GetXaxis().SetTitleOffset(1.04)
        histList[ih].GetXaxis().SetTitleFont(22)
        histList[ih].GetXaxis().SetTickLength(0.07)
        histList[ih].GetXaxis().SetLabelFont(22)
        histList[ih].GetYaxis().SetLabelFont(22)
        # histList[ih].GetXaxis().SetNdivisions(508)
        #

        i = i + 1
    pt = TPaveText(0.0877181, 0.91, 0.9580537, 0.95, "brNDC")
    pt.SetBorderSize(0)
    pt.SetTextAlign(12)
    pt.SetFillStyle(0)
    pt.SetTextFont(22)
    pt.SetTextSize(0.046)
    #    text = pt.AddText(0.05,0.5,"ATLAS Internal ")
    text = pt.AddText(0.65, 1.0, "#sqrt{s} = 13 TeV (36fb^{-1})")
    pt.Draw()

    t2a = TPaveText(0.277181, 0.81, 0.6780537, 0.89, "brNDC")
    t2a.SetBorderSize(0)
    t2a.SetFillStyle(0)
    t2a.SetTextSize(0.046)
    t2a.SetTextAlign(12)
    t2a.SetTextFont(22)
    histolabel1 = str(fileVec[(len(fileVec) - 1)])
    text1 = t2a.AddText(0.065, 0.5, "ATLAS Internal")
    t2a.Draw()
    leg.Draw()
    #
    c.cd()

    h_inc = histList1[0].Clone()
    h_vbs = histList1[1].Clone()
    h_qcd = histList1[2].Clone()
    h_int = histList1[3].Clone()

    h_inc.Add(h_vbs, -1)
    h_inc.Add(h_qcd, -1)

    ## ratio plot
    hdivide_1 = h_int.Clone()
    hdivide_1.Divide(h_vbs)
    # hdivide_1.GetYaxis().SetTitle(str(legendtext[(len(legendtext)-1)]))
    hdivide_1.GetYaxis().SetTitleSize(0.11)
    hdivide_1.GetYaxis().SetTitle("ratio")

    hdivide_1.GetYaxis().SetTitleOffset(0.43)
    hdivide_1.GetYaxis().SetTitleFont(22)
    hdivide_1.GetYaxis().SetLabelSize(0.13)
    hdivide_1.GetYaxis().CenterTitle()

    hdivide_1.GetXaxis().SetTitleSize(0.14)
    hdivide_1.GetXaxis().SetTitleOffset(0.89)
    hdivide_1.GetXaxis().SetTitleFont(22)
    hdivide_1.GetXaxis().SetLabelSize(0.14)

    hdivide_1.GetYaxis().SetLabelFont(22)
    c.cd()
    c1_1.cd()

    #    c1_1.Range(-7.862408,-629.6193,53.07125,486.5489)
    c1_1.SetFillColor(0)
    c1_1.SetTicky(1)
    c1_1.SetTopMargin(0.0510)
    c1_1.SetBottomMargin(0.3006666678814)
    c1_1.SetFrameFillStyle(0)
    c1_1.SetFrameBorderMode(0)
    c1_1.SetFrameFillStyle(0)
    c1_1.SetFrameBorderMode(0)
    c1_1.SetLogy(0)
    hdivide_1.GetXaxis().SetRangeUser(xRange[0], xRange[1])
    hdivide_1.GetYaxis().SetNdivisions(505)
    c1_1.Draw()
    c1_1.SetGridy()

    hdivide_1.SetMarkerStyle(20)
    hdivide_1.SetMarkerColor(2)
    hdivide_1.SetMarkerSize(1)
    hdivide_1.Draw("P")

    hdivide_2 = h_int.Clone()
    hdivide_2.Divide(h_qcd)
    hdivide_2.SetMarkerStyle(21)
    hdivide_2.SetMarkerColor(4)
    hdivide_2.SetMarkerSize(1)
    hdivide_2.Draw("Psame")

    hdivide_3 = h_int.Clone()
    hdivide_3.Divide(h_inc)
    hdivide_3.SetMarkerStyle(22)
    hdivide_3.SetMarkerColor(1)
    hdivide_3.SetMarkerSize(1)
    hdivide_3.Draw("Psame")

    hdivide_1.SetMinimum(-1.01)
    hdivide_1.SetMaximum(3.20)

    c.cd()

    outputdirname = 'plots/'
    histname = outputdirname + pngname
    c.SaveAs(histname + '.png')
    c.SaveAs(histname + '.pdf')
    c.SaveAs(histname + '.root')
    c.SaveAs(histname + '.C')
示例#6
0
def PlotSig(templates, sideband_templates):
    print "Plotting Signal Template"
    data = templates["Data"].histo.Clone()
    gjets = templates["GJets"].histo.Clone()
    sideband = sideband_templates["Sideband"].histo.Clone()
    full = gjets.Clone()
    full.Add(sideband)

    c = TCanvas("c", "canvas", 800, 800)
    gStyle.SetOptStat(0)
    gStyle.SetLegendBorderSize(0)
    # c.SetLeftMargin(0.15);
    # c.SetLogy();
    #c.cd();

    pad1 = TPad("pad1", "pad1", 0.01, 0.25, 0.99, 0.99)
    pad1.SetBottomMargin(0.)
    pad1.Draw()
    pad1.cd()
    pad1.SetLogy()
    pad1.SetFillColor(0)
    pad1.SetFrameBorderMode(0)
    pad1.SetBorderMode(0)

    temp_style(gjets, kGreen + 2)
    temp_style(sideband, kRed)
    temp_style(full, kBlue + 2)
    DataStyle(data)

    leg = getLegend(xmax=0.7, ymin=0.55, ymax=0.75)

    gjets.Draw("hist")

    gjets.SetTitle("")
    gjets.GetYaxis().SetTitle("Events")

    if templates.variable.xaxis_title == "":
        templates.variable.xaxis_title = next(
            (vartitle for var, vartitle in xaxismap.items()
             if var in templates.variable.variable), "")
    gjets.GetXaxis().SetTitle(templates.variable.xaxis_title)

    sideband.Draw("hist same")
    full.Draw("hist same")
    data.Draw("pex0 same")
    hslist = [data, gjets, sideband]

    leg.AddEntry(data, "Data", "lp")
    leg.AddEntry(gjets, "Real GJets", "l")
    leg.AddEntry(sideband, "QCD Fake Template", "l")
    leg.AddEntry(full, "Full", "l")

    VarBounds(templates.variable.variable, hslist)
    leg.Draw()

    lumi_label = '%s' % float('%.3g' % (templates.lumi / 1000.)) + " fb^{-1}"
    texLumi, texCMS = getCMSText(lumi_label, templates.year)

    if re.search("(\d+to\d+|\d+toInf)", templates.variable.variable):
        ptrange = templates.variable.variable.split("_")[1].split("to")
        rtext = PtRangeText(ptrange=ptrange)

    c.cd()
    pad2 = TPad("pad2", "pad2", 0.01, 0.01, 0.99, 0.25)
    pad2.SetGridy()
    pad2.Draw()
    pad2.cd()
    pad2.SetFillColor(0)
    pad2.SetFrameBorderMode(0)
    pad2.SetBorderMode(0)
    pad2.SetTopMargin(0)
    pad2.SetBottomMargin(0.35)

    ratio = GetRatio(data, full)

    RatioStyle(ratio,
               rymin=-0.25,
               rymax=2.25,
               xname=templates.variable.xaxis_title,
               yname="Data/Full")
    ratio.Draw("pex0")
    line = getRatioLine(data.GetXaxis().GetXmin(), data.GetXaxis().GetXmax())
    line.Draw("same")

    SaveAs(c,
           "real_template_%s" % templates.varname,
           year=templates.year,
           sub="GammaPurity")
示例#7
0
def drawHisto(histlist,histoD,xTitle,yTitle,outName,uncHists=""):    
    legend = TLegend(0.35, 0.72, 0.85, 0.92,"") #,"brNDC"
    legend.SetFillStyle(0)

    legend.SetTextSize(0.045)
    legend.SetNColumns(2)

    myStack = THStack("myStack","")
    for hist in histlist:
        myStack.Add(hist,"hist")
    
    histoErr = histlist[0].Clone()
    for iHist in range(1,len(histlist)):
        histoErr.Add(histlist[iHist])
        
    MCCount = myStack.GetStack().Last().Integral()
    DataCount = histoD.Integral()
    MCNormFactor = DataCount/MCCount

#    myStack.Delete()
    myStack = THStack("myStack","")

    for hist in histlist:
        hist.Scale(MCNormFactor)
        myStack.Add(hist,"hist")
        

    histoErr.Scale(MCNormFactor)
    

    if uncHists!="":
        MCMean = myStack.GetStack().Last().Clone()
        MCErr = MCMean.Clone()
        MCErr.Reset()
        for ibin in range(1,MCMean.GetNbinsX()+1):
            thisbinunc = 0
            for ihist in uncHists:
                thisbinunc += ihist.GetBinContent(ibin)**2     #sum assuming correlated
            sfunc = thisbinunc**0.5*MCNormFactor
            if args.nostat:
                MCMean.SetBinError(ibin, sfunc)
                MCErr.SetBinContent(ibin, sfunc)
            else:
                MCMean.SetBinError(ibin, math.sqrt(sfunc**2 + histoErr.GetBinError(ibin)**2))
                MCErr.SetBinContent(ibin, math.sqrt(sfunc**2 + histoErr.GetBinError(ibin)**2))
        
    c = TCanvas("main","main",1200,1200)
    c.SetCanvasSize(1200,1200)
    
    upperCanvas = TPad("up","up",0,0.24,1,1)
    upperCanvas.SetBottomMargin(0.03)
    upperCanvas.SetLeftMargin(0.12)
    upperCanvas.SetTopMargin(0.06)
#    upperCanvas.SetLogy(isLog)
    upperCanvas.Draw()
    upperCanvas.cd()
    
    myStack.Draw()
    myStack.GetYaxis().SetTitle(yTitle)
    myStack.GetHistogram().GetZaxis().SetTitle()

    myStack.GetYaxis().SetLabelSize(0.048)
    myStack.GetYaxis().SetTitleSize(0.07)
    myStack.GetYaxis().SetTitleOffset(0.78)
    myStack.GetYaxis().SetTitleFont(42)
    legend.Draw("same")

    if uncHists!="":
        MCMean.Draw("e2 same")
        MCMean.SetFillColor(kGreen)
        MCMean.SetLineColor(kGreen)
        MCMean.SetMarkerSize(0)
        MCMean.SetFillStyle(3013)
        
    if not args.nostat:
        histoErr.Draw("e2 same")
        # histoErr.Sumw2()
        histoErr.SetFillColor(kGray+3)
        histoErr.SetLineColor(kGray+3)
        histoErr.SetMarkerSize(0)
        histoErr.SetFillStyle(3013)       

    histoMC = myStack.GetStack().Last()
    histoBkg= myStack.GetStack().Before(histoMC)

    print "Total MC:", histoMC.Integral(), ", Data:",histoD.Integral()


    histoD.Draw("same p e")
    legend.AddEntry(histoD,"Data","PE")
    
    for hist in histlist:
        legend.AddEntry(hist,hist.GetName().replace('uds','udsg').replace('b','bottom').replace('c','charm'),'f')
    legend.AddEntry(histoErr,"MC Stat. Unc.",'f')
    if uncHists!="": legend.AddEntry(MCMean,"SF Unc.",'f')
    maxY = histoD.GetMaximum()
    
    #if myStack.GetMinimum() >= 0:
    myStack.SetMinimum(1e-3)
    myStack.SetMaximum(maxY*1.35)


    c.cd()
    lowerCanvas = TPad("down","down",0,0,1,0.26)
    lowerCanvas.Draw()
    lowerCanvas.cd()
    lowerCanvas.SetTicky(1)
    lowerCanvas.SetLeftMargin(0.1)
    lowerCanvas.SetRightMargin(0.1)
    lowerCanvas.SetTopMargin(0.0)
    lowerCanvas.SetBottomMargin(0.4)
    lowerCanvas.SetLeftMargin(0.12)
    lowerCanvas.SetFrameFillStyle(0)
    lowerCanvas.SetFrameBorderMode(0)
    lowerCanvas.SetGridy()

    histoRatio = histoD.Clone()
    histoRatio.Divide(histoMC)
    for ib in range(1,histoRatio.GetNbinsX()+1):
        if histoD.GetBinContent(ib) != 0:
            histoRatio.SetBinError(ib, histoD.GetBinError(ib)/histoD.GetBinContent(ib)*histoRatio.GetBinContent(ib))
        else:
            histoRatio.SetBinError(ib,0.)

    histoRatio.GetYaxis().SetTitle("Data/MC")
    histoRatio.GetYaxis().SetTitleSize(0.15)
    histoRatio.GetYaxis().SetTitleOffset(0.38)
    histoRatio.GetYaxis().SetTitleFont(42)
    histoRatio.GetYaxis().SetLabelSize(0.14)
    histoRatio.GetYaxis().CenterTitle()
    histoRatio.GetYaxis().SetLabelFont(42)
    histoRatio.GetYaxis().SetNdivisions(5)

    histoRatio.GetXaxis().SetTitle(xTitle)
    histoRatio.GetXaxis().SetLabelSize(0.14)
    histoRatio.GetXaxis().SetTitleSize(0.19)
    histoRatio.GetXaxis().SetTitleOffset(0.88)
    histoRatio.GetXaxis().SetTitleFont(42)
    histoRatio.GetXaxis().SetTickLength(0.07)
    histoRatio.GetXaxis().SetLabelFont(42)
    histoRatio.SetTitle("")

    histoRatio.Draw("P e")
    histoRatio.SetMaximum(1.49)
    histoRatio.SetMinimum(0.51)

    if uncHists!="":
        RatioErr = MCErr.Clone()
        RatioErr.Divide(histoMC)
        RatioErrVals = RatioErr.Clone()
        for ibin in range(1,RatioErr.GetNbinsX()+1):
            RatioErrVals.SetBinContent(ibin,1.)
            RatioErrVals.SetBinError(ibin,RatioErr.GetBinContent(ibin))

        RatioErrVals.Draw("e2 same")
        RatioErrVals.SetFillColor(kGreen)
        RatioErrVals.SetLineColor(kGreen)
        RatioErrVals.SetMarkerSize(0)
        RatioErrVals.SetFillStyle(3013)

    if not args.nostat:
        RatioStatUnc = histoMC.Clone()
        for ibin in range(1,RatioStatUnc.GetNbinsX()+1):
            RatioStatUnc.SetBinContent(ibin,1)
            if histoMC.GetBinContent(ibin) != 0:
                RatioStatUnc.SetBinError(ibin,histoMC.GetBinError(ibin)/histoMC.GetBinContent(ibin))
            else:
                RatioStatUnc.SetBinError(ibin,0)

        RatioStatUnc.Draw("e2 same")
        RatioStatUnc.SetFillColor(kGray+3)
        RatioStatUnc.SetLineColor(kGray+3)
        RatioStatUnc.SetMarkerSize(0)
        RatioStatUnc.SetFillStyle(3013) 
    
    histoRatio.Draw("P e same")  # To bring on top

    hLine = TLine(-0.2,1,1,1)
    hLine.SetLineColor(kRed)
    hLine.Draw()

#    if drawDataMCRatioLine:
#        MCCount = histoMC.Integral()
#        if MCCount > 0.:
#            DataCount = histoD.Integral()
#            MCNormFactor = DataCount/MCCount
#            hLine2 = TLine(start,MCNormFactor,end,MCNormFactor)
#            hLine2.SetLineColor(kBlue)
#            hLine2.Draw()

    # ----------------------------------------------------------

    # ======================== LaTeX ==========================
    texTL = TLatex()
    texTL.SetTextSize(0.07)
    texTL.SetTextAlign(13)

    texTR = TLatex()
    texTR.SetTextSize(0.05)
    texTR.SetTextAlign(31)

#    if dataset=="" or noRatio:
#        texTL.DrawLatexNDC(0.13,0.87, "CMS #it{#bf{Preliminary}}")
#        texTR.DrawLatexNDC(0.89,0.91, "#bf{"+str(lumi/1000.)+" fb^{-1} (13 TeV)}")
#    else:
    upperCanvas.cd()
    
    texTL.DrawLatexNDC(0.16,0.92, "CMS") # #it{#bf{Preliminary}}")
    if args.prelim:
        texTL2 = TLatex()
        texTL2.SetTextSize(0.05)
        texTL2.SetTextAlign(13)
        suff = "#it{#bf{Preliminary}}"
        texTL2.DrawLatexNDC(0.16,0.85, suff)

    texTR.DrawLatexNDC(0.89,0.95, "#bf{"+str(lumi/1000.)+" fb^{-1} (13 TeV)}")
    # ----------------------------------------------------------

    c.SaveAs(outName+".pdf")
    c.SaveAs(outName+".png")