def buildJetList(self, event): self.quarks = [] self.nq = 0 for ptc in self.mchandles['genParticles'].product(): #print GenParticle(ptc) # Find four jet events if abs(ptc.pdgId()) > 6 and ptc.pdgId() != 21: continue #print 'a quark : ',ptc.pdgId() if not (ptc.numberOfMothers()): continue moth = ptc.mother(0) #print 'Mother : ',moth.pdgId() if moth.numberOfMothers() and moth.mother(0).pdgId() == 25: continue if abs(moth.pdgId()) != 23 and \ abs(moth.pdgId()) != 25 : continue self.nq += 1 # counters of quarks with H or Z as a mothers self.quarks.append( GenParticle(ptc) ) # list of GenParticle objects for the quarks that have H or Z as a mothers #if self.nq == 4 : print 'A Four Jet Event !' self.jets = [] for ptj in self.handles['jets'].product(): self.jets.append( Jet(ptj)) # list of Jets object for all the jets in input #print jet, jet.nConstituents(), jet.component(1).number(), Jet(ptj).component(1).fraction() if len(self.jets) > 4: self.jets.sort(key=lambda a: a.energy(), reverse=True) tmpJets = set(self.jets) #print 'Jets Avant : ' #for jet in tmpJets: # print jet, jet.nConstituents(), jet.component(1).number(), jet.component(1).fraction(), jet.mass(), jet.btag(7) while len(tmpJets) != 4: dijets = self.findPairs(tmpJets) dijets.sort(key=lambda a: a.M()) #print dijets[0],dijets[0].M() tmpJets.remove( dijets[0].leg1 ) # it removes from the list of all Jets the components of the diJet object with the lowest mass tmpJets.remove(dijets[0].leg2) # "" tmpJets.add( dijets[0] ) # it add the diJet object with lowest mass to the list of Jets #print 'Jets apres : ' self.jets = [] for jet in tmpJets: #print jet,jet.nConstituents(), jet.mass(), jet.btag(7) #print jet,jet.nConstituents(), jet.component(1).number(), jet.component(1).fraction(), jet.mass(), jet.btag(7) self.jets.append(jet) self.jets.sort(key=lambda a: a.btag(7), reverse=True) # list of Jet objects
def buildLeptonList(self, event): self.jets = [] for ptj in self.handles['jets'].product(): self.jets.append(Jet(ptj)) self.leptons = [] for pte in self.handles['electrons'].product(): self.leptons.append(Lepton(pte)) for ptm in self.handles['muons'].product(): self.leptons.append(Lepton(ptm)) self.photons = [] for ptg in self.handles['photons'].product(): if ptg.energy() > 1.0: self.photons.append(Photon(ptg)) # Find FSR tmpLept = set(self.leptons) for lept in self.leptons: leptonIso = self.leptonIso(lept) if leptonIso > 0.2: tmpLept.remove(lept) continue #if (self.eleele or self.mumu or self.tautau ) : # print self.eleele, self.mumu, self.tautau,lept, leptonIso for i, ph in enumerate(self.photons): dr = deltaR(lept.eta(), lept.phi(), ph.eta(), ph.phi()) if (abs(lept.pdgId()) == 11 or abs(lept.pdgId()) == 13) and dr < 0.4: #print 'found ISR ',ph,lept leph = lept.p4() p4ph = ph.p4() p4 = ph.p4() + lept.p4() lept.setP4(p4) #print 'added ISR ',ph,lept self.leptons = [] for lept in tmpLept: self.leptons.append(lept) self.leptonJets = [] for lept in self.leptons: drmin = 999. ijet = -1 for i, jet in enumerate(self.jets): dr = deltaR(lept.eta(), lept.phi(), jet.eta(), jet.phi()) if dr < drmin: drmin = dr ijet = i if ijet >= 0: if drmin < 0.1: self.jets[ijet].setP4(lept.p4()) self.jets[ijet].setPdgId(lept.pdgId()) self.leptonJets.append(self.jets[ijet])
def process(self, iEvent, event): self.readCollections( iEvent ) eventNumber = iEvent.eventAuxiliary().id().event() # print "=========================================================" # print "event:",eventNumber myEvent = Event(event.iEv) setattr(event, self.name, myEvent) event = myEvent self.buildMCinfo( event ) self.counters.counter('h_gen').inc('All events') self.counters.counter('h_rec').inc('All events') if self.isHZ: self.counters.counter('h_gen').inc('All hz events') if len(self.tau)==2: self.counters.counter('h_gen').inc('h->tt') if self.isHZqq: self.counters.counter('h_gen').inc('Z->qq and h->tt') if self.isHZbb: self.counters.counter('h_gen').inc('Z->bb and h->tt') event.isHZ = 0 event.isHZqq = 0 event.isHZbb = 0 if self.isHZ: event.isHZ=1 event.H=self.H event.Z=self.Z if self.isHZqq: event.isHZqq=1 if self.isHZbb: event.isHZbb=1 event.ishtt = 0 if len(self.tau)==2: event.ishtt =1 event.tau1=self.tau[0] event.tau2=self.tau[1] #save the two leading muons and the two leading electrons self.muons = [] for muon in self.handles['muons'].product(): self.muons.append(Muon(muon)) self.muons.sort(key=lambda a: a.energy(), reverse = True) event.leadingMuon_en=-1 if len(self.muons)>=1: event.leadingMuon_en=self.muons[0].energy() event.leadingMuons = [] for muon in self.muons: if len(event.leadingMuons) >= 2: break event.leadingMuons.append(muon) # this is needed because the energy of the muon is changed later on if it is a tau candidate self.electrons = [] for electron in self.handles['electrons'].product(): self.electrons.append(Electron(electron)) self.electrons.sort(key=lambda a: a.energy(), reverse = True) event.leadingElectron_en=-1 if len(self.electrons)>=1: event.leadingElectron_en=self.electrons[0].energy() event.leadingElectrons = [] for electron in self.electrons: if len(event.leadingElectrons) >= 2: break event.leadingElectrons.append(electron) self.hadtaus = [] for tau in self.handles['taus'].product(): self.hadtaus.append(Tau(tau)) self.hadtaus.sort(key=lambda a: a.energy(), reverse = True) # prepare jets ordered in energy self.jets=[] for ptj in self.handles['jets'].product(): self.jets.append(Jet(ptj)) # order them in energy self.jets.sort(key=lambda a: a.energy(), reverse = True) event.njets=len(self.jets) # loop over ele,muo,hadtaus and define "isolation", and jetindex # iso is the closest jet (excluding itself) distance # index is the pfjet index of the object (ele,muo,tau) # dist is the distance of the object to the matched pfjet (it is trivially 1 for pfjets) self.jetiso,self.jetindex,self.jetdist=self.findiso(self.jets,self.jets) self.muoiso,self.muoindex,self.muodist=self.findiso(self.jets,self.muons) self.eleiso,self.eleindex,self.eledist=self.findiso(self.jets,self.electrons) self.hadtauiso,self.hadtauindex,self.hadtaudist=self.findiso(self.jets,self.hadtaus) # look now at the tau jets self.taugenjet=[] self.taucos=[] self.tauiso=[] self.taumatchingjetindex=[] # self.nontaugenjet = copy.copy(self.jets) # self.nontaugenjetiso = copy.copy(self.jetiso) if event.ishtt==1: # loop over jets and find closest gen tau if event.njets>=2: for thistau in self.tau: cosmax=-2 tauindex=-10 for ind in range(0,len(self.jets)): thisjet=self.jets[ind] thiscos=self.cosdelta(thistau,thisjet) if thiscos>cosmax: cosmax=thiscos taucand=thisjet iso=self.jetiso[ind] tauindex=ind if cosmax!=-2: self.taugenjet.append(taucand) self.tauiso.append(iso) #self.nontaugenjet.pop(tauindex) #self.nontaugenjetiso.pop(tauindex) self.taumatchingjetindex.append(tauindex) else: self.taugenjet.append(thistau) # just to fill it up self.taucos.append(cosmax) event.tau1genjet = self.taugenjet[0] event.tau1cosjet = self.taucos[0] event.tau1iso = self.tauiso[0] event.tau2genjet = self.taugenjet[1] event.tau2cosjet = self.taucos[1] event.tau2iso = self.tauiso[1] event.tau1matchingjetindex=self.taumatchingjetindex[0] event.tau2matchingjetindex=self.taumatchingjetindex[1] # self.nontaugenjet.sort(key=lambda a: a.energy(), reverse = True) # event.nontaugenjet = self.nontaugenjet # event.nontaugenjetiso = self.nontaugenjetiso # debug # if event.ishtt==1 and event.isHZqq==1: # print "tau gen:" # # print event.tau1 # print event.tau2 # # print "jets" # for jet in self.jets: # print jet # print "muons" # for jet in self.muons: # print jet # print "electrons" # for jet in self.electrons: # print jet # print "taus" # for jet in self.hadtaus: # print jet # start here the real selection event.step=0 event.matchedRecGenDistances = [] event.hz = [] # first look for at least four jets and two of them isolated and low #tracks if event.njets<4: return event.step+=1 # 1 #test for jets above threshold etest = self.testE() if etest: self.counters.counter('h_rec').inc('4 jets above threshold') else: return event.step+=1 #2 # print "njets=",event.njets,len(self.jets) # requires at least 2 taucandidates as isolated jets (0.5) and with 1 or 3 tracks event.taucand=[] event.taucandtype=[] event.taucandiso=[] event.taucandindex=[] event.taucandcharge=[] event.nontaucand=[] event.nontaucandiso=[] event.acoll = -99. event.acopl = -99. removedjet=[] # first build up tau dandidates from hadtaus, electrons and muons # print "#jets,muons,elec,hadtaus:",len(self.jets),len(self.muons),len(self.electrons),len(self.hadtaus) for ind in range(0,len(self.muons)): if self.testmuoId(ind): if self.muoindex[ind] not in removedjet: # be sure you get only one copy event.taucand.append(self.muons[ind]) event.taucandtype.append(0) event.taucandiso.append(self.muoiso[ind]) event.taucandindex.append(self.muoindex[ind]) removedjet.append(self.muoindex[ind]) # print "added muon ",event.taucandindex[len(event.taucand)-1] for ind in range(0,len(self.electrons)): if self.testeleId(ind): if self.eleindex[ind] not in removedjet: # prefer muons over ele if belonging to same matched jet event.taucand.append(self.electrons[ind]) event.taucandtype.append(1) event.taucandiso.append(self.eleiso[ind]) event.taucandindex.append(self.eleindex[ind]) removedjet.append(self.eleindex[ind]) # print "added ele ",event.taucandindex[len(event.taucand)-1] for ind in range(0,len(self.hadtaus)): if self.testhadtauId(ind): if self.hadtauindex[ind] not in removedjet: # prefer electrons,muons over had taus if belonging to same matched jet event.taucand.append(self.hadtaus[ind]) event.taucandtype.append(2) event.taucandiso.append(self.hadtauiso[ind]) event.taucandindex.append(self.hadtauindex[ind]) removedjet.append(self.hadtauindex[ind]) # print "added hadtau ",event.taucandindex[len(event.taucand)-1] # now we can add the jets not associated to candidate taus (ele, muo, hadtau) to the jet list for ind in range(0,len(self.jets)): if ind not in removedjet: event.nontaucand.append(self.jets[ind]) event.nontaucandiso.append(self.jetiso[ind]) # print "added jet ",event.nontaucand[len(event.nontaucand)-1].energy() # check for 2 tau candidates if len(event.taucand)<2: return self.counters.counter('h_rec').inc('2 tau candidates') event.step+=1 #3 if len(event.taucand)==2: #electrons + muons + charged hadrons ntr1=self.ntrtaucand(event.taucand[0],event.taucandtype[0]) ntr2=self.ntrtaucand(event.taucand[1],event.taucandtype[1]) if (ntr1*ntr2)!=1 and (ntr1*ntr2)!=3: return # now iterate on all possible pairs of tau candidates, if more than two, and keep only the ones which give # opposite mass closer to Z peak if len(event.taucand)>2: dm=9999. taupair=(-1,-1) # fit version of tau finding optimization for ind1, ind2 in itertools.combinations(range(len(event.taucand)), 2): ntr1=self.ntrtaucand(event.taucand[0],event.taucandtype[0]) ntr2=self.ntrtaucand(event.taucand[1],event.taucandtype[1]) # consider only combination 1,1 1,3 3,1 if (ntr1*ntr2)!=1 and (ntr1*ntr2)!=3: continue nt=[] nj=[] nt.append(event.taucand[ind1]) nt.append(event.taucand[ind2]) nj=copy.copy(event.nontaucand) for i in range(len(event.taucand)): if i!=ind1 and i!=ind2: nj.append(self.jets[event.taucandindex[i]]) # nt and nj are potential tau and nontaucandidates # now clusterize the nontaucand tmpJets=list(self.buildClusteredJets(nj)) # poor man kf chi2,newjets,newtaus = self.beta4(tmpJets, nt, 120.,False) jet0=newjets[0] jet1=newjets[1] mnontau=(jet0[3]+jet1[3])**2-(jet0[2]+jet1[2])**2-(jet0[1]+jet1[1])**2-(jet0[0]+jet1[0])**2 if mnontau>0: mnontau=sqrt(mnontau) else: mnontau=0 # print ind1,ind2,mnontau if abs(mnontau-91.2)<dm: taupair=(ind1,ind2) dm=abs(mnontau-91.2) # now we have taupair and we can adapt the candidates (t1,t2)=taupair # print "chosen",t1,t2 if t1<0 or t2<0: return # print "chosen",t1,t2 t1temp=event.taucand[t1] t1isotemp=event.taucandiso[t1] t1typetemp=event.taucandtype[t1] t1indextemp=event.taucandindex[t1] t2temp=event.taucand[t2] t2isotemp=event.taucandiso[t2] t2typetemp=event.taucandtype[t2] t2indextemp=event.taucandindex[t2] for tauind in range(0,len(event.taucand)): if tauind!=t1 and tauind!=t2: #remove thsi tau candidate and add it to nontaucandidate # print "removing ",tauind event.nontaucand.append(self.jets[event.taucandindex[tauind]]) event.nontaucandiso.append(self.jetiso[event.taucandindex[tauind]]) # cleanup the taucand list event.taucand=[t1temp,t2temp] event.taucandiso=[t1isotemp,t2isotemp] event.taucandtype=[t1typetemp,t2typetemp] event.taucandindex=[t1indextemp,t2indextemp] self.counters.counter('h_rec').inc('2 tau candidates good pair') event.step+=1 #4 #fill jet charges for tau candidates # for tau in event.taucand: # print tau.numberOfSourceCandidatePtrs() # constituents = tau.sourcePtr().getPFConstituents() # charge = 0 # for constituent in constituents: # charge += constituent.charge() # event.taucandcharge.append[charge] #fill acollinearity for tau candidates tnorm1 = event.taucand[0].p4().P() tnorm2 = event.taucand[1].p4().P() ttdot = event.taucand[0].px()*event.taucand[1].px() + event.taucand[0].py()*event.taucand[1].py() + event.taucand[0].pz()*event.taucand[1].pz() event.acoll = ttdot/(tnorm1*tnorm2) #MC matching if event.ishtt==1 and event.isHZqq==1: #check the closest gen tau self.matched=[] self.matcheddistance=[] for taucand in event.taucand: cosmax=-2 for gentau in self.tau: thiscos=self.cosdelta(taucand,gentau) if thiscos>cosmax: cosmax=thiscos event.matchedRecGenDistances.append(cosmax) #cluster the remaining part of the event # self.buildClusteredJets_old(event) tmpJets=self.buildClusteredJets(event.nontaucand) # rewrites the jets after clustering event.nontaucand = [] for jet in tmpJets: event.nontaucand.append(jet) # debug # if event.ishtt==1 and event.isHZqq==1: # print "tau cand" # for jet in event.taucand: # print jet # print "non taucand" # for jet in event.nontaucand: # print jet jnorm1 = event.nontaucand[0].p4().P() jnorm2 = event.nontaucand[1].p4().P() jjdot = event.nontaucand[0].px()*event.nontaucand[1].px() + event.nontaucand[0].py()*event.nontaucand[1].py() + event.nontaucand[0].pz()*event.nontaucand[1].pz() event.jcoll = jjdot/(jnorm1*jnorm2) event.btag_tt=0 event.btag_jj=0 # for jet in event.taucand: # event.btag_tt+=jet.btag(7) for jet in event.nontaucand: event.btag_jj+=jet.btag(7) # store variables before rescaling event.t1_px=event.taucand[0].px() event.t1_py=event.taucand[0].py() event.t1_pz=event.taucand[0].pz() event.t1_en=event.taucand[0].energy() event.t2_px=event.taucand[1].px() event.t2_py=event.taucand[1].py() event.t2_pz=event.taucand[1].pz() event.t2_en=event.taucand[1].energy() # store variables before rescaling event.j1_px=event.nontaucand[0].px() event.j1_py=event.nontaucand[0].py() event.j1_pz=event.nontaucand[0].pz() event.j1_en=event.nontaucand[0].energy() event.j2_px=event.nontaucand[1].px() event.j2_py=event.nontaucand[1].py() event.j2_pz=event.nontaucand[1].pz() event.j2_en=event.nontaucand[1].energy() # print "before rescaling:",event.t1_en,event.t2_en,event.j1_en,event.j2_en # print "before rescaling from jet:",self.jets[event.taucandindex[0]].energy(),self.jets[event.taucandindex[1]].energy() #check that the clustered jets pass id (this also does the momentum rescaling) idpass,newjets,newtaus = self.testId(event.nontaucand, event.taucand,True) # we need also to replace self.jets px,py,pz (will be used for findww,findzz and findhz) for ind in range(0,len(event.taucand)): j_ind=event.taucandindex[ind] p4=self.jets[j_ind].p4() p4.SetPxPyPzE(event.taucand[ind].px(),event.taucand[ind].py(),event.taucand[ind].pz(),event.taucand[ind].energy()) self.jets[j_ind].setP4(p4) #fill acoplanarity between met and the plane containinf the taus t1 = numpy.array([event.taucand[0].px(), event.taucand[0].py(), event.taucand[0].pz()]) t2 = numpy.array([event.taucand[1].px(), event.taucand[1].py(), event.taucand[1].pz()]) norm = numpy.cross(t1,t2) #now extract the angle wrt mmissing energy me = ([-self.px, -self.py, -self.pz]) pscal = numpy.dot(norm,me) norm_p = numpy.linalg.norm(norm) me_p = numpy.linalg.norm(me) if norm_p>0 and me_p>0: event.acopl = pscal/(norm_p * me_p) # store variables after rescaling event.t1s_px=event.taucand[0].px() event.t1s_py=event.taucand[0].py() event.t1s_pz=event.taucand[0].pz() event.t1s_en=event.taucand[0].energy() event.t2s_px=event.taucand[1].px() event.t2s_py=event.taucand[1].py() event.t2s_pz=event.taucand[1].pz() event.t2s_en=event.taucand[1].energy() # store variables after rescaling event.j1s_px=event.nontaucand[0].px() event.j1s_py=event.nontaucand[0].py() event.j1s_pz=event.nontaucand[0].pz() event.j1s_en=event.nontaucand[0].energy() event.j2s_px=event.nontaucand[1].px() event.j2s_py=event.nontaucand[1].py() event.j2s_pz=event.nontaucand[1].pz() event.j2s_en=event.nontaucand[1].energy() # print "after rescaling:",event.t1s_en,event.t2s_en,event.j1s_en,event.j2s_en # print "after rescaling from jet:",self.jets[event.taucandindex[0]].energy(),self.jets[event.taucandindex[1]].energy() event.mvis = self.mvis event.px = self.px event.py = self.py event.pz = self.pz event.evis = self.evis event.chi2 = self.chi2 event.mmin = self.mmin event.alljets=[] # print "alljets",len(self.jets),event.taucandindex[0],event.taucandindex[1] # this is needed to use findXX (for Diobject, otherwise btag is not defined) event.taucandjets=[self.jets[event.taucandindex[0]],self.jets[event.taucandindex[1]]] for jet in event.nontaucand: event.alljets.append(jet) for jet in event.taucandjets: event.alljets.append(jet) #event.alljets contains all jets now (j1,j2,t1,t2) event.jetPairs = self.findPairs( event.alljets ) event.ww, event.wwMin = self.findWW ( event.jetPairs ) event.zz, event.zzMin = self.findZZ ( event.jetPairs ) if not idpass: return self.counters.counter('h_rec').inc('2 jets with id') event.step+=1 #5 #finally find the HZ candidates reshz,self.hz = self.findHZ(event.nontaucand, event.taucandjets) event.hz = self.hz # print "dijetmass ",event.hz[1].M() if not reshz: return event.step+=1 #6 self.counters.counter('h_rec').inc('passed') if event.ishtt==1 and event.isHZqq==1: self.counters.counter('h_gen').inc('Z->qq and h->tt selected') if event.ishtt==1 and event.isHZbb==1: self.counters.counter('h_gen').inc('Z->bb and h->tt selected') # apply btag here if event.btag_jj<0.95: return event.step+=1 #7 if event.ishtt==1 and event.isHZqq==1: self.counters.counter('h_gen').inc('Z->qq and h->tt b-selected') if event.ishtt==1 and event.isHZbb==1: self.counters.counter('h_gen').inc('Z->bb and h->tt b-selected') return True
def process(self, iEvent, event): self.readCollections( iEvent ) cmgJets = 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 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 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.cleanJets)<2: return True return True
def process(self, iEvent, event): self.readCollections( iEvent ) 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 #jet.genJet = None else: jet.genJet = 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
def selectionSequence(self, event, fillCounter, leg1IsoCut=None, leg2IsoCut=None): if fillCounter: self.counters.counter('DiLepton').inc('all events') if len(event.diLeptons) == 0: return False if fillCounter: self.counters.counter('DiLepton').inc('> 0 di-lepton') # testing di-lepton itself selDiLeptons = event.diLeptons if not self.leptonAccept(event.leptons): return False if fillCounter: self.counters.counter('DiLepton').inc('lepton accept') if not hasattr(event, 'hltPaths'): event.hltPaths = [] #import pdb ; pdb.set_trace() matching = {} for trig in event.hltPaths: matching.update({trig: [-99, -99, -99]}) # {trigName:leg1,leg2,jet} event.diLeptonsTrigMatched = [] for trig in event.hltPaths: if self.cfg_comp.isEmbed: continue ## no matching for the embed selDiLeptons = event.diLeptons if len(self.cfg_comp.triggers) > 0: # trigger matching leg1 selDiLeptons = [ diL for diL in selDiLeptons if self.trigMatched(event, diL.leg1(), 'leg1', trig) ] if len(selDiLeptons) == 0: matching[trig][0] = 0 else: if fillCounter: self.counters.counter('DiLepton').inc( 'leg1 trig matched') matching[trig][0] = 1 if len(self.cfg_comp.triggers) > 0: # trigger matching leg2 selDiLeptons = [ diL for diL in selDiLeptons if self.trigMatched(event, diL.leg2(), 'leg2', trig) ] if len(selDiLeptons) == 0: matching[trig][1] = 0 else: if fillCounter: self.counters.counter('DiLepton').inc( 'leg2 trig matched') matching[trig][1] = 1 if len(self.cfg_comp.triggers) > 0 and len( self.cfg_ana.triggerMap[trig]) > 2: # trigger matching jet cmgJets = self.handles['jets'].product() jets = [] for cmgJet in cmgJets: jet = Jet(cmgJet) if self.testJet(jet): jets.append(jet) selDiLeptonsNew = [] for diL in selDiLeptons: cleanJets, dummy = cleanObjectCollection( jets, masks=[diL.leg1(), diL.leg2()], deltaRMin=0.5) if len(cleanJets) > 0 and self.trigMatched( event, cleanJets[0], 'jet', trig): selDiLeptonsNew += [diL] selDiLeptons = selDiLeptonsNew if len(selDiLeptons) == 0: matching[trig][2] = 0 else: if fillCounter: self.counters.counter('DiLepton').inc( 'jet trig matched') matching[trig][2] = 1 event.diLeptonsTrigMatched += selDiLeptons event.diLeptonsTrigMatched = set(event.diLeptonsTrigMatched) ### need unix style wild card to macth different trigger versions in data for trig in matching.keys(): if fnm(trig, 'HLT_DoubleMediumIsoPFTau35_Trk*_eta2p1_v*'): event.l1TrigMatched_diTau = matching[trig][0] event.l2TrigMatched_diTau = matching[trig][1] if fnm(trig, 'HLT_DoubleMediumIsoPFTau*_Trk*_eta2p1_Jet30_v*'): event.l1TrigMatched_diTauJet = matching[trig][0] event.l2TrigMatched_diTauJet = matching[trig][1] event.jetTrigMatched_diTauJet = matching[trig][2] # testing leg1 selDiLeptons = [ diL for diL in selDiLeptons if self.testLeg(diL.leg1()) ] if len(selDiLeptons) == 0: return False else: if fillCounter: self.counters.counter('DiLepton').inc( 'leg1 offline cuts passed') # testing leg2 selDiLeptons = [ diL for diL in selDiLeptons if self.testLeg(diL.leg2()) ] if len(selDiLeptons) == 0: return False else: if fillCounter: self.counters.counter('DiLepton').inc( 'leg2 offline cuts passed') # mass cut selDiLeptons = [diL for diL in selDiLeptons if self.testMass(diL)] if len(selDiLeptons) == 0: return False else: if fillCounter: self.counters.counter('DiLepton').inc( '{min:3.1f} < m < {max:3.1f}'.format( min=self.cfg_ana.m_min, max=self.cfg_ana.m_max)) # exactly one? if len(selDiLeptons) == 0: return False elif len(selDiLeptons) == 1: if fillCounter: self.counters.counter('DiLepton').inc('exactly 1 di-lepton') event.diLepton = self.bestDiLepton(selDiLeptons) event.leg1 = event.diLepton.leg1() event.leg2 = event.diLepton.leg2() # match with L1 particles, whether Central Jets or Taus L1Taus = self.handles['L1JetsTau'].product() L1Jets = self.handles['L1JetsCentral'].product() L1dR1T, L1dR2T, index1T, index2T = self.match(event.diLepton, L1Taus) L1dR1J, L1dR2J, index1J, index2J = self.match(event.diLepton, L1Jets) if L1dR1T < L1dR1J and L1dR1T <= 0.5: if L1Taus[index1T].pt() > 44. and abs(L1Taus[index1T].eta()) < 2.1: event.leg1.L1particle = L1Taus[index1T] if L1dR1J < L1dR1T and L1dR1J <= 0.5: if L1Jets[index1J].pt() > 64.: event.leg1.L1particle = L1Jets[index1J] if L1dR2T < L1dR2J and L1dR2T <= 0.5: if L1Taus[index2T].pt() > 44. and abs(L1Taus[index2T].eta()) < 2.1: event.leg2.L1particle = L1Taus[index2T] if L1dR2J < L1dR2T and L1dR2J <= 0.5: if L1Jets[index2J].pt() > 64.: event.leg2.L1particle = L1Jets[index2J] ### require trigger bit in Embedded RecHit if self.cfg_comp.isEmbed: #if len(event.hltPath)==0 : return False if not event.hltPath: return False #import pdb ; pdb.set_trace() return True
def process(self, iEvent, event): self.readCollections(iEvent) eventNumber = iEvent.eventAuxiliary().id().event() myEvent = Event(event.iEv) setattr(event, self.name, myEvent) event = myEvent self.buildMCinfo(event) self.counters.counter('h_gen').inc('All events') self.counters.counter('h_rec').inc('All events') if self.isHZ: self.counters.counter('h_gen').inc('All hz events') if len(self.tau) == 2: self.counters.counter('h_gen').inc('h->tt') if self.isHZqq: self.counters.counter('h_gen').inc('Z->qq and h->tt') if self.isHZbb: self.counters.counter('h_gen').inc('Z->bb and h->tt') event.isHZ = 0 event.isHZqq = 0 event.isHZbb = 0 if self.isHZ: event.isHZ = 1 event.H = self.H event.Z = self.Z if self.isHZqq: event.isHZqq = 1 if self.isHZbb: event.isHZbb = 1 event.ishtt = 0 if len(self.tau) == 2: event.ishtt = 1 event.tau1 = self.tau[0] event.tau2 = self.tau[1] #save the two leading muons and the two leading electrons self.muons = [] for muon in self.handles['muons'].product(): self.muons.append(Muon(muon)) self.muons.sort(key=lambda a: a.energy(), reverse=True) event.leadingMuons = [] for muon in self.muons: if len(event.leadingMuons) >= 2: break event.leadingMuons.append(muon) self.electrons = [] for electron in self.handles['electrons'].product(): self.electrons.append(Electron(electron)) self.electrons.sort(key=lambda a: a.energy(), reverse=True) event.leadingElectrons = [] for electron in self.electrons: if len(event.leadingElectrons) >= 2: break event.leadingElectrons.append(electron) # prepare jets ordered in energy self.jets = [] for ptj in self.handles['jets'].product(): self.jets.append(Jet(ptj)) # order them in energy self.jets.sort(key=lambda a: a.energy(), reverse=True) event.njets = len(self.jets) # loop over jets and define "isolation" self.jetiso = [] for ind1 in range(0, len(self.jets)): cosmax = -2 for ind2 in range(ind1 + 1, len(self.jets)): cos = self.cosdelta(self.jets[ind1], self.jets[ind2]) if cos > cosmax: cosmax = cos self.jetiso.append(cosmax) # # prepare electrons ordered in energy # self.electrons=[] # for ptj in self.handles['electrons'].product(): # self.electrons.append(Electron(ptj)) # # # order them in energy # self.electrons.sort(key=lambda a: a.energy(), reverse = True) # # # loop over electron and define "isolation" # self.eleiso=[] # for ind1 in range(0,len(self.electrons)): # cosmax=-2 # for ind2 in range(ind1+1,len(self.jets)): # cos=self.cosdelta(self.electrons[ind1],self.jets[ind2]) # if cos> cosmax: # cosmax=cos # self.eleiso.append(cosmax) # # # prepare muons ordered in energy # self.muons=[] # for ptj in self.handles['muons'].product(): # self.muons.append(Electron(ptj)) # # # order them in energy # self.muons.sort(key=lambda a: a.energy(), reverse = True) # # # loop over jets and define "isolation" # self.muoiso=[] # for ind1 in range(0,len(self.muons)): # cosmax=-2 # for ind2 in range(ind1+1,len(self.jets)): # cos=self.cosdelta(self.muons[ind1],self.jets[ind2]) # if cos> cosmax: # cosmax=cos # self.muoiso.append(cosmax) # look now at the tau jets self.taugenjet = [] self.taucos = [] self.tauiso = [] self.taumatchingjetindex = [] # self.nontaugenjet = copy.copy(self.jets) # self.nontaugenjetiso = copy.copy(self.jetiso) if event.ishtt == 1: # loop over jets and find closest gen tau if event.njets >= 2: for thistau in self.tau: cosmax = -2 tauindex = -10 for ind in range(0, len(self.jets)): thisjet = self.jets[ind] thiscos = self.cosdelta(thistau, thisjet) if thiscos > cosmax: cosmax = thiscos taucand = thisjet iso = self.jetiso[ind] tauindex = ind if cosmax != -2: self.taugenjet.append(taucand) self.tauiso.append(iso) #self.nontaugenjet.pop(tauindex) #self.nontaugenjetiso.pop(tauindex) self.taumatchingjetindex.append(tauindex) else: self.taugenjet.append(thistau) # just to fill it up self.taucos.append(cosmax) event.tau1genjet = self.taugenjet[0] event.tau1cosjet = self.taucos[0] event.tau1iso = self.tauiso[0] event.tau2genjet = self.taugenjet[1] event.tau2cosjet = self.taucos[1] event.tau2iso = self.tauiso[1] event.tau1matchingjetindex = self.taumatchingjetindex[0] event.tau2matchingjetindex = self.taumatchingjetindex[1] # self.nontaugenjet.sort(key=lambda a: a.energy(), reverse = True) # event.nontaugenjet = self.nontaugenjet # event.nontaugenjetiso = self.nontaugenjetiso # start here the real selection event.step = 0 event.matchedRecGenDistances = [] event.hz = [] # first look for at least four jets and two of them isolated and low #tracks if event.njets < 4: return event.step += 1 # 1 #test for jets above threshold etest = self.testE() if etest: self.counters.counter('h_rec').inc('4 jets above threshold') else: return event.step += 1 #2 # print "njets=",event.njets,len(self.jets) # requires at least 2 taucandidates as isolated jets (0.5) and with 1,2 or 3 tracks event.taucand = [] event.taucandiso = [] event.taucandcharge = [] event.nontaucand = [] event.nontaucandiso = [] event.acoll = -99. event.acopl = -99. for ind in range(0, len(self.jets)): # print "evaluating ",ind # ntrk=self.jets[ind].component(1).number() # the two most energetic jets are not considered among taus # if ind>=2 and ntrk>0 and ntrk<=3 and self.jetiso[ind]<0.5 and len(event.taucand)<2: # if ntrk>=1 and ntrk<=3 and self.jetiso[ind]<0.8 and len(event.taucand)<2: # if ntrk>=0 and ntrk<=3 and self.jetiso[ind]<0.5 and len(event.taucand)<2: # if ntrk>=1 and ntrk<=3 and self.jetiso[ind]<0.9: # if ntrk>=1 and ntrk<=3: # if ntrk==1 or ntrk==3: if self.testTauId(self.jets[ind]): event.taucand.append(self.jets[ind]) event.taucandiso.append(self.jetiso[ind]) # print "adding to tau" else: event.nontaucand.append(self.jets[ind]) event.nontaucandiso.append(self.jetiso[ind]) # print "adding to non tau" # check for 2 tau candidates if len(event.taucand) < 2: return self.counters.counter('h_rec').inc('2 tau candidates') event.step += 1 #3 if len(event.taucand) == 2: #electrons + muons + charged hadrons ntr1 = event.taucand[0].component( 1).number() + event.taucand[0].component( 2).number() + event.taucand[0].component(3).number() ntr2 = event.taucand[1].component( 1).number() + event.taucand[1].component( 2).number() + event.taucand[1].component(3).number() if (ntr1 * ntr2) != 1 and (ntr1 * ntr2) != 3: return # now iterate on all possible pairs of tau candidates, if more than two, and keep only the ones which give # opposite mass closer to Z peak if len(event.taucand) > 2: dm = 9999. taupair = (-1, -1) # fit version of tau finding optimization for ind1, ind2 in itertools.combinations(range(len(event.taucand)), 2): ntr1 = event.taucand[ind1].component( 1).number() + event.taucand[ind1].component(2).number( ) + event.taucand[ind1].component(3).number() ntr2 = event.taucand[ind2].component( 1).number() + event.taucand[ind2].component(2).number( ) + event.taucand[ind2].component(3).number() # consider only combination 1,1 1,3 3,1 if (ntr1 * ntr2) != 1 and (ntr1 * ntr2) != 3: continue nt = [] nj = [] nt.append(event.taucand[ind1]) nt.append(event.taucand[ind2]) nj = copy.copy(event.nontaucand) for i in range(len(event.taucand)): if i != ind1 and i != ind2: nj.append(event.taucand[i]) # nt and nj are potential tau and nontaucandidates # now clusterize the nontaucand tmpJets = list(self.buildClusteredJets(nj)) # poor man kf chi2, newjets, newtaus = self.beta4(tmpJets, nt, 120., False) jet0 = newjets[0] jet1 = newjets[1] mnontau = (jet0[3] + jet1[3])**2 - (jet0[2] + jet1[2])**2 - ( jet0[1] + jet1[1])**2 - (jet0[0] + jet1[0])**2 if mnontau > 0: mnontau = sqrt(mnontau) else: mnontau = 0 # print ind1,ind2,mnontau if abs(mnontau - 91.2) < dm: taupair = (ind1, ind2) dm = abs(mnontau - 91.2) # poor man optimization of tau candidates # first sum-up nontaucand momentum # pnontau=[0,0,0,0] # for jet in event.nontaucand: # pnontau[0]+=jet.px() # pnontau[1]+=jet.py() # pnontau[2]+=jet.pz() # pnontau[3]+=jet.energy() # for ind1, ind2 in itertools.combinations(range(len(event.taucand)), 2): # ntr1=event.taucand[ind1].component(1).number() # ntr2=event.taucand[ind2].component(1).number() # if (ntr1*ntr2)!=1 and (ntr1*ntr2)!=3: # continue # ptau=[0,0,0,0] # for tauind in range(0,len(event.taucand)): # if tauind!=ind1 and tauind!=ind2: # ptau[0]+=event.taucand[tauind].px() # ptau[1]+=event.taucand[tauind].py() # ptau[2]+=event.taucand[tauind].pz() # ptau[3]+=event.taucand[tauind].energy() # pall=[0,0,0,0] # for comp in range(0,4): pall[comp]=pnontau[comp]+ptau[comp] # mnontau=pall[3]**2-pall[0]**2-pall[1]**2-pall[2]**2 # if mnontau<0: mnontau=0 # mnontau=sqrt(mnontau) # # print ind1,ind2,mnontau # if abs(mnontau-91.2)<dm: # taupair=(ind1,ind2) # dm=abs(mnontau-91.2) # now we have taupair and we can adapt the candidates (t1, t2) = taupair # print "chosen",t1,t2 if t1 < 0 or t2 < 0: return # print "chosen",t1,t2 t1temp = event.taucand[t1] t1isotemp = event.taucandiso[t1] t2temp = event.taucand[t2] t2isotemp = event.taucandiso[t2] for tauind in range(0, len(event.taucand)): if tauind != t1 and tauind != t2: #remove thsi tau candidate and add it to nontaucandidate # print "removing ",tauind event.nontaucand.append(event.taucand[tauind]) event.nontaucandiso.append(event.taucandiso[tauind]) # cleanup the taucand list event.taucand = [t1temp, t2temp] event.taucandiso = [t1isotemp, t2isotemp] self.counters.counter('h_rec').inc('2 tau candidates good pair') event.step += 1 #4 #fill jet charges for tau candidates # for tau in event.taucand: # print tau.numberOfSourceCandidatePtrs() # constituents = tau.sourcePtr().getPFConstituents() # charge = 0 # for constituent in constituents: # charge += constituent.charge() # event.taucandcharge.append[charge] #fill acollinearity for tau candidates tnorm1 = event.taucand[0].p4().P() tnorm2 = event.taucand[1].p4().P() ttdot = event.taucand[0].px() * event.taucand[1].px( ) + event.taucand[0].py() * event.taucand[1].py( ) + event.taucand[0].pz() * event.taucand[1].pz() event.acoll = ttdot / (tnorm1 * tnorm2) #MC matching if event.ishtt == 1 and event.isHZqq == 1: #check the closest gen tau self.matched = [] self.matcheddistance = [] for taucand in event.taucand: cosmax = -2 for gentau in self.tau: thiscos = self.cosdelta(taucand, gentau) if thiscos > cosmax: cosmax = thiscos event.matchedRecGenDistances.append(cosmax) #cluster the remaining part of the event # self.buildClusteredJets_old(event) tmpJets = self.buildClusteredJets(event.nontaucand) # rewrites the jets after clustering event.nontaucand = [] for jet in tmpJets: event.nontaucand.append(jet) jnorm1 = event.nontaucand[0].p4().P() jnorm2 = event.nontaucand[1].p4().P() jjdot = event.nontaucand[0].px() * event.nontaucand[1].px( ) + event.nontaucand[0].py() * event.nontaucand[1].py( ) + event.nontaucand[0].pz() * event.nontaucand[1].pz() event.jcoll = jjdot / (jnorm1 * jnorm2) event.btag_tt = 0 event.btag_jj = 0 for jet in event.taucand: event.btag_tt += jet.btag(7) for jet in event.nontaucand: event.btag_jj += jet.btag(7) # store variables before rescaling event.t1_px = event.taucand[0].px() event.t1_py = event.taucand[0].py() event.t1_pz = event.taucand[0].pz() event.t1_en = event.taucand[0].energy() event.t2_px = event.taucand[1].px() event.t2_py = event.taucand[1].py() event.t2_pz = event.taucand[1].pz() event.t2_en = event.taucand[1].energy() # store variables before rescaling event.j1_px = event.nontaucand[0].px() event.j1_py = event.nontaucand[0].py() event.j1_pz = event.nontaucand[0].pz() event.j1_en = event.nontaucand[0].energy() event.j2_px = event.nontaucand[1].px() event.j2_py = event.nontaucand[1].py() event.j2_pz = event.nontaucand[1].pz() event.j2_en = event.nontaucand[1].energy() #check that the clustered jets pass id (this also does the momentum rescaling) idpass, newjets, newtaus = self.testId(event.nontaucand, event.taucand, True) #fill acoplanarity between met and the plane containinf the taus t1 = numpy.array([ event.taucand[0].px(), event.taucand[0].py(), event.taucand[0].pz() ]) t2 = numpy.array([ event.taucand[1].px(), event.taucand[1].py(), event.taucand[1].pz() ]) norm = numpy.cross(t1, t2) #now extract the angle wrt mmissing energy me = ([-self.px, -self.py, -self.pz]) pscal = numpy.dot(norm, me) norm_p = numpy.linalg.norm(norm) me_p = numpy.linalg.norm(me) if norm_p > 0 and me_p > 0: event.acopl = pscal / (norm_p * me_p) # store variables after rescaling event.t1s_px = event.taucand[0].px() event.t1s_py = event.taucand[0].py() event.t1s_pz = event.taucand[0].pz() event.t1s_en = event.taucand[0].energy() event.t2s_px = event.taucand[1].px() event.t2s_py = event.taucand[1].py() event.t2s_pz = event.taucand[1].pz() event.t2s_en = event.taucand[1].energy() # store variables after rescaling event.j1s_px = event.nontaucand[0].px() event.j1s_py = event.nontaucand[0].py() event.j1s_pz = event.nontaucand[0].pz() event.j1s_en = event.nontaucand[0].energy() event.j2s_px = event.nontaucand[1].px() event.j2s_py = event.nontaucand[1].py() event.j2s_pz = event.nontaucand[1].pz() event.j2s_en = event.nontaucand[1].energy() event.mvis = self.mvis event.px = self.px event.py = self.py event.pz = self.pz event.evis = self.evis event.chi2 = self.chi2 event.mmin = self.mmin event.alljets = [] for jet in event.nontaucand: event.alljets.append(jet) for jet in event.taucand: event.alljets.append(jet) #event.alljets contains all jets now (j1,j2,t1,t2) event.jetPairs = self.findPairs(event.alljets) event.ww, event.wwMin = self.findWW(event.jetPairs) event.zz, event.zzMin = self.findZZ(event.jetPairs) if not idpass: return self.counters.counter('h_rec').inc('2 jets with id') event.step += 1 #5 #finally find the HZ candidates reshz, self.hz = self.findHZ(event.nontaucand, event.taucand) event.hz = self.hz # print "dijetmass ",event.hz[1].M() if not reshz: return event.step += 1 #6 self.counters.counter('h_rec').inc('passed') if event.ishtt == 1 and event.isHZqq == 1: self.counters.counter('h_gen').inc('Z->qq and h->tt selected') if event.ishtt == 1 and event.isHZbb == 1: self.counters.counter('h_gen').inc('Z->bb and h->tt selected') # apply btag here if event.btag_jj < 0.95: return event.step += 1 #7 if event.ishtt == 1 and event.isHZqq == 1: self.counters.counter('h_gen').inc('Z->qq and h->tt b-selected') if event.ishtt == 1 and event.isHZbb == 1: self.counters.counter('h_gen').inc('Z->bb and h->tt b-selected') return True
def buildLeptonList(self, event): self.eleele = False self.mumu = False self.tautau = False self.electrons = [] self.muons = [] self.taus = [] for ptc in self.mchandles['genParticles'].product(): isElectron = abs(ptc.pdgId()) == 11 isMuon = abs(ptc.pdgId()) == 13 isTau = abs(ptc.pdgId()) == 15 if isElectron: if ptc.numberOfMothers() and ptc.mother(0).pdgId() != 23: continue self.electrons.append(GenParticle(ptc)) if isMuon: if ptc.numberOfMothers() and ptc.mother(0).pdgId() != 23: continue self.muons.append(GenParticle(ptc)) if isTau: if ptc.numberOfMothers() and ptc.mother(0).pdgId() != 23: continue self.taus.append(GenParticle(ptc)) if len(self.electrons) == 2: self.eleele = True if len(self.muons) == 2: self.mumu = True if len(self.taus) == 2: self.tautau = True event.eleele = self.eleele event.mumu = self.mumu event.tautau = self.tautau self.jets = [] for ptj in self.handles['jets'].product(): self.jets.append(Jet(ptj)) self.leptons = [] for pte in self.handles['electrons'].product(): self.leptons.append(Lepton(pte)) for ptm in self.handles['muons'].product(): self.leptons.append(Lepton(ptm)) self.photons = [] for ptg in self.handles['photons'].product(): if ptg.energy() > 1.0: self.photons.append(Photon(ptg)) # Find FSR tmpLept = set(self.leptons) for lept in self.leptons: leptonIso = self.leptonIso(lept) if leptonIso > 0.05: tmpLept.remove(lept) continue #if (self.eleele or self.mumu or self.tautau ) : # print self.eleele, self.mumu, self.tautau,lept, leptonIso for i, ph in enumerate(self.photons): dr = deltaR(lept.eta(), lept.phi(), ph.eta(), ph.phi()) if abs(lept.pdgId()) == 13 and dr < 0.4: #print 'found ISR ',ph,lept leph = lept.p4() p4ph = ph.p4() p4 = ph.p4() + lept.p4() lept.setP4(p4) #print 'added ISR ',ph,lept self.leptons = [] for lept in tmpLept: self.leptons.append(lept) for lept in self.leptons: drmin = 999. ijet = -1 for i, jet in enumerate(self.jets): dr = deltaR(lept.eta(), lept.phi(), jet.eta(), jet.phi()) if dr < drmin: drmin = dr ijet = i if ijet >= 0: if drmin < 0.1: self.jets[ijet].setP4(lept.p4()) self.jets[ijet].setPdgId(lept.pdgId())
def process(self, iEvent, event): self.readCollections( iEvent ) cmgJets = self.handles['jets'].product() print 'cmgJets = ', len(cmgJets) allJets = [] event.jets = [] event.bJets = [] event.cleanJets = [] event.cleanBJets = [] # import pdb; pdb.set_trace(); leptons = [] if hasattr(event, 'selectedLeptons'): leptons = event.selectedLeptons # elif hasattr(event, 'muoncand'): ## leptons = event.muoncand + event.electroncand + event.taucand # leptons = event.muon + event.electron + event.tau # print '# of leptons = ', len(leptons) 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 genJets: # Use DeltaR = 0.25 matching like JetMET pairs = matchObjectCollection( [jet], genJets, 0.25*0.25) if pairs[jet] is None: pass #jet.genJet = None else: jet.genJet = pairs[jet] # print jet, jet.genJet #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') # print 'check =', len(event.muon), len(event,electron), len(event.tau) # if isinstance(event.muoncand, list): # event.cleanBJets, dummy = cleanObjectCollection( event.bJets, # masks = event.muoncand, # deltaRMin = 0.4 ) # # event.cleanJets, dummy = cleanObjectCollection( event.jets, # masks = event.muoncand, # deltaRMin = 0.4 ) # # else: # event.cleanBJets, dummy = cleanObjectCollection( event.bJets, # masks = [event.muoncand], # deltaRMin = 0.4 ) # # event.cleanJets, dummy = cleanObjectCollection( event.jets, # masks = [event.muoncand], # deltaRMin = 0.4 ) # # if isinstance(event.electroncand, list): # event.cleanBJets, dummy = cleanObjectCollection( event.cleanBJets, # masks = event.electroncand, # deltaRMin = 0.4 ) # # event.cleanJets, dummy = cleanObjectCollection( event.cleanJets, # masks = event.electroncand, # deltaRMin = 0.4 ) # # else: # event.cleanBJets, dummy = cleanObjectCollection( event.cleanBJets, # masks = [event.electroncand], # deltaRMin = 0.4 ) # # # event.cleanJets, dummy = cleanObjectCollection( event.cleanJets, # masks = [event.electroncand], # deltaRMin = 0.4 ) # # # if isinstance(event.taucand, list): # event.cleanBJets, dummy = cleanObjectCollection( event.cleanBJets, # masks = event.taucand, # deltaRMin = 0.4 ) # # event.cleanJets, dummy = cleanObjectCollection( event.cleanJets, # masks = event.taucand, # deltaRMin = 0.4 ) # # else: # event.cleanBJets, dummy = cleanObjectCollection( event.cleanBJets, # masks = [event.taucand], # deltaRMin = 0.4 ) # # # event.cleanJets, dummy = cleanObjectCollection( event.cleanJets, # masks = [event.taucand], # deltaRMin = 0.4 ) 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') event.cleanJets = event.jets event.cleanBJets = event.bJets # print 'cleanJets = ', len(event.jets) # print 'clean_bJets = ', len(event.bJets) # if len(event.bJets)>0: # if len(event.cleanBJets)>0: # import pdb; pdb.set_trace() # return False self.counters.counter('jets').inc('No b jets') return True
def selectionSequence(self, event, fillCounter, leg1IsoCut=None, leg2IsoCut=None): if fillCounter: self.counters.counter('DiLepton').inc('all events') if len(event.diLeptons) == 0: return False if fillCounter: self.counters.counter('DiLepton').inc('> 0 di-lepton') # testing di-lepton itself selDiLeptons = event.diLeptons if not self.leptonAccept(event.leptons): return False if fillCounter: self.counters.counter('DiLepton').inc('lepton accept') ## for embedded no hltPaths (for now) if not hasattr(event, 'hltPaths'): event.hltPaths = [] matching = {} for trig in event.hltPaths: matching.update({trig: [-99, -99, -99]}) # {trigName:leg1,leg2,jet} event.diLeptonsTrigMatched = [] for trig in event.hltPaths: if self.cfg_comp.isEmbed: continue ## no matching for the embed selDiLeptons = event.diLeptons if len(self.cfg_comp.triggers) > 0: # trigger matching leg1 selDiLeptons = [ diL for diL in selDiLeptons if self.trigMatched(event, diL.leg1(), 'leg1', trig) ] if len(selDiLeptons) == 0: matching[trig][0] = 0 else: if fillCounter: self.counters.counter('DiLepton').inc( 'leg1 trig matched') matching[trig][0] = 1 if len(self.cfg_comp.triggers) > 0: # trigger matching leg2 selDiLeptons = [ diL for diL in selDiLeptons if self.trigMatched(event, diL.leg2(), 'leg2', trig) ] if len(selDiLeptons) == 0: matching[trig][1] = 0 else: if fillCounter: self.counters.counter('DiLepton').inc( 'leg2 trig matched') matching[trig][1] = 1 if len(self.cfg_comp.triggers) > 0 and len( self.cfg_ana.triggerMap[trig]) > 2: # trigger matching jet cmgJets = self.handles['jets'].product() jets = [] for cmgJet in cmgJets: jet = Jet(cmgJet) if self.testJet(jet): jets.append(jet) selDiLeptonsNew = [] for diL in selDiLeptons: cleanJets, dummy = cleanObjectCollection( jets, masks=[diL.leg1(), diL.leg2()], deltaRMin=0.5) if len(cleanJets) > 0 and self.trigMatched( event, cleanJets[0], 'jet', trig): selDiLeptonsNew += [diL] selDiLeptons = selDiLeptonsNew if len(selDiLeptons) == 0: matching[trig][2] = 0 else: if fillCounter: self.counters.counter('DiLepton').inc( 'jet trig matched') matching[trig][2] = 1 event.diLeptonsTrigMatched += selDiLeptons event.diLeptonsTrigMatched = set(event.diLeptonsTrigMatched) ### need unix style wild card to macth different trigger versions in data for trig in matching.keys(): if fnm(trig, 'HLT_DoubleMediumIsoPFTau35_Trk*_eta2p1_v*'): event.l1TrigMatched_diTau = matching[trig][0] event.l2TrigMatched_diTau = matching[trig][1] if fnm(trig, 'HLT_DoubleMediumIsoPFTau*_Trk*_eta2p1_Jet30_v*'): event.l1TrigMatched_diTauJet = matching[trig][0] event.l2TrigMatched_diTauJet = matching[trig][1] event.jetTrigMatched_diTauJet = matching[trig][2] # testing leg1 selDiLeptons = [ diL for diL in selDiLeptons if self.testLeg(diL.leg1()) ] if len(selDiLeptons) == 0: return False else: if fillCounter: self.counters.counter('DiLepton').inc( 'leg1 offline cuts passed') # testing leg2 selDiLeptons = [ diL for diL in selDiLeptons if self.testLeg(diL.leg2()) ] if len(selDiLeptons) == 0: return False else: if fillCounter: self.counters.counter('DiLepton').inc( 'leg2 offline cuts passed') # mass cut selDiLeptons = [diL for diL in selDiLeptons if self.testMass(diL)] if len(selDiLeptons) == 0: return False else: if fillCounter: self.counters.counter('DiLepton').inc( '{min:3.1f} < m < {max:3.1f}'.format( min=self.cfg_ana.m_min, max=self.cfg_ana.m_max)) # exactly one? if len(selDiLeptons) == 0: return False elif len(selDiLeptons) == 1: if fillCounter: self.counters.counter('DiLepton').inc('exactly 1 di-lepton') event.diLepton = self.bestDiLepton(selDiLeptons) event.leg1 = event.diLepton.leg1() event.leg2 = event.diLepton.leg2() ### require trigger bit in Embedded RecHit if self.cfg_comp.isEmbed: if not event.hltPath: return False if "DY" in self.cfg_comp.name or "Higgs" in self.cfg_comp.name or self.cfg_comp.isEmbed: if event.leg1.decayMode() == 0: event.leg1.prongWeight = 0.88 else: event.leg1.prongWeight = 1. if event.leg2.decayMode() == 0: event.leg2.prongWeight = 0.88 else: event.leg2.prongWeight = 1. #import pdb ; pdb.set_trace() return True
def buildLeptonList(self, event): self.hadtaus = [] for ptj in self.handles['taus'].product(): thistau = Tau(ptj) # add only "good" pftaus if thistau.tauID("decayModeFinding") != 1.0: continue if thistau.tauID("byVLooseCombinedIsolationDeltaBetaCorr") <= 0.: continue self.hadtaus.append(Tau(ptj)) self.jets = [] for ptj in self.handles['jets'].product(): # check if they are close to jets and then remove them from the jet list thisjet = Jet(ptj) matched = False # use the following if you want to remove hadtaus from jets # for tau in self.hadtaus: # if deltaR(tau.eta(),tau.phi(),thisjet.eta(),thisjet.phi()) < 0.2: # matched=True # break if not matched: self.jets.append(Jet(ptj)) self.leptons = [] for pte in self.handles['electrons'].product(): self.leptons.append(Lepton(pte)) for ptm in self.handles['muons'].product(): self.leptons.append(Lepton(ptm)) self.photons = [] for ptg in self.handles['photons'].product(): if ptg.energy() > 1.0: self.photons.append(Photon(ptg)) # Find FSR tmpLept = set(self.leptons) for lept in self.leptons: leptonIso = self.leptonIso(lept) if leptonIso > 0.05: tmpLept.remove(lept) continue #if (self.eleele or self.mumu or self.tautau ) : # print self.eleele, self.mumu, self.tautau,lept, leptonIso for i, ph in enumerate(self.photons): dr = deltaR(lept.eta(), lept.phi(), ph.eta(), ph.phi()) if abs(lept.pdgId()) == 13 and dr < 0.4: #print 'found ISR ',ph,lept leph = lept.p4() p4ph = ph.p4() p4 = ph.p4() + lept.p4() lept.setP4(p4) #print 'added ISR ',ph,lept self.leptons = [] for lept in tmpLept: self.leptons.append(lept) self.leptonJets = [] for lept in self.leptons: drmin = 999. ijet = -1 for i, jet in enumerate(self.jets): dr = deltaR(lept.eta(), lept.phi(), jet.eta(), jet.phi()) if dr < drmin: drmin = dr ijet = i if ijet >= 0: if drmin < 0.1: self.jets[ijet].setP4(lept.p4()) self.jets[ijet].setPdgId(lept.pdgId()) self.leptonJets.append(self.jets[ijet])
def process(self, iEvent, event): self.readCollections( iEvent ) cmgJets = self.handles['jets'].product() allJets = [] event.jets = [] event.bJets = [] event.cleanJets = [] event.cleanBJets = [] leptons = event.selectedLeptons 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 genJets: # Use DeltaR = 0.25 matching like JetMET pairs = matchObjectCollection( [jet], genJets, 0.25*0.25) if pairs[jet] is None: pass #jet.genJet = None else: jet.genJet = pairs[jet] # print jet, jet.genJet #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.cleanJets)<2: return True return True