def fillHiggsBQuarks(self, event,h): """Get the b quarks from top decays into event.genbquarksFromH""" for i in xrange( h.numberOfDaughters() ): dau = GenParticle(h.daughter(i)) if abs(dau.pdgId()) in [3,4,5]: event.genbquarksFromH.append( dau ) if dau.numberOfDaughters() == 1 : event.genbquarksFromHafterISR.append( GenParticle(dau.daughter(0)))
def allDaughters(particle, daughters, rank ): '''Fills daughters with all the daughters of particle. Recursive function.''' rank += 1 for i in range( particle.numberOfDaughters() ): dau = GenParticle(particle.daughter(i)) dau.rank = rank daughters.append( dau ) daughters = allDaughters( dau, daughters, rank ) return daughters
def fillWZQuarks(self, event, particle, isWZ=False, sourceId=25): """Descend daughters of 'particle', and add quarks from W,Z to event.genwzquarks isWZ is set to True if already processing daughters of W,Z's, to False before it""" for i in xrange( particle.numberOfDaughters() ): dau = GenParticle(particle.daughter(i)) dau.sourceId = sourceId id = abs(dau.pdgId()) if id <= 5 and isWZ: event.genwzquarks.append(dau) elif id in [22,23,24]: self.fillWZQuarks(event, dau, True, sourceId)
def fillTopQuarks(self, event): """Get the b quarks from top decays into event.genbquarksFromTop""" event.gentopquarks = [ p for p in event.genParticles if abs(p.pdgId()) == 6 and p.numberOfDaughters() > 0 and abs(p.daughter(0).pdgId()) != 6 ] #if len(event.gentopquarks) != 2: # print "Not two top quarks? \n%s\n" % event.gentopquarks for tq in event.gentopquarks: for i in xrange( tq.numberOfDaughters() ): dau = GenParticle(tq.daughter(i)) if abs(dau.pdgId()) == 5: dau.sourceId = 6 event.genbquarksFromTop.append( dau ) elif abs(dau.pdgId()) == 24: self.fillGenLeptons( event, dau, sourceId=6 ) self.fillWZQuarks( event, dau, True, sourceId=6 )
def findFirstDaughterGivenPdgId(self, givenMother, givenPdgIds): """Get the first gen level daughter particle with given pdgId""" mothers = [] if givenMother: mothers.append(givenMother) while len(mothers) > 0: daughters = [] for mother in mothers: for idxDaughter in range(mother.numberOfDaughters()): daughter = GenParticle(mother.daughter(idxDaughter)) daughters.append(daughter) for daughter in daughters: if daughter.pdgId() in givenPdgIds: return daughter mothers = daughters # no daughter particle with given pdgId found return None
def fillGenLeptons(self, event, particle, isTau=False, recovered=False, sourceId=25): """Get the gen level light leptons (prompt and/or from tau decays)""" for i in xrange( particle.numberOfDaughters() ): dau = GenParticle(particle.daughter(i)) dau.sourceId = sourceId dau.isTau = isTau id = abs(dau.pdgId()) moid = abs(dau.mother().pdgId()) if dau.mother() else 2212 #if no mom, let say it is a proton (consistent with CMSSW < 74X) if id in [11,13]: if not recovered: if isTau: event.gentauleps.append(dau) else: event.genleps.append(dau) else: if isTau: event.gentaulepsRecovered.append(dau) else: event.genlepsRecovered.append(dau) elif id == 15: if not recovered: if moid in [22,23,24]: event.gentaus.append(dau) self.fillGenLeptons(event, dau, True, sourceId) else: if moid in [22,23,24]: event.gentausRecovered.append(dau) self.fillGenLeptons(event, dau, True, True, sourceId) elif id in [22,23,24]: if not recovered: self.fillGenLeptons(event, dau, False, sourceId) else: self.fillGenLeptons(event, dau, False, True, sourceId)
def fillTopQuarks(self, event): """Get the b quarks from top decays into event.genbquarksFromTop""" event.gentopquarks = [ p for p in event.genParticles if abs(p.pdgId()) == 6 and p.numberOfDaughters() > 0 and abs(p.daughter(0).pdgId()) != 6 ] #Find the top decay mode (0 - leptonic, 1 - hadronic) for top in event.gentopquarks: ndaus = top.numberOfDaughters() top.decayMode = -1 #go over top daughters for idau in range(ndaus): dau = top.daughter(idau) #found the W if abs(dau.pdgId()) == 24: #find the true daughters of the W (in case of decay chain) W_daus = realGenDaughters(dau) decayMode = -1 #go over the daughters of the W for idauw in range(len(W_daus)): w_dau_id = abs(W_daus[idauw].pdgId()) #leptonic if w_dau_id in [11,12,13,14,15,16]: decayMode = 0 break #hadronic elif w_dau_id < 6: decayMode = 1 break top.decayMode = decayMode break for tq in event.gentopquarks: for i in xrange( tq.numberOfDaughters() ): dau = GenParticle(tq.daughter(i)) if abs(dau.pdgId()) == 5: dau.sourceId = 6 event.genbquarksFromTop.append( dau ) elif abs(dau.pdgId()) == 24: self.fillGenLeptons( event, dau, sourceId=6 ) self.fillWZQuarks( event, dau, True, sourceId=6 )
def makeMCInfo(self, event): event.genParticles = map(GenParticle, self.mchandles['genParticles'].product()) if False: for i, p in enumerate(event.genParticles): print " %5d: pdgId %+5d status %3d pt %6.1f " % ( i, p.pdgId(), p.status(), p.pt()), if p.numberOfMothers() > 0: imom, mom = p.motherRef().key(), p.mother() print " | mother %5d pdgId %+5d status %3d pt %6.1f " % ( imom, mom.pdgId(), mom.status(), mom.pt()), else: print " | no mother particle ", for j in xrange(min(3, p.numberOfDaughters())): idau, dau = p.daughterRef(j).key(), p.daughter(j) print " | dau[%d] %5d pdgId %+5d status %3d pt %6.1f " % ( j, idau, dau.pdgId(), dau.status(), dau.pt()), print "" event.genHiggsBoson = None event.genleps = [] event.gentauleps = [] event.gentaus = [] event.genbquarks = [] event.genwzquarks = [] event.gentopquarks = [] higgsBosons = [ p for p in event.genParticles if (p.pdgId() == 25) and p.numberOfDaughters() > 0 and abs(p.daughter(0).pdgId()) != 25 ] if len(higgsBosons) == 0: event.genHiggsDecayMode = 0 ## Matching that can be done also on non-Higgs events ## First, top quarks self.fillTopQuarks(event) self.countBPartons(event) ## Then W,Z,gamma from hard scattering and that don't come from a top and don't rescatter def hasAncestor(particle, filter): for i in xrange(particle.numberOfMothers()): mom = particle.mother(i) if filter(mom) or hasAncestor(mom, filter): return True return False def hasDescendent(particle, filter): for i in xrange(particle.numberOfDaughters()): dau = particle.daughter(i) if filter(dau) or hasDescendent(dau, filter): return True return False bosons = [ gp for gp in event.genParticles if gp.status() > 2 and abs(gp.pdgId()) in [22, 23, 24] ] for b in bosons: if hasAncestor(b, lambda gp: abs(gp.pdgId()) == 6): continue if hasDescendent( b, lambda gp: abs(gp.pdgId()) in [22, 23, 24] and gp. status() > 2): continue self.fillGenLeptons(event, b, sourceId=abs(b.pdgId())) self.fillWZQuarks(event, b, isWZ=True, sourceId=abs(b.pdgId())) else: if len(higgsBosons) > 1: print "More than one higgs? \n%s\n" % higgsBosons event.genHiggsBoson = GenParticle(higgsBosons[-1]) event.genHiggsDecayMode = abs( event.genHiggsBoson.daughter(0).pdgId()) self.fillTopQuarks(event) self.countBPartons(event) self.fillWZQuarks(event, event.genHiggsBoson) self.fillGenLeptons(event, event.genHiggsBoson, sourceId=25) if self.cfg_ana.verbose: print "Higgs boson decay mode: ", event.genHiggsDecayMode print "Generator level prompt light leptons:\n", "\n".join( ["\t%s" % p for p in event.genleps]) print "Generator level light leptons from taus:\n", "\n".join( ["\t%s" % p for p in event.gentauleps]) print "Generator level prompt tau leptons:\n", "\n".join( ["\t%s" % p for p in event.gentaus]) print "Generator level b quarks from top:\n", "\n".join( ["\t%s" % p for p in event.genbquarks]) print "Generator level quarks from W, Z decays:\n", "\n".join( ["\t%s" % p for p in event.genwzquarks]) # make sure prompt leptons have a non-zero sourceId for p in event.genParticles: if isPromptLepton(p, True, includeTauDecays=True, includeMotherless=False): if getattr(p, 'sourceId', 0) == 0: p.sourceId = 99
def makeMCInfo(self, event): LHE_weights_scale = [] LHE_weights_pdf = [] # https://twiki.cern.ch/twiki/bin/viewauth/CMS/LHEReaderCMSSW) for w in event.LHE_weights: wid = int(w.id) #print wid, ": ", w.wgt # for LO samples, the scale weights are 1,...,9; for NLO 1001,1009 if (wid in range(1,10)) or (wid in range(1001,1010)): wid = wid%1000 if wid not in [1,6,8]: # these are the nominal and anticorrelated muR,muF variations w.wgt = w.wgt/event.LHE_originalWeight if abs(event.LHE_originalWeight)>0 else w.wgt w.order = LHE_scale_order[wid] #print int(w.id), ": ", w.wgt, " (pos ", w.order, ")" LHE_weights_scale.append(w) # thse are up/down variations for the NNPDF: https://twiki.cern.ch/twiki/bin/view/CMS/TTbarHbbRun2ReferenceAnalysisLimits if wid in LHE_pdf_ids: #print w.wgt w.wgt = w.wgt/event.LHE_originalWeight if abs(event.LHE_originalWeight)>0 else w.wgt LHE_weights_pdf.append(w) event.LHE_weights_scale = sorted(LHE_weights_scale, key=lambda w : w.order) event.LHE_weights_pdf = LHE_weights_pdf # event.genParticles = map( GenParticle, self.mchandles['genParticles'].product() ) event.genParticles = list(self.mchandles['genParticles'].product() ) if False: for i,p in enumerate(event.genParticles): print " %5d: pdgId %+5d status %3d pt %6.1f " % (i, p.pdgId(),p.status(),p.pt()), if p.numberOfMothers() > 0: imom, mom = p.motherRef().key(), p.mother() print " | mother %5d pdgId %+5d status %3d pt %6.1f " % (imom, mom.pdgId(),mom.status(),mom.pt()), else: print " | no mother particle ", for j in xrange(min(3, p.numberOfDaughters())): idau, dau = p.daughterRef(j).key(), p.daughter(j) print " | dau[%d] %5d pdgId %+5d status %3d pt %6.1f " % (j,idau,dau.pdgId(),dau.status(),dau.pt()), print "" event.genHiggsBosons = [] event.genHiggsSisters = [] event.genleps = [] event.gentauleps = [] event.gentaus = [] event.genlepsRecovered = [] event.gentaulepsRecovered = [] event.gentausRecovered = [] event.genbquarksFromTop = [] event.genbquarksFromH = [] event.genbquarksFromHafterISR = [] event.genallbquarks = [] event.genwzquarks = [] event.gentopquarks = [] event.genallstatus2bhadrons = [ p for p in event.genParticles if p.status() ==2 and self.hasBottom(p.pdgId()) ] event.genallcquarks = [ p for p in event.genParticles if abs(p.pdgId()) == 4 and ( p.numberOfDaughters() == 0 or abs(p.daughter(0).pdgId()) != 4) ] # aggiunti da me #event.genHiggsToBB = [ p for p in event.genParticles if abs(p.pdgId())==25 and p.numberOfDaughters()==2 and abs(p.daughter(0).pdgId()) == 5 ] #event.genvbosonsToLL = [ p for p in event.genParticles if abs(p.pdgId()) in [23,24] and abs(p.mother().pdgId()) in [23,24] and p.numberOfDaughters()==2 and abs(p.daughter(0).pdgId()) in [11,13,15] ] #event.genZbosonsToLL = [ p for p in event.genParticles if abs(p.pdgId()) in [23] and abs(p.daughter(0).pdgId())!= abs(p.pdgId()) ] #event.genWbosonsToLL = [ p for p in event.genParticles if abs(p.pdgId()) in [24] and abs(p.daughter(0).pdgId())!= abs(p.pdgId()) ] event.genvbosons = [ p for p in event.genParticles if abs(p.pdgId()) in [23,24] and p.numberOfDaughters()>0 and abs(p.daughter(0).pdgId()) != abs(p.pdgId()) and p.mass() > 30 ] event.genvbosonsRecovered=[] if not event.genvbosons: if abs(event.genParticles[4].pdgId()) in [11,12,13,14,15,16] and abs(event.genParticles[4].pdgId())==abs(event.genParticles[5].pdgId()): l1=event.genParticles[4] l2=event.genParticles[5] V=GenParticle(0, l1.p4()+l2.p4(), l1.vertex(), 23, -123, 0) V.addDaughter(l1.daughterRef(0).motherRef(0)) V.addDaughter(l2.daughterRef(0).motherRef(0)) event.genvbosonsRecovered.append(V) #bosons = [ gp for gp in event.genParticles if gp.status() > 2 and abs(gp.pdgId()) in [22,23,24] ] for b in event.genvbosons: if b.numberOfDaughters()>0 : self.fillGenLeptons(event, b, sourceId=abs(b.pdgId())) #selezione su leptoni fatta dentro la funzione stessa self.fillWZQuarks(event, b, isWZ=True, sourceId=abs(b.pdgId())) for b in event.genvbosonsRecovered: if b.numberOfDaughters()>0 : self.fillGenLeptons(event, b, recovered=True, sourceId=abs(b.pdgId())) higgsBosons = [ p for p in event.genParticles if (p.pdgId() == 25) and p.numberOfDaughters() > 0 and abs(p.daughter(0).pdgId()) != 25 ] higgsBosonsFirst = [ p for p in event.genParticles if (p.pdgId() == 25) and p.numberOfMothers() > 0 and abs(p.mother(0).pdgId()) != 25 ] higgsMothers = [x.mother(0) for x in higgsBosonsFirst] #print higgsMothers event.genHiggsSisters = [p for p in event.genParticles if p.mother(0) in higgsMothers and p.pdgId() != 25 ] #print "higgsBosons: ", len(higgsBosons) #print "higgsBosonsFirst: ", len(higgsBosonsFirst) #print "higgsMothers: ", len(higgsMothers) if len(higgsBosons) == 0: event.genHiggsDecayMode = 0 ## Matching that can be done also on non-Higgs events ## First, top quarks self.fillTopQuarks( event ) self.countBPartons( event ) ## Then W,Z,gamma from hard scattering and that don't come from a top and don't rescatter def hasAncestor(particle, filter): for i in xrange(particle.numberOfMothers()): mom = particle.mother(i) if filter(mom) or hasAncestor(mom, filter): return True return False def hasDescendent(particle, filter): for i in xrange(particle.numberOfDaughters()): dau = particle.daughter(i) if filter(dau) or hasDescendent(dau, filter): return True return False """ bosons = [ gp for gp in event.genParticles if gp.status() > 2 and abs(gp.pdgId()) in [22,23,24] ] for b in bosons: if hasAncestor(b, lambda gp : abs(gp.pdgId()) == 6): continue if hasDescendent(b, lambda gp : abs(gp.pdgId()) in [22,23,24] and gp.status() > 2): continue self.fillGenLeptons(event, b, sourceId=abs(b.pdgId())) self.fillWZQuarks(event, b, isWZ=True, sourceId=abs(b.pdgId())) """ else: # if len(higgsBosons) > 1: # print "More than one higgs? \n%s\n" % higgsBosons #questo blocco viene eseguito quando c'e' almeno un higgs event.genHiggsBoson = higgsBosons[-1] #event.genHiggsBoson = [GenParticle(higgsBosons[-1])] event.genHiggsBosons = higgsBosons event.genHiggsDecayMode = abs( event.genHiggsBoson.daughter(0).pdgId() if event.genHiggsBoson.numberOfDaughters() >= 1 else 0) event.genHiggsDecayMode += abs( event.genHiggsBoson.daughter(1).pdgId() if event.genHiggsBoson.numberOfDaughters() >= 2 and abs( event.genHiggsBoson.daughter(1).pdgId() ) != abs( event.genHiggsBoson.daughter(0).pdgId()) else 0) * 10000 self.fillTopQuarks( event ) self.countBPartons( event ) #self.fillWZQuarks( event, event.genHiggsBoson ) #self.fillWZQuarks( event, event.protons[0], sourceId=2212) : non serve, quando c'e' higgs non ci sn quarks da WZ for h in event.genHiggsBosons : self.fillHiggsBQuarks( event, h) #event.genHiggsBoson = [GenParticle(higgsBosons[-1])] #self.fillGenLeptons( event, event.genHiggsBoson, sourceId=25 ) #if self.cfg_ana.verbose: if False: print "Higgs boson decay mode: ", event.genHiggsDecayMode print "Generator level prompt light leptons:\n", "\n".join(["\t%s" % p for p in event.genleps]) print "Generator level light leptons from taus:\n", "\n".join(["\t%s" % p for p in event.gentauleps]) print "Generator level prompt tau leptons:\n", "\n".join(["\t%s" % p for p in event.gentaus]) print "Generator level b quarks from top:\n", "\n".join(["\t%s" % p for p in event.genbquarksFromTop]) print "Generator level quarks from W, Z decays:\n", "\n".join(["\t%s" % p for p in event.genwzquarks]) # make sure prompt leptons have a non-zero sourceId for p in event.genParticles: if isPromptLepton(p, True, includeTauDecays=True, includeMotherless=False): if getattr(p, 'sourceId', 0) == 0: p.sourceId = 99