Ejemplo n.º 1
0
# pf = theFitter.residualPlot(mf, "h_total", "", True)
pf = pulls.createPull(mf.getHist('theData'), mf.getCurve('h_total'))
pf.SetName("Mjj_Pull")
## lf = theFitter.stackedPlot(True)

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

cs = TCanvas("cs", "stacked")
mf.Draw()
# l.DrawLatex(0.66, 0.55,
#             '#chi^{{2}}/dof = {0:0.3f}/{1}'.format(chi2, ndf)
#             )
pyroot_logon.cmsPrelim(cs, fitterPars.intLumi/1000)
cs.Print('H{2}_Mjj_{0}_{1}jets_Stacked.pdf'.format(modeString, opts.Nj, opts.mH))
cs.Print('H{2}_Mjj_{0}_{1}jets_Stacked.png'.format(modeString, opts.Nj, opts.mH))

# if (fr.covQual() != 3):
#     print "Fit did not converge with a good error matrix. Bailing out."
#     assert(False)

c4 = TCanvas("c4", "pull")
pf.Draw('ap')
c4.SetGridy()
c4.Update()
pf.SetMinimum(-5.)
pf.SetMaximum(5.)
pf.GetXaxis().SetLimits(fitterPars.minMass, fitterPars.maxMass)
pf.GetXaxis().SetTitle('m_{jj} (GeV)')
# pf = theFitter.residualPlot(mf, "h_total", "", True)
pf = pulls.createPull(mf.getHist('theData'), mf.getCurve('h_total'))
pf.SetName("Mjj_Pull")
## lf = theFitter.stackedPlot(True)

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

cs = TCanvas("cs", "stacked")
mf.Draw()
# l.DrawLatex(0.66, 0.55,
#             '#chi^{{2}}/dof = {0:0.3f}/{1}'.format(chi2, ndf)
#             )
pyroot_logon.cmsPrelim(cs, fitterPars.intLumi/1000)
cs.Print('H{2}_Mjj_{0}_{1}jets_Stacked.pdf'.format(modeString, opts.Nj, opts.mH))
cs.Print('H{2}_Mjj_{0}_{1}jets_Stacked.png'.format(modeString, opts.Nj, opts.mH))

# if (fr.covQual() != 3):
#     print "Fit did not converge with a good error matrix. Bailing out."
#     assert(False)

c4 = TCanvas("c4", "pull")
pf.Draw('ap')
c4.SetGridy()
c4.Update()
pf.SetMinimum(-5.)
pf.SetMaximum(5.)
pf.GetXaxis().SetLimits(fitterPars.minMass, fitterPars.maxMass)
pf.GetXaxis().SetTitle('m_{jj} (GeV)')
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])
tmpLeg.AddEntry(h_total_up, "bkg syst.", "l")
entries = tmpLeg.GetListOfPrimitives()
theEntry = entries.Before(entries.Last())
#print theEntry
theEntry.Print()
oldLabel = theEntry.GetLabel()
#print oldLabel
theEntry.SetLabel(oldLabel.replace('#times2', '#times%.0f' % (2*scaleUp)))


c4blog = TCanvas("c4vlog", "4 body stacked log")
mf4log.Draw()
c4blog.SetLogy()
c4blog.Update()
SigVisualLog.Draw("samehist")
pyroot_logon.cmsPrelim(c4blog, opts.lumi/1000.)
h_total_up.Draw("l")
h_total_down.Draw("l")

c4blog.Update()

c4blog.Print("%s_Mlvjj_%s_%s_Stacked_log.pdf" % (fparts[0], fparts[1], 
                                                 fparts[2]))
c4blog.Print("%s_Mlvjj_%s_%s_Stacked_log.png" % (fparts[0], fparts[1], 
                                                 fparts[2].lower()))

if opts.blind:
    mf = fin.Get("Mjj_Stacked")
    blind = TBox(65., mf.GetMinimum(), 95., mf.GetMaximum())
    blind.SetFillColor(kGray+3)
if (sig2 - totalYield) > 0:
    sigSig2 = (sqrt(sig2-totalYield)/totalYield*sigYield)**2
else:
    sigSig2 = 0.
sigSig2 += sigYield
print '-------------------------------'
print 'total yield = %0.0f +/- %0.0f' % (sigYield, sqrt(sigSig2))
print '-------------------------------'

print 'data in signal box:',theFitter.getWorkSpace().data('data').reduce(RooFit.CutRange('signal')).numEntries()

if opts.qplot:
    import makeQPlot
    qplotPars = makeQPlot.theConfig(fitterPars, 'lastMjjSigYield.txt')
    (cq, shapeHist, totalHist) = makeQPlot.qPlot(qplotPars)
    pyroot_logon.cmsPrelim(cq, fitterPars.intLumi/1000)
    cq.Print('Wjj_Mjj_%s_%ijets_Q.pdf' % (modeString, opts.Nj))
    cq.Print('Wjj_Mjj_%s_%ijets_Q.png' % (modeString, opts.Nj))

fr.SetName('nll')
fr.Print()
nll=fr.minNll()
print '***** nll = ',nll,' ***** \n'
print 'total yield: %0.0f +/- %0.0f' % (totalYield, sqrt(sig2))

print 'shape file created'
ShapeFile = TFile('Mjj_%s_%iJets_Fit_Shapes.root' % (modeString, opts.Nj),
                  'recreate')
h_total.Write()
theData.Write()
fr.Write()
Ejemplo n.º 6
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])
tmpLeg = mf4log.findObject('theLegend')
tmpLeg.AddEntry(h_total_up, "bkg syst.", "l")
entries = tmpLeg.GetListOfPrimitives()
theEntry = entries.Before(entries.Last())
#print theEntry
theEntry.Print()
oldLabel = theEntry.GetLabel()
#print oldLabel
theEntry.SetLabel(oldLabel.replace('#times2', '#times%.0f' % (2 * scaleUp)))

c4blog = TCanvas("c4vlog", "4 body stacked log")
mf4log.Draw()
c4blog.SetLogy()
c4blog.Update()
SigVisualLog.Draw("samehist")
pyroot_logon.cmsPrelim(c4blog, opts.lumi / 1000.)
h_total_up.Draw("l")
h_total_down.Draw("l")

c4blog.Update()

c4blog.Print("%s_Mlvjj_%s_%s_Stacked_log.pdf" %
             (fparts[0], fparts[1], fparts[2]))
c4blog.Print("%s_Mlvjj_%s_%s_Stacked_log.png" %
             (fparts[0], fparts[1], fparts[2].lower()))

if opts.blind:
    mf = fin.Get("Mjj_Stacked")
    blind = TBox(65., mf.GetMinimum(), 95., mf.GetMaximum())
    blind.SetFillColor(kGray + 3)