Exemple #1
0
 def processTaus(self):
     for l in xrange(self.chain._nLight, self.chain._nL):
         self.chain.is_loose_lepton[l] = (isGoodLepton(
             self.chain, l, 'loose'), self.chain.obj_sel['tau_algo'])
         self.chain.is_FO_lepton[l] = (isGoodLepton(self.chain, l, 'FO'),
                                       self.chain.obj_sel['tau_algo'])
         self.chain.is_tight_lepton[l] = (isGoodLepton(
             self.chain, l, 'tight'), self.chain.obj_sel['tau_algo'])
Exemple #2
0
 def processElectrons(self):
     for l in xrange(self.chain._nEle):
         self.chain.is_loose_lepton[l] = (isGoodLepton(
             self.chain, l, 'loose'), self.chain.obj_sel['light_algo'])
         self.chain.is_FO_lepton[l] = (isGoodLepton(self.chain, l, 'FO'),
                                       self.chain.obj_sel['light_algo'])
         self.chain.is_tight_lepton[l] = (isGoodLepton(
             self.chain, l, 'tight'), self.chain.obj_sel['light_algo'])
Exemple #3
0
def applyConeCorrection(chain,
                        new_chain,
                        light_algo=None,
                        tau_algo=None,
                        analysis=None):
    for i, chain_index in enumerate(new_chain.l_indices):
        if isGoodLepton(chain, chain_index,
                        workingpoint='FO') and not isGoodLepton(
                            chain, chain_index, workingpoint='tight'):
            new_chain.l_pt[i] *= coneCorrection(chain, chain_index, light_algo)
            new_chain.l_e[i] *= coneCorrection(chain, chain_index, light_algo)
    def hasCorrectNumberOfFakes(self):
        self.loose_leptons_of_interest = []
        is_tight_lep = []
        is_fake_lep = []
        translated_flavors_of_interest = [
            self.flavor_dict[i] for i in self.flavors_of_interest
        ]
        # print translated_flavors_of_interest

        #Make input more readable
        for l in xrange(len(self.new_chain.l_pt)):
            #Rule out all other flavors so we are only continuing with general functions on leptons of the correct flavor
            if not self.new_chain.l_flavor[l] in translated_flavors_of_interest:
                if isFakeLepton(self.chain, self.new_chain.l_indices[l]):
                    return False
                else:
                    continue

            self.loose_leptons_of_interest.append(l)
            if isGoodLepton(self.chain, self.new_chain.l_indices[l], 'tight'):
                is_tight_lep.append(True)
            else:
                is_tight_lep.append(False)

            if isFakeLepton(self.chain, self.new_chain.l_indices[l]):
                is_fake_lep.append(True)
            else:
                is_fake_lep.append(False)

        #If not lepton with flavor of interest, return False
        if len(self.loose_leptons_of_interest) == 0:
            return False
        #If only one lep of interest present in the event, it is required to be fake
        elif len(self.loose_leptons_of_interest) == 1:
            return is_fake_lep[0]
        elif len(self.loose_leptons_of_interest) == 2:

            #If both leps are tight...
            if (is_tight_lep[0] and is_tight_lep[1]):
                # At least one of the two has to be a fake
                return is_fake_lep[0] or is_fake_lep[1]
            #Otherwise all loose leps should be fake
            else:
                if not is_tight_lep[0] and not is_fake_lep[0]: return False
                if not is_tight_lep[1] and not is_fake_lep[1]: return False
                return True
        elif len(self.loose_leptons_of_interest) == 3:

            #If both leps are tight...
            if (is_tight_lep[0] and is_tight_lep[1] and is_tight_lep[2]):
                # At least one of the two has to be a fake
                return is_fake_lep[0] or is_fake_lep[1] or is_fake_lep[2]
            #Otherwise all loose leps should be fake
            else:
                if not is_tight_lep[0] and not is_fake_lep[0]: return False
                if not is_tight_lep[1] and not is_fake_lep[1]: return False
                if not is_tight_lep[2] and not is_fake_lep[2]: return False
                return True
        else:
            return False
Exemple #5
0
def isCleanFromLeptons(chain, index, wp):
    for l in xrange(chain._nL):
        # for l in xrange(chain._nLight):
        if not isGoodLepton(chain, l, wp): continue
        if deltaR(chain._lEta[l], chain._jetEta[index], chain._lPhi[l],
                  chain._jetPhi[index]) < 0.4:
            return False
    return True
Exemple #6
0
def fourthFOVeto(chain, no_tau=False):
    if no_tau: collection = chain._nLight
    else:
        collection = chain._nL

    for lepton in xrange(collection):
        if lepton in chain.l_indices: continue
        if isGoodLepton(chain, lepton, 'FO'): return True
    return False
Exemple #7
0
def select3Leptons(chain,
                   new_chain,
                   no_tau=False,
                   light_algo=None,
                   tau_algo=None,
                   cutter=None,
                   workingpoint='tight'):

    if chain is new_chain:
        new_chain.l_pt = [0.0] * 3
        new_chain.l_eta = [0.0] * 3
        new_chain.l_phi = [0.0] * 3
        new_chain.l_charge = [0.0] * 3
        new_chain.l_flavor = [0.0] * 3
        new_chain.l_e = [0.0] * 3
        new_chain.l_indices = [0.0] * 3

    collection = chain._nL if not no_tau else chain._nLight
    chain.leptons = [(chain._lPt[l], l) for l in xrange(collection)
                     if isGoodLepton(chain,
                                     l,
                                     algo=light_algo,
                                     tau_algo=tau_algo,
                                     workingpoint=workingpoint)]
    if cutter is not None:
        cutter.cut(len(chain.leptons) > 0, 'at_least_1_lep')
        cutter.cut(len(chain.leptons) > 1, 'at_least_2_lep')
        cutter.cut(len(chain.leptons) > 2, 'at_least_3_lep')
    if len(chain.leptons) != 3: return False

    ptAndIndex = sorted(chain.leptons, reverse=True, key=getSortKey)

    new_chain.l1 = ptAndIndex[0][1]
    new_chain.l2 = ptAndIndex[1][1]
    new_chain.l3 = ptAndIndex[2][1]
    for i in xrange(3):
        new_chain.l_indices[i] = ptAndIndex[i][1]
        new_chain.l_pt[i] = ptAndIndex[i][0]
        new_chain.l_eta[i] = chain._lEta[ptAndIndex[i][1]]
        new_chain.l_phi[i] = chain._lPhi[ptAndIndex[i][1]]
        new_chain.l_e[i] = chain._lE[ptAndIndex[i][1]]
        new_chain.l_charge[i] = chain._lCharge[ptAndIndex[i][1]]
        new_chain.l_flavor[i] = chain._lFlavor[ptAndIndex[i][1]]
    # print "NEW"
    # print chain.l_indices
    # print chain.l_flavor
    # print chain.l_charge
    # print [f for f in chain._lFlavor]

    return True
Exemple #8
0
def selectLeptonsGeneral(chain,
                         new_chain,
                         nL,
                         no_tau=False,
                         light_algo=None,
                         tau_algo=None,
                         cutter=None,
                         workingpoint='tight'):

    collection = chain._nL if not no_tau else chain._nLight
    chain.leptons = [(chain._lPt[l], l) for l in xrange(collection)
                     if isGoodLepton(chain,
                                     l,
                                     algo=light_algo,
                                     tau_algo=tau_algo,
                                     workingpoint=workingpoint)]
    if cutter is not None:
        cutter.cut(len(chain.leptons) > 0, 'at_least_1_lep')
        cutter.cut(len(chain.leptons) > 1, 'at_least_2_lep')
        cutter.cut(len(chain.leptons) > 2, 'at_least_3_lep')
    if len(chain.leptons) != nL: return False

    if chain is new_chain:
        new_chain.l_pt = [0.0] * nL
        new_chain.l_eta = [0.0] * nL
        new_chain.l_phi = [0.0] * nL
        new_chain.l_charge = [0.0] * nL
        new_chain.l_flavor = [0.0] * nL
        new_chain.l_e = [0.0] * nL
        new_chain.l_indices = [0.0] * nL

    ptAndIndex = sorted(chain.leptons, reverse=True, key=getSortKey)

    for i in xrange(nL):
        chain.l_indices[i] = ptAndIndex[i][1]
        new_chain.l_pt[i] = ptAndIndex[i][0]
        new_chain.l_eta[i] = chain._lEta[ptAndIndex[i][1]]
        new_chain.l_phi[i] = chain._lPhi[ptAndIndex[i][1]]
        new_chain.l_e[i] = chain._lE[ptAndIndex[i][1]]
        new_chain.l_charge[i] = chain._lCharge[ptAndIndex[i][1]]
        new_chain.l_flavor[i] = chain._lFlavor[ptAndIndex[i][1]]

    return True
Exemple #9
0
    def passedFilter(self, cutter, for_training=False):
        if not self.initEvent(cutter): return False

        if self.sideband:
            nfail = 0
            for l in self.chain.l_indices:
                if not isGoodLepton(self.chain, l, 'tight'):
                    nfail += 1
            if nfail < 1: return False

        if not self.is_reco_level:
            return True  #It has passed the basic selection

        if not self.passBaseCuts(cutter): return False

        if self.region == 'lowMassSR':
            return self.passLowMassSelection(cutter, for_training=for_training)
        elif self.region == 'highMassSR':
            return self.passHighMassSelection(cutter)
        elif self.region == 'baseline':
            return True
        else:
            raise RuntimeError("Unknown signal region: " + self.region)
Exemple #10
0
                    cutter, only_muons=True, require_jets=True):
                continue
        else:
            if not event.event_selector.passedFilter(cutter, sample.output):
                continue
        fake_index = event.event_selector.selector.getFakeIndex()

        if args.inData and not chain.is_data:
            if not chain._lIsPrompt[chain.l_indices[fake_index]]: continue
            passed = True  #Always fill both denom and enum for this case (subtraction of prompt contribution)
            weight = -1. * reweighter.getLumiWeight()
        else:
            if not chain.is_data and not cutter.cut(chain.l_isfake[fake_index],
                                                    'fake lepton'):
                continue
            passed = isGoodLepton(chain, chain.l_indices[fake_index], 'tight')
            weight = reweighter.getLumiWeight()

        fakerates.fillFakeRates(chain, weight, passed, index=fake_index)

    print fakerates.getFakeRate('total').getNumerator().GetSumOfWeights(
    ), fakerates.getFakeRate('total').getDenominator().GetSumOfWeights()
    print fakerates.getFakeRate('total').getNumerator().GetEntries(
    ), fakerates.getFakeRate('total').getDenominator().GetEntries()
    print fakerates.getFakeRate('0jets').getNumerator().GetSumOfWeights(
    ), fakerates.getFakeRate('0jets').getDenominator().GetSumOfWeights()
    print fakerates.getFakeRate('0jets').getNumerator().GetEntries(
    ), fakerates.getFakeRate('0jets').getDenominator().GetEntries()
    print fakerates.getFakeRate('njets').getNumerator().GetSumOfWeights(
    ), fakerates.getFakeRate('njets').getDenominator().GetSumOfWeights()
    print fakerates.getFakeRate('njets').getNumerator().GetEntries(
Exemple #11
0
    for entry in event_range:
        
        chain.GetEntry(entry)
        progress(entry - event_range[0], len(event_range))
        
        if not selectGenLeptonsGeneral(chain, chain, 3):   continue
        
        if args.genLevel:
            slm = SignalLeptonMatcher(chain)
            slm.saveNewOrder()

        true_cat = ec.returnCategory()

        passed_baseline_cut = True
        if args.baselineCut == 'FObase':
            tmp = [l for l in xrange(chain._nL) if isGoodLepton(chain, l, 'FO')]
            if len(tmp) < 3: passed_baseline_cut = False 
        elif args.baselineCut == 'threeLeptonGenFilter' or args.baselineCut == 'threeLeptonGenFilterInverted':
            tmp = [l for l in xrange(chain._gen_nL) if chain._gen_lFlavor[l] in (0, 1) and chain._gen_lPt[l] > 5]
            if len(tmp) < 3: passed_baseline_cut = False
        elif args.baselineCut == 'threeLeptonGenFilter7GeV' or args.baselineCut == 'threeLeptonGenFilter7GeVInverted':
            tmp = [l for l in xrange(chain._gen_nL) if chain._gen_lFlavor[l] in (0, 1) and chain._gen_lPt[l] > 7]
            if len(tmp) < 3: passed_baseline_cut = False
        elif args.baselineCut == 'hadronicTauGenFilter' or args.baselineCut == 'hadronicTauGenFilterInverted':
            tmp = [l for l in xrange(chain._gen_nL) if chain._gen_lFlavor[l] == 2 and chain._gen_lVisPt[l] > 18]
            if len(tmp) < 1: passed_baseline_cut = False
        elif args.baselineCut == 'tauPlusLightGenFilter' or args.baselineCut == 'tauPlusLightGenFilterInverted':
            tmp_tau = [l for l in xrange(chain._gen_nL) if chain._gen_lFlavor[l] == 2 and chain._gen_lVisPt[l] > 18]
            tmp_light = [l for l in xrange(chain._gen_nL) if chain._gen_lFlavor[l] in (0, 1) and chain._gen_lPt[l] > 15]
            if len(tmp_tau) < 1 or len(tmp_light) < 1: passed_baseline_cut = False
Exemple #12
0
def selectLeptonsGeneral(chain, new_chain, nL, cutter=None, sort_leptons=True):

    collection = chain._nL if not chain.obj_sel['notau'] else chain._nLight
    chain.leptons = []

    for l in xrange(collection):
        if chain._lFlavor[l] == 0:
            workingpoint = chain.obj_sel['ele_wp']
            pt_to_use = chain._lPtCorr[l]
        elif chain._lFlavor[l] == 1:
            workingpoint = chain.obj_sel['mu_wp']
            pt_to_use = chain._lPt[l]
        elif chain._lFlavor[l] == 2:
            workingpoint = chain.obj_sel['tau_wp']
            pt_to_use = chain._lPt[l]
        else:
            raise RuntimeError(
                'In selectLeptonsGeneral: flavor provided is neither an electron, muon or tau'
            )

        if isGoodLepton(chain, l):
            chain.leptons.append((pt_to_use, l))

    if len(chain.leptons) != nL: return False

    tes = TauEnergyScale(chain.year, chain.obj_sel['tau_algo'])

    if chain is new_chain:
        new_chain.l_pt = [0.0] * nL
        new_chain.l_eta = [0.0] * nL
        new_chain.l_phi = [0.0] * nL
        new_chain.l_charge = [0.0] * nL
        new_chain.l_flavor = [0.0] * nL
        new_chain.l_e = [0.0] * nL
        new_chain.l_indices = [0.0] * nL
        new_chain.l_isFO = [0.0] * nL
        new_chain.l_istight = [0.0] * nL
        if not chain.is_data:
            new_chain.l_isfake = [0.0] * nL

    if sort_leptons:
        ptAndIndex = sorted(chain.leptons, reverse=True, key=getSortKey)
    else:
        ptAndIndex = chain.leptons

    for i in xrange(nL):
        new_chain.l_indices[i] = ptAndIndex[i][1]
        new_chain.l_flavor[i] = chain._lFlavor[ptAndIndex[i][1]]
        new_chain.l_pt[i] = ptAndIndex[i][0]
        new_chain.l_eta[i] = chain._lEta[ptAndIndex[i][1]]
        new_chain.l_phi[i] = chain._lPhi[ptAndIndex[i][1]]
        new_chain.l_e[i] = chain._lECorr[
            ptAndIndex[i][1]] if chain.l_flavor[i] < 1 else chain._lE[
                ptAndIndex[i][1]]
        new_chain.l_charge[i] = chain._lCharge[ptAndIndex[i][1]]
        new_chain.l_isFO[i] = isGoodLepton(chain, ptAndIndex[i][1], 'FO')
        new_chain.l_istight[i] = isGoodLepton(chain, ptAndIndex[i][1], 'tight')
        if not chain.is_data:
            new_chain.l_isfake[i] = isFakeLepton(chain, ptAndIndex[i][1])

        #Apply tau energy scale
        # if not chain.is_data and new_chain.l_flavor[i] == 2:
        #     tlv = getFourVec(new_chain.l_pt[i], new_chain.l_eta[i], new_chain.l_phi[i], new_chain.l_e[i])
        #     new_chain.l_pt *= tes.readES(tlv, chain._tauDecayMode[new_chain.l_indices[i]], chain._tauGenStatus[new_chain.l_indices[i]])
        #     new_chain.l_e *= tes.readES(tlv, chain._tauDecayMode[new_chain.l_indices[i]], chain._tauGenStatus[new_chain.l_indices[i]])

    return True
Exemple #13
0
ec = EventCategory(chain)
for entry in event_range:

    chain.GetEntry(entry)
    progress(entry - event_range[0], len(event_range))

    #if not select3GenLeptons(chain, chain): continue

    #chain.event_supercategory = ec.returnSuperCategory()
    #if chain.event_supercategory > 2: continue

    #Add here any additional cuts
    list_of_numbers['total'] += 1

    chain.leptons = [
        l for l in xrange(chain._nLight) if isGoodLepton(chain, l, 'tight')
    ]
    if len(chain.leptons) == 0: continue
    list_of_numbers['single_lep'] += 1

    if chain._nTau == 0: continue
    list_of_numbers['reco_tau'] += 1

    chain.leptons = [
        l for l in xrange(chain._nLight, chain._nL)
        if isGoodTau(chain, l, 'tight')
    ]
    if len(chain.leptons) == 0: continue
    list_of_numbers['reco_tau_good'] += 1

    if chain._nTau < 2: continue