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()
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()
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)")
RooFit.LineStyle(kDashed)) if ret: sigPlot.getCurve().SetTitle('tail') # sigPlot.GetYaxis().SetTitle('Events / GeV') 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) 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)
fitter4.loadData() fitter4.make4BodyPdf(theFitter) fitter4.loadParameters(sigYieldFilename) ## assert(False) mf4 = fitter4.stackedPlot(False, RooWjjMjjFitter.mlnujj) mf4.SetName("Mlvjj_Stacked") ## sf4 = fitter4.residualPlot(mf4, "h_background", "dibosonPdf", False) # pf4 = fitter4.residualPlot(mf4, "h_total", "", True) pf4 = pulls.createPull(mf4.getHist('theData'), mf4.getCurve('h_total')) pf4.SetName("Mlvjj_Pull") rf4 = pulls.createResid(mf4.getHist('theData'), mf4.getCurve('h_total')) rf4.SetName("Mlvjj_Residuals") (chi2_4, ndf_4) = pulls.computeChi2(mf4.getHist('theData'), mf4.getCurve('h_total')) lf4 = fitter4.stackedPlot(True, RooWjjMjjFitter.mlnujj) lf4.SetName("Mlvjj_log") fitUtils = RooWjjFitterUtils(pars4) iwt = 0 if (opts.mH >= 500): iwt = 1 sigHists = HWWSignalShapes.GenHiggsHists(pars4, opts.mH, fitUtils, iwt = iwt) extraFactor = 2. otherdata = HWWSignalShapes.NgenHiggs(opts.mH, 'ggH') SigVisual = TH1D(sigHists['ggH']) SigVisual.Print() SigVisual.SetName('SigVisual') SigVisual.SetLineColor(kBlue)
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)
# errs.Print('v') # upper.Print('v') # lower.Print('v') c_mWW = TCanvas('c_mWW', pars_mWW.var[0] + ' plot') 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(
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)")
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)")
RooFit.LineStyle(kDashed)) if ret: sigPlot.getCurve().SetTitle('tail') # sigPlot.GetYaxis().SetTitle('Events / GeV') 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'