print sbf
    sbf.Print()

c.setVal(cNom + c.getError())
fitter4.getWorkSpace().pdf("WpJ4BodyPdf").plotOn(sbf, RooFit.LineStyle(2),
                                                 RooFit.Name('syst_up'),
                                                 RooFit.LineColor(kBlue+1))

c.setVal(cNom - c.getError())
fitter4.getWorkSpace().pdf("WpJ4BodyPdf").plotOn(sbf, RooFit.LineStyle(2),
                                                 RooFit.Name('syst_down'),
                                                 RooFit.LineColor(kBlue+1))

cWpJ.cd()
sbf.Draw()
pyroot_logon.cmsLabel(cWpJ, pars4.intLumi/1000, prelim=True)
cWpJ.Update()
cWpJ.Modified()
cWpJ.Update()

cWpJ.Print('H%i_Mlvjj_%s_%ijets_WpJShape.pdf' % (opts.mH, modeString,
                                                 opts.Nj))
cWpJ.Print('H%i_Mlvjj_%s_%ijets_WpJShape.png' % (opts.mH, modeString,
                                                 opts.Nj))
# cWpJ.Print('H%i_Mlvjj_%s_%ijets_WpJShape.root' % (opts.mH, modeString,
#                                                   opts.Nj))

sbf.SetMinimum(1e-4)
cWpJ.SetLogy()
cWpJ.Update()
cWpJ.Modified()
Example #2
0
            if thePlot:
                plots[plotName].append(thePlot.Clone())
        inF.Close()

    print plots

    cplots = []
    outFile = TFile(opts.output, 'recreate')
    for plot in plots:
        outPlot = addPlots(plots[plot])

        can = TCanvas()
        cans.append(can)
        cplots.append(outPlot)
        outPlot.Draw()
        pyroot_logon.cmsLabel(can, opts.lumi / 1000.)
        # gPad.WaitPrimitive()

        outPlot.Write()

        if (plot == '%s_subtracted' % (opts.prefix)):
            outPlot.SetMaximum(outPlot.GetMaximum() * 1.3)
            outPlot.SetMinimum(outPlot.GetMinimum() * 1.1)
            if outPlot.findObject('ErrBand'):
                corrPull = plotMjjFit.sub2pull(outPlot.getHist('theData'),
                                               outPlot.findObject('ErrBand'))
            else:
                corrPull = plotMjjFit.sub2pull(
                    outPlot.getHist('theData'),
                    outPlot.findObject('h_dibosonPdf'))
    print sbf
    sbf.Print()

c.setVal(cNom + c.getError())
fitter4.getWorkSpace().pdf("WpJ4BodyPdf").plotOn(sbf, RooFit.LineStyle(2),
                                                 RooFit.Name('syst_up'),
                                                 RooFit.LineColor(kBlue+1))

c.setVal(cNom - c.getError())
fitter4.getWorkSpace().pdf("WpJ4BodyPdf").plotOn(sbf, RooFit.LineStyle(2),
                                                 RooFit.Name('syst_down'),
                                                 RooFit.LineColor(kBlue+1))

cWpJ.cd()
sbf.Draw()
pyroot_logon.cmsLabel(cWpJ, pars4.intLumi/1000, prelim=True)
cWpJ.Update()
cWpJ.Modified()
cWpJ.Update()

cWpJ.Print('H%i_Mlvjj_%s_%ijets_WpJShape.pdf' % (opts.mH, modeString,
                                                 opts.Nj))
cWpJ.Print('H%i_Mlvjj_%s_%ijets_WpJShape.png' % (opts.mH, modeString,
                                                 opts.Nj))
# cWpJ.Print('H%i_Mlvjj_%s_%ijets_WpJShape.root' % (opts.mH, modeString,
#                                                   opts.Nj))

sbf.SetMinimum(1e-4)
cWpJ.SetLogy()
cWpJ.Update()
cWpJ.Modified()
def plot2BodyDist(theFitter, pars, chi2, ndf, Err=-1, NP=False, Prefix="Mjj", Left=False):
    from ROOT import (
        gPad,
        TLatex,
        TCanvas,
        kRed,
        kCyan,
        kBlue,
        RooFit,
        RooPlot,
        RooCurve,
        RooAbsReal,
        TGraphErrors,
        TLine,
        RooWjjMjjFitter,
    )

    if pars.includeMuons and pars.includeElectrons:
        modeString = ""
    elif pars.includeMuons:
        modeString = "Muon"
    elif pars.includeElectrons:
        modeString = "Electron"
    else:
        modeString = ""

    mf = theFitter.stackedPlot(False, RooWjjMjjFitter.mjj, Left)
    mf.SetName("%s_Stacked" % (Prefix))
    sf = theFitter.residualPlot(mf, "h_background", "dibosonPdf", False)
    sf.SetName("%s_Subtracted" % (Prefix))
    pf = theFitter.residualPlot(mf, "h_total", "", True)
    pf.SetName("%s_Pull" % (Prefix))
    pf2 = pf.emptyClone("%s_Pull_Corrected" % (Prefix))
    pf2.SetMinimum(-5.0)
    pf2.SetMaximum(5.0)
    corrPull = False
    lf = theFitter.stackedPlot(True, RooWjjMjjFitter.mjj, Left)
    lf.SetName("%s_Stacked_Log" % (Prefix))

    if Err > 0:
        totalPdf = theFitter.getWorkSpace().pdf("totalPdf")
        ## Ntotal = totalPdf.expectedEvents(iset)

        ## print 'Ntotal:',Ntotal
        h_dibosonPdf = sf.getCurve("h_dibosonPdf")
        totalPdf.plotOn(
            sf,
            RooFit.ProjWData(theFitter.getWorkSpace().data("data")),
            RooFit.Normalization(Err, RooAbsReal.Raw),
            # RooFit.AddTo('h_dibosonPdf', 1., 1.),
            # RooFit.Invisible(),
            RooFit.Name("h_ErrUp"),
            RooFit.Range("RangeForPlot"),
            RooFit.NormRange("RangeForPlot"),
            RooFit.LineColor(kRed),
            RooFit.LineStyle(3),
        )
        h_ErrUp = sf.getCurve("h_ErrUp")
        sf.remove("h_ErrUp", False)

        ErrBand = TGraphErrors(h_dibosonPdf.GetN(), h_dibosonPdf.GetX(), h_dibosonPdf.GetY())
        for pt in range(1, ErrBand.GetN()):
            ErrBand.SetPointError(pt, 0, h_ErrUp.interpolate(ErrBand.GetX()[pt]))
        ErrBand.SetName("ErrBand")
        ErrBand.SetTitle("Uncertainty")
        ErrBand.SetLineColor(kRed)
        ##         ErrBand.SetLineWidth(0)
        ##         ErrBand.SetLineStyle(0)
        ErrBand.SetFillColor(kRed)
        ErrBand.SetFillStyle(3353)

        # ErrBand.Draw('ap3')
        # h_ErrUp.Draw('lp')
        # gPad.Update()
        # gPad.WaitPrimitive()
        ##         h_ErrUp.Draw("al")
        ##         h_ErrUp.GetXaxis().Set(36, 40., 400.)
        ##         gPad.Update()
        ##         gPad.WaitPrimitive()
        ##         h_UpBand = RooCurve("h_UpBand", "Uncertainty", h_dibosonPdf, h_ErrUp,
        ##                             1., 1.)
        ##         h_UpBand.SetLineStyle(3)
        ##         h_UpBand.SetLineColor(kBlue+1)
        ##         h_DownBand = RooCurve("h_DownBand", "Uncertainty", h_dibosonPdf, h_ErrUp,
        ##                               1., -1.)
        ##         h_DownBand.SetLineStyle(3)
        ##         h_DownBand.SetLineColor(kBlue+1)

        ##         sf.addPlotable(h_UpBand, "L")
        ##         sf.addPlotable(h_DownBand, "L")
        sf.addObject(ErrBand, "3")
        # sf.Print("v")
        sf.drawAfter("h_dibosonPdf", "ErrBand")
        # sf.Print("v")
        sf.drawAfter("ErrBand", "theData")
        # sf.Print("v")
        sf.findObject("theLegend").AddEntry(ErrBand, "Uncertainty", "f")
        sf.findObject("theLegend").SetY1NDC(sf.findObject("theLegend").GetY1NDC() - 0.057)
        sf.findObject("theLegend").SetY1(sf.findObject("theLegend").GetY1NDC())

        corrPull = True
        pf2.addObject(sub2pull(sf.getHist("theData"), sf.findObject("ErrBand")), "p0")
        for item in range(0, int(pf.numItems())):
            firstItem = pf.getObject(item)
            if type(firstItem) == TLine:
                newLine = TLine(firstItem)
                newLine.SetY1(4.0)
                newLine.SetY2(-4.0)
                pf2.addObject(newLine, "l")
                # SetOwnership(newLine, False)

    if NP:
        NPPdf = theFitter.makeNPPdf()
        NPNorm = 4.0 * 0.11 * 46.8 / 12.0 * pars.intLumi

        if modeString == "Electron":
            if pars.njets == 2:
                NPNorm *= 0.0381
            elif pars.njets == 3:
                NPNorm *= 0.0123
        else:
            if pars.njets == 2:
                NPNorm *= 0.0550
            elif pars.njets == 3:
                NPNorm *= 0.0176

        print "**** N_NP:", NPNorm, "****"

        NPPdf.plotOn(
            sf,
            RooFit.ProjWData(theFitter.getWorkSpace().data("data")),
            RooFit.Normalization(NPNorm, RooAbsReal.Raw),
            RooFit.AddTo("h_dibosonPdf", 1.0, 1.0),
            RooFit.Name("h_NP"),
            RooFit.Range("RangeForPlot"),
            RooFit.NormRange("RangeForPlot"),
            RooFit.LineColor(kBlue),
            RooFit.LineStyle(2),
        )

        h_NP = sf.getCurve("h_NP")

        sf.drawBefore("h_dibosonPdf", "h_NP")
        # sf.Print("v")
        sf.findObject("theLegend").AddEntry(h_NP, "CDF-like Signal", "L")
        sf.findObject("theLegend").SetY1NDC(sf.findObject("theLegend").GetY1NDC() - 0.057)
        sf.findObject("theLegend").SetY1(sf.findObject("theLegend").GetY1NDC())

    l = TLatex()
    l.SetNDC()
    l.SetTextSize(0.045)
    l.SetTextFont(42)

    cstacked = TCanvas("cstacked", "stacked")
    mf.Draw()
    if chi2 > 0:
        l.DrawLatex(0.55, 0.49, "#chi^{2}/dof = %0.3f/%d" % (chi2, ndf))
    pyroot_logon.cmsLabel(cstacked, pars.intLumi / 1000, prelim=True)
    cstacked.Print("Wjj_%s_%s_%ijets_Stacked.pdf" % (Prefix, modeString, pars.njets))
    cstacked.Print("Wjj_%s_%s_%ijets_Stacked.png" % (Prefix, modeString, pars.njets))
    c2 = TCanvas("c2", "stacked_log")
    c2.SetLogy()
    lf.Draw()
    pyroot_logon.cmsPrelim(c2, pars.intLumi / 1000)
    c2.Print("Wjj_%s_%s_%ijets_Stacked_log.pdf" % (Prefix, modeString, pars.njets))
    c2.Print("Wjj_%s_%s_%ijets_Stacked_log.png" % (Prefix, modeString, pars.njets))
    c3 = TCanvas("c3", "subtracted")
    sf.Draw()
    pyroot_logon.cmsLabel(c3, pars.intLumi / 1000, prelim=True)
    c3.Print("Wjj_%s_%s_%ijets_Subtracted.pdf" % (Prefix, modeString, pars.njets))
    c3.Print("Wjj_%s_%s_%ijets_Subtracted.png" % (Prefix, modeString, pars.njets))
    c4 = TCanvas("c4", "pull")
    pf.Draw()
    c4.SetGridy()
    pyroot_logon.cmsPrelim(c4, pars.intLumi / 1000)
    c4.Print("Wjj_%s_%s_%ijets_Pull.pdf" % (Prefix, modeString, pars.njets))
    c4.Print("Wjj_%s_%s_%ijets_Pull.png" % (Prefix, modeString, pars.njets))

    c5 = None
    if corrPull:
        c5 = TCanvas("c5", "corrected pull")
        pf2.Draw()
        c5.SetGridy()
        pyroot_logon.cmsPrelim(c5, pars.intLumi / 1000)
        c5.Print("Wjj_%s_%s_%ijets_Pull_Corrected.pdf" % (Prefix, modeString, pars.njets))
        c5.Print("Wjj_%s_%s_%ijets_Pull_Corrected.png" % (Prefix, modeString, pars.njets))

    return ([mf, sf, pf2, lf], [cstacked, c2, c3, c5])
            if thePlot:
                plots[plotName].append(thePlot.Clone())
        inF.Close()

    print plots

    cplots = []
    outFile = TFile(opts.output, 'recreate')
    for plot in plots:
        outPlot = addPlots(plots[plot])

        can = TCanvas()
        cans.append(can)
        cplots.append(outPlot)
        outPlot.Draw()
        pyroot_logon.cmsLabel(can, opts.lumi/1000.)
        # gPad.WaitPrimitive()

        outPlot.Write()

        if (plot == '%s_subtracted' % (opts.prefix)):
            outPlot.SetMaximum(outPlot.GetMaximum()*1.3)
            outPlot.SetMinimum(outPlot.GetMinimum()*1.1)
            if outPlot.findObject('ErrBand'):
                corrPull = plotMjjFit.sub2pull(outPlot.getHist('theData'),
                                               outPlot.findObject('ErrBand'))
            else:
                corrPull = plotMjjFit.sub2pull(outPlot.getHist('theData'),
                                               outPlot.findObject('h_dibosonPdf'))
            
            corrPull.SetMinimum(-5.)
            scaleFunction = TF2("scaleFunction", "y*%.3f" % opts.scaler,
                                mjj_plot.GetXaxis().GetXmin(),
                                mjj_plot.GetXaxis().GetXmax(),
                                mjj_plot.GetMinimum(), mjj_plot.GetMaximum())
        sigCurve.Apply(scaleFunction)
        sigEntry = mjj_plot.findObject('theLegend').GetListOfPrimitives().Last().SetLabel('H(%i)#times%.0f' %(mH,opts.scaler*2))
#         SetOwnership(sigEntry, False)
# #         print sigEntry
# #         sigEntry.Print()
#         sigEntry.SetLabel('H(%i)#times%.0f' %(mH,opts.scaler*2))
        
        
    cs.cd()
    cs.SetLogy(False)
    mjj_plot.Draw()
    pyroot_logon.cmsLabel(cs, lumi = 19.2, prelim = opts.prelim)
    gPad.Update()
    gPad.Print('%s%s_%s_mjj_stacked.pdf' % (fname_path,fname_parts[0],
                                            fname_parts[1]))
    gPad.Print('%s%s_%s_mjj_stacked.png' % (fname_path,fname_parts[0],
                                            fname_parts[1]))

    mjj_pull = f.Get('Mass2j_PFCor_pull')
    cp.cd()
    mjj_pull.Draw('ap')
    pyroot_logon.cmsLabel(cp, lumi = 19.2, prelim = opts.prelim)
    gPad.Update()
    gPad.Print('%s%s_%s_mjj_pull.pdf' % (fname_path,fname_parts[0],
                                         fname_parts[1]))
    gPad.Print('%s%s_%s_mjj_pull.png' % (fname_path,fname_parts[0],
                                         fname_parts[1]))
Example #7
0
def plot2BodyDist(theFitter, pars, chi2, ndf, 
                  Err = -1, NP = False, Prefix = "Mjj", Left = False):
    from ROOT import gPad, TLatex, TCanvas, kRed, kCyan, kBlue, \
         RooFit, RooPlot, RooCurve, RooAbsReal, TGraphErrors, TLine, \
         RooWjjMjjFitter

    if pars.includeMuons and pars.includeElectrons:
        modeString = ''
    elif pars.includeMuons:
        modeString = 'Muon'
    elif pars.includeElectrons:
        modeString = 'Electron'
    else:
        modeString = ''

    mf = theFitter.stackedPlot(False, RooWjjMjjFitter.mjj, Left)
    mf.SetName("%s_Stacked" % (Prefix));
    sf = theFitter.residualPlot(mf, "h_background", "dibosonPdf", False)
    sf.SetName("%s_Subtracted" % (Prefix));
    pf = theFitter.residualPlot(mf, "h_total", "", True)
    pf.SetName("%s_Pull" % (Prefix))
    pf2 = pf.emptyClone("%s_Pull_Corrected" % (Prefix))
    pf2.SetMinimum(-5.)
    pf2.SetMaximum(5.)
    corrPull = False
    lf = theFitter.stackedPlot(True, RooWjjMjjFitter.mjj, Left)
    lf.SetName("%s_Stacked_Log" % (Prefix));

    if Err > 0:
        totalPdf = theFitter.getWorkSpace().pdf('totalPdf')
        ## Ntotal = totalPdf.expectedEvents(iset)

        ## print 'Ntotal:',Ntotal
        h_dibosonPdf = sf.getCurve('h_dibosonPdf')
        totalPdf.plotOn(sf,
                        RooFit.ProjWData(theFitter.getWorkSpace().data('data')),
                        RooFit.Normalization(Err, RooAbsReal.Raw),
                        #RooFit.AddTo('h_dibosonPdf', 1., 1.),
                        #RooFit.Invisible(),
                        RooFit.Name('h_ErrUp'),
                        RooFit.Range('RangeForPlot'),
                        RooFit.NormRange('RangeForPlot'),
                        RooFit.LineColor(kRed), RooFit.LineStyle(3))
        h_ErrUp = sf.getCurve('h_ErrUp')
        sf.remove('h_ErrUp', False)

        ErrBand = TGraphErrors(h_dibosonPdf.GetN(), h_dibosonPdf.GetX(),
                               h_dibosonPdf.GetY())
        for pt in range(1, ErrBand.GetN()):
            ErrBand.SetPointError(pt, 0,
                                  h_ErrUp.interpolate(ErrBand.GetX()[pt]))
        ErrBand.SetName("ErrBand")
        ErrBand.SetTitle("Uncertainty")
        ErrBand.SetLineColor(kRed)
##         ErrBand.SetLineWidth(0)
##         ErrBand.SetLineStyle(0)
        ErrBand.SetFillColor(kRed)
        ErrBand.SetFillStyle(3353)

        
        #ErrBand.Draw('ap3')
        #h_ErrUp.Draw('lp')
        #gPad.Update()
        #gPad.WaitPrimitive()
##         h_ErrUp.Draw("al")
##         h_ErrUp.GetXaxis().Set(36, 40., 400.)
##         gPad.Update()
##         gPad.WaitPrimitive()
##         h_UpBand = RooCurve("h_UpBand", "Uncertainty", h_dibosonPdf, h_ErrUp,
##                             1., 1.)
##         h_UpBand.SetLineStyle(3)
##         h_UpBand.SetLineColor(kBlue+1)
##         h_DownBand = RooCurve("h_DownBand", "Uncertainty", h_dibosonPdf, h_ErrUp,
##                               1., -1.)
##         h_DownBand.SetLineStyle(3)
##         h_DownBand.SetLineColor(kBlue+1)

##         sf.addPlotable(h_UpBand, "L")
##         sf.addPlotable(h_DownBand, "L")
        sf.addObject(ErrBand, "3")
        #sf.Print("v")
        sf.drawAfter('h_dibosonPdf', 'ErrBand')
        #sf.Print("v")
        sf.drawAfter('ErrBand', 'theData')
        #sf.Print("v")
        sf.findObject('theLegend').AddEntry(ErrBand, 'Uncertainty', 'f')
        sf.findObject('theLegend').SetY1NDC(sf.findObject('theLegend').GetY1NDC() - 0.057)
        sf.findObject('theLegend').SetY1(sf.findObject('theLegend').GetY1NDC())

        corrPull = True
        pf2.addObject(sub2pull(sf.getHist('theData'),
                               sf.findObject('ErrBand')),
                      'p0')
        for item in range(0, int(pf.numItems())):
            firstItem = pf.getObject(item)
            if (type(firstItem) == TLine):
                newLine = TLine(firstItem)
                newLine.SetY1(4.)
                newLine.SetY2(-4.)
                pf2.addObject(newLine, 'l')
                #SetOwnership(newLine, False)


    if NP:
        NPPdf = theFitter.makeNPPdf();
        NPNorm = 4.*0.11*46.8/12.*pars.intLumi

        if (modeString == 'Electron'):
            if pars.njets == 2:
                NPNorm *= 0.0381
            elif pars.njets == 3:
                NPNorm *= 0.0123
        else:
            if pars.njets == 2:
                NPNorm *= 0.0550
            elif pars.njets == 3:
                NPNorm *= 0.0176

        print '**** N_NP:', NPNorm,'****'

        NPPdf.plotOn(sf, RooFit.ProjWData(theFitter.getWorkSpace().data('data')),
                     RooFit.Normalization(NPNorm, RooAbsReal.Raw),
                     RooFit.AddTo('h_dibosonPdf', 1., 1.),
                     RooFit.Name('h_NP'),
                     RooFit.Range('RangeForPlot'),
                     RooFit.NormRange('RangeForPlot'),
                     RooFit.LineColor(kBlue), RooFit.LineStyle(2))

        h_NP = sf.getCurve('h_NP')

        sf.drawBefore('h_dibosonPdf', 'h_NP')
        #sf.Print("v")
        sf.findObject('theLegend').AddEntry(h_NP, "CDF-like Signal", "L")
        sf.findObject('theLegend').SetY1NDC(sf.findObject('theLegend').GetY1NDC() - 0.057)
        sf.findObject('theLegend').SetY1(sf.findObject('theLegend').GetY1NDC())

    l = TLatex()
    l.SetNDC()
    l.SetTextSize(0.045)
    l.SetTextFont(42)

    cstacked = TCanvas("cstacked", "stacked")
    mf.Draw()
    if (chi2 > 0):
        l.DrawLatex(0.55, 0.49,
                    '#chi^{2}/dof = %0.3f/%d' % (chi2, ndf)
                    )
    pyroot_logon.cmsLabel(cstacked, pars.intLumi/1000, prelim = True)
    cstacked.Print('Wjj_%s_%s_%ijets_Stacked.pdf' % (Prefix, modeString,
                                                     pars.njets))
    cstacked.Print('Wjj_%s_%s_%ijets_Stacked.png' % (Prefix, modeString,
                                                     pars.njets))
    c2 = TCanvas("c2", "stacked_log")
    c2.SetLogy()
    lf.Draw()
    pyroot_logon.cmsPrelim(c2, pars.intLumi/1000)
    c2.Print('Wjj_%s_%s_%ijets_Stacked_log.pdf' % (Prefix, modeString,
                                                    pars.njets))
    c2.Print('Wjj_%s_%s_%ijets_Stacked_log.png' % (Prefix, modeString,
                                                    pars.njets))
    c3 = TCanvas("c3", "subtracted")
    sf.Draw()
    pyroot_logon.cmsLabel(c3, pars.intLumi/1000, prelim = True)
    c3.Print('Wjj_%s_%s_%ijets_Subtracted.pdf' % (Prefix, modeString,
                                                  pars.njets))
    c3.Print('Wjj_%s_%s_%ijets_Subtracted.png' % (Prefix, modeString,
                                                  pars.njets))
    c4 = TCanvas("c4", "pull")
    pf.Draw()
    c4.SetGridy()
    pyroot_logon.cmsPrelim(c4, pars.intLumi/1000)
    c4.Print('Wjj_%s_%s_%ijets_Pull.pdf' % (Prefix, modeString, pars.njets))
    c4.Print('Wjj_%s_%s_%ijets_Pull.png' % (Prefix, modeString, pars.njets))

    c5 = None
    if corrPull:
        c5 = TCanvas("c5", "corrected pull")
        pf2.Draw()
        c5.SetGridy()
        pyroot_logon.cmsPrelim(c5, pars.intLumi/1000)
        c5.Print('Wjj_%s_%s_%ijets_Pull_Corrected.pdf' % (Prefix, modeString,
                                                          pars.njets))
        c5.Print('Wjj_%s_%s_%ijets_Pull_Corrected.png' % (Prefix, modeString,
                                                          pars.njets))

    return ([mf,sf,pf2,lf],[cstacked,c2,c3,c5])