5 from ROOT
import TLorentzVector
7 from PhysicsTools.Heppy.analyzers.core.Analyzer
import Analyzer
8 from PhysicsTools.HeppyCore.framework.event
import Event
9 from PhysicsTools.HeppyCore.statistics.counter
import Counter, Counters
10 from PhysicsTools.Heppy.analyzers.core.AutoHandle
import AutoHandle
11 from PhysicsTools.Heppy.physicsobjects.Lepton
import Lepton
12 from PhysicsTools.Heppy.physicsobjects.Photon
import Photon
13 from PhysicsTools.Heppy.physicsobjects.Electron
import Electron
14 from PhysicsTools.Heppy.physicsobjects.Muon
import Muon
15 from PhysicsTools.Heppy.physicsobjects.Jet
import Jet
16 from PhysicsTools.Heppy.physicsobjects.PhysicsObjects
import GenParticle
24 """Do generator-level analysis of a ttH->leptons decay:
27 event.genParticles = the gen particles (pruned, as default)
28 event.genHiggsDecayMode = 0 for non-Higgs
34 event.gentauleps = [ gen electrons and muons from hard scattering not from tau decays ]
35 event.gentaus = [ gen taus from from hard scattering ]
36 event.genleps = [ gen electrons and muons from hard scattering not from tau decays ]
37 event.genbquarks = [ gen b quarks from top quark decays ]
38 event.genwzquarks = [ gen quarks from hadronic W,Z decays ]
40 If filterHiggsDecays is set to a list of Higgs decay modes,
41 it will filter events that have those decay modes.
42 e.g. [0, 15, 23, 24] will keep data, non-Higgs MC and Higgs decays to (tau, Z, W)
43 but will drop Higgs decays to other particles (e.g. bb).
46 def __init__(self, cfg_ana, cfg_comp, looperName ):
47 super(GeneratorAnalyzer,self).
__init__(cfg_ana,cfg_comp,looperName)
48 self.
doPDFWeights = hasattr(self.cfg_ana,
"PDFWeights")
and len(self.cfg_ana.PDFWeights) > 0
60 self.mchandles[
'genParticles'] =
AutoHandle(
'prunedGenParticles',
61 'std::vector<reco::GenParticle>' )
63 self.mchandles[
'pdfstuff'] =
AutoHandle(
'generator',
'GenEventInfoProduct' )
69 """Get the gen level light leptons (prompt and/or from tau decays)"""
71 for i
in xrange( particle.numberOfDaughters() ):
73 dau.sourceId = sourceId
78 if isTau: event.gentauleps.append(dau)
79 else: event.genleps.append(dau)
81 if moid
in [22,23,24]:
82 event.gentaus.append(dau)
84 elif id
in [22,23,24]:
88 """Descend daughters of 'particle', and add quarks from W,Z to event.genwzquarks
89 isWZ is set to True if already processing daughters of W,Z's, to False before it"""
91 for i
in xrange( particle.numberOfDaughters() ):
93 dau.sourceId = sourceId
96 event.genwzquarks.append(dau)
97 elif id
in [22,23,24]:
101 """Get the b quarks from top decays into event.genbquarks"""
103 event.gentopquarks = [ p
for p
in event.genParticles
if abs(p.pdgId()) == 6
and p.numberOfDaughters() > 0
and abs(p.daughter(0).
pdgId()) != 6 ]
107 for tq
in event.gentopquarks:
108 for i
in xrange( tq.numberOfDaughters() ):
110 if abs(dau.pdgId()) == 5:
112 event.genbquarks.append( dau )
113 elif abs(dau.pdgId()) == 24:
118 event.genParticles =
map( GenParticle, self.mchandles[
'genParticles'].product() )
121 for i,p
in enumerate(event.genParticles):
122 print " %5d: pdgId %+5d status %3d pt %6.1f " % (i, p.pdgId(),p.status(),p.pt()),
123 if p.numberOfMothers() > 0:
124 imom, mom = p.motherRef().
key(), p.mother()
125 print " | mother %5d pdgId %+5d status %3d pt %6.1f " % (imom, mom.pdgId(),mom.status(),mom.pt()),
127 print " | no mother particle ",
129 for j
in xrange(
min(3, p.numberOfDaughters())):
130 idau, dau = p.daughterRef(j).
key(), p.daughter(j)
131 print " | dau[%d] %5d pdgId %+5d status %3d pt %6.1f " % (j,idau,dau.pdgId(),dau.status(),dau.pt()),
134 event.genHiggsBoson =
None
136 event.gentauleps = []
138 event.genbquarks = []
139 event.genwzquarks = []
140 event.gentopquarks = []
142 higgsBosons = [ p
for p
in event.genParticles
if (p.pdgId() == 25)
and p.numberOfDaughters() > 0
and abs(p.daughter(0).
pdgId()) != 25 ]
144 if len(higgsBosons) == 0:
145 event.genHiggsDecayMode = 0
154 for i
in xrange(particle.numberOfMothers()):
155 mom = particle.mother(i)
159 def hasDescendent(particle, filter):
160 for i
in xrange(particle.numberOfDaughters()):
161 dau = particle.daughter(i)
162 if filter(dau)
or hasDescendent(dau, filter):
166 bosons = [ gp
for gp
in event.genParticles
if gp.status() > 2
and abs(gp.pdgId())
in [22,23,24] ]
169 if hasDescendent(b,
lambda gp :
abs(gp.pdgId())
in [22,23,24]
and gp.status() > 2):
continue
173 if len(higgsBosons) > 1:
174 print "More than one higgs? \n%s\n" % higgsBosons
177 event.genHiggsDecayMode =
abs( event.genHiggsBoson.daughter(0).
pdgId() )
182 if self.cfg_ana.verbose:
183 print "Higgs boson decay mode: ", event.genHiggsDecayMode
184 print "Generator level prompt light leptons:\n",
"\n".
join([
"\t%s" % p
for p
in event.genleps])
185 print "Generator level light leptons from taus:\n",
"\n".
join([
"\t%s" % p
for p
in event.gentauleps])
186 print "Generator level prompt tau leptons:\n",
"\n".
join([
"\t%s" % p
for p
in event.gentaus])
187 print "Generator level b quarks from top:\n",
"\n".
join([
"\t%s" % p
for p
in event.genbquarks])
188 print "Generator level quarks from W, Z decays:\n",
"\n".
join([
"\t%s" % p
for p
in event.genwzquarks])
190 for p
in event.genParticles:
191 if isPromptLepton(p,
True, includeTauDecays=
True, includeMotherless=
False):
192 if getattr(p,
'sourceId', 0) == 0:
196 event.allBPartons = [ q
for q
in event.genParticles
if abs(q.pdgId()) == 5
and abs(q.status()) == 2
and abs(q.pt()) > 15 ]
197 event.allBPartons.sort(key =
lambda q : q.pt(), reverse =
True)
199 for q
in event.allBPartons:
201 for q2
in event.bPartons:
202 if deltaR(q.eta(),q.phi(),q2.eta(),q2.phi()) < 0.5:
205 if not duplicate: event.bPartons.append(q)
208 from ROOT
import PdfWeightProducerTool
211 for pdf
in self.cfg_ana.PDFWeights:
212 self.pdfWeightTool.addPdfSet(pdf+
".LHgrid")
213 self.pdfWeightTool.beginJob()
217 self.pdfWeightTool.processEvent(self.mchandles[
'pdfstuff'].product())
218 event.pdfWeights = {}
219 for pdf
in self.cfg_ana.PDFWeights:
220 ws = self.pdfWeightTool.getWeights(pdf+
".LHgrid")
221 event.pdfWeights[pdf] = [w
for w
in ws]
225 self.readCollections( iEvent )
233 if not self.cfg_comp.isMC:
241 if self.cfg_ana.filterHiggsDecays:
242 if event.genHiggsDecayMode
not in self.cfg_ana.filterHiggsDecays:
250 setattr(GeneratorAnalyzer,
"defaultConfig",cfg.Analyzer(
251 class_object=GeneratorAnalyzer,
252 filterHiggsDecays =
False,
Abs< T >::type abs(const T &t)
double deltaR(double eta1, double eta2, double phi1, double phi2)
static std::string join(char **cmd)
bool hasAncestor(const reco::GenParticle *particle, int pdgId, int status)
does the particle have an ancestor with this pdgId and this status?