Esempio n. 1
0
argParser.add_argument('--plot_directory', action='store', default='NLL_plots')
argParser.add_argument('--useBestFit',
                       action='store_true',
                       help="Use best fit value? Default is r=1")
args = argParser.parse_args()


def Eval(obj, x, params):
    return obj.Eval(x[0])


fitKey = "dNLL_postfit_r1" if not args.useBestFit else "dNLL_bestfit"

# get the absolute post fit NLL value of pure ttZ
#ttZ_res = getResult(ewkDM_ttZ_ll_noH)
ttZ_res = getResult(dim6top_LO_ttZ_ll_ctZ_0p00_ctZI_0p00)
ttZ_NLL_abs = float(ttZ_res["NLL_prefit"]) + float(ttZ_res[fitKey])

print "Max Likelihood ttZ SM"
print ttZ_NLL_abs

#signals = [ewkDM_ttZ_ll_DC1A_0p60_DC1V_m0p24_DC2A_m0p1767_DC2V_m0p1767, ewkDM_ttZ_ll_DC1A_0p60_DC1V_m0p24_DC2A_m0p1767_DC2V_0p1767]
#signals = [ewkDM_ttZ_ll_noH_DC2V_m0p25, ewkDM_ttZ_ll_noH_DC2V_m0p15, ewkDM_ttZ_ll_noH, ewkDM_ttZ_ll_noH_DC2V_0p05, ewkDM_ttZ_ll_noH_DC2V_0p10, ewkDM_ttZ_ll_noH_DC2V_0p20, ewkDM_ttZ_ll_noH_DC2V_0p30]

signals = [
    x for x in allSamples_dim6top
    if x.name.startswith("dim6top_LO_ttZ_ll_ctZ_0p00")
]

signals = [
    dim6top_LO_ttZ_ll_ctZ_0p00_ctZI_m2p00,
Esempio n. 2
0
def wrapper(s):
    
    logger.info("Now working on %s", s.name)
    xSecScale = 1
    c = cardFileWriter.cardFileWriter()
    c.releaseLocation = combineReleaseLocation

    for coup in nonZeroCouplings:
        modification_dict[coup] = getCouplingFromName(s.name, coup)
        logger.info("The following coupling is set to non-zero value: %s: %s", coup, modification_dict[coup])
    
    xsec = p.xsecDB.get(modification_dict)
    logger.info("Found modified x-sec of %s", xsec)
    
    cardFileName = os.path.join(limitDir, s.name+'.txt')
    if not os.path.exists(cardFileName) or overWrite:
        counter=0
        c.reset()
        c.addUncertainty('PU',          'lnN')
        c.addUncertainty('JEC',         'lnN')
        c.addUncertainty('btag',        'lnN')
        c.addUncertainty('trigger',     'lnN')
        c.addUncertainty('leptonSF',    'lnN')
        c.addUncertainty('scale',       'lnN')
        c.addUncertainty('scale_sig',   'lnN')
        c.addUncertainty('PDF',         'lnN')
        c.addUncertainty('nonprompt',   'lnN')
        c.addUncertainty('WZ_xsec',     'lnN')
        c.addUncertainty('ZZ_xsec',     'lnN')
        c.addUncertainty('rare',        'lnN')
        c.addUncertainty('ttX',         'lnN')
        c.addUncertainty('tZq',         'lnN')


        for setup in setups:
            signal      = MCBasedEstimate(name="TTZ", sample=setup.samples["TTZ"], cacheDir=setup.defaultCacheDir())
            observation = MCBasedEstimate(name="observation", sample=setup.samples["pseudoData"], cacheDir=setup.defaultCacheDir())
            #observation = DataObservation(name='Data', sample=setup.sample['pseudoData'], cacheDir=setup.defaultCacheDir())
            for e in setup.estimators: e.initCache(setup.defaultCacheDir())

            for r in setup.regions:
                for channel in setup.channels:
                    niceName = ' '.join([channel, r.__str__()])
                    binname = 'Bin'+str(counter)
                    counter += 1
                    c.addBin(binname, [e.name.split('-')[0] for e in setup.estimators], niceName)

                    for e in setup.estimators:
                        name = e.name.split('-')[0]
                        expected = e.cachedEstimate(r, channel, setup)
                        c.specifyExpectation(binname, name, round(expected.val,3) if expected.val > 0 else 0.01)

                        if expected.val>0:
                            if not args.statOnly:
                                c.specifyUncertainty('PU',          binname, name, 1.01) 
                                c.specifyUncertainty('JEC',         binname, name, 1.03) #1.05
                                c.specifyUncertainty('btag',        binname, name, 1.03) #1.05
                                c.specifyUncertainty('trigger',     binname, name, 1.03) #1.04
                                c.specifyUncertainty('leptonSF',    binname, name, 1.05) #1.07
                                c.specifyUncertainty('scale',       binname, name, 1.01) 
                                c.specifyUncertainty('PDF',         binname, name, 1.01) 

                                if name.count('ZZ'):      c.specifyUncertainty('ZZ_xsec',     binname, name, 1.20) #1.20
                                if name.count('WZ'):      c.specifyUncertainty('WZ_xsec',     binname, name, 1.10) #1.20
                                if name.count('nonprompt'):    c.specifyUncertainty('nonprompt',   binname, name, 1.30)
                                if name.count('rare'):    c.specifyUncertainty('rare',        binname, name, 1.50)
                                if name.count('TTX'):     c.specifyUncertainty('ttX',         binname, name, 1.10) #1.15
                                if name.count('TZQ'):     c.specifyUncertainty('tZq',         binname, name, 1.10) #1.15


                            #MC bkg stat (some condition to neglect the smaller ones?)
                            uname = 'Stat_'+binname+'_'+name
                            c.addUncertainty(uname, 'lnN')
                            c.specifyUncertainty(uname, binname, name, round(1+expected.sigma/expected.val,3) )

                    obs = observation.cachedEstimate(r, channel, setup)
                    c.specifyObservation(binname, int(round(obs.val,0)))

                    if args.useShape:
                        logger.info("Using 2D reweighting method for shapes")
                        if args.model == "dim6top_LO":
                            source_gen = dim6top_LO_ttZ_ll_ctZ_0p00_ctZI_0p00
                        elif args.model == "ewkDM":
                            source_gen = ewkDM_central
                        target_gen = s

                        signalReweighting = SignalReweighting( source_sample = source_gen, target_sample = s, cacheDir = reweightCache)
                        f = signalReweighting.cachedReweightingFunc( setup.genSelection )
                        sig = signal.reweight2D(r, channel, setup, f)
                    else:
                        sig = signal.cachedEstimate(r, channel, setup)

                    xSecMod = 1
                    if args.useXSec:
                        xSecMod = xsec.val/xsec_central.val
                    
                    logger.info("x-sec is multiplied by %s",xSecMod)
                    
                    c.specifyExpectation(binname, 'signal', round(sig.val*xSecScale * xSecMod, 3) )
                    
                    if sig.val>0:
                        if not args.statOnly:
                            c.specifyUncertainty('PU',          binname, "signal", 1.01)
                            c.specifyUncertainty('JEC',         binname, "signal", 1.03) #1.05
                            c.specifyUncertainty('btag',        binname, "signal", 1.03) #1.05
                            c.specifyUncertainty('trigger',     binname, "signal", 1.03) #1.04
                            c.specifyUncertainty('leptonSF',    binname, "signal", 1.05) #1.07
                            c.specifyUncertainty('scale_sig',   binname, "signal", 1.10) #1.30
                            c.specifyUncertainty('PDF',         binname, "signal", 1.05) #1.15

                        uname = 'Stat_'+binname+'_signal'
                        c.addUncertainty(uname, 'lnN')
                        c.specifyUncertainty(uname, binname, 'signal', round(1 + sig.sigma/sig.val,3) )
                    else:
                        uname = 'Stat_'+binname+'_signal'
                        c.addUncertainty(uname, 'lnN')
                        c.specifyUncertainty(uname, binname, 'signal', 1 )

                    
        c.addUncertainty('Lumi', 'lnN')
        c.specifyFlatUncertainty('Lumi', 1.026)
        cardFileName = c.writeToFile(cardFileName)
    else:
        logger.info("File %s found. Reusing.",cardFileName)
    
    res = {}
    
    if getResult(s) and not overWrite:
        res = getResult(s)
        logger.info("Found result for %s, reusing", s.name)
    else:
        # calculate the limit
        #limit = c.calcLimit(cardFileName)#, options="--run blind")
        #res.update({"exp":limit['0.500'], "obs":limit['-1.000'], "exp1up":limit['0.840'], "exp2up":limit['0.975'], "exp1down":limit['0.160'], "exp2down":limit['0.025']})
        res.update({"exp":0, "obs":0, "exp1up":0, "exp2up":0, "exp1down":0, "exp2down":0})
        # run the checks
        c.calcNuisances(cardFileName)
        # extract the NLL
        nll = c.calcNLL(cardFileName, options="--fastScan")
        if nll["nll0"] > 0:
            res.update({"dNLL_postfit_r1":nll["nll"], "dNLL_bestfit":nll["bestfit"], "NLL_prefit":nll["nll0"]})
        else:
            res.update({"dNLL_postfit_r1":-999, "dNLL_bestfit":-999, "NLL_prefit":-999})
            logger.info("Fits failed, adding values -999 as results")
        logger.info("Adding results to database")
        addResult(s, res, nll['nll_abs'], overwrite=True)
        
    print
    print "NLL results:"
    print "{:>15}{:>15}{:>15}".format("Pre-fit", "Post-fit r=1", "Best fit")
    print "{:15.2f}{:15.2f}{:15.2f}".format(float(res["NLL_prefit"]), float(res["NLL_prefit"])+float(res["dNLL_postfit_r1"]), float(res["NLL_prefit"])+float(res["dNLL_bestfit"]))
    
    if xSecScale != 1:
        for k in res:
            res[k] *= xSecScale
Esempio n. 3
0
def wrapper(s):
    xSecScale = 1
    c = cardFileWriter.cardFileWriter()
    c.releaseLocation = combineReleaseLocation

    cardFileName = os.path.join(limitDir, s.name + '.txt')
    if not os.path.exists(cardFileName) or overWrite:
        counter = 0
        c.reset()
        c.addUncertainty('PU', 'lnN')
        c.addUncertainty('JEC', 'lnN')
        c.addUncertainty('btag', 'lnN')
        c.addUncertainty('trigger', 'lnN')
        c.addUncertainty('leptonSF', 'lnN')
        c.addUncertainty('scale', 'lnN')
        c.addUncertainty('scale_sig', 'lnN')
        c.addUncertainty('PDF', 'lnN')
        c.addUncertainty('nonprompt', 'lnN')
        c.addUncertainty('WZ_xsec', 'lnN')
        c.addUncertainty('ZZ_xsec', 'lnN')
        c.addUncertainty('rare', 'lnN')
        c.addUncertainty('ttX', 'lnN')
        c.addUncertainty('tZq', 'lnN')

        for setup in setups:
            for r in setup.regions:
                for channel in setup.channels:
                    niceName = ' '.join([channel, r.__str__()])
                    binname = 'Bin' + str(counter)
                    counter += 1
                    c.addBin(binname, [p.name for p in processes], niceName)

                    for p in processes:
                        name = p.name
                        expected = getEstimate(
                            p, r, channel
                        )  #{"process":nam, "region":r, "lumi":35.9, "presel":"nLep==3&&isTTZ&&nJetGodd>2&&nBTag>0", "weightString":"weight", "channel":channel})
                        c.specifyExpectation(binname, name, expected.val)

                        if expected.val > 0:
                            c.specifyUncertainty('PU', binname, name, 1.01)
                            c.specifyUncertainty('JEC', binname, name, 1.05)
                            c.specifyUncertainty('btag', binname, name, 1.05)
                            c.specifyUncertainty('trigger', binname, name,
                                                 1.04)
                            c.specifyUncertainty('leptonSF', binname, name,
                                                 1.07)
                            c.specifyUncertainty('scale', binname, name, 1.01)
                            c.specifyUncertainty('PDF', binname, name, 1.01)

                            if name.count('ZZ'):
                                c.specifyUncertainty('ZZ_xsec', binname, name,
                                                     1.20)
                            if name.count('WZ'):
                                c.specifyUncertainty('WZ_xsec', binname, name,
                                                     1.20)
                            if name.count('nonprompt'):
                                c.specifyUncertainty('nonprompt', binname,
                                                     name, 1.30)
                            if name.count('rare'):
                                c.specifyUncertainty('rare', binname, name,
                                                     1.50)
                            if name.count('TTX'):
                                c.specifyUncertainty('ttX', binname, name,
                                                     1.15)
                            if name.count('TZQ'):
                                c.specifyUncertainty('tZq', binname, name,
                                                     1.15)

                            #MC bkg stat (some condition to neglect the smaller ones?)
                            uname = 'Stat_' + binname + '_' + name
                            c.addUncertainty(uname, 'lnN')
                            c.specifyUncertainty(
                                uname, binname, name,
                                1 + expected.sigma / expected.val)

                    c.specifyObservation(
                        binname,
                        int(round((getEstimate(
                            pseudoData, r,
                            channel).val))))  #can also be pseudoDataPriv

                    #signalSetup = setup.systematicClone()
                    signal = getEstimate(
                        s, r, channel
                    )  #resultsCache.get({"process":s.name, "region":r, "lumi":35.9, "presel":"nLep==3&&isTTZ&&nJetGodd>2&&nBTag>0", "weightString":"weight", "channel":channel})
                    c.specifyExpectation(binname, 'signal',
                                         signal.val * xSecScale)

                    if signal.val > 0:
                        c.specifyUncertainty('PU', binname, "signal", 1.01)
                        c.specifyUncertainty('JEC', binname, "signal", 1.05)
                        c.specifyUncertainty('btag', binname, "signal", 1.05)
                        c.specifyUncertainty('trigger', binname, "signal",
                                             1.04)
                        c.specifyUncertainty('leptonSF', binname, "signal",
                                             1.07)
                        c.specifyUncertainty('scale_sig', binname, "signal",
                                             1.30)
                        c.specifyUncertainty('PDF', binname, "signal", 1.15)

                        uname = 'Stat_' + binname + '_signal'
                        c.addUncertainty(uname, 'lnN')
                        c.specifyUncertainty(uname, binname, 'signal',
                                             1 + signal.sigma / signal.val)
                    else:
                        uname = 'Stat_' + binname + '_signal'
                        c.addUncertainty(uname, 'lnN')
                        c.specifyUncertainty(uname, binname, 'signal', 1)

                    ## alternative signal model
                    #signal_ALT = getEstimate(s, r, channel)
                    #c.specifyExpectation(binname, 'signal_ALT', signal_ALT.val*xSecScale )
                    #
                    #if signal_ALT.val>0:
                    #    c.specifyUncertainty('PU',          binname, "signal_ALT", 1.01)
                    #    c.specifyUncertainty('JEC',         binname, "signal_ALT", 1.05)
                    #    c.specifyUncertainty('btag',        binname, "signal_ALT", 1.05)
                    #    c.specifyUncertainty('trigger',     binname, "signal_ALT", 1.04)
                    #    c.specifyUncertainty('leptonSF',    binname, "signal_ALT", 1.07)
                    #    c.specifyUncertainty('scale_sig',   binname, "signal_ALT", 1.30)
                    #    c.specifyUncertainty('PDF',         binname, "signal_ALT", 1.15)

                    #    uname = 'Stat_'+binname+'_signal_ALT'
                    #    c.addUncertainty(uname, 'lnN')
                    #    c.specifyUncertainty(uname, binname, 'signal_ALT', 1 + signal_ALT.sigma/signal_ALT.val )
                    #else:
                    #    uname = 'Stat_'+binname+'_signal_ALT'
                    #    c.addUncertainty(uname, 'lnN')
                    #    c.specifyUncertainty(uname, binname, 'signal_ALT', 1 )

        c.addUncertainty('Lumi', 'lnN')
        c.specifyFlatUncertainty('Lumi', 1.026)
        cardFileName = c.writeToFile(cardFileName)
    else:
        print "File %s found. Reusing." % cardFileName

    res = {}

    if getResult(s) and not overWrite:
        res = getResult(s)
        print "Found result for %s, reusing" % s.name
    else:
        # calculate the limit
        limit = c.calcLimit(cardFileName)  #, options="--run blind")
        res.update({
            "exp": limit['0.500'],
            "obs": limit['-1.000'],
            "exp1up": limit['0.840'],
            "exp2up": limit['0.975'],
            "exp1down": limit['0.160'],
            "exp2down": limit['0.025']
        })
        # run the checks
        c.calcNuisances(cardFileName)
        # extract the NLL
        nll = c.calcNLL(cardFileName)
        res.update({
            "dNLL_postfit_r1": nll["nll"],
            "dNLL_bestfit": nll["bestfit"],
            "NLL_prefit": nll["nll0"]
        })
        addResult(s, res, nll['nll_abs'], overwrite=True)

    print "NLL results:"
    print "{:>15}{:>15}{:>15}".format("Pre-fit", "Post-fit r=1", "Best fit")
    print "{:15.2f}{:15.2f}{:15.2f}".format(
        float(res["NLL_prefit"]),
        float(res["NLL_prefit"]) + float(res["dNLL_postfit_r1"]),
        float(res["NLL_prefit"]) + float(res["dNLL_bestfit"]))

    if xSecScale != 1:
        for k in res:
            res[k] *= xSecScale
Esempio n. 4
0
        result.SetPoint(i, x[i], y[i], z[i])
    h = result.GetHistogram()
    h.SetMinimum(min(z))
    h.SetMaximum(max(z))
    c = ROOT.TCanvas()
    result.Draw()
    del c
    #res = ROOT.TGraphDelaunay(result)
    return result


fitKey = "dNLL_postfit_r1" if not args.useBestFit else "dNLL_bestfit"

# get the absolute post fit NLL value of pure ttZ
#ttZ_res = getResult(ewkDM_ttZ_ll_noH)
ttZ_res = getResult(dim6top_LO_ttZ_ll_cpQM_0p00_cpt_0p00)
ttZ_NLL_abs = float(ttZ_res["NLL_prefit"]) + float(ttZ_res[fitKey])

print "Max Likelihood ttZ SM"
print ttZ_NLL_abs

# load all samples, omit the 1/1 point
#signals = [ x for x in allSamples_dim6top if not x.name.startswith('dim6top_LO_ttZ_ll_ctZ_1p00_ctZI_1p00') ]
signals = [x for x in dim6top_currents]

ctZ_values = []
ctZi_values = []

for s in signals:
    s.ctZ = float(s.name.split('_')[5].replace('p', '.').replace('m', '-'))
    if not (s.ctZ in ctZ_values):
Esempio n. 5
0
    result.SetTitle(title)
    for i in range(length):
        result.SetPoint(i, x[i], y[i], z[i])
        debug.SetPoint(i, x[i], y[i])
    c = ROOT.TCanvas()
    result.Draw()
    debug.Draw()
    del c
    #res = ROOT.TGraphDelaunay(result)
    return result, debug


fitKey = "dNLL_postfit_r1" if not args.useBestFit else "dNLL_bestfit"

# get the absolute post fit NLL value of pure ttZ
ttZ_res = getResult(dim6top_central)
ttZ_NLL_abs = float(ttZ_res["NLL_prefit"]) + float(ttZ_res[fitKey])

print "Max Likelihood ttZ SM"
print "{:10.2f}".format(ttZ_NLL_abs)

# load all samples, omit the 1/1 point
#signals = [ x for x in allSamples_dim6top if not x.name.startswith('dim6top_LO_ttZ_ll_ctZ_1p00_ctZI_1p00') ]

if args.plane == "current":
    signals = [dim6top_central] + [x for x in dim6top_currents]
    x_var = 'cpQM'
    y_var = 'cpt'
    x_shift = 0.
    y_shift = 0.
Esempio n. 6
0
        result.SetPoint(i, x[i], y[i], z[i])
    #h = result.GetHistogram()
    #h.SetMinimum(min(z))
    #h.SetMaximum(max(z))
    c = ROOT.TCanvas()
    result.Draw()
    #debug.Draw("same")
    del c
    #res = ROOT.TGraphDelaunay(result)
    return result  #,debug


fitKey = "dNLL_postfit_r1" if not args.useBestFit else "dNLL_bestfit"

# get the absolute post fit NLL value of pure ttZ
ttZ_res = getResult(ewkDM_central)
#ttZ_res = getResult(dim6top_LO_ttZ_ll_cpQM_0p00_cpt_0p00)
ttZ_NLL_abs = float(ttZ_res["NLL_prefit"]) + float(ttZ_res[fitKey])

print "Max Likelihood ttZ SM"
print "{:10.2f}".format(ttZ_NLL_abs)

# load all samples, omit the 1/1 point
#signals = [ x for x in allSamples_dim6top if not x.name.startswith('dim6top_LO_ttZ_ll_ctZ_1p00_ctZI_1p00') ]

if args.plane == "current":
    signals = [ewkDM_central] + [x for x in ewkDM_currents]
    x_var = 'DC1V'
    y_var = 'DC1A'
    x_shift = 0.24
    y_shift = -0.60