-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathjetFlavourHelper.py
More file actions
316 lines (251 loc) · 13.3 KB
/
Copy pathjetFlavourHelper.py
File metadata and controls
316 lines (251 loc) · 13.3 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import sys
import json
import ROOT
ROOT.gROOT.SetBatch(True)
class JetFlavourHelper:
'''
NOTE: (May 2025) Once the full sim tagger is retrained on the new naming convention (see https://github.com/key4hep/k4MLJetTagger?tab=readme-ov-file#open-problems--further-work), the names defined here must be altered. Then, they will also nicely match the namings in ReconstructedParticle2Track.
'''
def __init__(self, coll, jet, jetc, tag="", sim_type="fast"):
'''
sim_type: fast or full
'''
# check if sim_type is valid
if sim_type not in ["fast", "full"]:
print("ERROR: sim_type must be either 'fast' or 'full'")
sys.exit()
self.jet = jet
self.const = jetc
self.tag = tag
if tag != "":
self.tag = "_{}".format(tag)
self.sim_type = sim_type
self.particle = coll["GenParticles"]
self.pfcand = coll["PFParticles"]
self.pftrack = coll["PFTracks"]
self.pfphoton = coll["PFPhotons"]
self.pfnh = coll["PFNeutralHadrons"]
self.trackstate = coll["TrackStates"]
self.tracks = coll["Tracks"]
self.trackerhits = coll["TrackerHits"]
self.calohits = coll["CalorimeterHits"]
self.l = coll["PathLength"]
if sim_type == "fast":
self.dndx = coll["dNdx"]
self.bz = coll["Bz"]
elif sim_type == "full":
self.bz = "2.0" # CLD #FIXME: this should be read from the geometry
self.dndx = None
self.primvertex = coll["PV"]
self.definition = dict()
# ===== VERTEX (reconstructed)
self.definition["pv{}".format(self.tag)] = "JetConstituentsUtils::get_primary_vertex({})".format(
self.primvertex
)
# build jet constituents lists
self.definition["pfcand_isMu{}".format(self.tag)] = "JetConstituentsUtils::get_isMu({})".format(self.const)
self.definition["pfcand_isEl{}".format(self.tag)] = "JetConstituentsUtils::get_isEl({})".format(self.const)
self.definition["pfcand_isChargedHad{}".format(self.tag)] = "JetConstituentsUtils::get_isChargedHad({})".format(
self.const
)
self.definition["pfcand_isGamma{}".format(self.tag)] = "JetConstituentsUtils::get_isGamma({})".format(
self.const
)
self.definition["pfcand_isNeutralHad{}".format(self.tag)] = "JetConstituentsUtils::get_isNeutralHad({})".format(
self.const
)
# kinematics, displacement, PID
self.definition["pfcand_e{}".format(self.tag)] = "JetConstituentsUtils::get_e({})".format(self.const)
self.definition["pfcand_p{}".format(self.tag)] = "JetConstituentsUtils::get_p({})".format(self.const)
self.definition["pfcand_theta{}".format(self.tag)] = "JetConstituentsUtils::get_theta({})".format(self.const)
self.definition["pfcand_phi{}".format(self.tag)] = "JetConstituentsUtils::get_phi({})".format(self.const)
self.definition["pfcand_charge{}".format(self.tag)] = "JetConstituentsUtils::get_charge({})".format(self.const)
self.definition["pfcand_type{}".format(self.tag)] = "JetConstituentsUtils::get_type({})".format(self.const)
self.definition["pfcand_erel{}".format(self.tag)] = "JetConstituentsUtils::get_erel_cluster({}, {})".format(
jet, self.const
)
self.definition[
"pfcand_erel_log{}".format(self.tag)
] = "JetConstituentsUtils::get_erel_log_cluster({}, {})".format(jet, self.const)
self.definition[
"pfcand_thetarel{}".format(self.tag)
] = "JetConstituentsUtils::get_thetarel_cluster({}, {})".format(jet, self.const)
self.definition["pfcand_phirel{}".format(self.tag)] = "JetConstituentsUtils::get_phirel_cluster({}, {})".format(
jet, self.const
)
if self.sim_type == "fast":
self.definition["Bz{}".format(self.tag)] = "{}[0]".format(self.bz)
self.definition[
"pfcand_dndx{}".format(self.tag)
] = "JetConstituentsUtils::get_dndx({}, {}, {}, pfcand_isChargedHad{})".format(
self.const, self.dndx, self.pftrack, self.tag
)
self.definition[
"pfcand_mtof{}".format(self.tag)
] = "JetConstituentsUtils::get_mtof({}, {}, {}, {}, {}, {}, {}, pv{})".format(
self.const, self.l, self.pftrack, self.trackerhits, self.pfphoton, self.pfnh, self.calohits, self.tag
)
elif self.sim_type == "full":
self.definition["Bz{}".format(self.tag)] = self.bz
# fill the dNdx and mtof variables with 0
self.definition[
"pfcand_dndx{}".format(self.tag)
] = "JetConstituentsUtils::get_dndx_dummy({})".format(self.const)
self.definition[
"pfcand_mtof{}".format(self.tag)
] = "JetConstituentsUtils::get_mtof_dummy({})".format(self.const)
self.definition["pfcand_dxy{}".format(self.tag)] = "JetConstituentsUtils::XPtoPar_dxy({}, {}, {}, pv{}, Bz{})".format(
self.const, self.trackstate, self.tracks, self.tag, self.tag
)
self.definition["pfcand_dz{}".format(self.tag)] = "JetConstituentsUtils::XPtoPar_dz({}, {}, {}, pv{}, Bz{})".format(
self.const, self.trackstate, self.tracks, self.tag, self.tag
)
self.definition["pfcand_phi0{}".format(self.tag)] = "JetConstituentsUtils::XPtoPar_phi({}, {}, {}, pv{}, Bz{})".format(
self.const, self.trackstate, self.tracks, self.tag, self.tag
)
self.definition["pfcand_C{}".format(self.tag)] = "JetConstituentsUtils::XPtoPar_C({}, {}, {}, Bz{})".format(
self.const, self.trackstate, self.tracks, self.tag
)
self.definition["pfcand_ct{}".format(self.tag)] = "JetConstituentsUtils::XPtoPar_ct({}, {}, {}, Bz{})".format(
self.const, self.trackstate, self.tracks, self.tag
)
# covariance matrix (fixed track state problem)
self.definition["pfcand_dptdpt{}".format(self.tag)] = "JetConstituentsUtils::get_omega_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_dxydxy{}".format(self.tag)] = "JetConstituentsUtils::get_d0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_dzdz{}".format(self.tag)] = "JetConstituentsUtils::get_z0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_dphidphi{}".format(self.tag)] = "JetConstituentsUtils::get_phi0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_detadeta{}".format(self.tag)] = "JetConstituentsUtils::get_tanlambda_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_dxydz{}".format(self.tag)] = "JetConstituentsUtils::get_d0_z0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_dphidxy{}".format(self.tag)] = "JetConstituentsUtils::get_phi0_d0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_phidz{}".format(self.tag)] = "JetConstituentsUtils::get_phi0_z0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_phictgtheta{}".format(self.tag)] = "JetConstituentsUtils::get_tanlambda_phi0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_dxyctgtheta{}".format(self.tag)] = "JetConstituentsUtils::get_tanlambda_d0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_dlambdadz{}".format(self.tag)] = "JetConstituentsUtils::get_tanlambda_z0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_cctgtheta{}".format(self.tag)] = "JetConstituentsUtils::get_omega_tanlambda_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_phic{}".format(self.tag)] = "JetConstituentsUtils::get_omega_phi0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_dxyc{}".format(self.tag)] = "JetConstituentsUtils::get_omega_d0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
self.definition["pfcand_cdz{}".format(self.tag)] = "JetConstituentsUtils::get_omega_z0_cov({}, {}, {})".format(
self.const, self.tracks, self.trackstate
)
# impact parameters
self.definition[
"pfcand_btagSip2dVal{}".format(self.tag)
] = "JetConstituentsUtils::get_Sip2dVal_clusterV({}, pfcand_dxy{}, pfcand_phi0{}, Bz{})".format(
jet, self.tag, self.tag, self.tag
)
self.definition[
"pfcand_btagSip2dSig{}".format(self.tag)
] = 'JetConstituentsUtils::get_Sip2dSig(pfcand_btagSip2dVal{}, pfcand_dxydxy{}, "{}")'.format(self.tag, self.tag, self.sim_type)
self.definition[
"pfcand_btagSip3dVal{}".format(self.tag)
] = "JetConstituentsUtils::get_Sip3dVal_clusterV({}, pfcand_dxy{}, pfcand_dz{}, pfcand_phi0{}, Bz{})".format(
jet, self.tag, self.tag, self.tag, self.tag
)
self.definition[
"pfcand_btagSip3dSig{}".format(self.tag)
] = 'JetConstituentsUtils::get_Sip3dSig(pfcand_btagSip3dVal{}, pfcand_dxydxy{}, pfcand_dzdz{}, "{}")'.format(
self.tag, self.tag, self.tag, self.sim_type
)
self.definition[
"pfcand_btagJetDistVal{}".format(self.tag)
] = "JetConstituentsUtils::get_JetDistVal_clusterV({}, {}, pfcand_dxy{}, pfcand_dz{}, pfcand_phi0{}, Bz{})".format(
jet, self.const, self.tag, self.tag, self.tag, self.tag
)
self.definition[
"pfcand_btagJetDistSig{}".format(self.tag)
] = 'JetConstituentsUtils::get_JetDistSig(pfcand_btagJetDistVal{}, pfcand_dxydxy{}, pfcand_dzdz{}, "{}")'.format(
self.tag, self.tag, self.tag, self.sim_type
)
# count number of particles in the jet
self.definition["jet_nmu{}".format(self.tag)] = "JetConstituentsUtils::count_type(pfcand_isMu{})".format(
self.tag
)
self.definition["jet_nel{}".format(self.tag)] = "JetConstituentsUtils::count_type(pfcand_isEl{})".format(
self.tag
)
self.definition[
"jet_nchad{}".format(self.tag)
] = "JetConstituentsUtils::count_type(pfcand_isChargedHad{})".format(self.tag)
self.definition["jet_ngamma{}".format(self.tag)] = "JetConstituentsUtils::count_type(pfcand_isGamma{})".format(
self.tag
)
self.definition[
"jet_nnhad{}".format(self.tag)
] = "JetConstituentsUtils::count_type(pfcand_isNeutralHad{})".format(self.tag)
def define(self, df):
for var, call in self.definition.items():
df = df.Define(var, call)
return df
def inference(self, jsonCfg, onnxCfg, df):
## extract input variables/score name and ordering from json file
initvars, self.variables, self.scores = [], [], []
f = open(jsonCfg)
data = json.load(f)
for varname in data["pf_features"]["var_names"]:
initvars.append(varname)
self.variables.append("{}{}".format(varname, self.tag))
for varname in data["pf_vectors"]["var_names"]:
initvars.append(varname)
self.variables.append("{}{}".format(varname, self.tag))
for scorename in data["output_names"]:
# self.scores.append(scorename)
# self.scores.append(scorename.replace("jet", "jet{}".format(self.tag)))
self.scores.append("{}{}".format(scorename, self.tag))
f.close()
# convert to tuple
initvars = tuple(initvars)
# check if all variables are defined
for varname in self.variables:
matches = [obs for obs in self.definition.keys() if obs == varname]
if len(matches) != 1:
print("ERROR: {} variables was not defined.".format(varname))
sys.exit()
# check if variables are filled with values - HOW?
self.get_weight_str = "JetFlavourUtils::get_weights(rdfslot_, "
for var in self.variables:
self.get_weight_str += "{},".format(var)
self.get_weight_str = "{})".format(self.get_weight_str[:-1])
from ROOT import JetFlavourUtils
weaver = JetFlavourUtils.setup_weaver(
onnxCfg, # name of the trained model exported
jsonCfg, # .json file produced by weaver during training
initvars,
ROOT.GetThreadPoolSize() if ROOT.GetThreadPoolSize() > 0 else 1,
)
# run inference and cast scores
df = df.Define("MVAVec_{}".format(self.tag), self.get_weight_str)
for i, scorename in enumerate(self.scores):
df = df.Define(scorename, "JetFlavourUtils::get_weight(MVAVec_{}, {})".format(self.tag, i))
return df
def outputBranches(self):
out = self.scores
out += [obs for obs in self.definition.keys() if "jet_" in obs]
return out