Example #1
0
def MakePlots(ev, var, cutmc, cutdata, xtitle, nbins, xlow, xup):
    
    hZH        = TH1F("ZH"       , "", nbins, xlow, xup)
    hWH        = TH1F("WH"       , "", nbins, xlow, xup)
    hWj0b      = TH1F("Wj0b"     , "", nbins, xlow, xup)
    hWj1b      = TH1F("Wj1b"     , "", nbins, xlow, xup)
    hWj2b      = TH1F("Wj2b"     , "", nbins, xlow, xup)
    hZj0b      = TH1F("Zj0b"     , "", nbins, xlow, xup)
    hZj1b      = TH1F("Zj1b"     , "", nbins, xlow, xup)
    hZj2b      = TH1F("Zj2b"     , "", nbins, xlow, xup)
    hTT        = TH1F("TT"       , "", nbins, xlow, xup)
    hs_Top     = TH1F("s_Top"    , "", nbins, xlow, xup)
    hVVLF      = TH1F("VVLF"     , "", nbins, xlow, xup)
    hVVHF      = TH1F("VVHF"     , "", nbins, xlow, xup)
    hQCD       = TH1F("QCD"      , "", nbins, xlow, xup)
    hVH        = TH1F("VH"       , "", nbins, xlow, xup)
    hVV        = TH1F("VV"       , "", nbins, xlow, xup)
    hmc_exp    = TH1F("mc_exp"   , "", nbins, xlow, xup)
    hdata_obs  = TH1F("data_obs" , "", nbins, xlow, xup)

    #ev.ZH.Project("ZH", var, cutmc.GetTitle())
    #ev.WH.Project("WH", var, cutmc.GetTitle())

    # Apply slope
    WJSlopeErr = 0.0020
    ZJSlopeErr = 0.0025
    WJSlope = -0.50
    ZJSlope = -1.00
    cutwjslope = TCut("apply_pt_slope(genW.pt, %f, 150)" % (WJSlope * WJSlopeErr))
    cutzjslope = TCut("apply_pt_slope(genZ.pt, %f, 130)" % (ZJSlope * ZJSlopeErr))

    # Apply scale factors
    scalefactors = (0.930, 2.124, 0.700, 1.175, 2.135, 1.125, 0.991)
    cutsf = TCut("%f" % scalefactors[0])
    ev.Wj0b.Project("Wj0b", var, (cutmc * cutsf * cutwjslope).GetTitle())
    cutsf = TCut("%f" % scalefactors[1])
    ev.Wj1b.Project("Wj1b", var, (cutmc * cutsf * cutwjslope).GetTitle())
    cutsf = TCut("%f" % scalefactors[2])
    ev.Wj2b.Project("Wj2b", var, (cutmc * cutsf * cutwjslope).GetTitle())
    cutsf = TCut("%f" % scalefactors[3])
    #ev.Zj0b.Project("Zj0b", var, (cutmc * cutsf * cutzjslope).GetTitle())
    cutsf = TCut("%f" % scalefactors[4])
    #ev.Zj1b.Project("Zj1b", var, (cutmc * cutsf * cutzjslope).GetTitle())
    cutsf = TCut("%f" % scalefactors[5])
    #ev.Zj2b.Project("Zj2b", var, (cutmc * cutsf * cutzjslope).GetTitle())
    cutsf = TCut("%f" % scalefactors[6])
    ev.TT.Project("TT", var, (cutmc * cutsf).GetTitle())
    
    ev.s_Top.Project("s_Top", var, cutmc.GetTitle())
    ev.VVLF.Project("VVLF", var, cutmc.GetTitle())
    ev.VVHF.Project("VVHF", var, cutmc.GetTitle())

    # Apply QCD scale factor
    scalefactorQCD = 0.005
    cutsf = TCut("%f" % scalefactorQCD)
    #ev.QCD.Project("QCD", var, (cutmc * cutsf).GetTitle())

    ev.data_obs.Project("data_obs", var, cutdata.GetTitle())
    
    hVH.Add(hZH)
    hVH.Add(hWH)

    hVV.Add(hVVLF)
    hVV.Add(hVVHF)

    hmc_exp.Add(hWj0b)
    hmc_exp.Add(hWj1b)
    hmc_exp.Add(hWj2b)
    hmc_exp.Add(hZj0b)
    hmc_exp.Add(hZj1b)
    hmc_exp.Add(hZj2b)
    hmc_exp.Add(hTT)
    hmc_exp.Add(hs_Top)
    hmc_exp.Add(hVV)
    hmc_exp.Add(hQCD)
    
    if True:
        print "MakePlots(): Setting up histograms..."

        # Setup canvas and pads
        c1 = TCanvas("c1", "c1", 700, 700)
        #c1 = TCanvas("c1", "c1", 600, 600)

        pad1 = TPad("pad1", "top pad"   , 0.0, 0.3, 1.0, 1.0)
        pad1.SetBottomMargin(0.0)
        pad1.Draw()
        pad2 = TPad("pad2", "bottom pad", 0.0, 0.0, 1.0, 0.3)
        pad2.SetTopMargin(0.0)
        pad2.SetBottomMargin(0.35)
        pad2.Draw()
        pad1.cd()
        pad1.SetLogy(plotLog)

        # Setup histogram styles and stack the histograms.
        setHisto(hVH, "VH")
        setHisto(hZH, "VH")
        setHisto(hWH, "VH")
        setHisto(hWj0b, "WjLF")
        setHisto(hWj1b, "WjHFc")
        setHisto(hWj2b, "WjHFb")
        setHisto(hZj0b, "ZjLF")
        setHisto(hZj1b, "ZjHFc")
        setHisto(hZj2b, "ZjHFb")
        setHisto(hTT, "TT")
        setHisto(hs_Top, "s_Top")
        setHisto(hVV, "VV")
        setHisto(hVVLF, "VV")
        setHisto(hVVHF, "VVHF")
        setHisto(hQCD, "QCD")
        
        hdata_plot = hdata_obs.Clone("hdata_plot")  # blinded plot
        hmc_test = hmc_exp.Clone("hmc_test")  # for chi2 and KS test
        hdata_plot.Sumw2()
        hmc_test.Sumw2()
        nbins_plot = hdata_plot.GetNbinsX()
        assert(nbins_plot == hmc_test.GetNbinsX())
        if not plotData:  # be blind to the most sensitive bins
            for i in xrange(max(int(nbins_plot*0.75), nbins_plot-5),nbins_plot+2):
                hdata_plot.SetBinContent(i, 0.)
                hdata_plot.SetBinError(i, 0.)
                hmc_test.SetBinContent(i, 0.)
                hmc_test.SetBinError(i, 0.)
                
        
        setHisto(hdata_plot, "data_obs")

        print "MakePlots(): Setting up the stack..."
        hs = THStack("hs", "")
#ifndef VVBDTANALYSIS
        hs.Add(hVV)
#else
        #hs.Add(hVVLF)
        #hs.Add(hVVHF)  // FIXME: should be plotted as signal?
#endif
        hs.Add(hQCD)
        hs.Add(hs_Top)
        hs.Add(hTT)
        hs.Add(hWj0b)
        hs.Add(hWj1b)
        hs.Add(hWj2b)
        hs.Add(hZj0b)
        hs.Add(hZj1b)
        hs.Add(hZj2b)
        if plotSig:
#ifdef VVBDTANALYSIS
        #hs.Add(hVVHF)
#endif
            hs.Add(hVH)
       
        
        ymax = max(hdata_plot.GetMaximum(), hs.GetMaximum())
        hs.SetMaximum(ymax + ymax / 2.0 + (sqrt(ymax) if ymax>1 else 0))
        if plotLog:
            hs.SetMaximum(ymax * 200 + (sqrt(ymax) if ymax>1 else 0))
        hs.SetMinimum(0.01)
        
        # Setup auxiliary histograms
        print "MakePlots(): Setting up auxiliary histograms..."
        staterr = hmc_exp.Clone("staterr")
        staterr.Sumw2()
        staterr.SetFillColor(kRed)
        staterr.SetMarkerSize(0)
        staterr.SetFillStyle(3013)
        
        ratio = hdata_plot.Clone("ratio")
        ratio.Sumw2()
        ratio.SetMarkerSize(0.8)
        #ratio.SetMarkerSize(0.5)
        ratio.Divide(hdata_plot, hmc_exp, 1., 1., "")
        
        ratiostaterr = hmc_exp.Clone("ratiostaterr")
        ratiostaterr.Sumw2()
        ratiostaterr.SetStats(0)
        ratiostaterr.SetTitle("")
        ratiostaterr.GetXaxis().SetTitle(xtitle)
        ratiostaterr.GetYaxis().SetTitle("Data/MC")
        ratiostaterr.SetMaximum(2.2)
        ratiostaterr.SetMinimum(0)
        ratiostaterr.SetMarkerSize(0)
        ratiostaterr.SetFillColor(kRed)
        ratiostaterr.SetFillStyle(3013)
        #ratiostaterr.SetFillStyle(3001)
        ratiostaterr.GetXaxis().CenterTitle()
        ratiostaterr.GetXaxis().SetLabelSize(0.12)
        ratiostaterr.GetXaxis().SetTitleSize(0.14)
        ratiostaterr.GetXaxis().SetTitleOffset(1.10)
        ratiostaterr.GetYaxis().CenterTitle()
        ratiostaterr.GetYaxis().SetLabelSize(0.10)
        ratiostaterr.GetYaxis().SetTitleSize(0.12)
        #ratiostaterr.GetYaxis().SetTitleSize(0.10)
        ratiostaterr.GetYaxis().SetTitleOffset(0.6)
        ratiostaterr.GetYaxis().SetNdivisions(505)
        
        for i in xrange(0, hmc_exp.GetNbinsX()+2):
            ratiostaterr.SetBinContent(i, 1.0)
            if (hmc_exp.GetBinContent(i) > 1e-6):  # use smaller tolerance?
                binerror = hmc_exp.GetBinError(i) / hmc_exp.GetBinContent(i)
                ratiostaterr.SetBinError(i, binerror)
            else:
                ratiostaterr.SetBinError(i, 999.)
           
            
            #if (!(hdata_plot.GetBinContent(i) > 1e-6)):
            #    ratiostaterr.SetBinError(i, 0.)
           
       
        
        ratiosysterr = ratiostaterr.Clone("ratiosysterr")
        ratiosysterr.Sumw2()
        ratiosysterr.SetMarkerSize(0)
        #ratiosysterr.SetFillColor(kBlue)
        ratiosysterr.SetFillColor(kYellow-4)
        #ratiosysterr.SetFillStyle(3002)
        ratiosysterr.SetFillStyle(1001)
        
        for i in xrange(0, hmc_exp.GetNbinsX()+2):
            if (hmc_exp.GetBinContent(i) > 1e-6):  # use smaller tolerance?
#ifndef VVBDTANALYSIS
                binerror2        = (pow(hmc_exp.GetBinError(i), 2) +
                                    pow(0.05 * hWj0b.GetBinContent(i), 2) +
                                    pow(0.25 * hWj1b.GetBinContent(i), 2) +
                                    pow(0.20 * hWj2b.GetBinContent(i), 2) +
                                    pow(0.05 * hZj0b.GetBinContent(i), 2) +
                                    pow(0.15 * hZj1b.GetBinContent(i), 2) +
                                    pow(0.10 * hZj2b.GetBinContent(i), 2) +
                                    pow(0.04 * hTT.GetBinContent(i), 2) +
                                    pow(0.25 * hs_Top.GetBinContent(i), 2) +
#ifndef VVBDTANALYSIS
                                    pow(0.30 * hVV.GetBinContent(i), 2))
#else
                                    #pow(0.30 * hVVLF.GetBinContent(i), 2))
#endif
                binerror = sqrt(binerror2) / hmc_exp.GetBinContent(i)
                ratiosysterr.SetBinError(i, binerror)
            
        

        # Setup legends
        print "MakePlots(): Setting up legends..."
        leg = TLegend(0.72, 0.62, 0.92, 0.92)
        leg.SetFillColor(0)
        leg.SetLineColor(0)
        leg.SetShadowColor(0)
        leg.SetTextFont(62)
        leg.SetTextSize(0.03)
        leg.AddEntry(hdata_plot, "Data", "p")
        if plotSig:
            leg.AddEntry(hVH, "VH(%i)" % massH, "l")
        leg.AddEntry(hZj2b, "Z + b#bar{b}", "f")
        leg.AddEntry(hZj1b, "Z + b", "f")
        leg.AddEntry(hZj0b, "Z + udscg", "f")
        leg.AddEntry(hWj2b, "W + b#bar{b}", "f")
        leg.AddEntry(hWj1b, "W + b", "f")
        leg.AddEntry(hWj0b, "W + udscg", "f")
        leg.AddEntry(hTT, "t#bar{t}", "f")
        leg.AddEntry(hs_Top, "single top", "f")
        leg.AddEntry(hQCD, "QCD", "f")
#ifndef VVBDTANALYSIS
        leg.AddEntry(hVV, "VV", "f")
#else
        #leg.AddEntry(hVVHF, "VV(b#bar{b})", "f")
        #leg.AddEntry(hVVLF, "VV(udscg)", "f")
#endif
        leg.AddEntry(staterr, "MC uncert. (stat)", "fl")

        ratioleg1 = TLegend(0.54, 0.86, 0.72, 0.96)
        #ratioleg1 = TLegend(0.50, 0.86, 0.69, 0.96)
        ratioleg1.AddEntry(ratiostaterr, "MC uncert. (stat)", "f")
        ratioleg1.SetFillColor(0)
        ratioleg1.SetLineColor(0)
        ratioleg1.SetShadowColor(0)
        ratioleg1.SetTextFont(62)
        ratioleg1.SetTextSize(0.06)
        ratioleg1.SetBorderSize(1)
        
        ratioleg2 = TLegend(0.72, 0.86, 0.95, 0.96)
        #ratioleg2 = TLegend(0.69, 0.86, 0.9, 0.96)
        ratioleg2.AddEntry(ratiosysterr, "MC uncert. (stat+syst)", "f")
        ratioleg2.SetFillColor(0)
        ratioleg2.SetLineColor(0)
        ratioleg2.SetShadowColor(0)
        ratioleg2.SetTextFont(62)
        ratioleg2.SetTextSize(0.06)
        ratioleg2.SetBorderSize(1)

        # Draw MC signal and background
        print "MakePlots(): Drawing..."
        pad1.cd()
        if plotLog: pad1.SetLogy()
        hs.Draw("hist")
        hs.GetXaxis().SetLabelSize(0)
        binwidth = (xup - xlow) / nbins_plot
        ytitle = "Events / %.3f" % binwidth
        hs.GetYaxis().SetTitle(ytitle)
        
        staterr.Draw("e2 same")
        if plotSig:
            hVH.SetLineWidth(3)
            hVH.SetFillColor(0)
            hVH.Draw("hist same")
       
        
        # Draw data
        hdata_plot.Draw("e1 same")
        
        # Draw legends
        leg.Draw()
        latex = TLatex()
        latex.SetNDC()
        latex.SetTextAlign(12)
        latex.SetTextFont(62)
        latex.SetTextSize(0.052)
        latex.DrawLatex(0.19, 0.89, "CMS Preliminary")
        latex.SetTextSize(0.04)
        latex.DrawLatex(0.19, 0.84, "#sqrt{s} = 8 TeV, L = 19.0 fb^{-1}")
        latex.DrawLatex(0.19, 0.79, "Z(#nu#bar{#nu})H(b#bar{b})")
        
        # Under/overflows a la TMVA
        uoflow = "U/O-flow (Data,MC): (%.1f, %.1f) / (%.1f, %.1f)" % (hdata_plot.GetBinContent(0), hmc_exp.GetBinContent(0), hdata_plot.GetBinContent(nbins_plot+1), hmc_exp.GetBinContent(nbins_plot+1))
        latex2 = TLatex(0.99, 0.1, uoflow)
        latex2.SetNDC()
        latex2.SetTextSize(0.02)
        latex2.SetTextAngle(90)
        latex2.AppendPad()
        
        # Draw ratio
        pad2.cd()
        pad2.SetGridy(0)
        ratiostaterr.Draw("e2")
        ratiosysterr.Draw("e2 same")
        ratiostaterr.Draw("e2 same")
        ratio.Draw("e1 same")
        
        # Draw ratio legends
        ratioleg1.Draw()
        ratioleg2.Draw()

        # Kolmogorov-Smirnov test and Chi2 test
        pave = TPaveText(0.18, 0.85, 0.35, 0.96, "brNDC")
        pave.SetTextAlign(12)
        pave.SetLineColor(0)
        pave.SetFillColor(0)
        pave.SetShadowColor(0)
        pave.SetBorderSize(1)
        nchisq = hdata_plot.Chi2Test(hmc_test, "UWCHI2/NDF")  # MC uncert. (stat)
        kolprob = hdata_plot.KolmogorovTest(hmc_test)  # MC uncert. (stat)
        text = pave.AddText("#chi_{#nu}^{2} = %.3f, K_{s} = %.3f" % (nchisq, kolprob))
        text.SetTextFont(62)
        text.SetTextSize(0.06)
        pave.Draw()

        print "MakePlots(): Printing..."
        pad1.cd()
        gPad.RedrawAxis()
        gPad.Modified()
        gPad.Update()
        pad2.cd()
        gPad.RedrawAxis()
        gPad.Modified()
        gPad.Update()
        c1.cd()
        plotname = TString("%s_%s_%s" % (channel, region, var))
        FormatFileName(plotname)
        gPad.Print(plotdir+plotname.Data()+".png")
        gPad.Print(plotdir+plotname.Data()+".pdf")
        
    return 0
Example #2
0
def MakePlots(varname, whatfit, options):
    if whatfit not in ["prefit", "postfit"]:
        raise ValueError("whatfit must be either 'prefit' or 'postfit': %s" % whatfit)
    
    massH    = 125
    plotData = True
    plotLog  = True
    plotSig  = True
    plotdir  = "./"
    kRed     = 632
    kYellow  = 400
    kBlue    = 600
    kGray    = 920
    
    if options.doMJJ:
        plotLog = False
    
    tfile = TFile.Open(options.outname, "READ")
    hZH        = tfile.Get(varname+"_"+"ZH_SM"+"_%s" % whatfit)
    hWH        = tfile.Get(varname+"_"+"WH_SM"+"_%s" % whatfit)
    hZbbHinv   = tfile.Get(varname+"_"+"ZH"+"_%s" % whatfit)
    hWj0b      = tfile.Get(varname+"_"+"Wj0b"+"_%s" % whatfit)
    hWj1b      = tfile.Get(varname+"_"+"Wj1b"+"_%s" % whatfit)
    hWj2b      = tfile.Get(varname+"_"+"Wj2b"+"_%s" % whatfit)
    hZj0b      = tfile.Get(varname+"_"+"Zj0b"+"_%s" % whatfit)
    hZj1b      = tfile.Get(varname+"_"+"Zj1b"+"_%s" % whatfit)
    hZj2b      = tfile.Get(varname+"_"+"Zj2b"+"_%s" % whatfit)
    hTT        = tfile.Get(varname+"_"+"TT"+"_%s" % whatfit)
    hs_Top     = tfile.Get(varname+"_"+"s_Top"+"_%s" % whatfit)
    hVVLF      = tfile.Get(varname+"_"+"VVLF"+"_%s" % whatfit)
    hWZHF      = tfile.Get(varname+"_"+"WZ"+"_%s" % whatfit)
    hZZHF      = tfile.Get(varname+"_"+"ZZ"+"_%s" % whatfit)
    hQCD       = tfile.Get(varname+"_"+"QCD"+"_%s" % whatfit)
    hdata_obs  = tfile.Get(varname+"_"+"data_obs")
    
    eZH        = tfile.Get(varname+"_"+"ZH_SM"+"_err%s" % whatfit)
    eWH        = tfile.Get(varname+"_"+"WH_SM"+"_err%s" % whatfit)
    eZbbHinv   = tfile.Get(varname+"_"+"ZbbHinv"+"_err%s" % whatfit)
    eWj0b      = tfile.Get(varname+"_"+"Wj0b"+"_err%s" % whatfit)
    eWj1b      = tfile.Get(varname+"_"+"Wj1b"+"_err%s" % whatfit)
    eWj2b      = tfile.Get(varname+"_"+"Wj2b"+"_err%s" % whatfit)
    eZj0b      = tfile.Get(varname+"_"+"Zj0b"+"_err%s" % whatfit)
    eZj1b      = tfile.Get(varname+"_"+"Zj1b"+"_err%s" % whatfit)
    eZj2b      = tfile.Get(varname+"_"+"Zj2b"+"_err%s" % whatfit)
    eTT        = tfile.Get(varname+"_"+"TT"+"_err%s" % whatfit)
    es_Top     = tfile.Get(varname+"_"+"s_Top"+"_err%s" % whatfit)
    eVVLF      = tfile.Get(varname+"_"+"VVLF"+"_err%s" % whatfit)
    eWZHF      = tfile.Get(varname+"_"+"WZ"+"_err%s" % whatfit)
    eZZHF      = tfile.Get(varname+"_"+"ZZ"+"_err%s" % whatfit)
    eQCD       = tfile.Get(varname+"_"+"QCD"+"_err%s" % whatfit)
    
    varname = hdata_obs.GetXaxis().GetTitle()
    xtitle = "BDT"
    if options.doMJJ:  xtitle = "m_{T} [GeV]"
    nbins = hdata_obs.GetNbinsX()
    xlow = hdata_obs.GetXaxis().GetXmin()
    xup = hdata_obs.GetXaxis().GetXmax()
    hVH        = TH1F("VH"       , "", nbins, xlow, xup)
    hVVHF      = TH1F("VVHF"     , "", nbins, xlow, xup)
    hVV        = TH1F("VV"       , "", nbins, xlow, xup)
    hmc_exp    = TH1F("mc_exp"   , "", nbins, xlow, xup)
    
    eVH        = TH1F("VH"+"_err"   , "", nbins, xlow, xup)
    eVVHF      = TH1F("VVHF"+"_err" , "", nbins, xlow, xup)
    eVV        = TH1F("VV"+"_err"   , "", nbins, xlow, xup)
    
    # Make sum of histograms
    hVH.Add(hZH)
    hVH.Add(hWH)
    eVH.Add(eZH)
    eVH.Add(eWH)

    hVVHF.Add(hWZHF)
    hVVHF.Add(hZZHF)
    eVVHF.Add(eWZHF)
    eVVHF.Add(eZZHF)

    hVV.Add(hVVLF)
    hVV.Add(hVVHF)
    eVV.Add(eVVLF)
    eVV.Add(eVVHF)

    hmc_exp.Add(hWj0b)
    hmc_exp.Add(hWj1b)
    hmc_exp.Add(hWj2b)
    hmc_exp.Add(hZj0b)
    hmc_exp.Add(hZj1b)
    hmc_exp.Add(hZj2b)
    hmc_exp.Add(hTT)
    hmc_exp.Add(hs_Top)
    if not options.doVV:
        hmc_exp.Add(hVVLF)
        hmc_exp.Add(hVVHF)
        hmc_exp.Add(hVH)  # VH is counted as background
    else:
        hmc_exp.Add(hVVLF)
        hmc_exp.Add(hVH)  # VH is counted as background
    hmc_exp.Add(hQCD)
    
    if True:
        print "MakePlots(): Setting up histograms..."

        # Setup canvas and pads
        c1 = TCanvas("c1", "c1", 700, 700)
        #c1 = TCanvas("c1", "c1", 600, 600)

        pad1 = TPad("pad1", "top pad"   , 0.0, 0.3, 1.0, 1.0)
        pad1.SetBottomMargin(0.0)
        pad1.Draw()
        pad2 = TPad("pad2", "bottom pad", 0.0, 0.0, 1.0, 0.3)
        pad2.SetTopMargin(0.0)
        pad2.SetBottomMargin(0.35)
        pad2.Draw()
        pad1.cd()
        pad1.SetLogy(plotLog)

        # Setup histogram styles and stack the histograms.
        setHisto(hVH, "VH")
        setHisto(hZH, "VH")
        setHisto(hWH, "VH")
        setHisto(hZbbHinv, "VH_1")
        setHisto(hWj0b, "WjLF")
        setHisto(hWj1b, "WjHFc")
        setHisto(hWj2b, "WjHFb")
        setHisto(hZj0b, "ZjLF")
        setHisto(hZj1b, "ZjHFc")
        setHisto(hZj2b, "ZjHFb")
        setHisto(hTT, "TT")
        setHisto(hs_Top, "s_Top")
        setHisto(hVV, "VV")
        setHisto(hVVLF, "VV")
        setHisto(hVVHF, "VVHF")
        setHisto(hQCD, "QCD")
        
        hdata_test = hdata_obs.Clone("hdata_test")  # blinded plot
        hmc_test = hmc_exp.Clone("hmc_test")  # for chi2 and KS test
        hdata_test.Sumw2()
        hmc_test.Sumw2()
        nbins_plot = hdata_test.GetNbinsX()
        assert(nbins_plot == hmc_test.GetNbinsX())
        if not plotData:  # be blind to the most sensitive bins
            if not options.doMJJ:
                for i in xrange(max(int(nbins_plot*0.75), nbins_plot-5),nbins_plot+2):
                    hdata_test.SetBinContent(i, 0.)
                    hdata_test.SetBinError(i, 0.)
                    hmc_test.SetBinContent(i, 0.)
                    hmc_test.SetBinError(i, 0.)
            
            else:
                #for i in xrange(hdata_test.FindFixBin(105+1), hdata_test.FindFixBin(150-1)+1):
                #for i in xrange(hdata_test.FindFixBin(105+1)-2, hdata_test.FindFixBin(150-1)+1):  # hide VV as well
                for i in xrange(1, hdata_test.GetNbinsX()+1):  # all bins
                    hdata_test.SetBinContent(i, 0.)
                    hdata_test.SetBinError(i, 0.)
                    hmc_test.SetBinContent(i, 0.)
                    hmc_test.SetBinError(i, 0.)
        
        
        
        setHisto(hdata_test, "data_obs")

        print "MakePlots(): Setting up the stack..."
        hs = THStack("hs", "")
        if not options.doVV and not options.doMJJ:
            hs.Add(hVVHF)
            hs.Add(hVVLF)
        elif not options.doMJJ:
            hs.Add(hVVLF)
        
        hs.Add(hQCD)
        hs.Add(hs_Top)
        hs.Add(hTT)
        hs.Add(hWj0b)
        hs.Add(hWj1b)
        hs.Add(hWj2b)
        hs.Add(hZj0b)
        hs.Add(hZj1b)
        hs.Add(hZj2b)
        if not options.doVV and not options.doMJJ:
            if plotSig:  hs.Add(hVH)
            if plotSig:  hs.Add(hZbbHinv)
        elif not options.doMJJ:
            hs.Add(hVVHF)
            if plotSig:  hs.Add(hVH)
            if plotSig:  hs.Add(hZbbHinv)

        if options.doMJJ:
            hs.Add(hVVLF)
            hs.Add(hVVHF)
            if plotSig:  hs.Add(hVH)
            if plotSig:  hs.Add(hZbbHinv)


        ymax = max(hdata_test.GetMaximum(), hs.GetMaximum())
        hs.SetMaximum(ymax * 1.7 + (sqrt(ymax) if ymax>1 else 0))
        if plotLog:
            hs.SetMaximum(ymax * 200 + (sqrt(ymax) if ymax>1 else 0))
        hs.SetMinimum(0.01)
        
        # Setup auxiliary histograms
        print "MakePlots(): Setting up auxiliary histograms..."
        staterr = hmc_exp.Clone("staterr")
        staterr.Sumw2()
        #staterr.SetFillColor(kRed)
        staterr.SetFillColor(kGray+3)
        staterr.SetMarkerSize(0)
        staterr.SetFillStyle(3013)
        
        ratio = hdata_test.Clone("ratio")
        ratio.Sumw2()
        ratio.SetMarkerSize(0.8)
        #ratio.SetMarkerSize(0.5)
        ratio.Divide(hdata_test, hmc_exp, 1., 1., "")
        
        ratiostaterr = hmc_exp.Clone("ratiostaterr")
        ratiostaterr.Sumw2()
        ratiostaterr.SetStats(0)
        ratiostaterr.SetTitle("")
        #ratiostaterr.GetXaxis().SetTitle(xtitle)
        ratiostaterr.SetTitle(";"+xtitle)
        ratiostaterr.GetYaxis().SetTitle("Data/MC")
        ratiostaterr.SetMaximum(2.2)
        ratiostaterr.SetMinimum(0)
        ratiostaterr.SetMarkerSize(0)
        #ratiostaterr.SetFillColor(kRed)
        ratiostaterr.SetFillColor(kGray+3)
        ratiostaterr.SetFillStyle(3013)
        #ratiostaterr.SetFillStyle(3001)
        #ratiostaterr.GetXaxis().CenterTitle()
        ratiostaterr.GetXaxis().SetLabelSize(0.12)
        ratiostaterr.GetXaxis().SetTitleSize(0.14)
        ratiostaterr.GetXaxis().SetTitleOffset(1.10)
        ratiostaterr.GetYaxis().CenterTitle()
        ratiostaterr.GetYaxis().SetLabelSize(0.10)
        ratiostaterr.GetYaxis().SetTitleSize(0.12)
        #ratiostaterr.GetYaxis().SetTitleSize(0.10)
        ratiostaterr.GetYaxis().SetTitleOffset(0.6)
        ratiostaterr.GetYaxis().SetNdivisions(505)
        
        ratiounity = TLine(xlow,1,xup,1)
        ratiounity.SetLineStyle(2)
        
        for i in xrange(0, hmc_exp.GetNbinsX()+2):
            ratiostaterr.SetBinContent(i, 1.0)
            if (hmc_exp.GetBinContent(i) > 1e-6):  # use smaller tolerance?
                binerror = hmc_exp.GetBinError(i) / hmc_exp.GetBinContent(i)
                ratiostaterr.SetBinError(i, binerror)
            else:
                ratiostaterr.SetBinError(i, 999.)
           
            
            #if (!(hdata_test.GetBinContent(i) > 1e-6)):
            #    ratiostaterr.SetBinError(i, 0.)
        
        
        
        ratiosysterr = ratiostaterr.Clone("ratiosysterr")
        ratiosysterr.Sumw2()
        ratiosysterr.SetMarkerSize(0)
        #ratiosysterr.SetFillColor(kBlue)
        ratiosysterr.SetFillColor(kYellow-4)
        #ratiosysterr.SetFillStyle(3002)
        ratiosysterr.SetFillStyle(1001)
        
        for i in xrange(1, hmc_exp.GetNbinsX()+1):
            if hmc_exp.GetBinContent(i) > 1e-6:  # use smaller tolerance?

                binerror2        = (pow(hmc_exp.GetBinError(i), 2) +
                                    pow(eWj0b.GetBinContent(i), 2) +
                                    pow(eWj1b.GetBinContent(i), 2) +
                                    pow(eWj2b.GetBinContent(i), 2) +
                                    pow(eZj0b.GetBinContent(i), 2) +
                                    pow(eZj1b.GetBinContent(i), 2) +
                                    pow(eZj2b.GetBinContent(i), 2) +
                                    pow(eTT.GetBinContent(i), 2) +
                                    pow(es_Top.GetBinContent(i), 2))
                if not options.doVV:
                    binerror2 += pow(eVVLF.GetBinContent(i), 2)
                    binerror2 += pow(eVVHF.GetBinContent(i), 2)
                    binerror2 += pow(eVH.GetBinContent(i), 2)
                else:
                    binerror2 += pow(eVVLF.GetBinContent(i), 2)
                    binerror2 += pow(eVH.GetBinContent(i), 2)

                binerror = sqrt(binerror2)
                ratiosysterr.SetBinError(i, binerror / hmc_exp.GetBinContent(i))
                
                if hmc_test.GetBinContent(i) > 1e-6:  # use smaller tolerance?
                    hmc_test.SetBinError(i, binerror)
                if hdata_test.GetBinContent(i) > 1e-6:
                    hdata_test.SetBinError(i, sqrt(hdata_test.GetBinContent(i)))
            
        

        # Setup legends
        print "MakePlots(): Setting up legends..."
        #leg = TLegend(0.74, 0.56, 0.92, 0.92)
        #leg.SetFillColor(0)
        #leg.SetLineColor(0)
        #leg.SetShadowColor(0)
        #leg.SetTextFont(62)
        #leg.SetTextSize(0.03)
        #leg.AddEntry(hdata_test, "Data", "p")
        #if plotSig:  leg.AddEntry(hVH, "VH(%i)" % massH, "l")
        #if plotSig:  leg.AddEntry(hZbbHinv, "ZH(inv)", "l")
        #if options.doVV:
        #    leg.AddEntry(hVVHF, "VV(b#bar{b})", "l")
        #
        #leg.AddEntry(hZj2b, "Z + b#bar{b}", "f")
        #leg.AddEntry(hZj1b, "Z + b", "f")
        #leg.AddEntry(hZj0b, "Z + udscg", "f")
        #leg.AddEntry(hWj2b, "W + b#bar{b}", "f")
        #leg.AddEntry(hWj1b, "W + b", "f")
        #leg.AddEntry(hWj0b, "W + udscg", "f")
        #leg.AddEntry(hTT, "t#bar{t}", "f")
        #leg.AddEntry(hs_Top, "single top", "f")
        #leg.AddEntry(hQCD, "QCD", "f")
        #leg.AddEntry(hVVLF, "VV(udscg)", "f")
        #if not options.doVV:
        #    leg.AddEntry(hVVHF, "VV(b#bar{b})", "f")
        #
        #leg.AddEntry(staterr, "MC uncert. (stat)", "fl")

        #leg1 = TLegend(0.58, 0.68, 0.76, 0.92)
        leg1 = TLegend(0.50, 0.60, 0.72, 0.92)
        leg1.SetFillColor(0)
        leg1.SetLineColor(0)
        leg1.SetShadowColor(0)
        leg1.SetTextFont(62)
        leg1.SetTextSize(0.03)
        leg1.AddEntry(hdata_test, "Data", "p")
        if plotSig:  leg1.AddEntry(hVH, "VH(%i)" % massH, "l")
        if plotSig:  leg1.AddEntry(hZbbHinv, "ZH(inv)", "l")
        if options.doVV:
            leg1.AddEntry(hVVHF, "VV(b#bar{b})", "l")
        
        leg1.AddEntry(hTT, "t#bar{t}", "f")
        leg1.AddEntry(hs_Top, "single top", "f")
        leg1.AddEntry(hQCD, "QCD", "f")
        leg1.AddEntry(hVVLF, "VV(udscg)", "f")
        if not options.doVV:
            leg1.AddEntry(hVVHF, "VZ(b#bar{b})", "f")


        #leg2 = TLegend(0.72, 0.60, 0.94, 0.92)
        leg2 = TLegend(0.72, 0.60, 0.94, 0.88)
        leg2.SetFillColor(0)
        leg2.SetLineColor(0)
        leg2.SetShadowColor(0)
        leg2.SetTextFont(62)
        leg2.SetTextSize(0.03)
        leg2.AddEntry(hWj2b, "W + b#bar{b}", "f")
        leg2.AddEntry(hWj1b, "W + b", "f")
        leg2.AddEntry(hWj0b, "W + udscg", "f")
        leg2.AddEntry(hZj2b, "Z + b#bar{b}", "f")
        leg2.AddEntry(hZj1b, "Z + b", "f")
        leg2.AddEntry(hZj0b, "Z + udscg", "f")
        leg2.AddEntry(staterr, "MC uncert. (stat)", "f")

        ratioleg1 = TLegend(0.54, 0.88, 0.72, 0.96)
        #ratioleg1 = TLegend(0.50, 0.86, 0.69, 0.96)
        ratioleg1.AddEntry(ratiostaterr, "MC uncert. (stat)", "f")
        ratioleg1.SetFillColor(0)
        ratioleg1.SetLineColor(0)
        ratioleg1.SetShadowColor(0)
        ratioleg1.SetTextFont(62)
        ratioleg1.SetTextSize(0.06)
        ratioleg1.SetBorderSize(1)
        
        ratioleg2 = TLegend(0.72, 0.88, 0.95, 0.96)
        #ratioleg2 = TLegend(0.69, 0.86, 0.9, 0.96)
        ratioleg2.AddEntry(ratiosysterr, "MC uncert. (stat+syst)", "f")
        ratioleg2.SetFillColor(0)
        ratioleg2.SetLineColor(0)
        ratioleg2.SetShadowColor(0)
        ratioleg2.SetTextFont(62)
        ratioleg2.SetTextSize(0.06)
        ratioleg2.SetBorderSize(1)
        
        #ratioleg1 = TLegend(0.72, 0.88, 0.94, 0.96)
        ##ratioleg1 = TLegend(0.50, 0.86, 0.69, 0.96)
        #ratioleg1.AddEntry(ratiostaterr, "MC uncert. (stat)", "f")
        #ratioleg1.SetFillColor(0)
        #ratioleg1.SetLineColor(0)
        #ratioleg1.SetShadowColor(0)
        #ratioleg1.SetTextFont(62)
        ##ratioleg1.SetTextSize(0.06)
        #ratioleg1.SetTextSize(0.07)
        #ratioleg1.SetBorderSize(1)

        # Draw MC signal and background
        print "MakePlots(): Drawing..."
        pad1.cd()
        if plotLog: pad1.SetLogy()
        hs.Draw("hist")
        hs.GetXaxis().SetLabelSize(0)
        binwidth = (xup - xlow) / nbins_plot
        ytitle = "Events / %.3f" % binwidth
        hs.GetYaxis().SetTitle(ytitle)
        if " ; " in xtitle:
            hs.SetTitle(";"+xtitle)
        
        staterr.Draw("e2 same")
        if plotSig:
            hVH.SetLineWidth(3)
            hVH.SetFillColor(0)
            hVH.Draw("hist same")

        if plotSig:
            hZbbHinv.SetLineWidth(3)
            hZbbHinv.SetFillColor(0)
            hZbbHinv.Draw("hist same")

        if options.doVV:
            hVVHF.SetLineWidth(3)
            hVVHF.SetLineColor(kGray + 2)
            hVVHF.SetFillColor(0)
            hVVHF.Draw("hist same")

        
        # Draw data
        hdata_test.Draw("e1 same")
        
        # Draw legends
        #leg.Draw()
        leg1.Draw()
        leg2.Draw()
        latex = TLatex()
        latex.SetNDC()
        latex.SetTextAlign(12)
        latex.SetTextFont(62)
        latex.SetTextSize(0.052)
        latex.DrawLatex(0.19, 0.89, "CMS Preliminary")
        latex.SetTextSize(0.04)

        latex.DrawLatex(0.19, 0.84, "#sqrt{s} = 8 TeV, L = 18.9 fb^{-1}")




        #latex.DrawLatex(0.19, 0.79, "Z(#nu#bar{#nu})H(b#bar{b})")
        latex.DrawLatex(0.19, 0.79, "Z(b#bar{b})H(inv)")
        
        # Under/overflows a la TMVA
        #uoflow = "U/O-flow (Data,MC): (%.1f, %.1f) / (%.1f, %.1f)" % (hdata_test.GetBinContent(0), hmc_exp.GetBinContent(0), hdata_test.GetBinContent(nbins_plot+1), hmc_exp.GetBinContent(nbins_plot+1))
        #latex2 = TLatex(0.99, 0.1, uoflow)
        #latex2.SetNDC()
        #latex2.SetTextSize(0.02)
        #latex2.SetTextAngle(90)
        #latex2.AppendPad()
        
        # Draw ratio
        pad2.cd()
        pad2.SetGridy(0)
        ratiostaterr.Draw("e2")
        ratiosysterr.Draw("e2 same")
        ratiostaterr.Draw("e2 same")
        ratiounity.Draw()
        ratio.Draw("e1 same")
        
        # Draw ratio legends
        ratioleg1.Draw()
        ratioleg2.Draw()

        # Kolmogorov-Smirnov test and Chi2 test
        pave = TPaveText(0.18, 0.86, 0.35, 0.96, "brNDC")
        #pave = TPaveText(0.18, 0.86, 0.28, 0.96, "brNDC")
        pave.SetTextAlign(12)
        pave.SetLineColor(0)
        pave.SetFillColor(0)
        pave.SetShadowColor(0)
        pave.SetBorderSize(1)
        nchisq = hdata_test.Chi2Test(hmc_test, "UWCHI2/NDF")  # MC uncert. (stat)
        kolprob = hdata_test.KolmogorovTest(hmc_test)  # MC uncert. (stat)
        text = pave.AddText("#chi_{#nu}^{2} = %.3f, K_{s} = %.3f" % (nchisq, kolprob))
        #text = pave.AddText("#chi_{#nu}^{2} = %.3f" % (nchisq))
        text.SetTextFont(62)
        text.SetTextSize(0.06)
        #text.SetTextSize(0.07)
        pave.Draw()

        print "MakePlots(): Printing..."
        pad1.cd()
        gPad.RedrawAxis()
        gPad.Modified()
        gPad.Update()
        pad2.cd()
        gPad.RedrawAxis()
        gPad.Modified()
        gPad.Update()
        c1.cd()
        #plotname = TString("%s_%s_%s" % (channel, region, var))
        #FormatFileName(plotname)
        #gPad.Print(plotdir+plotname.Data()+".png")
        #gPad.Print(plotdir+plotname.Data()+".pdf")
        gPad.Print(varname+"_%s.png" % whatfit)
        gPad.Print(varname+"_%s.pdf" % whatfit)

        
    return 0
Example #3
0
def MakePlots(ev, var, cutmc, cutdata, xtitle, nbins, xlow, xup):
    hP1 = TH1F("P1", "", nbins, xlow, xup)
    hP2 = TH1F("P2", "", nbins, xlow, xup)
    hP3 = TH1F("P3", "", nbins, xlow, xup)

    # Apply scale factors
    scalefactor1 = 1.0
    cutsf = TCut("%f" % scalefactor1)
    # ev.P1.Project("P1", var, (cutmc * cutsf).GetTitle())
    ev.P1.Project("P1", var, (cutdata).GetTitle())
    scalefactor2 = 1.0
    cutsf = TCut("%f" % scalefactor2)
    # ev.P2.Project("P2", var, (cutmc * cutsf).GetTitle())
    ev.P2.Project("P2", var, (cutdata).GetTitle())
    scalefactor3 = 1.0
    cutsf = TCut("%f" % scalefactor3)
    # ev.P3.Project("P3", var, (cutmc * cutsf).GetTitle())
    ev.P3.Project("P3", var, (cutdata).GetTitle())

    if True:
        print "MakePlots(): Setting up histograms..."

        # Setup canvas and pads
        c1 = TCanvas("c1", "c1", 700, 700)
        # c1 = TCanvas("c1", "c1", 600, 600)

        pad1 = TPad("pad1", "top pad", 0.0, 0.3, 1.0, 1.0)
        pad1.SetBottomMargin(0.0)
        pad1.Draw()
        pad2 = TPad("pad2", "bottom pad", 0.0, 0.0, 1.0, 0.3)
        pad2.SetTopMargin(0.0)
        pad2.SetBottomMargin(0.35)
        pad2.Draw()
        pad1.cd()
        pad1.SetLogy(plotLog)

        # Setup histogram styles and stack the histograms.
        setHisto(hP1, "data_obs")
        setHisto(hP2, "VH")
        setHisto(hP3, "TT")

        hP1.Scale(1.0 / hP1.GetSumOfWeights())
        hP2.Scale(1.0 / hP2.GetSumOfWeights())
        hP3.Scale(1.0 / hP3.GetSumOfWeights())

        ymax = max(hP1.GetMaximum(), hP2.GetMaximum(), hP3.GetMaximum())
        hP1.SetMaximum(ymax + ymax / 2.0 + (sqrt(ymax) if ymax > 1 else 0))
        if plotLog:
            hP1.SetMaximum(ymax * 200 + (sqrt(ymax) if ymax > 1 else 0))
        # hP1.SetMinimum(0.01)

        # Setup auxiliary histograms
        print "MakePlots(): Setting up auxiliary histograms..."

        ratio = hP2.Clone("ratio")
        ratio.Sumw2()
        ratio.SetMarkerSize(0.8)
        # ratio.SetMarkerSize(0.5)
        ratio.Divide(hP2, hP1, 1.0, 1.0, "")

        ratioP3 = hP3.Clone("ratio3")
        ratioP3.Sumw2()
        ratioP3.SetMarkerSize(0.8)
        # ratioP3.SetMarkerSize(0.5)
        ratioP3.Divide(hP3, hP1, 1.0, 1.0, "")

        ratio.SetStats(0)
        ratio.SetTitle("")
        ratio.GetXaxis().SetTitle(xtitle)
        ratio.GetYaxis().SetTitle("ratio")
        ratio.SetMaximum(2.2)
        ratio.SetMinimum(0)
        # ratio.SetMarkerSize(0)
        # ratio.SetFillColor(kRed)
        # ratio.SetFillStyle(3013)
        ##ratio.SetFillStyle(3001)
        ratio.GetXaxis().CenterTitle()
        ratio.GetXaxis().SetLabelSize(0.12)
        ratio.GetXaxis().SetTitleSize(0.14)
        ratio.GetXaxis().SetTitleOffset(1.10)
        ratio.GetYaxis().CenterTitle()
        ratio.GetYaxis().SetLabelSize(0.10)
        ratio.GetYaxis().SetTitleSize(0.12)
        # ratio.GetYaxis().SetTitleSize(0.10)
        ratio.GetYaxis().SetTitleOffset(0.6)
        ratio.GetYaxis().SetNdivisions(505)

        # Setup legends
        print "MakePlots(): Setting up legends..."
        leg = TLegend(0.62, 0.62, 0.92, 0.92)
        leg.SetFillColor(0)
        leg.SetLineColor(0)
        leg.SetShadowColor(0)
        leg.SetTextFont(62)
        leg.SetTextSize(0.03)
        leg.AddEntry(hP1, "2012ABC", "lp")
        leg.AddEntry(hP2, "2012D ![207883:208307]", "lp")
        leg.AddEntry(hP3, "2012D [207883:208307]", "lp")

        # Draw MC signal and background
        print "MakePlots(): Drawing..."
        pad1.cd()
        if plotLog:
            pad1.SetLogy()
        hP1.Draw("e1")
        hP1.GetXaxis().SetLabelSize(0)
        binwidth = (xup - xlow) / nbins
        ytitle = "(normalized) / %.3f" % binwidth
        hP1.GetYaxis().SetTitle(ytitle)

        hP3.Draw("e1 same")
        hP2.Draw("e1 same")

        # Draw legends
        leg.Draw()
        latex = TLatex()
        latex.SetNDC()
        latex.SetTextAlign(12)
        latex.SetTextFont(62)
        latex.SetTextSize(0.052)
        latex.DrawLatex(0.19, 0.89, "CMS Preliminary")
        latex.SetTextSize(0.04)
        latex.DrawLatex(0.19, 0.84, "#sqrt{s} = 8 TeV, L = 19.6 fb^{-1}")
        latex.DrawLatex(0.19, 0.79, "Z(#nu#bar{#nu})H(b#bar{b})")

        # Under/overflows a la TMVA
        uoflow = "U/O-flow (Data,MC): (%.1f, %.1f) / (%.1f, %.1f)" % (
            hP2.GetBinContent(0),
            hP1.GetBinContent(0),
            hP2.GetBinContent(nbins + 1),
            hP1.GetBinContent(nbins + 1),
        )
        latex2 = TLatex(0.99, 0.1, uoflow)
        latex2.SetNDC()
        latex2.SetTextSize(0.02)
        latex2.SetTextAngle(90)
        latex2.AppendPad()

        # Draw ratio
        pad2.cd()
        pad2.SetGridy(0)
        ratio.Draw("e1")

        ratioP3.Draw("e1 same")
        ratio.Draw("e1 same")

        # Kolmogorov-Smirnov test and Chi2 test
        pave = TPaveText(0.18, 0.85, 0.35, 0.96, "brNDC")
        pave.SetTextAlign(12)
        pave.SetLineColor(0)
        pave.SetFillColor(0)
        pave.SetShadowColor(0)
        pave.SetBorderSize(1)
        nchisq = hP2.Chi2Test(hP1, "UWCHI2/NDF")  # MC uncert. (stat)
        kolprob = hP2.KolmogorovTest(hP1)  # MC uncert. (stat)
        text = pave.AddText("#chi_{#nu}^{2} = %.3f, K_{s} = %.3f" % (nchisq, kolprob))
        text.SetTextFont(62)
        text.SetTextSize(0.06)
        pave.Draw()

        print "MakePlots(): Printing..."
        pad1.cd()
        gPad.RedrawAxis()
        gPad.Modified()
        gPad.Update()
        pad2.cd()
        gPad.RedrawAxis()
        gPad.Modified()
        gPad.Update()
        c1.cd()
        plotname = TString("%s_%s_diff_%s" % (channel, region, var))
        FormatFileName(plotname)
        gPad.Print(plotdir + plotname.Data() + ".png")
        gPad.Print(plotdir + plotname.Data() + ".pdf")

    return 0