-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenAnalyzer.py
More file actions
238 lines (204 loc) · 9.18 KB
/
GenAnalyzer.py
File metadata and controls
238 lines (204 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel import Collection
from PhysicsTools.NanoAODTools.postprocessing.framework.eventloop import Module
import ROOT
import os
import numpy as np
import itertools
ROOT.PyConfig.IgnoreCommandLineOptions = True
_rootLeafType2rootBranchType = {
'UChar_t': 'b', 'Char_t': 'B', 'UInt_t': 'i', 'Int_t': 'I', 'Float_t': 'F',
'Double_t': 'D', 'ULong64_t': 'l', 'Long64_t': 'L', 'Bool_t': 'O'}
class GenAnalyzer(Module):
def __init__(self, decay, motherName, daughterNames, variables, conjugate, mother_has_antipart, daughter_has_antipart,grandmother=None,skip=False):
self.decay=decay
self.motherName = motherName
self.daughterNames=daughterNames
self.variables=variables
self.conjugate = conjugate
self.mother_antipart=mother_has_antipart
self.daughter_antipart=daughter_has_antipart
self.grandmother=grandmother
self.skip=skip
pass
def beginJob(self):
pdgs= self.decay.split("->")
self.mom_pdg = float(pdgs[0])
self.daughters_pdg_name = { name:float(pdg) for name,pdg in zip(self.daughterNames,pdgs[1].split(","))}
self.cdaughters_pdg=[]
if self.conjugate:
for antipart,pdg in zip(self.daughter_antipart,pdgs[1].split(",")):
if antipart: self.cdaughters_pdg.append(-1*float(pdg))
else: self.cdaughters_pdg.append(float(pdg))
# for name,pdg in zip(self.daughterNames,pdgs[1].split(",")):
# self.daughters_pdg_name[float(pdg)] = name
pass
def endJob(self):
pass
def beginFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
self.out = wrappedOutputTree
for var in self.variables:
if self.grandmother==None:
self.out.branch("%s_%s"%(self.motherName,var),'F')
for name in self.daughterNames:
self.out.branch("%s_%s"%(name,var),'F')
if self.grandmother==None:
self.out.branch("%s_Idx"%(self.motherName),'F')
for name in self.daughterNames:
self.out.branch("%s_Idx"%(name),'F')
pass
def endFile(self, inputFile, outputFile, inputTree, wrappedOutputTree):
pass
def filterBranchNames(self, branches, collection):
out = []
for br in branches:
name = br.GetName()
if not name.startswith(collection + '_'):
continue
out.append(name.replace(collection + '_', ''))
self.branchType[out[-1]] = br.FindLeaf(br.GetName()).GetTypeName()
return out
def fillWithDefaults(self):
if self.grandmother==None:
self.out.fillBranch("%s_Idx" % (self.motherName), -99)
for vr in self.variables:
self.out.fillBranch("%s_%s" % (self.motherName, vr), -99)
self.out.fillBranch("%s_Idx" % (self.motherName), -99)
for daugther in self.daughterNames:
self.out.fillBranch("%s_Idx" % (daugther), -99)
for vr in self.variables:
self.out.fillBranch("%s_%s" % (daugther, vr), -99)
def analyze(self, event):
"""process event, return True (go to next module) or False (fail, go to next event)"""
if not hasattr(event, "nGenPart"): #data
self.fillWithDefaults()
return True
gens = Collection(event, "GenPart")
mom_cands=[]
if self.grandmother!=None:
mom_idx=getattr(event,self.grandmother)
given_mom=gens[mom_idx]
mom_cands.append((mom_idx,given_mom))
if mom_idx<0:
if self.skip:
print "GenAnalyzer: grandmother Idx=-99; Skip evt"
return False
else:
print "GenAnalyzer: grandmother Idx=-99"
self.fillWithDefaults()
return True
else:
for igen,gen in enumerate(gens):
if (getattr(gen,"pdgId")== self.mom_pdg or ( getattr(gen,"pdgId")== -1*self.mom_pdg and self.conjugate)) and (getattr(gen,"status")==62):
mom_cands.append((igen,gen))
if len(mom_cands)>1:
print "GenAnalyzer: more mothers than 1"
if self.skip:
print "GenAnalyzer: skipping evt"
return False
else:
self.fillWithDefaults()
return True
elif len(mom_cands)==0:
print "GenAnalyzer: no mothers"
if self.skip:
return False
else:
print "GenAnalyzer: skipping evt"
self.fillWithDefaults()
return True
mom_cand=mom_cands[0]
daughter_part={}
daughter_idx={}
correct_decay={self.daughters_pdg_name[key]:False for key in self.daughters_pdg_name.keys() }
daughter_label={ikey:[key,self.daughters_pdg_name[key],False] for ikey,key in enumerate(self.daughters_pdg_name.keys())}
conjugate_decay={self.daughters_pdg_name[key]:False for key in self.daughters_pdg_name.keys()}
is_conjugate=False
if (getattr(mom_cand[1],"pdgId")==-1*self.mom_pdg and self.conjugate) or(getattr(mom_cand[1],"pdgId")==self.mom_pdg and self.conjugate and not self.mother_antipart):
is_conjugate=True
if self.conjugate:
for pdg in self.cdaughters_pdg:
conjugate_decay[pdg]=False
not_expected_part=False
nfound_daughters=0
for igen,gen in enumerate(gens):
if mom_cand[0] != getattr(gen,"genPartIdxMother"):
continue
#guard againt part -> part +gamma (photos)
if getattr(gen,"pdgId")==22: continue
if getattr(gen,"pdgId")==getattr(mom_cand[1],"pdgId"):
mom_cand=(igen,gen)
continue;
# end guard
if (getattr(gen,"pdgId") in correct_decay.keys()) and getattr(mom_cand[1],"pdgId")==self.mom_pdg:
daughter_name="skata"
for label_key in daughter_label.keys():
if daughter_label[label_key][1] != getattr(gen,"pdgId"):
continue
if daughter_label[label_key][2]:
continue
daughter_name=daughter_label[label_key][0]
daughter_label[label_key][2]=True
break
#print "daughter name",daughter_name
daughter_part[daughter_name]=gen
correct_decay[daughter_name]=True
daughter_idx[daughter_name]=igen
nfound_daughters+=1
elif (getattr(gen,"pdgId") in conjugate_decay.keys()) and is_conjugate:
sgn=-1
if getattr(gen,"pdgId") in correct_decay.keys(): sgn=1
daughter_name="skata"
for label_key in daughter_label.keys():
if daughter_label[label_key][1] != -1*getattr(gen,"pdgId"):
continue
if daughter_label[label_key][2]:
continue
daughter_name=daughter_label[label_key][0]
daughter_label[label_key][2]=True
break
daughter_part[daughter_name]=gen
conjugate_decay[daughter_name]=True
daughter_idx[daughter_name]=igen
nfound_daughters+=1
else:
not_expected_part=True
print "GenAnalyzer: not expected particle",getattr(gen,"pdgId")
print "expected",correct_decay
if not_expected_part:
print "GenAnalyzer: not expected part"
if self.skip:
print "GenAnalyzer: skipping evt"
return False
else:
self.fillWithDefaults()
return True
if not all(correct_decay) and not is_conjugate:
print "GenAnalyzer: decay not found"
if self.skip:
print "GenAnalyzer: skipping evt"
return False
else:
self.fillWithDefaults()
return True
if not all(conjugate_decay) and is_conjugate:
print "GenAnalyzer: conjugate decay not found"
if self.skip:
print "GenAnalyzer: skipping evt"
return False
else:
fillWithDefaults()
return True
mother_daughter_cands={"mom":mom_cand[1],"daughter":daughter_part}
mother_daughter_idxs={"mom":mom_cand[0],"daughter":daughter_idx}
if self.grandmother==None:
self.out.fillBranch("%s_Idx" % (self.motherName), mother_daughter_idxs['mom'])
for vr in self.variables:
out=getattr(mother_daughter_cands['mom'],vr)
self.out.fillBranch("%s_%s" % (self.motherName, vr), out)
self.out.fillBranch("%s_Idx" % (self.motherName), mother_daughter_idxs['mom'])
for daugther_key in mother_daughter_cands['daughter'].keys():
self.out.fillBranch("%s_Idx" % (daugther_key), mother_daughter_idxs['daughter'][daugther_key])
for vr in self.variables:
out = getattr(mother_daughter_cands['daughter'][daugther_key],vr)
self.out.fillBranch("%s_%s" % (daugther_key, vr), out)
return True