Beispiel #1
0
    def gen_card(self, file_name, **kwargs):
        doDataDriven = kwargs.pop("doDataDriven", True)

        # get the plotter
        nl = 3
        sigMap = getSigMap(nl)
        intLumiMap = getIntLumiMap()
        mergeDict = getMergeDict(self.period)
        channelBackground = getChannelBackgrounds(self.period)
        channels, leptons = getChannels(nl)
        saves = "%s_%s_%iTeV" % (self.analysis, self.region, self.period)

        plotter = Plotter(
            self.region,
            ntupleDir=self.ntuple_dir,
            saveDir=saves,
            period=self.period,
            rootName="plots_limits_wz",
            mergeDict=mergeDict,
            scaleFactor=self.scalefactor,
            datadriven=True,
        )
        plotter.initializeBackgroundSamples(
            [sigMap[self.period][x] for x in channelBackground[self.region + "datadriven"]]
        )
        plotter.initializeDataSamples([sigMap[self.period]["data"]])
        plotter.setIntLumi(intLumiMap[self.period])

        # set expected and observed yields
        sources = (
            [
                x
                for x in channelBackground[self.region + "datadriven"]
                if x not in ["TT", "T", "DY", "Z", "Zfiltered", "WW"]
            ]
            + ["datadriven"]
            if doDataDriven
            else channelBackground[self.region]
        )
        for bg in sources:
            self.log.info("Processing {0}".format(bg))
            val, err = plotter.getNumEntries(self.selection, sigMap[self.period][bg], doError=True)
            statname = "stat_{0}_{1}".format(bg, file_name.split(".")[0])
            staterrs = {bg: err / val + 1.0 if val else 1.0}
            if self.doStat:
                self.add_systematics(statname, "lnN", **staterrs)
            if bg in ["WZ"]:
                self.datacard.add_sig(bg, val)
            else:
                self.datacard.add_bkg(bg, val)
        self.log.info("Processing observed")
        self.datacard.set_observed(plotter.getDataEntries(self.selection))

        # write out the datacard
        self.log.info("Saving card to file")

        with open("%s/%s" % (self.out_dir, file_name), "w") as outfile:
            outfile.write(self.datacard.dump())
Beispiel #2
0
def calculateLeptonSystematic(mass,chanCuts,chanScales,**kwargs):
    do4l = kwargs.pop('do4l',False)
    analysis = 'Hpp3l'
    region = 'Hpp3l'
    runPeriod = 8
    nl = 3
    ntuples = getNtupleDirectory(analysis,region,runPeriod)
    saves = '%s_%s_%sTeV' % (analysis,region,runPeriod)
    sigMap = getSigMap(4,mass) if do4l else getSigMap(nl,mass)
    intLumiMap = getIntLumiMap()
    mergeDict = getMergeDict(runPeriod)
    regionBackground = {
        'Hpp3l' : ['T','TT', 'TTV','W','Z','VVV','WW','ZZ','WZ'],
        'Hpp4l' : ['TT','Z','DB']
    }
    channels, leptons = getChannels(nl)

    plotter = Plotter(analysis,ntupleDir=ntuples,saveDir=saves,period=runPeriod,rootName='systematics',mergeDict=mergeDict)
    plotter.initializeBackgroundSamples([sigMap[runPeriod][x] for x in regionBackground[analysis]])
    plotter.initializeSignalSamples([sigMap[runPeriod]['Sig']])
    plotter.initializeDataSamples([sigMap[runPeriod]['data']])
    plotter.setIntLumi(intLumiMap[runPeriod])

    fullCut = 'finalstate.mass>100 && finalstate.sT>1.1*%f+60. && fabs(z1.mass-%f)>80. && h1.dPhi<%f/600.+1.95' %(mass,ZMASS,mass)
    finalSRCut = 'h1.mass>0.9*%f && h1.mass<1.1*%f' %(mass,mass)

    totBG = 0
    totBG_scaled = 0
    for c,s in zip(chanCuts,chanScales):
        chanBG = s*plotter.getNumEntries('%s && %s && %s' %(c,fullCut,finalSRCut),plotter.signal[0],scaleup=False)
        chanBG_scaled = s*plotter.getNumEntries('%s && %s && %s' %(c,fullCut,finalSRCut),plotter.signal[0],scaleup=True)
        totBG += chanBG
        totBG_scaled += chanBG_scaled
    sigSelSys = (totBG_scaled-totBG)/totBG

    return sigSelSys+1
Beispiel #3
0
 def getPlotter(self,analysis,region,runPeriod,mass,runTau,plotName,doFakes):
     nl = 3 if analysis=='Hpp3l' or analysis=='WZ' else 4
     ntuples = getNtupleDirectory(analysis,region,runPeriod)
     saves = '%s_%s_%iTeV' % (analysis,region,runPeriod)
     sigMap = getSigMap(nl,mass)
     intLumiMap = getIntLumiMap()
     mergeDict = getMergeDict(runPeriod)
     regionBackground = getChannelBackgrounds(runPeriod)
     channels, leptons = getChannels(nl,runTau=runTau)
 
     plotter = Plotter(region,ntupleDir=ntuples,saveDir=saves,period=runPeriod,rootName=plotName,mergeDict=mergeDict,scaleFactor=self.scalefactor)
     if not doFakes: plotter.initializeBackgroundSamples([sigMap[runPeriod][x] for x in regionBackground[analysis]])
     if runPeriod==8: plotter.initializeDataSamples([sigMap[runPeriod]['data']])
     plotter.setIntLumi(intLumiMap[runPeriod])
 
     return plotter
Beispiel #4
0
def limit(analysis,region,period,mass,**kwargs):
    cut = kwargs.pop('cut','1')
    doChannels = kwargs.pop('doChannels',False)
    doAlphaTest = kwargs.pop('doAlphaTest',False)
    unblind = kwargs.pop('unblind',False)
    name = kwargs.pop('name','card')
    bp = kwargs.pop('bp','')
    directory = kwargs.pop('directory','')
    channelScales = kwargs.pop('channelScales',[1.0])
    channelCuts = kwargs.pop('channelCuts',['1'])
    recoChannels = kwargs.pop('recoChannels',['1'])
    genChannels = kwargs.pop('genChannels',['1'])
    mode = kwargs.pop('mode','sideband')
    scalefactor = kwargs.pop('scalefactor','event.gen_weight*event.pu_weight*event.lep_scale*event.trig_scale')
    datacardDir = kwargs.pop('datacardDir','./datacards')
    do4l = kwargs.pop('do4l',False)
    doBoth = kwargs.pop('doBoth',False)
    logging.info("Processing BP %s; mass-point %i; card name %s" % (bp,mass,name))

    # get the cut maps for each gen channel
    genCutMap = {}
    alphaGenCuts = {}
    for genChan in genChannels:
        genCutMap[genChan] = getChannelCutFlowMap(analysis,genChan,mass=mass)
        alphaGenCuts[genChan] = getChannelSidebandSignalRegion(analysis,genChan,mass=mass)

    # and the reco channel
    recoCutMap = {}
    alphaRecoCuts = {}
    for recoChan in recoChannels:
        recoCutMap[recoChan] = getChannelCutFlowMap(analysis,recoChan,mass=mass)
        alphaRecoCuts[recoChan] = getChannelSidebandSignalRegion(analysis,recoChan,mass=mass)

    # if we are testing a tau hypothesis, explicitly use a wider mass window and the tau cuts
    # otherwise we need to just use reco
    if bp in ['et100','mt100','tt100']:
        theCutMap = getChannelCutFlowMap(analysis,bp[:2]+bp[:2],mass=mass)
        theAlphaCuts = getChannelSidebandSignalRegion(analysis,bp[:2]+bp[:2],mass=mass)
    else:
        theCutMap = recoCutMap[recoChannels[0]]       # only support 1 reco channel at a time
        theAlphaCuts = alphaRecoCuts[recoChannels[0]]

    fullCut = ' && '.join(theCutMap['cuts'])

    # setup sideband stuff
    chanCuts = '(' + ' || '.join(channelCuts) + ')' # gen cuts for h++ ORed with the default for BG 'aaa'

    sbCut = theAlphaCuts['sbcut']
    srCut = theAlphaCuts['srcut']
    finalSRCut = theAlphaCuts['srcut']

    channels, leptons = getChannels(3 if analysis=='Hpp3l' or analysis=='WZ' else 4)

    nl = 3 if analysis in ['Hpp3l', 'WZ'] else 4
    sigMap = getSigMap(4,mass) if do4l else getSigMap(nl,mass)
    intLumiMap = getIntLumiMap()


    # setup for final selection
    myCut = cut

    sbcut = '%s && %s && %s' %(myCut,sbCut,chanCuts)
    srcut = '%s && %s && %s && %s' %(myCut,srCut,fullCut, chanCuts)
    base_selections = '%s && %s && %s' %(myCut, srCut, fullCut)
    if region=='WZ': srcut = '%s && %s && %s' %(myCut,srCut, chanCuts)
    if doAlphaTest:
        sbcut = '%s && finalstate.sT<150. && z1.mass<110. && h1.mass<130.' %(chanCuts)
        srcut = '%s && finalstate.sT<400. && finalstate.sT>150. && z1.mass<110. && h1.mass<130.' %(chanCuts)
    if doAlphaTest: unblind = True

    logging.debug('Sideband cut: %s' % sbcut)
    logging.debug('Signal region cut: %s' % srcut)
    logging.debug('Base cut: %s' % base_selections)

    # create the limits object
    # base_selections is the cut applied on top of individual channel cuts
    # sbcut is the sideband selection for alpha calculation
    # srcut is the signal ragion selection for alpha calculation
    limits = Limits(analysis,region, period, base_selections, getNtupleDirectory(analysis,region,period),
                    '%s/%s_%itev_%s/%s/%s' % (datacardDir, analysis, period, region, directory, mass),
                    channels=['dblh%s' % analysis], lumi=intLumiMap[period],
                    blinded=not unblind, bgMode=mode, scalefactor=scalefactor,
                    sbcut=sbcut, srcut=srcut)

    signal =  sigMap[period]['SigPP'] if do4l or analysis in ['Hpp4l'] else sigMap[period]['SigAP']
    if mode=='sideband':
        # add groups, signal scales must be list of floats with same length as cuts list in gen card
        signame = "hpp%i_PP" % mass if do4l or analysis in ['Hpp4l'] else "hpp%i_AP" % mass
        limits.add_group(signame, signal, scale=channelScales, isSignal=True)
        if doBoth:
            signame_PP = "hpp%i_PP" % mass
            signal_PP =  sigMap[period]['SigPP']
            limits.add_group(signame_PP, signal_PP, scale=channelScales, isSignal=True)
        limits.add_group("bg", "bg")
        limits.add_group("data", "data_R*", isData=True)

        # luminosity systematics, 2.6 % for mc
        lumi = {signame: 1.026}
        if doBoth: lumi = {signame: 1.026, signame_PP: 1.026}
        limits.add_systematics("lumi", "lnN", **lumi)
    
        # lepton systematics, electron and muon separately for mc
        chanNames = recoChannels # only one supported for now
        scaleMap = calculateChannelLeptonSystematic(mass,chanNames,do4l=do4l,doBoth=doBoth)
        eid = {signame: "%0.3f" %scaleMap[chanNames[0]]['e']}
        if doBoth: eid = {signame: "%0.3f" %scaleMap[chanNames[0]]['e'], signame_PP: "%0.3f" %scaleMap[chanNames[0]]['e']}
        chgid = {signame: "%0.3f" %scaleMap[chanNames[0]]['chg']}
        if doBoth: chgid = {signame: "%0.3f" %scaleMap[chanNames[0]]['chg'], signame_PP: "%0.3f" %scaleMap[chanNames[0]]['chg']}
        mid = {signame: "%0.3f" %scaleMap[chanNames[0]]['m']}
        if doBoth: mid = {signame: "%0.3f" %scaleMap[chanNames[0]]['m'], signame_PP: "%0.3f" %scaleMap[chanNames[0]]['m']}
        limits.add_systematics('eid', 'lnN', **eid)
        limits.add_systematics('chgid', 'lnN', **chgid)
        limits.add_systematics('mid', 'lnN', **mid)
    
        # signal mc uncertainty
        sigmc = {signame: 1.15}
        if doBoth: sigmc = {signame: 1.15, signame_PP: 1.15}
        limits.add_systematics("sig_mc_err", "lnN", **sigmc)
    
        # uncertainty on bg estimation
        alpha_str = recoChannels[0] # only one supported for now
        alpha_pdf = {'bg': 1.1}
        limits.add_systematics("alpha_%s" %alpha_str, "lnN", **alpha_pdf)
    
        # generate the card, passing the cuts to be applied for each gen channel
        limits.gen_card("%s.txt" % name, mass=mass, cuts=channelCuts, doAlphaTest=doAlphaTest)

    elif mode=='mc':
        logging.error('MC needs to be reimplemented')
        #add_systematics_mc(limits,mass,signal,name,channelCuts,scale,period,bp,doAlphaTest,do4l)
    else:
        return 0