mssmPdfE = root.RooExtendPdf("mssmBakE","Extended Background PDF",mssmPdf,nBak)
bernPdfE = root.RooExtendPdf("bernBakE","Extended Background PDF",bernPdf,nBak)
mssmPdfSB = root.RooAddPdf("mssmPdfSB","S+B PDF",root.RooArgList(mssmPdfE,sigPdf))
bernPdfSB = root.RooAddPdf("bernPdfSB","S+B PDF",root.RooArgList(bernPdfE,sigPdf))

log = open("output/fitInvestigate3.log",'w')

nSigFitVals = []
rmpList = []
ds = data
dh = ds.binnedClone()
ds = dh

nTimesSMToInject = 5.2
nSigToInject = nTimesSMToInject*getSMSigCounts("Jets01PassPtG10BB",125)
nSigToInject = int(nSigToInject)
assert(nSigToInject*2 < nSig.getMax())
dataSigInject = sigPdf.generate(root.RooArgSet(dimuonMass),nSigToInject)
print "N Events Signal Expected: ", nSigToInject
print "N Events Signal Injected: ", dataSigInject.sumEntries()
dataSigInject.append(data)
print "N Events data+signal: ", dataSigInject.sumEntries()
print "N Events data: ", data.sumEntries()
nTimesSMToInjectStr=("{0:.2g}".format(nTimesSMToInject)).replace(".","p")

################################################3
log.write("\n"+'#'*30+"\n")
log.write("\nMSSM S+B Fit:\n")
fr = mssmPdfSB.fitTo(ds,
               root.RooFit.Minos(True),
def runStudy(catName,energyStr,truePdfName,dataFileNames,sigMasses):
      """
        Pure function so that we can do multiprocessing!!
      """

      dataTree = root.TChain()
      for i in dataFileNames:
        dataTree.Add(i+"/outtree"+catName)
      dataTree.SetCacheSize(10000000);
      dataTree.AddBranchToCache("*");

      truePdfFunc = None
      if truePdfName == "Bernstein" or truePdfName == "Chebychev" or truePdfName == "Polynomial" or truePdfName == "SumExp" or truePdfName == "SumPow" or truePdfName == "Laurent" or truePdfName == "ExpTimesBernstein" or truePdfName == "ExpTimesChebychev" or truePdfName == "ExpTimesPolynomial":
        truePdfFunc = getattr(fitOrderChooser,"makePDFBak"+truePdfName)
      else:
        truePdfFunc = getattr(makeCards,"makePDFBak"+truePdfName)

      dimuonMass = root.RooRealVar("dimuonMass","m [GeV/c^{2}]",110.,160.)
      dimuonMass.setBins(50)
      dimuonMass.setRange("exprange",120,160)
      dimuonMass.setRange("whole",110,160)
      dimuonMass.setRange("low",110,120) # Silly ranges for old fit functionality
      dimuonMass.setRange("high",130,160)
      dimuonMass.setRange("signal",120,130)
      dimuonMass.setRange("signalfit",110,140)
      dimuonMass.setRange("annaRegion",123.5,127.5)
      dimuonMassArgSet = root.RooArgSet(dimuonMass)
      wTrue = root.RooWorkspace("wTrue")
      wTrueImport = getattr(wTrue,"import")

      canvas = root.TCanvas("canvas"+catName+energyStr+truePdfName)
      tlatex = root.TLatex()
      tlatex.SetNDC()
      tlatex.SetTextFont(root.gStyle.GetLabelFont())
      tlatex.SetTextSize(0.04)

      # Hack to Make makePDFBakOld work
      minMassZ = 88.
      maxMassZ = 94.
      dimuonMassZ = root.RooRealVar("dimuonMass","dimuonMass",minMassZ,maxMassZ)

      ### Load data
      
      realData = root.RooDataSet("realData"+catName+energyStr,
                                      "realData"+catName+energyStr,
                                          dataTree,root.RooArgSet(dimuonMass)
                                        )
      realDataHist = realData.binnedClone("realDataHist"+catName+energyStr)
      nData = realData.sumEntries()
      realDataZ = root.RooDataSet("realDataZ"+catName+energyStr,
                                      "realDataZ"+catName+energyStr,
                                          dataTree,root.RooArgSet(dimuonMassZ)
                                        )

      ### Make Bak Pdfs

      trashParamList, trashBakNormTup, trashDebug, trueOrder = truePdfFunc(truePdfName+catName+energyStr,realData,dimuonMass,110,160,wTrueImport,dimuonMassZ,realDataZ)
      truePdf = wTrue.pdf("bak")
      truePdf.SetName(truePdfName)
      truePdf.SetTitle("True PDF ")

      nDataVar = root.RooFit.RooConst(nData)
      nBakVar = root.RooRealVar("nBak","N_{B}",nData/2.,nData*2)
      truePdfE = root.RooExtendPdf(truePdfName+"E","True PDF Extended",truePdf,nBakVar)

      # Make sure Voigt params are set to True vals and constant
      if truePdfName == "Old":
        for xTrue in rooArgSet2List(truePdf.getParameters(realData)):
          if not ("voit" in xTrue.GetName()):
            continue
          for xToy in rooArgSet2List(trueToyPdf.getParameters(realData)):
            trueMatch = re.match(r".*(_voit.*)",xTrue.GetName()) 
            toyMatch = re.match(r".*(_voit.*)",xToy.GetName()) 
            assert(trueMatch)
            if not toyMatch:
                continue
            trueBaseName = trueMatch.group(1)
            toyBaseName = toyMatch.group(1)
            if not ( trueBaseName == toyBaseName ):
              continue
            xToy.setVal(xTrue.getVal())
            xTrue.setConstant(True)
            xToy.setConstant(True)

      ### Now load Signal PDFs
      nSigVarBounds = nData/2.
      nSigVar = root.RooRealVar("nSig","N_{S}",-nSigVarBounds,nSigVarBounds)
      sigPdfs = []
      sigPdfEs = []
      wSigs = []
      for hmass in sigMasses:
        wSig = root.RooWorkspace("signal"+catName+energyStr+str(hmass))
        makeCards.makePDFSigNew(catName+energyStr,"sig_ggH",dimuonMass,float(hmass),
                                getattr(wSig,"import")
                               )
        sigPdf = wSig.pdf("ggH")
        sigPdf.SetName("sigPDF_"+str(hmass)+"_"+catName+energyStr)
        sigPdfs.append(sigPdf)
        wSigs.append(wSig)
        sigPdfE = root.RooExtendPdf(sigPdf.GetName()+"E",sigPdf.GetTitle()+" Extended",sigPdf,nSigVar)
        sigPdfEs.append(sigPdfE)

      ## Load the 1*SM N signal events
      nSigSMs = []
      for hmass in sigMasses:
        nSigSMs.append(getSMSigCounts(catName,hmass,energy=energyStr))

      result = {}

      ### Do S+B Fits
      for hmass,sigPdf,sigPdfE,nSigSM in zip(sigMasses,sigPdfs,sigPdfEs,nSigSMs):
        truePdfPlusSigPdf = root.RooAddPdf("truePdfPlusSigPdf"+catName+energyStr,"",root.RooArgList(truePdfE,sigPdfE))
        fr = truePdfPlusSigPdf.fitTo(realData,
                             PRINTLEVEL,
                             root.RooFit.Save(1)
                           )
        #frPars = fr.floatParsFinal()
        #for i in range(frPars.getSize()):
        #  frPars[i].Print()
        #nSigVar.Print()

        result[hmass] = nSigVar.getError()
        #result[hmass] = nSigVar.getError()/nSigSM

        # Debug plot for fit to data
        frame = dimuonMass.frame()
        chi2RealDataVar = truePdfPlusSigPdf.createChi2(realDataHist)
        ndfRealData = dimuonMass.getBins() - 1  # b/c roofit normalizes
        ndfRealData -= rooPdfNFreeParams(truePdfPlusSigPdf,realDataHist)
        realData.plotOn(frame)
        errVisArg = root.RooFit.VisualizeError(fr,1,True)
        errFillArg = root.RooFit.FillStyle(3001)
        truePdfPlusSigPdf.plotOn(frame,root.RooFit.Range('low,signal,high'),root.RooFit.NormRange('low,signal,high'),errVisArg,errFillArg,root.RooFit.FillColor(root.kGreen-7))
        truePdfPlusSigPdf.plotOn(frame,root.RooFit.Range('low,signal,high'),root.RooFit.NormRange('low,signal,high'),root.RooFit.Components(truePdf.GetName()),root.RooFit.LineStyle(2),root.RooFit.LineColor(root.kRed+1))
        truePdfPlusSigPdf.plotOn(frame,root.RooFit.Range('low,signal,high'),root.RooFit.NormRange('low,signal,high'))
        #truePdfPlusSigPdf.plotOn(frame,root.RooFit.Range('low,signal,high'),root.RooFit.NormRange('low,signal,high'),root.RooFit.Components(sigPdf.GetName()),root.RooFit.LineColor(root.kRed+1))
        
        frame.Draw()
        frame.SetTitle("")
        frame.GetYaxis().SetTitle("Events / 1 GeV/c^{2}")
        tlatex.SetTextAlign(12)
        tlatex.DrawLatex(gStyle.GetPadLeftMargin(),0.96,"CMS Internal")
        tlatex.DrawLatex(0.02+gStyle.GetPadLeftMargin(),0.85,"Ref PDF: "+truePdfName)
        tlatex.SetTextAlign(32)
        tlatex.DrawLatex(0.99-gStyle.GetPadRightMargin(),0.96,catName+" "+energyStr)
        tlatex.DrawLatex(0.97-gStyle.GetPadRightMargin(),0.85,"Ref. S+B Fit to Real Data")
        tlatex.DrawLatex(0.97-gStyle.GetPadRightMargin(),0.80,"Ref. GOF: {0:.2f}".format(scipy.stats.chi2.sf(chi2RealDataVar.getVal(),ndfRealData)))
        tlatex.DrawLatex(0.97-gStyle.GetPadRightMargin(),0.75,"Ref. #chi^{{2}}/NDF: {0:.2f}".format(chi2RealDataVar.getVal()/ndfRealData))
        canvas.SaveAs("output/debug_oneSig_RealData_"+truePdfName+"_"+catName+"_"+energyStr+"_"+str(hmass)+".png")

      return result
newData = {}
thisMass = None

for line in newTxtF:
  if line[:4] == "==> ":
    match = re.match(r"^==> [0-9A-Za-z]+_7TeV_([0-9]+)\.0\.txt\.mu <==$",line)
    assert(match)
    thisMass = int(match.group(1))
  if line[:5] == "Best ":
    match = re.search(r"[0-9.]+/\+([0-9.]+)[\s]+\(68% CL\)",line)
    if not match:
      print line
      continue
    thisLimit = float(match.group(1))
    newData[thisMass] = thisLimit*getSMSigCounts("Jet2CutsVBFPass",thisMass,"7TeV")
    print thisMass,newData[thisMass]
newTxtF.close()

newData[150] = 33.*getSMSigCounts("Jet2CutsVBFPass",thisMass,"7TeV")

oldPklF = open("pklfiles/oneSig.pkl")
data = cPickle.load(oldPklF)
oldPklF.close()

for mass in [120,125,130,135,140,150]:
  print "{0} {1:10.2f} {2:10.2f}".format(mass,data['7TeV']['Jet2CutsVBFPass'][mass],newData[mass])
  #print "{0} {1:10.2f} {2:10.2f}".format(mass,data['7TeV']['Jet2CutsVBFPass'][mass]/getSMSigCounts("Jet2CutsVBFPass",mass,"7TeV"),newData[mass]/getSMSigCounts("Jet2CutsVBFPass",mass,"7TeV"))

data['7TeV']['Jet2CutsVBFPass'] = newData
newPklF = open("pklfiles/oneSig.pkl.new",'w')