Esempio n. 1
0
    def process(self, event):

        self.readCollections(event.input)
        miniaodjets = self.handles['jets'].product()

        allJets = []
        event.jets = []
        event.bJets = []
        event.cleanJets = []
        event.cleanBJets = []

        leptons = []
        if hasattr(event, 'selectedLeptons'):
            leptons = event.selectedLeptons

        genJets = None
        if self.cfg_comp.isMC:
            genJets = map(GenJet, self.mchandles['genJets'].product())

        for maodjet in miniaodjets:
            jet = Jet(maodjet)
            allJets.append(jet)
            if self.cfg_comp.isMC and hasattr(self.cfg_comp, 'jetScale'):
                scale = random.gauss(self.cfg_comp.jetScale,
                                     self.cfg_comp.jetSmear)
                jet.scaleEnergy(scale)
            if genJets:
                # Use DeltaR = 0.25 matching like JetMET
                pairs = matchObjectCollection([jet], genJets, 0.25 * 0.25)
                if pairs[jet] is None:
                    pass
                else:
                    jet.matchedGenJet = pairs[jet]
            #Add JER correction for MC jets. Requires gen-jet matching.
            if self.cfg_comp.isMC and hasattr(
                    self.cfg_ana, 'jerCorr') and self.cfg_ana.jerCorr:
                self.jerCorrection(jet)
            #Add JES correction for MC jets.
            if self.cfg_comp.isMC and hasattr(self.cfg_ana, 'jesCorr'):
                self.jesCorrection(jet, self.cfg_ana.jesCorr)
            if self.testJet(jet):
                event.jets.append(jet)
            if self.testBJet(jet):
                event.bJets.append(jet)

        self.counters.counter('jets').inc('all events')

        event.cleanJets, dummy = cleanObjectCollection(event.jets,
                                                       masks=leptons,
                                                       deltaRMin=0.5)
        event.cleanBJets, dummy = cleanObjectCollection(event.bJets,
                                                        masks=leptons,
                                                        deltaRMin=0.5)

        pairs = matchObjectCollection(leptons, allJets, 0.5 * 0.5)
        # associating a jet to each lepton
        for lepton in leptons:
            jet = pairs[lepton]
            if jet is None:
                lepton.jet = lepton
            else:
                lepton.jet = jet

        # associating a leg to each clean jet
        invpairs = matchObjectCollection(event.cleanJets, leptons, 99999.)
        for jet in event.cleanJets:
            leg = invpairs[jet]
            jet.leg = leg

        for jet in event.cleanJets:
            jet.matchGenParton = 999.0

        if self.cfg_comp.isMC and "BB" in self.cfg_comp.name:
            genParticles = self.mchandles['genParticles'].product()
            event.genParticles = map(GenParticle, genParticles)
            for gen in genParticles:
                if abs(gen.pdgId()) == 5 and gen.mother() and abs(
                        gen.mother().pdgId()) == 21:
                    for jet in event.cleanJets:
                        dR = deltaR2(jet.eta(), jet.phi(), gen.eta(),
                                     gen.phi())
                        if dR < jet.matchGenParton:
                            jet.matchGenParton = dR

        event.jets30 = [jet for jet in event.jets if jet.pt() > 30]
        event.cleanJets30 = [jet for jet in event.cleanJets if jet.pt() > 30]
        if len(event.jets30) >= 2:
            self.counters.counter('jets').inc('at least 2 good jets')
        if len(event.cleanJets30) >= 2:
            self.counters.counter('jets').inc('at least 2 clean jets')
        if len(event.cleanBJets) > 0:
            self.counters.counter('jets').inc('at least 1 b jet')
            if len(event.cleanBJets) > 1:
                self.counters.counter('jets').inc('at least 2 b jets')
        return True
    def evaluateRegressionAK08(self, event):
#self.readCollections( event.input )
        reg_fj = []
        for ung_fj in getattr(event, "ak08"):         

            # We need the closest ungroomed fatjet to get the JEC:                                                                                                                                              # - Make a list of pairs: deltaR(ungroomed fj, groomed fj) for all ungroomed fatjets                                                                                                                # - Sort by deltaR                                                                                                                                                                                  # - And take the minimum            
            if len(getattr(event, "ak08pruned")):
                closest_pr_fj_and_dr = sorted( [(pr_fj, deltaR2(ung_fj, pr_fj)) for pr_fj in getattr(event, "ak08pruned")],  key=lambda x:x[1])[0] 
            else:
                print "WARNING: No pruned fatjets found in event with ungroomed fatjet. Skipping"
                continue

            # Use the jet cone size for matching                                                                                                                                                    
            minimal_dr_groomed_ungroomed = 0.8
            if closest_pr_fj_and_dr[1] > minimal_dr_groomed_ungroomed:
                print "WARNING: No pruned fatjet found close to ungroomed fatjet. Skipping"
                continue
            
            pr_jet = Jet(closest_pr_fj_and_dr[0])
            
            if len(getattr(event, "ak08prunedcal")):
                closest_cal_fj_and_dr = sorted( [(cal_fj, deltaR2(ung_fj, cal_fj)) for cal_fj in getattr(event, "ak08prunedcal")],  key=lambda x:x[1])[0] 
            else:
                print "WARNING: No calib groomed fatjets found in event with ungroomed fatjet. Skipping"
                continue

            if closest_cal_fj_and_dr[1] > minimal_dr_groomed_ungroomed:
                print "WARNING: No calib fatjet found close to ungroomed fatjet. Skipping"
                continue


            cal_jet = Jet(closest_cal_fj_and_dr[0])

            # now check the AK08 jet is not a lepton  
#            if len(getattr(event, "selectedLeptons")):
#                closest_pr_lep_and_dr = sorted( [(lep, deltaR2(cal_jet,lep)) for lep in getattr(event, "vLeptons")],  key=lambda x:x[1])[0]
#            if closest_pr_lep_and_dr[1] < (0.4 * 0.4):
#              print "WARNING: No groomed fatjet is overlapping with a vLepton. Skipping"
#              continue

            # Need to do a deep-copy. Otherwise the original jet will be modified                                                                                                                   
            reg_groomed_fj = PhysicsObject(closest_cal_fj_and_dr[0]).__copy__()
#        for j in event.FatjetAK08pruned:

            self.FatjetAK08ungroomed_pt[0] = ung_fj.pt()
#            print 'ung_fj.pt() ', ung_fj.pt()
            self.FatjetAK08pruned_pt[0] = pr_jet.pt()
#            print 'pr_jet.pt() ',  pr_jet.pt()
            self.FatjetAK08prunedCal_pt[0] = cal_jet.pt()
#            print 'cal_jet.pt() ', cal_jet.pt()
            self.FatjetAK08prunedCal_eta[0] = cal_jet.eta()
            self.FatjetAK08ungroomed_vertexNTracks[0] = ung_fj.vertexNTracks
            self.FatjetAK08ungroomed_SV_mass_0[0] = ung_fj.SV_mass_0
            self.FatjetAK08ungroomed_SV_EnergyRatio_0[0] = ung_fj.SV_EnergyRatio_0
            self.FatjetAK08ungroomed_SV_EnergyRatio_1[0] = ung_fj.SV_EnergyRatio_1
            self.FatjetAK08ungroomed_PFLepton_ptrel[0] = ung_fj.PFLepton_ptrel
            self.FatjetAK08ungroomed_nSL[0] = ung_fj.nSL
#            print 'ung_fj.nSL ', ung_fj.nSL

            reg_groomed_fj.scaleEnergy(self.reader.EvaluateRegression(self.name)[0])


            reg_fj.append(reg_groomed_fj)
#            print 'reg_groomed_fj.pt() ', reg_groomed_fj.pt()
  
        
        setattr(event, 'ak08prunedreg',  reg_fj )
Esempio n. 3
0
    def process(self, event):

        self.readCollections(event.input)
        miniaodjets = self.handles['jets'].product()

        allJets = []
        event.jets = []
        event.bJets = []
        event.cleanJets = []
        event.cleanBJets = []

        leptons = []
        if hasattr(event, 'selectedLeptons'):
            leptons = event.selectedLeptons

        genJets = None
        if self.cfg_comp.isMC:
            genJets = map(GenJet, self.mchandles['genJets'].product())

        for maodjet in miniaodjets:
            jet = Jet(maodjet)
            allJets.append(jet)
            if self.cfg_comp.isMC and hasattr(self.cfg_comp, 'jetScale'):
                scale = random.gauss(self.cfg_comp.jetScale,
                                     self.cfg_comp.jetSmear)
                jet.scaleEnergy(scale)
            if genJets:
                # Use DeltaR = 0.25 matching like JetMET
                pairs = matchObjectCollection([jet], genJets, 0.25 * 0.25)
                if pairs[jet] is None:
                    pass
                else:
                    jet.matchedGenJet = pairs[jet]
            # Add JER correction for MC jets. Requires gen-jet matching.
            if self.cfg_comp.isMC and hasattr(self.cfg_ana, 'jerCorr') and self.cfg_ana.jerCorr:
                self.jerCorrection(jet)
            # Add JES correction for MC jets.
            if self.cfg_comp.isMC and hasattr(self.cfg_ana, 'jesCorr'):
                self.jesCorrection(jet, self.cfg_ana.jesCorr)
            if self.testJet(jet):
                event.jets.append(jet)
            if self.testBJet(jet):
                event.bJets.append(jet)

        self.counters.counter('jets').inc('all events')

        event.cleanJets, dummy = cleanObjectCollection(event.jets,
                                                       masks=leptons,
                                                       deltaRMin=0.5)
        event.cleanBJets, dummy = cleanObjectCollection(event.bJets,
                                                        masks=leptons,
                                                        deltaRMin=0.5)

        pairs = matchObjectCollection(leptons, allJets, 0.5 * 0.5)
        # associating a jet to each lepton
        for lepton in leptons:
            jet = pairs[lepton]
            if jet is None:
                lepton.jet = lepton
            else:
                lepton.jet = jet

        # associating a leg to each clean jet
        invpairs = matchObjectCollection(event.cleanJets, leptons, 99999.)
        for jet in event.cleanJets:
            leg = invpairs[jet]
            jet.leg = leg

        for jet in event.cleanJets:
            jet.matchGenParton = 999.0

        if self.cfg_comp.isMC and "BB" in self.cfg_comp.name:
            genParticles = self.mchandles['genParticles'].product()
            event.genParticles = map(GenParticle, genParticles)
            for gen in genParticles:
                if abs(gen.pdgId()) == 5 and gen.mother() and abs(gen.mother().pdgId()) == 21:
                    for jet in event.cleanJets:
                        dR = deltaR2(jet.eta(), jet.phi(), gen.eta(), gen.phi())
                        if dR < jet.matchGenParton:
                            jet.matchGenParton = dR

        event.jets30 = [jet for jet in event.jets if jet.pt() > 30]
        event.cleanJets30 = [jet for jet in event.cleanJets if jet.pt() > 30]
        if len(event.jets30) >= 2:
            self.counters.counter('jets').inc('at least 2 good jets')
        if len(event.cleanJets30) >= 2:
            self.counters.counter('jets').inc('at least 2 clean jets')
        if len(event.cleanBJets) > 0:
            self.counters.counter('jets').inc('at least 1 b jet')
            if len(event.cleanBJets) > 1:
                self.counters.counter('jets').inc('at least 2 b jets')
        return True
Esempio n. 4
0
    def evaluateRegressionAK08(self, event):
        #self.readCollections( event.input )
        reg_fj = []
        for ung_fj in getattr(event, "ak08"):

            # We need the closest ungroomed fatjet to get the JEC:                                                                                                                                              # - Make a list of pairs: deltaR(ungroomed fj, groomed fj) for all ungroomed fatjets                                                                                                                # - Sort by deltaR                                                                                                                                                                                  # - And take the minimum
            if len(getattr(event, "ak08pruned")):
                closest_pr_fj_and_dr = sorted(
                    [(pr_fj, deltaR2(ung_fj, pr_fj))
                     for pr_fj in getattr(event, "ak08pruned")],
                    key=lambda x: x[1])[0]
            else:
                print "WARNING: No pruned fatjets found in event with ungroomed fatjet. Skipping"
                continue

            # Use the jet cone size for matching
            minimal_dr_groomed_ungroomed = 0.8
            if closest_pr_fj_and_dr[1] > minimal_dr_groomed_ungroomed:
                print "WARNING: No pruned fatjet found close to ungroomed fatjet. Skipping"
                continue

            pr_jet = Jet(closest_pr_fj_and_dr[0])

            if len(getattr(event, "ak08prunedcal")):
                closest_cal_fj_and_dr = sorted(
                    [(cal_fj, deltaR2(ung_fj, cal_fj))
                     for cal_fj in getattr(event, "ak08prunedcal")],
                    key=lambda x: x[1])[0]
            else:
                print "WARNING: No calib groomed fatjets found in event with ungroomed fatjet. Skipping"
                continue

            if closest_cal_fj_and_dr[1] > minimal_dr_groomed_ungroomed:
                print "WARNING: No calib fatjet found close to ungroomed fatjet. Skipping"
                continue

            cal_jet = Jet(closest_cal_fj_and_dr[0])

            # now check the AK08 jet is not a lepton
            #            if len(getattr(event, "selectedLeptons")):
            #                closest_pr_lep_and_dr = sorted( [(lep, deltaR2(cal_jet,lep)) for lep in getattr(event, "vLeptons")],  key=lambda x:x[1])[0]
            #            if closest_pr_lep_and_dr[1] < (0.4 * 0.4):
            #              print "WARNING: No groomed fatjet is overlapping with a vLepton. Skipping"
            #              continue

            # Need to do a deep-copy. Otherwise the original jet will be modified
            reg_groomed_fj = PhysicsObject(closest_cal_fj_and_dr[0]).__copy__()
            #        for j in event.FatjetAK08pruned:

            self.FatjetAK08ungroomed_pt[0] = ung_fj.pt()
            #            print 'ung_fj.pt() ', ung_fj.pt()
            self.FatjetAK08pruned_pt[0] = pr_jet.pt()
            #            print 'pr_jet.pt() ',  pr_jet.pt()
            self.FatjetAK08prunedCal_pt[0] = cal_jet.pt()
            #            print 'cal_jet.pt() ', cal_jet.pt()
            self.FatjetAK08prunedCal_eta[0] = cal_jet.eta()
            self.FatjetAK08ungroomed_vertexNTracks[0] = ung_fj.vertexNTracks
            self.FatjetAK08ungroomed_SV_mass_0[0] = ung_fj.SV_mass_0
            self.FatjetAK08ungroomed_SV_EnergyRatio_0[
                0] = ung_fj.SV_EnergyRatio_0
            self.FatjetAK08ungroomed_SV_EnergyRatio_1[
                0] = ung_fj.SV_EnergyRatio_1
            self.FatjetAK08ungroomed_PFLepton_ptrel[0] = ung_fj.PFLepton_ptrel
            self.FatjetAK08ungroomed_nSL[0] = ung_fj.nSL
            #            print 'ung_fj.nSL ', ung_fj.nSL

            reg_groomed_fj.scaleEnergy(
                self.reader.EvaluateRegression(self.name)[0])

            reg_fj.append(reg_groomed_fj)
#            print 'reg_groomed_fj.pt() ', reg_groomed_fj.pt()

        setattr(event, 'ak08prunedreg', reg_fj)
    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:
                    DYJetsFakeAnalyzer.genMatch(event, tau, ptSelGentauleps,
                                                ptSelGenleps, ptSelGenSummary)
                    DYJetsFakeAnalyzer.attachGenStatusFlag(tau)
                DYJetsFakeAnalyzer.genMatch(event, muon, ptSelGentauleps,
                                            ptSelGenleps, ptSelGenSummary)
                DYJetsFakeAnalyzer.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)
Esempio n. 6
0
    def process(self, event):

        self.readCollections(event.input)
        cmgJets = self.handles['jets'].product()

        allJets = []
        event.jets = []
        event.bJets = []
        event.cleanJets = []
        event.cleanBJets = []

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

        genJets = None
        if self.cfg_comp.isMC:
            genJets = map(GenJet, self.mchandles['genJets'].product())

        for cmgJet in cmgJets:
            jet = Jet(cmgJet)
            allJets.append(jet)
            if self.cfg_comp.isMC and hasattr(self.cfg_comp, 'jetScale'):
                scale = random.gauss(self.cfg_comp.jetScale,
                                     self.cfg_comp.jetSmear)
                jet.scaleEnergy(scale)

            if self.testJet(jet):
                event.jets.append(jet)
            if self.testBJet(jet):
                event.bJets.append(jet)
            if genJets:
                # Use DeltaR = 0.25 matching like JetMET
                pairs = matchObjectCollection([jet], genJets, 0.25 * 0.25)
                if pairs[jet] is None:
                    pass
                else:
                    jet.matchedGenJet = pairs[jet]

            #Add JER correction for MC jets. Requires gen-jet matching
            if self.cfg_comp.isMC and hasattr(
                    self.cfg_ana, 'jerCorr') and self.cfg_ana.jerCorr:
                self.jerCorrection(jet)

        self.counters.counter('VBF').inc('all events')

        event.cleanJets, dummy = cleanObjectCollection(event.jets,
                                                       masks=[leg1, leg2],
                                                       deltaRMin=0.5)

        event.cleanBJets, dummy = cleanObjectCollection(event.bJets,
                                                        masks=[leg1, leg2],
                                                        deltaRMin=0.5)

        pairs = matchObjectCollection([leg1, leg2], allJets, 0.5 * 0.5)

        # associating a jet to each leg
        leg1.jet = pairs[leg1]
        leg2.jet = pairs[leg2]
        if leg1.jet is None:  #COLIN: I don't understand the code below...
            leg1.jet = leg1
        if leg2.jet is None:
            leg2.jet = leg2

        # associating a leg to each clean jet
        invpairs = matchObjectCollection(event.cleanJets, [leg1, leg2], 99999.)
        for jet in event.cleanJets:
            leg = invpairs[jet]
            jet.leg = leg

        for jet in event.cleanJets:
            jet.matchGenParton = 999.0

        if self.cfg_comp.isMC and "BB" in self.cfg_comp.name:
            genParticles = self.mchandles['genParticles'].product()
            event.genParticles = map(GenParticle, genParticles)
            for gen in genParticles:
                if abs(gen.pdgId()) == 5 and gen.mother() and abs(
                        gen.mother().pdgId()) == 21:
                    for jet in event.cleanJets:
                        dR = deltaR2(jet.eta(), jet.phi(), gen.eta(),
                                     gen.phi())
                        if dR < jet.matchGenParton:
                            jet.matchGenParton = dR

        event.jets30 = [jet for jet in event.jets if jet.pt() > 30]
        event.cleanJets30 = [jet for jet in event.cleanJets if jet.pt() > 30]

        if len(event.jets30) >= 2:
            self.counters.counter('VBF').inc('at least 2 good jets')

        if len(event.cleanJets30) >= 2:
            self.counters.counter('VBF').inc('at least 2 clean jets')

        if len(event.cleanJets) < 2:
            return True

        event.vbf = VBF(event.cleanJets, event.diLepton, None,
                        self.cfg_ana.cjvPtCut)
        if event.vbf.mjj > self.cfg_ana.Mjj:
            self.counters.counter('VBF').inc(
                'M_jj > {cut:3.1f}'.format(cut=self.cfg_ana.Mjj))
        else:
            return True
        if abs(event.vbf.deta) > self.cfg_ana.deltaEta:
            self.counters.counter('VBF').inc(
                'delta Eta > {cut:3.1f}'.format(cut=self.cfg_ana.deltaEta))
        else:
            return True
        if len(event.vbf.centralJets) == 0:
            self.counters.counter('VBF').inc('no central jets')
        else:
            return True

        return True