Esempio n. 1
0
    fitter.ws.var('r_signal').setVal(w.var('r').getVal())
    if sig == 'b':
        fitter.ws.var('r_signal').setVal(0.)

    print 'using r for signal:',fitter.ws.var('r_signal').getVal(),
    print 'value from combine:',w.var('r').getVal()
    print

    for v in pars.var:
        can = TCanvas('can_%s_%s' % (v,sig) , '%s plot %s' % (v,sig))
        plot = plotAVariable(v, fitter)
        plot.SetName('%s_%s' % (plot.GetName(), sig))
        plot.Draw()
        can.Update()
        pullplot = pulls.createPull(plot.getHist('theData'), plot.getObject(1))
        pullplot.SetName('%s_pull_%s' % (v, sig))
        canpull = TCanvas('can_%s_pull_%s' % (v,sig), '%s pulls %s' % (v,sig))
        canpull.SetGridy()
        pullplot.Draw('ap')
        canpull.Update()
        pullplot.GetXaxis().SetLimits(pars.varRanges[v][1], pars.varRanges[v][2])
        pullplot.GetXaxis().SetTitle(fitter.ws.var(v).getTitle(True).Data())
        pullplot.GetYaxis().SetTitle('pull')
        canpull.Update()
        plots += [plot, pullplot]
        cans += [can, canpull]
    
mode = 'muon'
if opts.isElectron:
    mode = 'electron'
Esempio n. 2
0
if fr:
    floatVars = [ fr.floatParsFinal().at(i).GetName() \
                      for i in range(0, fr.floatParsFinal().getSize()) ]
    fitter.ws.defineSet('floatingParams', ','.join(floatVars))
    fitter.ws.saveSnapshot("fitPars", ','.join(floatVars))
    ndf = fr.floatParsFinal().getSize() - \
        fitter.ws.set('constraintSet').getSize()
    fr.Print('v')

print '%i free parameters in the fit' % ndf

firstCurve1 = plot1.getObject(1)
# firstCurve1.Print()

(chi2_1, ndf_1) = pulls.computeChi2(plot1.getHist('theData'), firstCurve1)
pull1 = pulls.createPull(plot1.getHist('theData'), firstCurve1)

firstCurve2 = plot2.getObject(1)
# firstCurve2.Print()

(chi2_2, ndf_2) = pulls.computeChi2(plot2.getHist('theData'), firstCurve2)
pull2 = pulls.createPull(plot2.getHist('theData'), firstCurve2)

chi2 = chi2_1 + chi2_2
ndf = ndf_1 + ndf_2 - ndf

cp1 = TCanvas("cp1", fitter.ws.var(pars.var[0]).GetTitle() + ' pull')
pull1.Draw('ap')
pull1.SetName(pars.var[0] + "_pull")
cp1.SetGridy()
cp1.Update()
if fr:
    floatVars = [ fr.floatParsFinal().at(i).GetName() \
                      for i in range(0, fr.floatParsFinal().getSize()) ]
    fitter.ws.defineSet('floatingParams', ','.join(floatVars))
    fitter.ws.saveSnapshot("fitPars", ','.join(floatVars))
    ndf = fr.floatParsFinal().getSize() - \
        fitter.ws.set('constraintSet').getSize()
    fr.Print('v')

print '%i free parameters in the fit' % ndf

firstCurve1 = plot1.getObject(1)
# firstCurve1.Print()

(chi2_1, ndf_1) = pulls.computeChi2(plot1.getHist('theData'), firstCurve1)
pull1 = pulls.createPull(plot1.getHist('theData'), firstCurve1)

chi2 = chi2_1
ndf = ndf_1-ndf

cp1 = TCanvas("cp1", xvar.GetTitle() + ' pull')
pull1.Draw('ap')
pull1.SetName(xvar.GetTitle() + "_pull")
cp1.SetGridy()
cp1.Update()
pull1.GetXaxis().SetLimits(pars.varRanges[pars.var[0]][1], 
                           pars.varRanges[pars.var[0]][2])
pull1.GetXaxis().SetTitle(xvar.getTitle(True).Data())
pull1.GetYaxis().SetTitle("pull (#sigma)")
cp1.Update()
Esempio n. 4
0
if fr:
    floatVars = [ fr.floatParsFinal().at(i).GetName() \
                      for i in range(0, fr.floatParsFinal().getSize()) ]
    fitter.ws.defineSet('floatingParams', ','.join(floatVars))
    fitter.ws.saveSnapshot("fitPars", ','.join(floatVars))
    ndf = fr.floatParsFinal().getSize() - \
        fitter.ws.set('constraintSet').getSize()
    # fr.Print('v')

print '%i free parameters in the fit' % ndf

firstCurve1 = plot1.getObject(1)
# firstCurve1.Print()

(chi2_1, ndf_1) = pulls.computeChi2(plot1.getHist('theData'), firstCurve1)
pull1 = pulls.createPull(plot1.getHist('theData'), firstCurve1)
    

chi2 = chi2_1
ndf = ndf_1-ndf

cp1 = TCanvas("cp1", pars.var[0] + ' pull')
pull1.Draw('ap')
pull1.SetName(pars.var[0] + "_pull")
cp1.SetGridy()
cp1.Update()
pull1.GetXaxis().SetLimits(pars.varRanges[pars.var[0]][1], 
                           pars.varRanges[pars.var[0]][2])
pull1.GetXaxis().SetTitle(fitter.ws.var(pars.var[0]).getTitle(True).Data())
pull1.GetYaxis().SetTitle("pull (#sigma)")
iset = RooArgSet(mass)
nexp = theFitter.makeFitter().expectedEvents(iset)

ndf2 = Long(ndf)
print 'Corrected chi2'
#chi2 = ndf*1.0
chi2 = theFitter.computeChi2(ndf)
print 'Raw chi2'
#chi2Raw = ndf2*1.0
chi2Raw = theFitter.computeChi2(ndf2, False)

mf = theFitter.stackedPlot()
mf.SetName("Mjj_Stacked")
## sf = theFitter.residualPlot(mf, "h_background", "dibosonPdf", False)
# 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))
iset = RooArgSet(mass)
nexp = theFitter.makeFitter().expectedEvents(iset)

ndf2 = Long(ndf)
print 'Corrected chi2'
#chi2 = ndf*1.0
chi2 = theFitter.computeChi2(ndf)
print 'Raw chi2'
#chi2Raw = ndf2*1.0
chi2Raw = theFitter.computeChi2(ndf2, False)

mf = theFitter.stackedPlot()
mf.SetName("Mjj_Stacked")
## sf = theFitter.residualPlot(mf, "h_background", "dibosonPdf", False)
# 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))
iset = RooArgSet(mass)
nexp = theFitter.makeFitter().expectedEvents(iset)

ndf2 = Long(ndf)
print "Corrected chi2"
# chi2 = ndf*1.0
chi2 = theFitter.computeChi2(ndf)
print "Raw chi2"
# chi2Raw = ndf2*1.0
chi2Raw = theFitter.computeChi2(ndf2, False)

mf = theFitter.stackedPlot()
mf.SetName("Mjj_Stacked")
## sf = theFitter.residualPlot(mf, "h_background", "dibosonPdf", False)
# 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))
Esempio n. 8
0
    chi2s.append(chi2_1)
    ndfs.append(ndf_1)

    residuals = pulls.createResid(sigPlot.getHist('theData'),
                                  sigPlot.getCurve('fitCurve'))
    plots.append(residuals)
    residuals.SetName('%s_Residuals' % par)
    c3 = TCanvas('c%i_residual' % i, par + ' residual')
    c3.SetGridy()
    residuals.Draw('ap')
    residuals.GetXaxis().SetLimits(fitter.ws.var(par).getMin(),
                                   fitter.ws.var(par).getMax())
    c3.Update()
    cans.append(c3)

    pull = pulls.createPull(sigPlot.getHist('theData'),
                            sigPlot.getCurve('fitCurve'))
    plots.append(pull)
    pull.SetName('%s_Pulls' % par)
    c2 = TCanvas('c%i_pull' % i, par + ' pull')
    c2.SetGridy()
    pull.Draw('ap')
    pull.GetXaxis().SetLimits(fitter.ws.var(par).getMin(),
                              fitter.ws.var(par).getMax())
    c2.Update()
    cans.append(c2)

    c1.Print('%s_compFit.png' % opts.bn)
#    c1.Print('%s_compFit.pdf' % opts.bn)

mode = 'muon'
if opts.isElectron:
Esempio n. 9
0
def mjjfitting(fitter):

    if opts.ws:
        fitter.loadWorkspaceFromFile(opts.ws, getFloatPars = False,
                                     wsname = 'w_mjj')

    fitter.readParametersFromFile()
    fitter.expectedFromPars()
    fitter.resetYields()

    startpars = totalPdf.getParameters(fitter.ws.set('obsSet'))
    fitter.ws.defineSet("params", startpars)
    fitter.ws.saveSnapshot("initPars", startpars)

    if opts.toy and not opts.ws:
        #generate toy dataset
        print 'Generated parameters'
        fitter.ws.set('params').Print('v')
        fitter.ws.saveSnapshot("genPars", startpars)

        data = totalPdf.generate(fitter.ws.set('obsSet'), RooFit.Name('data_obs'),
                                 RooFit.Extended())
        if fitter.pars.binData:
            data = RooDataHist('data_obs', 'data_obs', fitter.ws.set('obsSet'),
                               data)
            data.Print('v')
        getattr(fitter.ws, 'import')(data)
    else:
        data = fitter.loadData()

    data.Print()
    startpars.IsA().Destructor(startpars)

    print 'Time elapsed: %.1f sec' % timer.RealTime()
    print 'CPU time used: %.1f sec' % timer.CpuTime()
    print 'starting fitting routine'
    timer.Continue()
    fitter.ws.var('diboson_nrm').setConstant()
    fitter.ws.var('top_nrm').setConstant()
    fitter.ws.var('r_signal').setVal(1.0)
    fitter.ws.var('r_signal').setError(0.04)

    fr = fitter.fit(overrideRangeCmd=True)

    plot1 = fitter.stackedPlot(pars.var[0])

    sigPdf = fitter.ws.pdf('ggH')
    n_sig = fitter.ws.var('n_ggH').getVal() + fitter.ws.var('n_qqH').getVal()
    print "N HWW:", n_sig

    sigPdf.plotOn(plot1, RooFit.Range('plotRange'),
                  RooFit.NormRange('plotRange'),
                  RooFit.Normalization(n_sig*sigScaler, RooAbsReal.NumEvent),
                  RooFit.Name('signal_HWW'),
                  RooFit.LineColor(kBlue+2))
    plot1.getCurve().SetTitle("H(%i)#times%.0f" % (opts.mH, sigScaler))

    leg1 = RooWjj2DFitter.Wjj2DFitter.legend4Plot(plot1)

    c1 = TCanvas('c1', pars.var[0] + ' plot')
    plot1.addObject(leg1)
    plot1.Draw()
    c1.Update()

    ndf = 0

    if fr:
        floatVars = [ fr.floatParsFinal().at(i).GetName() \
                          for i in range(0, fr.floatParsFinal().getSize()) ]
        fitter.ws.defineSet('floatingParams', ','.join(floatVars))
        fitter.ws.saveSnapshot("fitPars", ','.join(floatVars))
        ndf = fr.floatParsFinal().getSize() - \
            fitter.ws.set('constraintSet').getSize()
        # fr.Print('v')

    print '%i free parameters in the fit' % ndf

    firstCurve1 = plot1.getObject(1)
    # firstCurve1.Print()

    (chi2_1, ndf_1) = pulls.computeChi2(plot1.getHist('theData'), firstCurve1)
    pull1 = pulls.createPull(plot1.getHist('theData'), firstCurve1)


    chi2 = chi2_1
    ndf = ndf_1-ndf

    cp1 = TCanvas("cp1", pars.var[0] + ' pull')
    pull1.Draw('ap')
    pull1.SetName(pars.var[0] + "_pull")
    cp1.SetGridy()
    cp1.Update()
    pull1.GetXaxis().SetLimits(pars.varRanges[pars.var[0]][1], 
                               pars.varRanges[pars.var[0]][2])
    pull1.GetXaxis().SetTitle(fitter.ws.var(pars.var[0]).getTitle(True).Data())
    pull1.GetYaxis().SetTitle("pull (#sigma)")

    blinder = plot1.findObject('TBox')
    blinder2 = None
    if blinder:
        blinder2 = TBox(pars.exclude[pars.var[0]][0], pull1.GetYaxis().GetXmin(),
                        pars.exclude[pars.var[0]][1], pull1.GetYaxis().GetXmax())
        blinder2.SetFillColor(blinder.GetFillColor())
        blinder2.SetFillStyle(blinder.GetFillStyle())

    if blinder2:
        #blinder2.Print()
        blinder2.Draw()

    cp1.Update()

    print 'Time elapsed: %.1f sec' % timer.RealTime()
    print 'CPU time used: %.1f sec' % timer.CpuTime()

    timer.Continue()

    return (plot1, pull1, chi2, ndf, fr)
Esempio n. 10
0
plot_mWW.addObject(leg_mWW)
plot_mWW.Draw()
# if blinder:
#     plot_mWW.setInvisible('theData', True)
c_mWW.Update()

c_mWW_err = TCanvas('c_mWW_err', 'with errors')
plot_mWW_withErrs.Draw()
c_mWW_err.Update()

(chi2_mWW, ndf_mWW) = pulls.computeChi2(plot_mWW.getHist('theData'), 
                                        plot_mWW.getObject(1))
if fr_mWW:
    ndf_mWW -= fr_mWW.floatParsFinal().getSize() - \
        fitter_mWW.ws.set('constraintSet').getSize()
pull_mWW = pulls.createPull(plot_mWW.getHist('theData'), 
                            plot_mWW.getObject(1))
cp_mWW = TCanvas('cp_mWW', pars_mWW.var[0] + ' mWW pull')
pull_mWW.SetName('mWW_pull')
pull_mWW.Draw('ap')
cp_mWW.SetGridy()
cp_mWW.Update()
pull_mWW.GetXaxis().SetLimits(
    pars_mWW.varRanges[pars_mWW.var[0]][1], 
    pars_mWW.varRanges[pars_mWW.var[0]][2]
    )
pull_mWW.GetXaxis().SetTitle(
    fitter_mWW.ws.var(pars_mWW.var[0]).getTitle(True).Data()
    )
pull_mWW.GetYaxis().SetTitle("pull")
cp_mWW.Update()
ndf = 0

if fr:
    floatVars = [fr.floatParsFinal().at(i).GetName() for i in range(0, fr.floatParsFinal().getSize())]
    fitter.ws.defineSet("floatingParams", ",".join(floatVars))
    fitter.ws.saveSnapshot("fitPars", ",".join(floatVars))
    ndf = fr.floatParsFinal().getSize() - fitter.ws.set("constraintSet").getSize()
    # fr.Print('v')

print "%i free parameters in the fit" % ndf

firstCurve1 = plot1.getObject(1)
# firstCurve1.Print()

(chi2_1, ndf_1) = pulls.computeChi2(plot1.getHist("theData"), firstCurve1)
pull1 = pulls.createPull(plot1.getHist("theData"), firstCurve1)


chi2 = chi2_1
ndf = ndf_1 - ndf

cp1 = TCanvas("cp1", vName1 + " pull")
pull1.Draw("ap")
pull1.SetName(vName1 + "_pull")
cp1.SetGridy()
cp1.Update()
pull1.GetXaxis().SetLimits(pars.varRanges[pars.var[0]][1], pars.varRanges[pars.var[0]][2])
pull1.GetXaxis().SetTitle(fitter.ws.var(vName1).getTitle(True).Data())
pull1.GetYaxis().SetTitle("pull (#sigma)")

blinder = plot1.findObject("TBox")
if fr:
    floatVars = [ fr.floatParsFinal().at(i).GetName() \
                      for i in range(0, fr.floatParsFinal().getSize()) ]
    fitter.ws.defineSet('floatingParams', ','.join(floatVars))
    fitter.ws.saveSnapshot("fitPars", ','.join(floatVars))
    ndf = fr.floatParsFinal().getSize() - \
        fitter.ws.set('constraintSet').getSize()
    # fr.Print('v')

print '%i free parameters in the fit' % ndf

firstCurve1 = plot1.getObject(1)
# firstCurve1.Print()

(chi2_1, ndf_1) = pulls.computeChi2(plot1.getHist('theData'), firstCurve1)
pull1 = pulls.createPull(plot1.getHist('theData'), firstCurve1)

chi2 = chi2_1
ndf = ndf_1 - ndf

cp1 = TCanvas("cp1", vName1 + ' pull')
pull1.Draw('ap')
pull1.SetName(vName1 + "_pull")
cp1.SetGridy()
cp1.Update()
pull1.GetXaxis().SetLimits(pars.varRanges[pars.var[0]][1],
                           pars.varRanges[pars.var[0]][2])
pull1.GetXaxis().SetTitle(fitter.ws.var(vName1).getTitle(True).Data())
pull1.GetYaxis().SetTitle("pull (#sigma)")

blinder = plot1.findObject('TBox')
Esempio n. 13
0
    sigPlot.getHist('theData').SetTitle('%s MC' % compName)

    leg = RooWjj2DFitter.Wjj2DFitter.legend4Plot(sigPlot)
    sigPlot.addObject(leg)
    sigPlot.Draw()
    c1.Update()
    sigPlot.SetMinimum(1e-6)
    cans.append(c1)
    plots.append(sigPlot)
    (chi2_1, ndf_1) = pulls.computeChi2(sigPlot.getHist('theData'),
                                        sigPlot.getCurve('fitCurve'))
    chi2s.append(chi2_1)
    ndfs.append(ndf_1)

    pull = pulls.createPull(sigPlot.getHist('theData'),
                            sigPlot.getCurve('fitCurve'))
    plots.append(pull)
    c2 = TCanvas('c%i_pull' % i, par + ' pull')
    c2.SetGridy()
    pull.Draw('ap')
    cans.append(c2)

    c1.Print('%s_compFit.png' % opts.bn)
#    c1.Print('%s_compFit.pdf' % opts.bn)

mode = 'muon'
if opts.isElectron:
    mode = 'electron'

## lgnd = TLegend(0.65, 0.72, 0.92, 0.89, '', 'NDC')
## lgnd.AddEntry('fitCurve', '%s fit pdf' % opts.component, 'l')