def buildDiLeptonsSingle(self, leptons, event):
        '''
        '''
        di_objects = []

        if hasattr(event, 'calibratedTaus'):
            taus = event.calibratedTaus
        else:
            taus = self.handles['taus'].product()

        met = event.pfmet

        taus = [Tau(tau) for tau in taus]

        if getattr(self.cfg_ana, 'scaleTaus', False):
            for tau in taus:
                HTTGenAnalyzer.genMatch(event, tau, event.ptSelGentauleps,
                                        event.ptSelGenleps,
                                        event.ptSelGenSummary)
                HTTGenAnalyzer.attachGenStatusFlag(tau)
                self.scaleP4(tau, self.cfg_ana.tes_scale)

        for leg1 in taus:
            for leg2 in taus:
                if leg1 != leg2:
                    di_tau = DirectTauTau(leg1, leg2, met)
                    di_tau.leg2().associatedVertex = event.goodVertices[0]
                    di_tau.leg1().associatedVertex = event.goodVertices[0]
                    di_tau.leg1().event = event.input.object()
                    di_tau.leg2().event = event.input.object()
                    di_tau.mvaMetSig = None
                    di_objects.append(di_tau)
        return di_objects
Exemplo n.º 2
0
    def process(self, event):
        self.readCollections(event.input)

        event.taus = [Tau(tau) for tau in self.handles['taus'].product()]
        event.gen = self.handles['gen'].product()
        event.genParticles = event.gen
        event.genJets = self.handles['gen_jets'].product()

        for pu_info in self.handles['pu'].product():
            if pu_info.getBunchCrossing() == 0:
                event.n_true_interactions = pu_info.getTrueNumInteractions()

        event.genleps = [p for p in event.gen if abs(p.pdgId()) in [11, 13] and p.statusFlags().isPrompt()]
        event.gentauleps = [p for p in event.gen if abs(p.pdgId()) in [11, 13] and p.statusFlags().isDirectPromptTauDecayProduct()]
        event.gentaus = [p for p in event.gen if abs(p.pdgId()) == 15 and p.statusFlags().isPrompt() and not any(abs(HTTGenAnalyzer.getFinalTau(p).daughter(i_d).pdgId()) in [11, 13] for i_d in xrange(HTTGenAnalyzer.getFinalTau(p).numberOfDaughters()))]

        HTTGenAnalyzer.getGenTauJets(event) # saves event.genTauJets

        for gen_tau in event.genTauJets:
            gen_tau.charged = [d for d in gen_tau.daughters if d.charge()]
            gen_tau.pizeros = [daughters(d) for d in gen_tau.daughters if d.pdgId() == 111]

        pf_candidates = self.handles['pf_candidates'].product()

        for tau in event.taus:
            TauIsoAnalyzer.addInfo(tau, event, [c for c in pf_candidates if abs(c.pdgId()) == 211], maxDeltaR=0.8)
            matched_gen_jet, dr2 = bestMatch(tau, event.genJets)
            if dr2 < 0.25:
                tau.gen_jet = matched_gen_jet

        return True
Exemplo n.º 3
0
    def process(self, event):
        # needed when doing handle.product(), goes back to
        # PhysicsTools.Heppy.analyzers.core.Analyzer
        self.readCollections(event.input)

        if not eval(self.skimFunction):
            return False

        event.genJets = self.mchandles['genJets'].product()

        ptcut = 8.
        ptSelGentauleps = [lep for lep in event.gentauleps if lep.pt() > ptcut]
        ptSelGenleps = [lep for lep in event.genleps if lep.pt() > ptcut]
        ptSelGenSummary = [
            p for p in event.generatorSummary if p.pt() > ptcut
            and abs(p.pdgId()) not in [6, 23, 24, 25, 35, 36, 37]
        ]

        for tau in event.selectedTaus:
            HTTGenAnalyzer.genMatch(event, tau, ptSelGentauleps, ptSelGenleps,
                                    ptSelGenSummary)

        for i_tau, tau in enumerate(event.selectedTaus):

            if i_tau < self.maxNTaus:
                self.tree.reset()
                self.fillJetMETVars(event)
                self.fillTau(self.tree, 'tau', tau)
                self.fill(self.tree, 'tau_puppi_iso_pt', tau.puppi_iso_pt)
                self.fill(self.tree, 'tau_puppi_iso04_pt', tau.puppi_iso04_pt)
                self.fill(self.tree, 'tau_puppi_iso03_pt', tau.puppi_iso03_pt)
                self.fill(self.tree, 'tau_trigger_iso', tau.trigger_iso)
                if tau.genp:
                    self.fillGenParticle(self.tree, 'tau_gen', tau.genp)
                    if tau.genJet():
                        self.fillGenParticle(self.tree, 'tau_gen_vis',
                                             tau.genJet())
                        self.fill(self.tree, 'tau_gen_decayMode',
                                  tauDecayModes.genDecayModeInt(tau.genJet()))
                if not tau.leadNeutralCand().isNull():
                    self.fillParticle(self.tree, 'tau_lead_neutral',
                                      tau.leadNeutralCand())
                if not tau.leadChargedHadrCand().isNull():
                    self.fillParticle(self.tree, 'tau_lead_charged',
                                      tau.leadChargedHadrCand())
                self.fill(self.tree, 'tau_charged_iso', tau.chargedPtSumIso)
                self.fill(self.tree, 'tau_gamma_iso', tau.gammaPtSumIso)
                self.fill(self.tree, 'tau_neutral_iso', tau.neutralPtSumIso)
                self.fill(self.tree, 'tau_charged_sig',
                          tau.chargedCandsPtSumSignal)
                self.fill(self.tree, 'tau_gamma_sig',
                          tau.gammaCandsPtSumSignal)
                self.fill(self.tree, 'tau_neutral_sign',
                          tau.neutralCandsPtSumSignal)

                self.fillTree(event)
Exemplo n.º 4
0
    def process(self, event):

        self.l1 = event.diLepton.leg1()
        self.l2 = event.diLepton.leg2()

        HTTGenAnalyzer.genMatch(event, self.l1, event.ptSelGentauleps, event.ptSelGenleps, event.ptSelGenSummary)
        HTTGenAnalyzer.genMatch(event, self.l2, event.ptSelGentauleps, event.ptSelGenleps, event.ptSelGenSummary)

        HTTGenAnalyzer.attachGenStatusFlag(self.l1)
        HTTGenAnalyzer.attachGenStatusFlag(self.l2)

        if hasattr(event, 'selectedTaus'):
            for tau in event.selectedTaus:
                HTTGenAnalyzer.genMatch(event, tau, event.ptSelGentauleps, event.ptSelGenleps, event.ptSelGenSummary)
 def Scale(self, tau, ptSelGentauleps, ptSelGenleps, event):
     # this function should take values of scales from a file
     tau.unscaledP4 = copy.deepcopy(tau.p4())
     HTTGenAnalyzer.genMatch(event, tau, ptSelGentauleps, ptSelGenleps, [])
     HTTGenAnalyzer.attachGenStatusFlag(tau)
     if tau.gen_match == 5:
         if tau.decayMode() == 0:
             tau.scaleEnergy(0.995)
         elif tau.decayMode() == 1:
             tau.scaleEnergy(1.011)
         elif tau.decayMode() == 10:
             tau.scaleEnergy(1.006)
     elif tau.gen_match == 1:
         if tau.decayMode() == 0:
             tau.scaleEnergy(1.024)
         elif tau.decayMode() == 1:
             tau.scaleEnergy(1.076)
Exemplo n.º 6
0
    def buildDiLeptons(self, cmgDiLeptons, event):
        '''Build di-leptons, associate best vertex to both legs.'''
        diLeptons = []
        for index, dil in enumerate(cmgDiLeptons):
            pydil = TauTau(dil, iso=self.cfg_ana.isolation)
            pydil.leg1().associatedVertex = event.goodVertices[0]
            pydil.leg2().associatedVertex = event.goodVertices[0]
            pydil.leg1().event = event.input.object()
            pydil.leg2().event = event.input.object()
            diLeptons.append(pydil)
            pydil.mvaMetSig = pydil.met().getSignificanceMatrix()
            if getattr(self.cfg_ana, 'scaleTaus', False):
                for leg in [pydil.leg1(), pydil.leg2()]:
                    HTTGenAnalyzer.genMatch(event, leg, event.ptSelGentauleps,
                                            event.ptSelGenleps,
                                            event.ptSelGenSummary)
                    HTTGenAnalyzer.attachGenStatusFlag(leg)
                    self.scaleP4(leg, self.cfg_ana.tes_scale)

        return diLeptons
    def TauEnergyScale(self, diLeptons, event):
        # get genmatch-needed informations, needs to be done in this analyzer to scale, though information is usually gathered afterwards in HTTGenAnalyzer
        genParticles = self.handles['genParticles'].product()

        ptSelGentauleps = [
            p for p in genParticles if abs(p.pdgId()) in [11, 13]
            and p.statusFlags().isDirectPromptTauDecayProduct() and p.pt() > 8.
        ]

        ptSelGenleps = [
            p for p in genParticles if abs(p.pdgId()) in [11, 13]
            and p.statusFlags().isPrompt() and p.pt() > 8.
        ]

        event.gentaus = [
            p for p in event.genParticles
            if abs(p.pdgId()) == 15 and p.statusFlags().isPrompt() and not any(
                abs(HTTGenAnalyzer.getFinalTau(p).daughter(i_d).pdgId()) in
                [11, 13] for i_d in xrange(
                    HTTGenAnalyzer.getFinalTau(p).numberOfDaughters()))
        ]

        # create a list of all used leptons without duplicates, using the ROOT.pat::Tau objects as discriminant
        leps = []

        for dilep in diLeptons:
            if dilep.leg1().tau not in [lep.tau for lep in leps]:
                leps.append(dilep.leg1())
            if dilep.leg2().tau not in [lep.tau for lep in leps]:
                leps.append(dilep.leg2())

        # scale leptons
        for lep in leps:
            self.Scale(lep, ptSelGentauleps, ptSelGenleps, event)
            for dilep in diLeptons:
                for leg in [dilep.leg1(), dilep.leg2()]:
                    if leg.tau == lep.tau and (not hasattr(leg, 'unscaledP4')):
                        leg.unscaledP4 = lep.unscaledP4
Exemplo n.º 8
0
 def addInfo(tau, event, cands=None, maxDeltaR=None):
     HTTGenAnalyzer.genMatch(event, tau, event.gentauleps, event.genleps, [], dR=0.2, matchAll=True)
     HTTGenAnalyzer.attachGenStatusFlag(tau)
     TauIsoAnalyzer.tauIsoBreakdown(tau, cands, maxDeltaR=maxDeltaR)
     tau.nphotons = sum(1 for cand in TauIsoAnalyzer.tauFilteredPhotons(tau))
Exemplo n.º 9
0
    def process(self, event):

        super(H2TauTauTreeProducerTauTau, self).process(event)

        isSUSY = getattr(self.cfg_ana, 'isSUSY', False)

        tau1 = event.diLepton.leg1()
        tau2 = event.diLepton.leg2()

        self.fillTau(self.tree, 'l1', tau1)
        self.fillTau(self.tree, 'l2', tau2)

        if hasattr(tau1, 'genp'):
            if tau1.genp:
                self.fillGenParticle(self.tree, 'l1_gen', tau1.genp)
        if hasattr(tau2, 'genp'):
            if tau2.genp:
                self.fillGenParticle(self.tree, 'l2_gen', tau2.genp)

        # save the p4 of the visible tau products at the generator level
        # make sure that the reco tau matches with a gen tau that decays into hadrons

        if tau1.genJet() and hasattr(tau1, 'genp') and tau1.genp and abs(tau1.genp.pdgId()) == 15:
            self.fillParticle(self.tree, 'l1_gen_vis', tau1.physObj.genJet())
            tau_gen_dm = tauDecayModes.translateGenModeToInt(tauDecayModes.genDecayModeFromGenJet(tau1.physObj.genJet()))
            self.fill(self.tree, 'l1_gen_decaymode', tau_gen_dm)

        if tau2.genJet() and hasattr(tau2, 'genp') and tau2.genp and abs(tau2.genp.pdgId()) == 15:
            self.fillParticle(self.tree, 'l2_gen_vis', tau2.physObj.genJet())
            tau_gen_dm = tauDecayModes.translateGenModeToInt(tauDecayModes.genDecayModeFromGenJet(tau2.physObj.genJet()))
            self.fill(self.tree, 'l2_gen_decaymode', tau_gen_dm)

        if hasattr(tau1, 'weight_trigger'):
            self.fill(self.tree, 'l1_trigger_weight', tau1.weight_trigger)
            self.fill(self.tree, 'l1_trigger_weight_up', getattr(tau1, 'weight_trigger_up', 1.))
            self.fill(self.tree, 'l1_trigger_weight_down', getattr(tau1, 'weight_trigger_down', 1.))

            self.fill(self.tree, 'l2_trigger_weight', tau2.weight_trigger)
            self.fill(self.tree, 'l2_trigger_weight_up', getattr(tau2, 'weight_trigger_up', 1.))
            self.fill(self.tree, 'l2_trigger_weight_down', getattr(tau2, 'weight_trigger_down', 1.))

        self.fill(self.tree, 'mt2',  event.mt2)
        self.fill(self.tree, 'mt2_lep',  event.mt2_lep)
        self.fill(self.tree, 'mt2_mvamet',  event.mt2_mvamet)
        self.fill(self.tree, 'mt2_rawpfmet',  event.mt2_rawpfmet)
        
        self.fill(self.tree, 'minDphiMETJets', event.minDphiMETJets)


        fired_triggers = [info.name for info in getattr(event, 'trigger_infos', []) if info.fired]

        self.fill(self.tree, 'trigger_ditau35', any('HLT_DoubleMediumIsoPFTau35_Trk1_eta2p1_Reg_v' in name for name in fired_triggers) or isSUSY)
        self.fill(self.tree, 'trigger_ditau35_combiso', any('HLT_DoubleMediumCombinedIsoPFTau35_Trk1_eta2p1_Reg_' in name for name in fired_triggers) or isSUSY)
        self.fill(self.tree, 'trigger_singletau140', any('HLT_VLooseIsoPFTau140_Trk50_eta2p1_v' in name for name in fired_triggers) or isSUSY)
        self.fill(self.tree, 'trigger_singletau120', any('HLT_VLooseIsoPFTau120_Trk50_eta2p1_v' in name for name in fired_triggers) or isSUSY)

        matched_paths = getattr(event.diLepton, 'matchedPaths', [])
        self.fill(self.tree, 'trigger_matched_ditau35', any('HLT_DoubleMediumIsoPFTau35_Trk1_eta2p1_Reg_v' in name for name in matched_paths) or isSUSY)
        self.fill(self.tree, 'trigger_matched_ditau35_combiso', any('HLT_DoubleMediumCombinedIsoPFTau35_Trk1_eta2p1_Reg_' in name for name in matched_paths) or isSUSY)
        self.fill(self.tree, 'trigger_matched_singletau140', any('HLT_VLooseIsoPFTau140_Trk50_eta2p1_v' in name for name in matched_paths) or isSUSY)
        self.fill(self.tree, 'trigger_matched_singletau120', any('HLT_VLooseIsoPFTau120_Trk50_eta2p1_v' in name for name in matched_paths) or isSUSY)

        if self.cfg_comp.isMC and isSUSY:
            self.fill(self.tree, 'GenSusyMScan1',  getattr(event, 'genSusyMScan1', -999.))
            self.fill(self.tree, 'GenSusyMScan2',  getattr(event, 'genSusyMScan2', -999.))
            self.fill(self.tree, 'GenSusyMScan3',  getattr(event, 'genSusyMScan3', -999.))
            self.fill(self.tree, 'GenSusyMScan4',  getattr(event, 'genSusyMScan4', -999.))
            self.fill(self.tree, 'GenSusyMNeutralino',  getattr(event, 'genSusyMNeutralino', -999.))
            self.fill(self.tree, 'GenSusyMChargino',  getattr(event, 'genSusyMChargino', -999.))
            self.fill(self.tree, 'GenSusyMStau',  getattr(event, 'genSusyMStau', -999.))
            self.fill(self.tree, 'GenSusyMStau2',  getattr(event, 'genSusyMStau2', -999.))

        
            self.fillLHEWeights(self.tree, event)

            self.fill(self.tree, 'gen_dichargino_pt', HTTGenAnalyzer.getSusySystem(event).pt())

        self.fillTree(event)
Exemplo n.º 10
0
    def process(self, event):
        self.readCollections(event.input)

        event.rho = self.handles['rho'].product()[0]

        event.taus = [Tau(tau) for tau in self.handles['taus'].product()]
        dms = self.handles['DM'].product() 
        loose_db_isos = self.handles['looseDBIso'].product()

        if len(event.taus) != len(dms) or len(event.taus) != len(loose_db_isos):
            import pdb; pdb.set_trace()

        for i_tau, tau in enumerate(event.taus):
            tau.dm = dms.value(i_tau)
            tau.loose_db_iso = loose_db_isos.value(i_tau)
            if tau.dm > 1:
                import pdb; pdb.set_trace()

        event.hlt_taus = []
        if self.handles['hlt_taus'].isValid():
            event.hlt_taus = [Tau(tau) for tau in self.handles['hlt_taus'].product()]

        if event.hlt_taus:
            hlt_dms = self.handles['hltDM'].product() 
            hlt_loose_db_isos = self.handles['hltLooseDB'].product()
            for i_tau, tau in enumerate(event.hlt_taus):
                tau.dm = hlt_dms.value(i_tau)
                tau.loose_db_iso = hlt_loose_db_isos.value(i_tau)
        else:
            # print self.handles['hlt_taus']._exception
            if self.handles['hlt_taus']._exception is None:
                import pdb; pdb.set_trace()


        event.hlt_single_taus = []
        if self.handles['hltSingle_taus'].isValid():
            event.hlt_single_taus = [Tau(tau) for tau in self.handles['hltSingle_taus'].product()]

        if event.hlt_single_taus:
            hlt_dms = self.handles['hltSingleDM'].product() 
            hlt_loose_db_isos = self.handles['hltSingleLooseDB'].product()
            for i_tau, tau in enumerate(event.hlt_single_taus):
                tau.dm = hlt_dms.value(i_tau)
                tau.loose_db_iso = hlt_loose_db_isos.value(i_tau)
        else:
            # print self.handles['hlt_taus']._exception
            if self.handles['hltSingle_taus']._exception is None:
                import pdb; pdb.set_trace()


        event.genParticles = self.handles['genParticles'].product()

        event.genleps = [p for p in event.genParticles if abs(p.pdgId()) in [11, 13] and p.statusFlags().isPrompt()]
        event.gentauleps = [p for p in event.genParticles if abs(p.pdgId()) in [11, 13] and p.statusFlags().isDirectPromptTauDecayProduct()]
        event.gentaus = [p for p in event.genParticles if abs(p.pdgId()) == 15 and p.statusFlags().isPrompt() and not any(abs(HTTGenAnalyzer.getFinalTau(p).daughter(i_d).pdgId()) in [11, 13] for i_d in xrange(HTTGenAnalyzer.getFinalTau(p).numberOfDaughters()))]

        def addInfo(tau, cands=None, maxDeltaR=None):
            HTTGenAnalyzer.genMatch(event, tau, event.gentauleps, event.genleps, [], 
                 dR=0.2, matchAll=True)
            self.tauIsoBreakdown(tau, cands, maxDeltaR=maxDeltaR)
            tau.nphotons = sum(1 for cand in TauHLTAnalyzer.tauFilteredPhotons(tau))

        pfCandidates = self.handles['pfCandidates'].product()
        hltPfCandidates = self.handles['hltPfCandidates'].product() if self.handles['hltPfCandidates'].isValid() else None
        hltSinglePfCandidates = self.handles['hltSinglePfCandidates'].product() if self.handles['hltSinglePfCandidates'].isValid() else None

        for tau in event.taus:
            addInfo(tau, [c for c in pfCandidates if abs(c.pdgId()) == 211], maxDeltaR=0.8)

        for tau in event.hlt_taus :
            addInfo(tau, [c for c in hltPfCandidates if abs(c.pdgId()) == 211], maxDeltaR=0.5)

        for tau in event.hlt_single_taus:
            addInfo(tau, [c for c in hltSinglePfCandidates if abs(c.pdgId()) == 211], maxDeltaR=0.5)

        return True
Exemplo n.º 11
0
    def process(self, event):
        # needed when doing handle.product(), goes back to
        # PhysicsTools.Heppy.analyzers.core.Analyzer
        self.readCollections(event.input)

        if not eval(self.skimFunction):
            return False

        ptSelGentauleps = []
        ptSelGenleps = []
        ptSelGenSummary = []

        if self.cfg_comp.isMC:
            event.genJets = self.mchandles['genJets'].product()

            ptcut = 8.
            ptSelGentauleps = [
                lep for lep in event.gentauleps if lep.pt() > ptcut
            ]
            ptSelGenleps = [lep for lep in event.genleps if lep.pt() > ptcut]
            ptSelGenSummary = [
                p for p in event.generatorSummary if p.pt() > ptcut
                and abs(p.pdgId()) not in [6, 23, 24, 25, 35, 36, 37]
            ]

        for i_dil, dil in enumerate(event.selDiLeptons):

            muon = dil.leg1()
            jet = dil.leg2()
            found = False
            for corr_jet in event.jets:
                if deltaR2(jet.eta(), jet.phi(), corr_jet.eta(),
                           corr_jet.phi()) < 0.01:
                    pt = max(
                        corr_jet.pt(),
                        corr_jet.pt() * corr_jet.corrJECUp / corr_jet.corr,
                        corr_jet.pt() * corr_jet.corrJECDown / corr_jet.corr)
                    if pt < 20.:
                        continue
                    found = True

            if not found:
                continue

            tau = jet.tau if hasattr(jet, 'tau') else None
            if self.cfg_comp.isMC:
                if tau:
                    HTTGenAnalyzer.genMatch(event, tau, ptSelGentauleps,
                                            ptSelGenleps, ptSelGenSummary)
                    HTTGenAnalyzer.attachGenStatusFlag(tau)
                HTTGenAnalyzer.genMatch(event, muon, ptSelGentauleps,
                                        ptSelGenleps, ptSelGenSummary)
                HTTGenAnalyzer.attachGenStatusFlag(muon)

            self.tree.reset()
            self.fillEvent(self.tree, event)
            self.fillDiLepton(self.tree, event.diLepton, fill_svfit=False)
            self.fillExtraMetInfo(self.tree, event)
            self.fillGenInfo(self.tree, event)

            self.fillJetMETVars(event)
            self.fillMuon(self.tree, 'muon', muon)
            jet = Jet(jet)
            jet.btagMVA = jet.btag(
                'pfCombinedInclusiveSecondaryVertexV2BJetTags')
            jet.btagFlag = jet.btagMVA > 0.8
            self.fillJet(self.tree, 'oriJet', jet)
            self.fill(self.tree, 'jet_nth', i_dil)

            for corr_jet in event.jets:
                if deltaR2(jet.eta(), jet.phi(), corr_jet.eta(),
                           corr_jet.phi()) < 0.01:
                    self.fillJet(self.tree, 'jet', corr_jet)
                    self.fill(self.tree, 'jet_nooverlap',
                              True if corr_jet in event.cleanJets else False)
                    self.fill(self.tree, 'jet_corrJECUp',
                              corr_jet.corrJECUp / corr_jet.corr)
                    self.fill(self.tree, 'jet_corrJECDown',
                              corr_jet.corrJECDown / corr_jet.corr)
                    self.fill(self.tree, 'jet_corr', corr_jet.corr)

            if tau:
                self.fillTau(self.tree, 'tau', tau)

                if hasattr(tau, 'genp') and tau.genp:
                    self.fillGenParticle(self.tree, 'tau_gen', tau.genp)
                    if tau.genJet():
                        self.fillGenParticle(self.tree, 'tau_gen_vis',
                                             tau.genJet())
                        self.fill(self.tree, 'tau_gen_decayMode',
                                  tauDecayModes.genDecayModeInt(tau.genJet()))

            self.fillTree(event)