def interp1Sigma(th1f_nom, th1f_down, th1f_up):
    nomE = th1f_nom.Integral()
    if nomE == 0:
        return [1.000, 1.000]
    downE = th1f_down.Integral() / nomE
    upE = th1f_up.Integral() / nomE
    if options.quadInterpolate != 0:
        downE = quadInterpolate(
            -1.0,
            -1.0 * options.quadInterpolate,
            0.0,
            1.0 * options.quadInterpolate,
            th1f_down.Integral(),
            th1f_nom.Integral(),
            th1f_up.Integral(),
        )
        upE = quadInterpolate(
            1.0,
            -1.0 * options.quadInterpolate,
            0.0,
            1.0 * options.quadInterpolate,
            th1f_down.Integral(),
            th1f_nom.Integral(),
            th1f_up.Integral(),
        )
        if upE != upE:
            upE = 1.000
        if downE != downE:
            downE = 1.000
    return [downE, upE]
예제 #2
0
def py_quadInterpolate(C,X1,X2,X3,Y1,Y2,Y3):
	resL = quadInterpolate(-1*C,X1,X2,X3,Y1,Y2,Y3)
	resH = quadInterpolate(C,X1,X2,X3,Y1,Y2,Y3)
	if math.isnan(resL) or math.isinf(resL) or  math.isnan(resH) or math.isinf(resL): return " - "
	if abs(resL - 1) < 0.00001 or abs(resL - 1) > 1: return " - "
	if abs(resH - 1) < 0.00001 or abs(resH - 1) > 1: return " - "
	return " %.3f/%.3f "%(resL,resH) 
def interp1Sigma(th1f_nom,th1f_down,th1f_up):
	nomE = th1f_nom.Integral()
	if nomE==0: nomE=float('NaN')
	downE = th1f_down.Integral()/nomE
	upE = th1f_up.Integral()/nomE
	if options.quadInterpolate!=0:
		downE = quadInterpolate(-1.,-1.*options.quadInterpolate,0.,1.*options.quadInterpolate,th1f_down.Integral(),th1f_nom.Integral(),th1f_up.Integral())
		upE = quadInterpolate(1.,-1.*options.quadInterpolate,0.,1.*options.quadInterpolate,th1f_down.Integral(),th1f_nom.Integral(),th1f_up.Integral())
		if upE != upE: upE=1.000
		if downE != downE: downE=1.000
	return [downE,upE]
def interp1Sigma(th1f_nom,th1f_down,th1f_up):
	nomE = th1f_nom.Integral()
	if nomE==0:
		return [1.000,1.000]
	downE = th1f_down.Integral()/nomE
	upE = th1f_up.Integral()/nomE
	if options.quadInterpolate!=0:
		downE = quadInterpolate(-1.,-1.*options.quadInterpolate,0.,1.*options.quadInterpolate,th1f_down.Integral(),th1f_nom.Integral(),th1f_up.Integral())
		upE = quadInterpolate(1.,-1.*options.quadInterpolate,0.,1.*options.quadInterpolate,th1f_down.Integral(),th1f_nom.Integral(),th1f_up.Integral())
		if upE != upE: upE=1.000
		if downE != downE: downE=1.000
	return [downE,upE]
예제 #5
0
def writeCard(tfile, mass, scaleErr):

    lumistring = "%1.1f fb^{-1}" % (options.intLumi)
    print "Writing Datacard for mass -> ", mass
    outPut = open(
        cardOutDir + "/mva-datacard_" + runtype + "_%3.1f.txt" % mass, "w")

    # Get All of the histograms we are going to use
    # Data ->
    dataHist = tfile.Get("th1f_data_" + runtype + "_%3.1f" % mass)
    nBins = dataHist.GetNbinsX()
    print "Number of Channels -> ", nBins
    # bkg model ->
    bkgHist = tfile.Get("th1f_bkg_" + runtype + "_%3.1f" % mass)

    if options.Bias:
        bkgHistCorr = tfile.Get("th1f_bkg_" + runtype +
                                "_%3.1f_fitsb_biascorr" % mass)
    #if options.Bias: bkgHistCorr   = tfile.Get("th1f_bkg_mc_"+runtype+"_%3.1f"%mass)
    # 4 signal channels ->
    gghHist = tfile.Get("th1f_sig_" + runtype + "_ggh_%3.1f" % mass)
    vbfHist = tfile.Get("th1f_sig_" + runtype + "_vbf_%3.1f" % mass)
    wzhHist = tfile.Get("th1f_sig_" + runtype + "_wzh_%3.1f" % mass)
    tthHist = tfile.Get("th1f_sig_" + runtype + "_tth_%3.1f" % mass)

    if options.signalyieldsweight > 0:
        print "Re-weighting Signal yields x %d" % signalyieldsweight
        gghHist.Scale(signalyieldsweight)
        vbfHist.Scale(signalyieldsweight)
        wzhHist.Scale(signalyieldsweight)
        tthHist.Scale(signalyieldsweight)

    ###############################################################################
    # Write the basics
    outPut.write("Mva Binned Analysis DataCard (mH=%3.1f) \n" % mass)
    outPut.write("DataCard extracted from %s \n" % tfile.GetName())
    outPut.write(
        "--------------------------------------------------------------\n")
    outPut.write("imax *\n")
    outPut.write("jmax *\n")
    outPut.write("kmax *\n")
    outPut.write(
        "--------------------------------------------------------------\n")
    ## Now its the observation
    outPut.write("bin 	     ")
    binL = 1
    binH = nBins + 1
    if options.binfromright > -1:
        binL = nBins + 1 - options.binfromright
        binH = binL + 1
        if binL < 1:
            sys.exit("Not enough bins to write %d" % options.binfromright)
    for b in range(binL, binH):
        outPut.write(" cat%d " % b)
    outPut.write("\nobservation")

    #  backgroundContents = [bkgHist.GetBinContent(b) for b in range(1,nBins+1)]
    backgroundContents = []
    if options.Bias:
        print "Using Bkg Model Corrected for mass bias"
        backgroundContents = [
            bkgHistCorr.GetBinContent(b) for b in range(1, nBins + 1)
        ]
    else:
        sys.exit("Simple Background model no longer available !!!! ")

    if options.throwToy or options.throwGlobalToy or options.throwAsimov:
        print "Throwing toy dataset"

        #	if options.throwGlobalToy: pseudoBackgroundOnlyDataset=generateFixedNData(backgroundContents,g_normalisation_toy)
        if options.throwGlobalToy and not options.throwAsimov:
            pseudoBackgroundOnlyDataset = fillToyBDT(dataHist)
        else:
            pseudoBackgroundOnlyDataset = [
                g_r.Poisson(backgroundContents[b - 1])
                for b in range(1, nBins + 1)
            ]

        if options.throwAsimov:
            pseudoBackgroundOnlyDataset = fillAsimovBDT(dataHist, bkgHistCorr)

        for b in range(1, nBins + 1):
            nd = pseudoBackgroundOnlyDataset[b - 1]
            ns = 0
            if options.expSig > 0 and not options.throwGlobalToy and not options.throwAsimov:
                print "Injecting %.f x SM" % options.expSig
                ns += getPoissonBinContent(gghHist, b, options.expSig)
                ns += getPoissonBinContent(vbfHist, b, options.expSig)
                ns += getPoissonBinContent(wzhHist, b, options.expSig)
                ns += getPoissonBinContent(tthHist, b, options.expSig)

            print 'here'
            if options.throwAsimov: outPut.write(" %.2f " % (nd + ns))
            else: outPut.write(" %d " % (nd + ns))
            dataHist.SetBinContent(b, nd + ns)
            dataHist.SetBinError(b, (nd + ns)**0.5)

    else:
        for b in range(binL, binH):
            outPut.write(" %d " % dataHist.GetBinContent(b))
    outPut.write(
        "\n--------------------------------------------------------------\n")
    ## Now we do the signal and background parts
    outPut.write("bin 	     ")
    for b in range(binL, binH):
        outPut.write(" cat%d  cat%d  cat%d  cat%d  cat%d " % (b, b, b, b, b))
    outPut.write("\nprocess    ")
    for b in range(binL, binH):
        outPut.write("  ggh    vbf    wzh    tth    bkg  ")
    outPut.write("\nprocess    ")
    for b in range(binL, binH):
        outPut.write("   0      0      0      0    1    ")
    outPut.write("\nrate       ")

    # if global toy need to randomise background within its error as well

    if options.randomizeBackground:
        # get signed error matrix from fit
        th2f_errmat = tfile.Get("fUncorrErr_%s_%3.1f" %
                                (options.bdtType, mass))
        for b in range(binL, binH):
            # first randomise by scale error
            randBkg = backgroundContents[b - 1] * (scaleErr**(1 + g_r.Gaus()))

            # then randomise by bin to bin correlation error
            for q in range(1, nBins):
                berr = th2f_errmat.GetBinContent(q, b)
                bkgCont = backgroundContents[b - 1] / sum(backgroundContents)
                bias = quadInterpolate(1., -1., 0., 1., bkgCont - berr,
                                       bkgCont, bkgCont + berr)
                randBkg = randBkg * (bias**(1 + g_r.Gaus()))

            outPut.write(" %.3f   %.3f   %.3f   %.3f   %.3f "\
            %(getBinContent(gghHist,b),getBinContent(vbfHist,b),getBinContent(wzhHist,b),getBinContent(tthHist,b)\
             ,randBkg))
        outPut.write(
            "\n--------------------------------------------------------------\n"
        )

    else:
        for b in range(binL, binH):            outPut.write(" %.3f   %.3f   %.3f   %.3f   %.3f "\
%(getBinContent(gghHist,b),getBinContent(vbfHist,b),getBinContent(wzhHist,b),getBinContent(tthHist,b)\
,backgroundContents[b-1]))
        outPut.write(
            "\n--------------------------------------------------------------\n"
        )

    # This next bit is for the signal systematics, first lets do the easy ones, lumi and theory
    outPut.write("\nlumi       lnN ")
    for b in range(binL, binH):
        outPut.write(" %s  %s  %s  %s  -  " % (lumi, lumi, lumi, lumi))

    if options.theorySys:
        outPut.write("\nQCDscale_ggH  lnN ")
        for b in range(binL, binH):
            outPut.write(" %s  -   -   -   -  " % (QCDscale_ggH))
        outPut.write("\nQCDscale_qqH  lnN ")
        for b in range(binL, binH):
            outPut.write(" -   %s  -   -   -  " % (QCDscale_qqH))
        outPut.write("\nQCDscale_VH   lnN ")
        for b in range(binL, binH):
            outPut.write(" -   -   %s  -   -  " % (QCDscale_VH))
        outPut.write("\nQCDscale_ttH  lnN ")
        for b in range(binL, binH):
            outPut.write(" -   -   -   %s  -  " % (QCDscale_ttH))
        outPut.write("\nPDF_ggH  lnN ")
        for b in range(binL, binH):
            outPut.write(" %s  -   -   -   -  " % (PDF_ggH))
        outPut.write("\nPDF_qqH  lnN ")
        for b in range(binL, binH):
            outPut.write(" -   %s  -   -   -  " % (PDF_qqH))
        outPut.write("\nPDF_VH   lnN ")
        for b in range(binL, binH):
            outPut.write(" -   -   %s  -   -  " % (PDF_VH))
        outPut.write("\nPDF_ttH  lnN ")
        for b in range(binL, binH):
            outPut.write(" -   -   -   %s  -  " % (PDF_ttH))

    outPut.write("\n")

    # includeVBF means the last bin is the VBF tagged bin and we apply and additional
    # 70% GGH(TTH) and 10% on the VBF(WZH) part of that category (configurable above)
    nBins_inclusive = 0
    if options.includeVBF:
        print "Including VBF Systematics"
        for b in range(binL, nBins + 1):
            if dataHist.GetBinLowEdge(b) < 1: nBins_inclusive += 1

        nBins_exclusive = nBins_vbf + nBins_vh
        print "Number of Non VBF channels -> ", nBins_inclusive
        print "Number of VBF channels -> ", nBins_vbf
        print "Number of VH channels -> ", nBins_vh
        print "Number of Exclu channels -> ", nBins_exclusive

        # First is UEPS
        # calculate the effect on each bin
        numberOfGGH_dijet = sum([
            gghHist.GetBinContent(b) * UEPS_ggH
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfTTH_dijet = sum([
            tthHist.GetBinContent(b) * UEPS_ttH
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfVBF_dijet = sum([
            vbfHist.GetBinContent(b) * UEPS_qqH
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfWZH_dijet = sum([
            wzhHist.GetBinContent(b) * UEPS_VH
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])

        numberOfGGH_incl = sum([
            gghHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfTTH_incl = sum([
            tthHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfVBF_incl = sum([
            vbfHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfWZH_incl = sum([
            wzhHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])

        outPut.write("\nUEPS  lnN ")

        # inclusive bins
        for b in range(binL, nBins_inclusive + binL):            outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
(1.-(numberOfGGH_dijet/numberOfGGH_incl),1.+(numberOfGGH_dijet/numberOfGGH_incl),\
1.-(numberOfVBF_dijet/numberOfVBF_incl),1.+(numberOfVBF_dijet/numberOfVBF_incl),\
1.-(numberOfWZH_dijet/numberOfWZH_incl),1.+(numberOfWZH_dijet/numberOfWZH_incl),\
1.-(numberOfTTH_dijet/numberOfTTH_incl),1.+(numberOfTTH_dijet/numberOfTTH_incl)))
        # exclusive bins - vbf
        for b in range(binH - nBins_exclusive, binH - nBins_vh):            outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
(1+UEPS_ggH,1-UEPS_ggH,1+UEPS_qqH,1-UEPS_qqH,1+UEPS_VH,1-UEPS_VH,1+UEPS_ttH,1-UEPS_ttH))
        # exclusive bins - vh
        for b in range(binH - nBins_vh, binH):
            outPut.write(" -   -   -   -   -  ")
        outPut.write("\n")

        # Next is JEC
        numberOfGGH_dijet = sum([
            gghHist.GetBinContent(b) * JEC_ggH
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfTTH_dijet = sum([
            tthHist.GetBinContent(b) * JEC_ttH
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfVBF_dijet = sum([
            vbfHist.GetBinContent(b) * JEC_qqH
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfWZH_dijet = sum([
            wzhHist.GetBinContent(b) * JEC_VH
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])

        numberOfGGH_incl = sum([
            gghHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfTTH_incl = sum([
            tthHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfVBF_incl = sum([
            vbfHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfWZH_incl = sum([
            wzhHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])

        outPut.write("\nJEC   lnN")
        # inclusive bins
        for b in range(binL, nBins_inclusive + binL):            outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
(1.-(numberOfGGH_dijet/numberOfGGH_incl),1.+(numberOfGGH_dijet/numberOfGGH_incl),\
1.-(numberOfVBF_dijet/numberOfVBF_incl),1.+(numberOfVBF_dijet/numberOfVBF_incl),\
1.-(numberOfWZH_dijet/numberOfWZH_incl),1.+(numberOfWZH_dijet/numberOfWZH_incl),\
1.-(numberOfTTH_dijet/numberOfTTH_incl),1.+(numberOfTTH_dijet/numberOfTTH_incl)))
        # exclusive bins - vbf
        for b in range(binH - nBins_exclusive, binH - nBins_vh):            outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
(1+JEC_ggH,1-JEC_ggH,1+JEC_qqH,1-JEC_qqH,1+JEC_VH,1-JEC_VH,1+JEC_ttH,1-JEC_ttH))
        # exclusive bins - vh
        for b in range(binH - nBins_vh, binH):
            outPut.write(" -   -   -   -   -  ")

        # Now do PUJetID
        numberOfGGH_dijet = sum([
            gghHist.GetBinContent(b) * PUJetIDEff
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfTTH_dijet = sum([
            tthHist.GetBinContent(b) * PUJetIDEff
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfVBF_dijet = sum([
            vbfHist.GetBinContent(b) * PUJetIDEff
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])
        numberOfWZH_dijet = sum([
            wzhHist.GetBinContent(b) * PUJetIDEff
            for b in range(binH - nBins_exclusive, binH - nBins_vh)
        ])

        numberOfGGH_incl = sum([
            gghHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfTTH_incl = sum([
            tthHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfVBF_incl = sum([
            vbfHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])
        numberOfWZH_incl = sum([
            wzhHist.GetBinContent(b)
            for b in range(binL, nBins_inclusive + binL)
        ])

        outPut.write("\nCMS_eff_j   lnN")
        # inclusive bins
        for b in range(binL, nBins_inclusive + binL):            outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
(1.-(numberOfGGH_dijet/numberOfGGH_incl),1.+(numberOfGGH_dijet/numberOfGGH_incl),\
1.-(numberOfVBF_dijet/numberOfVBF_incl),1.+(numberOfVBF_dijet/numberOfVBF_incl),\
1.-(numberOfWZH_dijet/numberOfWZH_incl),1.+(numberOfWZH_dijet/numberOfWZH_incl),\
1.-(numberOfTTH_dijet/numberOfTTH_incl),1.+(numberOfTTH_dijet/numberOfTTH_incl)))
        # exclusive bins - vbf
        for b in range(binH - nBins_exclusive, binH - nBins_vh):            outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
(1+PUJetIDEff,1-PUJetIDEff,1+PUJetIDEff,1-PUJetIDEff,1+PUJetIDEff,1-PUJetIDEff,1+PUJetIDEff,1-PUJetIDEff))
        # exclusive bins - vh
        for b in range(binH - nBins_vh, binH):
            outPut.write(" -   -   -   -   -  ")

        # Now do UEPSMigration
        outPut.write("\nUEPSMigration  lnN")
        numberOfGGH_loose = gghHist.GetBinContent(
            binL + nBins_inclusive) * UEPS_Migration_ggH
        numberOfVBF_loose = vbfHist.GetBinContent(
            binL + nBins_inclusive) * UEPS_Migration_qqH
        numberOfWZH_loose = wzhHist.GetBinContent(
            binL + nBins_inclusive) * UEPS_Migration_VH
        numberOfTTH_loose = tthHist.GetBinContent(
            binL + nBins_inclusive) * UEPS_Migration_ttH
        numberOfGGH_tight = gghHist.GetBinContent(
            binH -
            nBins_vh) if gghHist.GetBinContent(binH - nBins_vh) > 0 else 1.e10
        numberOfVBF_tight = vbfHist.GetBinContent(
            binH -
            nBins_vh) if vbfHist.GetBinContent(binH - nBins_vh) > 0 else 1.e10
        numberOfWZH_tight = wzhHist.GetBinContent(
            binH -
            nBins_vh) if wzhHist.GetBinContent(binH - nBins_vh) > 0 else 1.e10
        numberOfTTH_tight = tthHist.GetBinContent(
            binH -
            nBins_vh) if tthHist.GetBinContent(binH - nBins_vh) > 0 else 1.e10

        if abs(numberOfGGH_loose / numberOfGGH_tight) > 1.:
            numberOfGGH_loose = 0.
        if abs(numberOfVBF_loose / numberOfVBF_tight) > 1.:
            numberOfVBF_loose = 0.
        if abs(numberOfWZH_loose / numberOfWZH_tight) > 1.:
            numberOfWZH_loose = 0.
        if abs(numberOfTTH_loose / numberOfTTH_tight) > 1.:
            numberOfTTH_loose = 0.

        for b in range(binL, nBins_inclusive + binL):
            outPut.write(" -   -   -   -   -")
        if nBins_vbf > 0:            outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
(1+UEPS_Migration_ggH,1-UEPS_Migration_ggH,1+UEPS_Migration_qqH,1-UEPS_Migration_qqH,1+UEPS_Migration_VH,1-UEPS_Migration_VH,1+UEPS_Migration_ttH,1-UEPS_Migration_ttH,\
1.-(numberOfGGH_loose/numberOfGGH_tight),1.+(numberOfGGH_loose/numberOfGGH_tight),\
1.-(numberOfVBF_loose/numberOfVBF_tight),1.+(numberOfVBF_loose/numberOfVBF_tight),\
1.-(numberOfWZH_loose/numberOfWZH_tight),1.+(numberOfWZH_loose/numberOfWZH_tight),\
1.-(numberOfTTH_loose/numberOfTTH_tight),1.+(numberOfTTH_loose/numberOfTTH_tight)))
        if nBins_vh:
            outPut.write(
                " -   -   -   -   - -   -   -   -   - -   -   -   -   - \n")

        # Now do JECMigration
        outPut.write("\nJECMigration  lnN")
        numberOfGGH_loose = gghHist.GetBinContent(
            binL + nBins_inclusive) * JEC_Migration_ggH
        numberOfVBF_loose = vbfHist.GetBinContent(
            binL + nBins_inclusive) * JEC_Migration_qqH
        numberOfWZH_loose = wzhHist.GetBinContent(
            binL + nBins_inclusive) * JEC_Migration_VH
        numberOfTTH_loose = tthHist.GetBinContent(
            binL + nBins_inclusive) * JEC_Migration_ttH
        numberOfGGH_tight = gghHist.GetBinContent(
            binH -
            nBins_vh) if gghHist.GetBinContent(binH - nBins_vh) > 0 else 1.e10
        numberOfVBF_tight = vbfHist.GetBinContent(
            binH -
            nBins_vh) if vbfHist.GetBinContent(binH - nBins_vh) > 0 else 1.e10
        numberOfWZH_tight = wzhHist.GetBinContent(
            binH -
            nBins_vh) if wzhHist.GetBinContent(binH - nBins_vh) > 0 else 1.e10
        numberOfTTH_tight = tthHist.GetBinContent(
            binH -
            nBins_vh) if tthHist.GetBinContent(binH - nBins_vh) > 0 else 1.e10

        if abs(numberOfGGH_loose / numberOfGGH_tight) > 1.:
            numberOfGGH_loose = 0.
        if abs(numberOfVBF_loose / numberOfVBF_tight) > 1.:
            numberOfVBF_loose = 0.
        if abs(numberOfWZH_loose / numberOfWZH_tight) > 1.:
            numberOfWZH_loose = 0.
        if abs(numberOfTTH_loose / numberOfTTH_tight) > 1.:
            numberOfTTH_loose = 0.

        for b in range(binL, nBins_inclusive + binL):
            outPut.write(" -   -   -   -   -")
        if nBins_vbf > 0:            outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
(1+JEC_Migration_ggH,1-JEC_Migration_ggH,1+JEC_Migration_qqH,1-JEC_Migration_qqH,1+JEC_Migration_VH,1-JEC_Migration_VH,1+JEC_Migration_ttH,1-JEC_Migration_ttH,\
1.-(numberOfGGH_loose/numberOfGGH_tight),1.+(numberOfGGH_loose/numberOfGGH_tight),\
1.-(numberOfVBF_loose/numberOfVBF_tight),1.+(numberOfVBF_loose/numberOfVBF_tight),\
1.-(numberOfWZH_loose/numberOfWZH_tight),1.+(numberOfWZH_loose/numberOfWZH_tight),\
1.-(numberOfTTH_loose/numberOfTTH_tight),1.+(numberOfTTH_loose/numberOfTTH_tight)))
        if nBins_vh > 0:
            outPut.write(
                " -   -   -   -   - -   -   -   -   - -   -   -   -   - \n")

    # Now do other exclusive tag systematics
    # muon tag
    outPut.write("\nCMS_eff_m   lnN    ")
    for b in range(binL, nBins_inclusive + nBins_vbf + binL):
        outPut.write(" -   -   -   -   -")
    if nBins_vh > 0:
        outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  " %
                     (1. - muon_tag_eff_ggH, 1. + muon_tag_eff_ggH,
                      1. - muon_tag_eff_qqH, 1. + muon_tag_eff_qqH,
                      1. - muon_tag_eff_VH, 1. + muon_tag_eff_VH,
                      1. - muon_tag_eff_ttH, 1. + muon_tag_eff_ttH))
    for b in range(nBins_vbf + binL + 2, binH):
        outPut.write(" -   -   -   -   -")
    # electron tag
    outPut.write("\nCMS_eff_e   lnN    ")
    for b in range(binL, nBins_inclusive + nBins_vbf + binL + 1):
        outPut.write(" -   -   -   -   -")
    if nBins_vh > 0:
        outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  " %
                     (1. - elec_tag_eff_ggH, 1. + elec_tag_eff_ggH,
                      1. - elec_tag_eff_qqH, 1. + elec_tag_eff_qqH,
                      1. - elec_tag_eff_VH, 1. + elec_tag_eff_VH,
                      1. - elec_tag_eff_ttH, 1. + elec_tag_eff_ttH))
    for b in range(nBins_vbf + binL + 3, binH):
        outPut.write(" -   -   -   -   -")
    # MET tag
    outPut.write("\nCMS_eff_met   lnN    ")
    for b in range(binL, nBins_inclusive + nBins_vbf + binL + 2):
        outPut.write(" -   -   -   -   -")
    if nBins_vh > 0:
        outPut.write(
            " %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  " %
            (1. - met_tag_eff_ggH, 1. + met_tag_eff_ggH, 1. - met_tag_eff_qqH,
             1. + met_tag_eff_qqH, 1. - met_tag_eff_VH, 1. + met_tag_eff_VH,
             1. - met_tag_eff_ttH, 1. + met_tag_eff_ttH))

    outPut.write("\n")

    # Now is the very tedious part of the signal shape systematics, for each shape, simply do -/+ sigma

    if options.signalSys:
        print "Writing Systematics Part (could be slow)"
        for sys in systematics:

            gghHistU = tfile.Get("th1f_sig_" + runtype +
                                 "_ggh_%3.1f_%sUp01_sigma" % (mass, sys))
            vbfHistU = tfile.Get("th1f_sig_" + runtype +
                                 "_vbf_%3.1f_%sUp01_sigma" % (mass, sys))
            wzhHistU = tfile.Get("th1f_sig_" + runtype +
                                 "_wzh_%3.1f_%sUp01_sigma" % (mass, sys))
            tthHistU = tfile.Get("th1f_sig_" + runtype +
                                 "_tth_%3.1f_%sUp01_sigma" % (mass, sys))
            gghHistD = tfile.Get("th1f_sig_" + runtype +
                                 "_ggh_%3.1f_%sDown01_sigma" % (mass, sys))
            vbfHistD = tfile.Get("th1f_sig_" + runtype +
                                 "_vbf_%3.1f_%sDown01_sigma" % (mass, sys))
            wzhHistD = tfile.Get("th1f_sig_" + runtype +
                                 "_wzh_%3.1f_%sDown01_sigma" % (mass, sys))
            tthHistD = tfile.Get("th1f_sig_" + runtype +
                                 "_tth_%3.1f_%sDown01_sigma" % (mass, sys))

            #print gghHistU.GetName(), vbfHistU.GetName(), wzhHistU.GetName(), tthHistU.GetName()
            #print gghHistD.GetName(), vbfHistD.GetName(), wzhHistD.GetName(), tthHistD.GetName()

            if options.signalyieldsweight > 0:
                gghHistU.Scale(signalyieldsweight)
                vbfHistU.Scale(signalyieldsweight)
                wzhHistU.Scale(signalyieldsweight)
                tthHistU.Scale(signalyieldsweight)
                gghHistD.Scale(signalyieldsweight)
                vbfHistD.Scale(signalyieldsweight)
                wzhHistD.Scale(signalyieldsweight)
                tthHistD.Scale(signalyieldsweight)

            if options.is2011: outPut.write("\n%s lnN " % sys)
            else: outPut.write("\n%s lnN " % sys)

            for b in range(binL, binH):
                outPut.write(" %s %s %s %s - "%(\
                       py_quadInterpolate(1.,-3.,0.,3.,gghHistD.GetBinContent(b)  \
                               ,gghHist.GetBinContent(b)  \
                                                           ,gghHistU.GetBinContent(b)) \
                      ,py_quadInterpolate(1.,-3.,0.,3.,vbfHistD.GetBinContent(b)  \
                               ,vbfHist.GetBinContent(b)  \
                                                           ,vbfHistU.GetBinContent(b)) \
                      ,py_quadInterpolate(1.,-3.,0.,3.,wzhHistD.GetBinContent(b)  \
                               ,wzhHist.GetBinContent(b)  \
                                                           ,wzhHistU.GetBinContent(b)) \
                      ,py_quadInterpolate(1.,-3.,0.,3.,tthHistD.GetBinContent(b)  \
                               ,tthHist.GetBinContent(b)  \
                                                           ,tthHistU.GetBinContent(b)) \
                       ))
    outPut.write("\n")
    # Finally the background errors, these are realtively simple
    if options.is2011: outPut.write("\nbkg_norm lnN ")
    else: outPut.write("\nbkg_norm_8TeV lnN ")
    for b in range(binL, binH):
        outPut.write(" -   -   -   -  %.3f " % (scaleErr))

    ## now for the David errors
    if options.Bias:
        print "Including Mass Bias nuisances (bin-to-bin stat error included)"

        # Input Signed Error Matrix from Fit
        th2f_errmatrix = tfile.Get("fUncorrErr_%s_%3.1f" %
                                   (options.bdtType, mass))
        for b in range(
                1, nBins
        ):  # This error matrix is nBins-1 X nBins-1 due to constraint on sum on fractions
            if options.is2011: outPut.write("\nmassBias%d lnN" % b)
            else: outPut.write("\nmassBias%d_8TeV lnN" % b)
            for q in range(binL, binH):
                f_errentry = th2f_errmatrix.GetBinContent(b, q)
                bkgC = backgroundContents[q - 1] / sum(backgroundContents)
                bias_nuis = py_quadInterpolate(1., -1., 0., 1.,
                                               bkgC - f_errentry, bkgC,
                                               bkgC + f_errentry)
                outPut.write(" - - - - %s " % (bias_nuis))

        outPut.write("\n")

    if options.B2B and (not options.Bias):
        # bkg bins will be gmN errors instead
        for b in range(binL, binH):
            bkgScale = bkgHist.Integral() / bkgHist.GetEntries()
            if options.is2011:
                outPut.write("\nbkg_stat%d gmN %d " %
                             (b, int(backgroundContents[b - 1] / bkgScale)))
            else:
                outPut.write("\nbkg_stat%d_8TeV gmN %d " %
                             (b, int(backgroundContents[b - 1] / bkgScale)))
            for q in range(binL, binH):
                if q == b: outPut.write(" - - - - %.3f " % bkgScale)
                else: outPut.write(" - - - - - ")

    # Finally make a plot of what will go into the limit
    if options.makePlot:
        print "Plotting Limit Setting inputs to m%3.1f.pdf" % mass
        if options.Bias: plotBKG = bkgHistCorr.Clone()
        else: plotBKG = bkgHist.Clone()
        plotDistributions(mass,dataHist.Clone() \
          ,[gghHist.Clone(),wzhHist.Clone(),tthHist.Clone(),vbfHist.Clone()]\
          ,plotBKG \
          ,[scaleErr-1. for b in range(nBins)])# put VBF at the end for plot option

    outPut.write("\n")  # done
    outPut.close()
예제 #6
0
if syst == 'all':
    allSyst = getListOfSystematicSources(file)
else:
    allSyst = [ syst ]

for syst in allSyst:
    nuis = {}
    for cat in range(ncat):
        nuis[cat]={}
        for proc in procs:
            nominal = file.Get("th1f_sig_%s_mass_m%d_cat%d" % (proc,mass, cat) )
            up      = file.Get("th1f_sig_%s_mass_m%d_cat%d_%sUp01_sigma" % (proc, mass, cat,syst) )
            down    = file.Get("th1f_sig_%s_mass_m%d_cat%d_%sDown01_sigma" % (proc,mass, cat,syst) )

            if nominal.Integral() != 0:
                downE = quadInterpolate(1.,-3.,0.,3.,down.Integral(),nominal.Integral(),up.Integral())
                upE = quadInterpolate(-1.,-3.,0.,1.,down.Integral(),nominal.Integral(),up.Integral())
                nuis[cat][proc] = (downE,upE) 
            else:
                nuis[cat][proc] = (1.,1.)

    print syst, '  lnN  ',  
    for cat in range(ncat):
        for proc in procs:
            print "%1.3f/%1.3f " % nuis[cat][proc],
        print "-     ",

    for cat in range(ncat,ntot):
        for proc in procs:
            print "%1.3f/%1.3f " % (1.,1.),
        print "-     ",
예제 #7
0
def writeCard(tfile,mass,scaleErr):

  lumistring = "%1.1f fb^{-1}"%(options.intLumi)
  print "Writing Datacard for mass -> ", mass
  outPut = open(cardOutDir+"/mva-datacard_"+runtype+"_%3.1f.txt"%mass,"w")

  # Get All of the histograms we are going to use
  # Data ->
  dataHist = tfile.Get("th1f_data_"+runtype+"_%3.1f"%mass)
  nBins    = dataHist.GetNbinsX()
  print "Number of Channels -> ", nBins
  # bkg model ->
  bkgHist  	= tfile.Get("th1f_bkg_"+runtype+"_%3.1f"%mass)
  
  if options.Bias: bkgHistCorr   = tfile.Get("th1f_bkg_"+runtype+"_%3.1f_fitsb_biascorr"%mass)
  #if options.Bias: bkgHistCorr   = tfile.Get("th1f_bkg_mc_"+runtype+"_%3.1f"%mass)
  # 4 signal channels ->
  gghHist  = tfile.Get("th1f_sig_"+runtype+"_ggh_%3.1f"%mass)
  vbfHist  = tfile.Get("th1f_sig_"+runtype+"_vbf_%3.1f"%mass)
  wzhHist  = tfile.Get("th1f_sig_"+runtype+"_wzh_%3.1f"%mass)
  tthHist  = tfile.Get("th1f_sig_"+runtype+"_tth_%3.1f"%mass)

  if options.signalyieldsweight > 0:
    print "Re-weighting Signal yields x %d"%signalyieldsweight
    gghHist.Scale(signalyieldsweight)
    vbfHist.Scale(signalyieldsweight)
    wzhHist.Scale(signalyieldsweight)
    tthHist.Scale(signalyieldsweight)
 
  ###############################################################################
  # Write the basics
  outPut.write("Mva Binned Analysis DataCard (mH=%3.1f) \n"%mass)
  outPut.write("DataCard extracted from %s \n"%tfile.GetName())
  outPut.write("--------------------------------------------------------------\n")
  outPut.write("imax *\n")
  outPut.write("jmax *\n")
  outPut.write("kmax *\n")
  outPut.write("--------------------------------------------------------------\n")
  ## Now its the observation
  outPut.write("bin 	     ")
  binL=1
  binH=nBins+1
  if options.binfromright>-1:
    binL=nBins+1-options.binfromright
    binH=binL+1
    if binL<1:
      sys.exit("Not enough bins to write %d" %options.binfromright)
  for b in range(binL,binH): outPut.write(" cat%d "%b)
  outPut.write("\nobservation")

#  backgroundContents = [bkgHist.GetBinContent(b) for b in range(1,nBins+1)]
  backgroundContents = []
  if options.Bias:
	print "Using Bkg Model Corrected for mass bias"
	backgroundContents = [bkgHistCorr.GetBinContent(b) for b in range(1,nBins+1)]
  else: sys.exit("Simple Background model no longer available !!!! ")

  if options.throwToy or options.throwGlobalToy or options.throwAsimov:
        print "Throwing toy dataset"

#	if options.throwGlobalToy: pseudoBackgroundOnlyDataset=generateFixedNData(backgroundContents,g_normalisation_toy)
	if options.throwGlobalToy and not options.throwAsimov :pseudoBackgroundOnlyDataset=fillToyBDT(dataHist)
	else: pseudoBackgroundOnlyDataset=[g_r.Poisson(backgroundContents[b-1]) for b in range(1,nBins+1)]

	if options.throwAsimov: pseudoBackgroundOnlyDataset=fillAsimovBDT(dataHist,bkgHistCorr)

	for b in range(1,nBins+1): 
		nd = pseudoBackgroundOnlyDataset[b-1]
		ns = 0
		if options.expSig>0 and not options.throwGlobalToy and not options.throwAsimov:
			print "Injecting %.f x SM"%options.expSig
			ns+=getPoissonBinContent(gghHist,b,options.expSig)
			ns+=getPoissonBinContent(vbfHist,b,options.expSig)
			ns+=getPoissonBinContent(wzhHist,b,options.expSig)
			ns+=getPoissonBinContent(tthHist,b,options.expSig)
		
		print 'here'
		if options.throwAsimov: outPut.write(" %.2f "%(nd+ns))
		else: outPut.write(" %d "%(nd+ns))
		dataHist.SetBinContent(b,nd+ns)
		dataHist.SetBinError(b,(nd+ns)**0.5)

  else:
	for b in range(binL,binH): outPut.write(" %d "%dataHist.GetBinContent(b))
  outPut.write("\n--------------------------------------------------------------\n")
  ## Now we do the signal and background parts
  outPut.write("bin 	     ")
  for b in range(binL,binH): outPut.write(" cat%d  cat%d  cat%d  cat%d  cat%d "%(b,b,b,b,b))
  outPut.write("\nprocess    ")
  for b in range(binL,binH): outPut.write("  ggh    vbf    wzh    tth    bkg  ")
  outPut.write("\nprocess    ")
  for b in range(binL,binH): outPut.write("   0      0      0      0    1    ")
  outPut.write("\nrate       ")

  # if global toy need to randomise background within its error as well

  if options.randomizeBackground:
    # get signed error matrix from fit
    th2f_errmat = tfile.Get("fUncorrErr_%s_%3.1f"%(options.bdtType,mass))
    for b in range(binL,binH):
      # first randomise by scale error
      randBkg = backgroundContents[b-1]*(scaleErr**(1+g_r.Gaus()))

    # then randomise by bin to bin correlation error
      for q in range(1,nBins):
        berr = th2f_errmat.GetBinContent(q,b)
        bkgCont = backgroundContents[b-1]/sum(backgroundContents)
        bias = quadInterpolate(1.,-1.,0.,1.,bkgCont-berr,bkgCont,bkgCont+berr)
        randBkg = randBkg*(bias**(1+g_r.Gaus()))
      
      outPut.write(" %.3f   %.3f   %.3f   %.3f   %.3f "\
      %(getBinContent(gghHist,b),getBinContent(vbfHist,b),getBinContent(wzhHist,b),getBinContent(tthHist,b)\
       ,randBkg))
    outPut.write("\n--------------------------------------------------------------\n")

  else:
    for b in range(binL,binH): outPut.write(" %.3f   %.3f   %.3f   %.3f   %.3f "\
      %(getBinContent(gghHist,b),getBinContent(vbfHist,b),getBinContent(wzhHist,b),getBinContent(tthHist,b)\
       ,backgroundContents[b-1]))
    outPut.write("\n--------------------------------------------------------------\n")


  # This next bit is for the signal systematics, first lets do the easy ones, lumi and theory
  outPut.write("\nlumi       lnN ")
  for b in range(binL,binH): outPut.write(" %s  %s  %s  %s  -  "%(lumi,lumi,lumi,lumi))

  if options.theorySys:
    outPut.write("\nQCDscale_ggH  lnN ")
    for b in range(binL,binH): outPut.write(" %s  -   -   -   -  "%(QCDscale_ggH))
    outPut.write("\nQCDscale_qqH  lnN ")
    for b in range(binL,binH): outPut.write(" -   %s  -   -   -  "%(QCDscale_qqH))
    outPut.write("\nQCDscale_VH   lnN ")
    for b in range(binL,binH): outPut.write(" -   -   %s  -   -  "%(QCDscale_VH))
    outPut.write("\nQCDscale_ttH  lnN ")
    for b in range(binL,binH): outPut.write(" -   -   -   %s  -  "%(QCDscale_ttH))
    outPut.write("\nPDF_ggH  lnN ")
    for b in range(binL,binH): outPut.write(" %s  -   -   -   -  "%(PDF_ggH))
    outPut.write("\nPDF_qqH  lnN ")
    for b in range(binL,binH): outPut.write(" -   %s  -   -   -  "%(PDF_qqH))
    outPut.write("\nPDF_VH   lnN ")
    for b in range(binL,binH): outPut.write(" -   -   %s  -   -  "%(PDF_VH))
    outPut.write("\nPDF_ttH  lnN ")
    for b in range(binL,binH): outPut.write(" -   -   -   %s  -  "%(PDF_ttH))

  outPut.write("\n")

  # includeVBF means the last bin is the VBF tagged bin and we apply and additional 
  # 70% GGH(TTH) and 10% on the VBF(WZH) part of that category (configurable above)
  nBins_inclusive=0
  if options.includeVBF:
    print "Including VBF Systematics"
    for b in range(binL,nBins+1):  
	if dataHist.GetBinLowEdge(b)<1: nBins_inclusive+=1

    nBins_exclusive=nBins_vbf+nBins_vh
    print "Number of Non VBF channels -> ", nBins_inclusive
    print "Number of VBF channels -> ", nBins_vbf
    print "Number of VH channels -> ", nBins_vh
    print "Number of Exclu channels -> ", nBins_exclusive

    # First is UEPS
    # calculate the effect on each bin
    numberOfGGH_dijet = sum([gghHist.GetBinContent(b)*UEPS_ggH for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfTTH_dijet = sum([tthHist.GetBinContent(b)*UEPS_ttH for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfVBF_dijet = sum([vbfHist.GetBinContent(b)*UEPS_qqH for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfWZH_dijet = sum([wzhHist.GetBinContent(b)*UEPS_VH  for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    
    numberOfGGH_incl  = sum([gghHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfTTH_incl  = sum([tthHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfVBF_incl  = sum([vbfHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfWZH_incl  = sum([wzhHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])

    outPut.write("\nUEPS  lnN ")
    
    # inclusive bins
    for b in range(binL,nBins_inclusive+binL): outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
		    (1.-(numberOfGGH_dijet/numberOfGGH_incl),1.+(numberOfGGH_dijet/numberOfGGH_incl),\
		     1.-(numberOfVBF_dijet/numberOfVBF_incl),1.+(numberOfVBF_dijet/numberOfVBF_incl),\
		     1.-(numberOfWZH_dijet/numberOfWZH_incl),1.+(numberOfWZH_dijet/numberOfWZH_incl),\
		     1.-(numberOfTTH_dijet/numberOfTTH_incl),1.+(numberOfTTH_dijet/numberOfTTH_incl)))

    # exclusive bins - vbf
    for b in range(binH-nBins_exclusive,binH-nBins_vh): outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
      (1+UEPS_ggH,1-UEPS_ggH,1+UEPS_qqH,1-UEPS_qqH,1+UEPS_VH,1-UEPS_VH,1+UEPS_ttH,1-UEPS_ttH))
    # exclusive bins - vh
    for b in range(binH-nBins_vh,binH): outPut.write(" -   -   -   -   -  ")
    outPut.write("\n")

    # Next is JEC
    numberOfGGH_dijet = sum([gghHist.GetBinContent(b)*JEC_ggH for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfTTH_dijet = sum([tthHist.GetBinContent(b)*JEC_ttH for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfVBF_dijet = sum([vbfHist.GetBinContent(b)*JEC_qqH for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfWZH_dijet = sum([wzhHist.GetBinContent(b)*JEC_VH  for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    
    numberOfGGH_incl  = sum([gghHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfTTH_incl  = sum([tthHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfVBF_incl  = sum([vbfHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfWZH_incl  = sum([wzhHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])

    outPut.write("\nJEC   lnN")
    # inclusive bins
    for b in range(binL,nBins_inclusive+binL): outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
		    (1.-(numberOfGGH_dijet/numberOfGGH_incl),1.+(numberOfGGH_dijet/numberOfGGH_incl),\
		     1.-(numberOfVBF_dijet/numberOfVBF_incl),1.+(numberOfVBF_dijet/numberOfVBF_incl),\
		     1.-(numberOfWZH_dijet/numberOfWZH_incl),1.+(numberOfWZH_dijet/numberOfWZH_incl),\
		     1.-(numberOfTTH_dijet/numberOfTTH_incl),1.+(numberOfTTH_dijet/numberOfTTH_incl)))
    # exclusive bins - vbf
    for b in range(binH-nBins_exclusive,binH-nBins_vh): outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
      (1+JEC_ggH,1-JEC_ggH,1+JEC_qqH,1-JEC_qqH,1+JEC_VH,1-JEC_VH,1+JEC_ttH,1-JEC_ttH))
    # exclusive bins - vh
    for b in range(binH-nBins_vh,binH): outPut.write(" -   -   -   -   -  ")

    # Now do PUJetID
    numberOfGGH_dijet = sum([gghHist.GetBinContent(b)*PUJetIDEff for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfTTH_dijet = sum([tthHist.GetBinContent(b)*PUJetIDEff for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfVBF_dijet = sum([vbfHist.GetBinContent(b)*PUJetIDEff for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    numberOfWZH_dijet = sum([wzhHist.GetBinContent(b)*PUJetIDEff for b in range(binH-nBins_exclusive,binH-nBins_vh)])
    
    numberOfGGH_incl  = sum([gghHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfTTH_incl  = sum([tthHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfVBF_incl  = sum([vbfHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])
    numberOfWZH_incl  = sum([wzhHist.GetBinContent(b) for b in range(binL,nBins_inclusive+binL)])

    outPut.write("\nCMS_eff_j   lnN")
    # inclusive bins
    for b in range(binL,nBins_inclusive+binL): outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
		    (1.-(numberOfGGH_dijet/numberOfGGH_incl),1.+(numberOfGGH_dijet/numberOfGGH_incl),\
		     1.-(numberOfVBF_dijet/numberOfVBF_incl),1.+(numberOfVBF_dijet/numberOfVBF_incl),\
		     1.-(numberOfWZH_dijet/numberOfWZH_incl),1.+(numberOfWZH_dijet/numberOfWZH_incl),\
		     1.-(numberOfTTH_dijet/numberOfTTH_incl),1.+(numberOfTTH_dijet/numberOfTTH_incl)))
    # exclusive bins - vbf
    for b in range(binH-nBins_exclusive,binH-nBins_vh): outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%\
      (1+PUJetIDEff,1-PUJetIDEff,1+PUJetIDEff,1-PUJetIDEff,1+PUJetIDEff,1-PUJetIDEff,1+PUJetIDEff,1-PUJetIDEff))
    # exclusive bins - vh
    for b in range(binH-nBins_vh,binH): outPut.write(" -   -   -   -   -  ")

    # Now do UEPSMigration    
    outPut.write("\nUEPSMigration  lnN")
    numberOfGGH_loose = gghHist.GetBinContent(binL+nBins_inclusive)*UEPS_Migration_ggH
    numberOfVBF_loose = vbfHist.GetBinContent(binL+nBins_inclusive)*UEPS_Migration_qqH
    numberOfWZH_loose = wzhHist.GetBinContent(binL+nBins_inclusive)*UEPS_Migration_VH
    numberOfTTH_loose = tthHist.GetBinContent(binL+nBins_inclusive)*UEPS_Migration_ttH
    numberOfGGH_tight = gghHist.GetBinContent(binH-nBins_vh) if gghHist.GetBinContent(binH-nBins_vh)>0 else 1.e10
    numberOfVBF_tight = vbfHist.GetBinContent(binH-nBins_vh) if vbfHist.GetBinContent(binH-nBins_vh)>0 else 1.e10
    numberOfWZH_tight = wzhHist.GetBinContent(binH-nBins_vh) if wzhHist.GetBinContent(binH-nBins_vh)>0 else 1.e10
    numberOfTTH_tight = tthHist.GetBinContent(binH-nBins_vh) if tthHist.GetBinContent(binH-nBins_vh)>0 else 1.e10

    if abs(numberOfGGH_loose/numberOfGGH_tight)>1.: numberOfGGH_loose=0.
    if abs(numberOfVBF_loose/numberOfVBF_tight)>1.: numberOfVBF_loose=0.
    if abs(numberOfWZH_loose/numberOfWZH_tight)>1.: numberOfWZH_loose=0.
    if abs(numberOfTTH_loose/numberOfTTH_tight)>1.: numberOfTTH_loose=0.

    for b in range(binL,nBins_inclusive+binL): outPut.write(" -   -   -   -   -")
    if nBins_vbf>1:
     outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   - "%\
	(1+UEPS_Migration_ggH,1-UEPS_Migration_ggH,1+UEPS_Migration_qqH,1-UEPS_Migration_qqH
        ,1+UEPS_Migration_VH,1-UEPS_Migration_VH,1+UEPS_Migration_ttH,1-UEPS_Migration_ttH))
     for b in Bins_vbf[1:nBins_vbf]:
       outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f  -  "%\
		     (1.-(numberOfGGH_loose/numberOfGGH_tight),1.+(numberOfGGH_loose/numberOfGGH_tight),\
		     1.-(numberOfVBF_loose/numberOfVBF_tight),1.+(numberOfVBF_loose/numberOfVBF_tight),\
		     1.-(numberOfWZH_loose/numberOfWZH_tight),1.+(numberOfWZH_loose/numberOfWZH_tight),\
		     1.-(numberOfTTH_loose/numberOfTTH_tight),1.+(numberOfTTH_loose/numberOfTTH_tight)))
    for b in Bins_vh : outPut.write(" -   -   -   -   -  ")
    outPut.write("\n")
  
    # Now do JECMigration    
    outPut.write("\nJECMigration  lnN")
    numberOfGGH_loose = gghHist.GetBinContent(binL+nBins_inclusive)*JEC_Migration_ggH
    numberOfVBF_loose = vbfHist.GetBinContent(binL+nBins_inclusive)*JEC_Migration_qqH
    numberOfWZH_loose = wzhHist.GetBinContent(binL+nBins_inclusive)*JEC_Migration_VH
    numberOfTTH_loose = tthHist.GetBinContent(binL+nBins_inclusive)*JEC_Migration_ttH
    numberOfGGH_tight = gghHist.GetBinContent(binH-nBins_vh) if gghHist.GetBinContent(binH-nBins_vh)>0 else 1.e10
    numberOfVBF_tight = vbfHist.GetBinContent(binH-nBins_vh) if vbfHist.GetBinContent(binH-nBins_vh)>0 else 1.e10
    numberOfWZH_tight = wzhHist.GetBinContent(binH-nBins_vh) if wzhHist.GetBinContent(binH-nBins_vh)>0 else 1.e10
    numberOfTTH_tight = tthHist.GetBinContent(binH-nBins_vh) if tthHist.GetBinContent(binH-nBins_vh)>0 else 1.e10

    if abs(numberOfGGH_loose/numberOfGGH_tight)>1.: numberOfGGH_loose=0.
    if abs(numberOfVBF_loose/numberOfVBF_tight)>1.: numberOfVBF_loose=0.
    if abs(numberOfWZH_loose/numberOfWZH_tight)>1.: numberOfWZH_loose=0.
    if abs(numberOfTTH_loose/numberOfTTH_tight)>1.: numberOfTTH_loose=0.

    for b in range(binL,nBins_inclusive+binL): outPut.write(" -   -   -   -   -")
    if nBins_vbf>1:
     outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   - "%\
	(1+JEC_Migration_ggH,1-JEC_Migration_ggH,1+JEC_Migration_qqH,1-JEC_Migration_qqH
        ,1+JEC_Migration_VH,1-JEC_Migration_VH,1+JEC_Migration_ttH,1-JEC_Migration_ttH))
     for b in Bins_vbf[1:nBins_vbf]:
       outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f  -  "%\
		     (1.-(numberOfGGH_loose/numberOfGGH_tight),1.+(numberOfGGH_loose/numberOfGGH_tight),\
		     1.-(numberOfVBF_loose/numberOfVBF_tight),1.+(numberOfVBF_loose/numberOfVBF_tight),\
		     1.-(numberOfWZH_loose/numberOfWZH_tight),1.+(numberOfWZH_loose/numberOfWZH_tight),\
		     1.-(numberOfTTH_loose/numberOfTTH_tight),1.+(numberOfTTH_loose/numberOfTTH_tight)))
    for b in Bins_vh : outPut.write(" -   -   -   -   -  ")
    outPut.write("\n")
  
  # Now do other exclusive tag systematics
  # muon tag
  outPut.write("\nCMS_eff_m   lnN    ")
  for b in range(binL,nBins_inclusive+nBins_vbf+binL): outPut.write(" -   -   -   -   -")
  for b in Bins_vh : 
	if b in Muon_tags : outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%(1.-muon_tag_eff_ggH,1.+muon_tag_eff_ggH,1.-muon_tag_eff_qqH,1.+muon_tag_eff_qqH,1.-muon_tag_eff_VH,1.+muon_tag_eff_VH,1.-muon_tag_eff_ttH,1.+muon_tag_eff_ttH))
  	else: outPut.write(" -   -   -   -   -")
  # electron tag
  outPut.write("\nCMS_eff_e   lnN    ")
  for b in range(binL,nBins_inclusive+nBins_vbf+binL+1): outPut.write(" -   -   -   -   -")
  for b in Bins_vh : 
 	if b in Elec_tags : outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%(1.-elec_tag_eff_ggH,1.+elec_tag_eff_ggH,1.-elec_tag_eff_qqH,1.+elec_tag_eff_qqH,1.-elec_tag_eff_VH,1.+elec_tag_eff_VH,1.-elec_tag_eff_ttH,1.+elec_tag_eff_ttH))
  	else: outPut.write(" -   -   -   -   -")
  # MET tag
  outPut.write("\nCMS_eff_met   lnN    ")
  for b in range(binL,nBins_inclusive+nBins_vbf+binL+2): outPut.write(" -   -   -   -   -")
  for b in Bins_vh : 
 	if b in MET_tags :  outPut.write(" %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   %.3f/%.3f   -  "%(1.-met_tag_eff_ggH,1.+met_tag_eff_ggH,1.-met_tag_eff_qqH,1.+met_tag_eff_qqH,1.-met_tag_eff_VH,1.+met_tag_eff_VH,1.-met_tag_eff_ttH,1.+met_tag_eff_ttH))
  	else: outPut.write(" -   -   -   -   -")
  
  outPut.write("\n")

  # Now is the very tedious part of the signal shape systematics, for each shape, simply do -/+ sigma
  
  if options.signalSys:
   print "Writing Systematics Part (could be slow)"
   for sys in systematics:

    gghHistU  = tfile.Get("th1f_sig_"+runtype+"_ggh_%3.1f_%sUp01_sigma"%(mass,sys))
    vbfHistU  = tfile.Get("th1f_sig_"+runtype+"_vbf_%3.1f_%sUp01_sigma"%(mass,sys))
    wzhHistU  = tfile.Get("th1f_sig_"+runtype+"_wzh_%3.1f_%sUp01_sigma"%(mass,sys))
    tthHistU  = tfile.Get("th1f_sig_"+runtype+"_tth_%3.1f_%sUp01_sigma"%(mass,sys))
    gghHistD  = tfile.Get("th1f_sig_"+runtype+"_ggh_%3.1f_%sDown01_sigma"%(mass,sys))
    vbfHistD  = tfile.Get("th1f_sig_"+runtype+"_vbf_%3.1f_%sDown01_sigma"%(mass,sys))
    wzhHistD  = tfile.Get("th1f_sig_"+runtype+"_wzh_%3.1f_%sDown01_sigma"%(mass,sys))
    tthHistD  = tfile.Get("th1f_sig_"+runtype+"_tth_%3.1f_%sDown01_sigma"%(mass,sys))
    
    #print gghHistU.GetName(), vbfHistU.GetName(), wzhHistU.GetName(), tthHistU.GetName() 
    #print gghHistD.GetName(), vbfHistD.GetName(), wzhHistD.GetName(), tthHistD.GetName() 

    if options.signalyieldsweight > 0:
      gghHistU.Scale(signalyieldsweight)
      vbfHistU.Scale(signalyieldsweight)
      wzhHistU.Scale(signalyieldsweight)
      tthHistU.Scale(signalyieldsweight)
      gghHistD.Scale(signalyieldsweight)
      vbfHistD.Scale(signalyieldsweight)
      wzhHistD.Scale(signalyieldsweight)
      tthHistD.Scale(signalyieldsweight)

    if options.is2011: outPut.write("\n%s lnN "%sys)
    else: outPut.write("\n%s lnN "%sys)

    for b in range(binL,binH):
	 outPut.write(" %s %s %s %s - "%(\
				     py_quadInterpolate(1.,-3.,0.,3.,gghHistD.GetBinContent(b)  \
				        			  ,gghHist.GetBinContent(b)  \
                                        			  ,gghHistU.GetBinContent(b)) \
				    ,py_quadInterpolate(1.,-3.,0.,3.,vbfHistD.GetBinContent(b)  \
				        			  ,vbfHist.GetBinContent(b)  \
                                        			  ,vbfHistU.GetBinContent(b)) \
				    ,py_quadInterpolate(1.,-3.,0.,3.,wzhHistD.GetBinContent(b)  \
				        			  ,wzhHist.GetBinContent(b)  \
                                        			  ,wzhHistU.GetBinContent(b)) \
				    ,py_quadInterpolate(1.,-3.,0.,3.,tthHistD.GetBinContent(b)  \
				        			  ,tthHist.GetBinContent(b)  \
                                        			  ,tthHistU.GetBinContent(b)) \
 				    ))
  outPut.write("\n")
  # Finally the background errors, these are realtively simple
  if options.is2011: outPut.write("\nbkg_norm lnN ")
  else: outPut.write("\nbkg_norm_8TeV lnN ")
  for b in range(binL,binH): outPut.write(" -   -   -   -  %.3f "%(scaleErr))

  ## now for the David errors
  if options.Bias:
	print "Including Mass Bias nuisances (bin-to-bin stat error included)"

	# Input Signed Error Matrix from Fit 
	th2f_errmatrix = tfile.Get("fUncorrErr_%s_%3.1f"%(options.bdtType,mass))
	for b in range(1,nBins):  # This error matrix is nBins-1 X nBins-1 due to constraint on sum on fractions
           if options.is2011: outPut.write("\nmassBias%d lnN"%b)
           else: outPut.write("\nmassBias%d_8TeV lnN"%b)
	   for q in range(binL,binH):
	   	f_errentry = th2f_errmatrix.GetBinContent(b,q)
		bkgC = backgroundContents[q-1]/sum(backgroundContents)
		bias_nuis  = py_quadInterpolate(1.,-1.,0.,1.,bkgC-f_errentry,bkgC,bkgC+f_errentry)
		outPut.write(" - - - - %s "%(bias_nuis))
        	
        outPut.write("\n")
	

  if options.B2B and (not options.Bias):
   # bkg bins will be gmN errors instead 
   for b in range(binL,binH):
        bkgScale = bkgHist.Integral()/bkgHist.GetEntries()
        if options.is2011: outPut.write("\nbkg_stat%d gmN %d "%(b,int(backgroundContents[b-1]/bkgScale)))
        else: outPut.write("\nbkg_stat%d_8TeV gmN %d "%(b,int(backgroundContents[b-1]/bkgScale)))
	for q in range(binL,binH):
		if q==b: outPut.write(" - - - - %.3f "%bkgScale)
		else:    outPut.write(" - - - - - ")

  # Finally make a plot of what will go into the limit
  if options.makePlot: 
	print "Plotting Limit Setting inputs to m%3.1f.pdf"%mass
	if options.Bias : plotBKG = bkgHistCorr.Clone()
	else: plotBKG = bkgHist.Clone()
	plotDistributions(mass,dataHist.Clone() \
			,[gghHist.Clone(),wzhHist.Clone(),tthHist.Clone(),vbfHist.Clone()]\
			,plotBKG \
			,[scaleErr-1. for b in range(nBins)])# put VBF at the end for plot option

  outPut.write("\n")  # done
  outPut.close()
def interp1SigmaFrom3Sigma(th1f_nom, th1f_down, th1f_up):
    downE = quadInterpolate(-1., -3., 0., 3., th1f_down.Integral(),
                            th1f_nom.Integral(), th1f_up.Integral())
    upE = quadInterpolate(1., -3., 0., 3., th1f_down.Integral(),
                          th1f_nom.Integral(), th1f_up.Integral())
    return [downE, upE]
예제 #9
0
def interp1SigmaFrom3Sigma(th1f_nom,th1f_down,th1f_up):
	downE = quadInterpolate(-1.,-3.,0.,3.,th1f_down.Integral(),th1f_nom.Integral(),th1f_up.Integral())
	upE = quadInterpolate(1.,-3.,0.,3.,th1f_down.Integral(),th1f_nom.Integral(),th1f_up.Integral())
	return [downE,upE]
예제 #10
0
    allSyst = [syst]

for syst in allSyst:
    nuis = {}
    for cat in range(ncat):
        nuis[cat] = {}
        for proc in procs:
            nominal = file.Get("th1f_sig_%s_mass_m%d_cat%d" %
                               (proc, mass, cat))
            up = file.Get("th1f_sig_%s_mass_m%d_cat%d_%sUp01_sigma" %
                          (proc, mass, cat, syst))
            down = file.Get("th1f_sig_%s_mass_m%d_cat%d_%sDown01_sigma" %
                            (proc, mass, cat, syst))

            if nominal.Integral() != 0:
                downE = quadInterpolate(1., -3., 0., 3., down.Integral(),
                                        nominal.Integral(), up.Integral())
                upE = quadInterpolate(-1., -3., 0., 1., down.Integral(),
                                      nominal.Integral(), up.Integral())
                nuis[cat][proc] = (downE, upE)
            else:
                nuis[cat][proc] = (1., 1.)

    print syst, '  lnN  ',
    for cat in range(ncat):
        for proc in procs:
            print "%1.3f/%1.3f " % nuis[cat][proc],
        print "-     ",

    for cat in range(ncat, ntot):
        for proc in procs:
            print "%1.3f/%1.3f " % (1., 1.),