# theFitter.getWorkSpace().var('nSingleTop').setConstant()
    theFitter.getWorkSpace().var('nQCD').setConstant()
    # theFitter.getWorkSpace().var('nZjets').setConstant()
    #theFitter.getWorkSpace().var('nWjets').setConstant()
    fr = theFitter.fit()
    ndf = Long(fr.floatParsFinal().getSize())
    tries += 1

mass = theFitter.getWorkSpace().var(fitterPars.var)
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)
#theFitter.getWorkSpace().Print()
fr = theFitter.fit()

# chi2 = Double(0.)
#ndf = Long(2)
extraNdf = 0
if not opts.btag:
    extraNdf = 2
## if (fitterPars.doNewPhysics):
##     extraNdf += 1
## if not fitterPars.constrainDiboson:
##     extraNdf += 1
## ndf = Long(3+extraNdf)
ndf = Long(1+extraNdf)
print ' *** uncorrected chi2 ***'
chi2 = theFitter.computeChi2(ndf, False)
print ' *** corrected chi2 ***'
ndf = Long(1+extraNdf)
chi2 = theFitter.computeChi2(ndf, True)
print ' *** '
chi2 = 0
# chi2frame.Draw()

# assert False, "fit done"

yields = fr.floatParsFinal()
mass = theFitter.getWorkSpace().var(fitterPars.var)
iset = RooArgSet(mass)
covMatrix = TMatrixDSym(fr.covarianceMatrix())

sig2 = 0.
    # theFitter.getWorkSpace().var('nSingleTop').setConstant()
    theFitter.getWorkSpace().var('nQCD').setConstant()
    # theFitter.getWorkSpace().var('nZjets').setConstant()
    #theFitter.getWorkSpace().var('nWjets').setConstant()
    fr = theFitter.fit()
    ndf = Long(fr.floatParsFinal().getSize())
    tries += 1

mass = theFitter.getWorkSpace().var(fitterPars.var)
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)
    modeString = 'Electron'
else:
    modeString = ''

theFitter = RooWjjMjjFitter(fitterPars)

theFitter.makeFitter(False)

#theFitter.getWorkSpace().Print()
fr = theFitter.fit()

extraNdf = 0
if not fitterPars.constrainDiboson:
    extraNdf += 1
ndf = Long(fr.floatParsFinal().getSize()-5+extraNdf)
chi2 = theFitter.computeChi2(ndf)
# chi2frame.Draw()

# assert False, "fit done"

yields = fr.floatParsFinal()
mass = theFitter.getWorkSpace().var(fitterPars.var)
iset = RooArgSet(mass)
covMatrix = TMatrixDSym(fr.covarianceMatrix())

sig2 = 0.
for v1 in range(0, covMatrix.GetNrows()):
    for v2 in range(0, covMatrix.GetNcols()):
        if ((yields[v1].GetName())[0] == 'n') and \
               ((yields[v2].GetName())[0] == 'n'):
            #print v1, yields[v1].GetName(),',', v2, yields[v2].GetName()