/
nanoaod.py
654 lines (491 loc) · 18.9 KB
/
nanoaod.py
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
"""Mixins for the CMS NanoAOD schema"""
import warnings
import awkward
from dask_awkward import dask_property
from coffea.nanoevents.methods import base, candidate, vector
behavior = {}
behavior.update(base.behavior)
# vector behavior is included in candidate behavior
behavior.update(candidate.behavior)
class _NanoAODEvents(behavior["NanoEvents"]):
def __repr__(self):
return f"<event {getattr(self,'run','??')}:\
{getattr(self,'luminosityBlock','??')}:\
{getattr(self,'event','??')}>"
behavior["NanoEvents"] = _NanoAODEvents
def _set_repr_name(classname):
def namefcn(self):
return classname
# behavior[("__typestr__", classname)] = classname[0].lower() + classname[1:]
behavior[classname].__repr__ = namefcn
@awkward.mixin_class(behavior)
class PtEtaPhiMCollection(vector.PtEtaPhiMLorentzVector, base.NanoCollection):
"""Generic collection that has Lorentz vector properties"""
pass
@awkward.mixin_class(behavior)
class GenParticle(vector.PtEtaPhiMLorentzVector, base.NanoCollection):
"""NanoAOD generator-level particle object, including parent and child self-references
Parent and child self-references are constructed from the ``genPartIdxMother`` column, where
for each entry, the mother entry index is recorded, or -1 if no mother exists.
"""
FLAGS = [
"isPrompt",
"isDecayedLeptonHadron",
"isTauDecayProduct",
"isPromptTauDecayProduct",
"isDirectTauDecayProduct",
"isDirectPromptTauDecayProduct",
"isDirectHadronDecayProduct",
"isHardProcess",
"fromHardProcess",
"isHardProcessTauDecayProduct",
"isDirectHardProcessTauDecayProduct",
"fromHardProcessBeforeFSR",
"isFirstCopy",
"isLastCopy",
"isLastCopyBeforeFSR",
]
"""bit-packed statusFlags interpretations. Use `GenParticle.hasFlags` to query"""
def hasFlags(self, *flags):
"""Check if one or more status flags are set
Parameters
----------
flags : str or list
A list of flags that are required to be set true. If the first argument
is a list, it is expanded and subsequent arguments ignored.
Possible flags are enumerated in the `FLAGS` attribute
Returns a boolean array
"""
if not len(flags):
raise ValueError("No flags specified")
elif isinstance(flags[0], list):
flags = flags[0]
mask = 0
for flag in flags:
mask |= 1 << self.FLAGS.index(flag)
return (self.statusFlags & mask) == mask
@dask_property
def parent(self):
return self._events().GenPart._apply_global_index(self.genPartIdxMotherG)
@parent.dask
def parent(self, dask_array):
return dask_array._events().GenPart._apply_global_index(
dask_array.genPartIdxMotherG
)
@dask_property
def distinctParent(self):
return self._events().GenPart._apply_global_index(self.distinctParentIdxG)
@distinctParent.dask
def distinctParent(self, dask_array):
return dask_array._events().GenPart._apply_global_index(
dask_array.distinctParentIdxG
)
@dask_property
def children(self):
return self._events().GenPart._apply_global_index(self.childrenIdxG)
@children.dask
def children(self, dask_array):
return dask_array._events().GenPart._apply_global_index(dask_array.childrenIdxG)
@dask_property
def distinctChildren(self):
return self._events().GenPart._apply_global_index(self.distinctChildrenIdxG)
@distinctChildren.dask
def distinctChildren(self, dask_array):
return dask_array._events().GenPart._apply_global_index(
dask_array.distinctChildrenIdxG
)
@dask_property
def distinctChildrenDeep(self):
"""Accessor to distinct child particles with different PDG id, or last ones in the chain"""
warnings.warn(
"distinctChildrenDeep may not give correct answers for all generators!"
)
return self._events().GenPart._apply_global_index(self.distinctChildrenDeepIdxG)
@distinctChildrenDeep.dask
def distinctChildrenDeep(self, dask_array):
"""Accessor to distinct child particles with different PDG id, or last ones in the chain"""
warnings.warn(
"distinctChildrenDeep may not give correct answers for all generators!"
)
return dask_array._events().GenPart._apply_global_index(
dask_array.distinctChildrenDeepIdxG
)
_set_repr_name("GenParticle")
@awkward.mixin_class(behavior)
class GenVisTau(candidate.PtEtaPhiMCandidate, base.NanoCollection):
"""NanoAOD visible tau object"""
@dask_property
def parent(self):
"""Accessor to the parent particle"""
return self._events().GenPart._apply_global_index(self.genPartIdxMotherG)
@parent.dask
def parent(self, dask_array):
"""Accessor to the parent particle"""
return dask_array._events().GenPart._apply_global_index(
dask_array.genPartIdxMotherG
)
_set_repr_name("GenVisTau")
@awkward.mixin_class(behavior)
class Electron(candidate.PtEtaPhiMCandidate, base.NanoCollection, base.Systematic):
"""NanoAOD electron object"""
FAIL = 0
"cutBased selection minimum value"
VETO = 1
"cutBased selection minimum value"
LOOSE = 2
"cutBased selection minimum value"
MEDIUM = 3
"cutBased selection minimum value"
TIGHT = 4
"cutBased selection minimum value"
pass
@property
def isVeto(self):
"""Returns a boolean array marking veto cut-based electrons"""
return self.cutBased >= self.VETO
@property
def isLoose(self):
"""Returns a boolean array marking loose cut-based electrons"""
return self.cutBased >= self.LOOSE
@property
def isMedium(self):
"""Returns a boolean array marking medium cut-based electrons"""
return self.cutBased >= self.MEDIUM
@property
def isTight(self):
"""Returns a boolean array marking tight cut-based electrons"""
return self.cutBased >= self.TIGHT
@dask_property
def matched_gen(self):
return self._events().GenPart._apply_global_index(self.genPartIdxG)
@matched_gen.dask
def matched_gen(self, dask_array):
return dask_array._events().GenPart._apply_global_index(dask_array.genPartIdxG)
@dask_property
def matched_jet(self):
return self._events().Jet._apply_global_index(self.jetIdxG)
@matched_jet.dask
def matched_jet(self, dask_array):
return dask_array._events().Jet._apply_global_index(dask_array.jetIdxG)
@dask_property
def matched_photon(self):
return self._events().Photon._apply_global_index(self.photonIdxG)
@matched_photon.dask
def matched_photon(self, dask_array):
return dask_array._events().Photon._apply_global_index(dask_array.photonIdxG)
_set_repr_name("Electron")
@awkward.mixin_class(behavior)
class Muon(candidate.PtEtaPhiMCandidate, base.NanoCollection, base.Systematic):
"""NanoAOD muon object"""
@dask_property
def matched_fsrPhoton(self):
return self._events().FsrPhoton._apply_global_index(self.fsrPhotonIdxG)
@matched_fsrPhoton.dask
def matched_fsrPhoton(self, dask_array):
return dask_array._events().FsrPhoton._apply_global_index(
dask_array.fsrPhotonIdxG
)
@dask_property
def matched_gen(self):
return self._events().GenPart._apply_global_index(self.genPartIdxG)
@matched_gen.dask
def matched_gen(self, dask_array):
return dask_array._events().GenPart._apply_global_index(dask_array.genPartIdxG)
@dask_property
def matched_jet(self):
return self._events().Jet._apply_global_index(self.jetIdxG)
@matched_jet.dask
def matched_jet(self, dask_array):
return dask_array._events().Jet._apply_global_index(dask_array.jetIdxG)
_set_repr_name("Muon")
@awkward.mixin_class(behavior)
class Tau(candidate.PtEtaPhiMCandidate, base.NanoCollection, base.Systematic):
"""NanoAOD tau object"""
@dask_property
def matched_gen(self):
return self._events().GenPart._apply_global_index(self.genPartIdxG)
@matched_gen.dask
def matched_gen(self, dask_array):
return dask_array._events().GenPart._apply_global_index(dask_array.genPartIdxG)
@dask_property
def matched_jet(self):
return self._events().Jet._apply_global_index(self.jetIdxG)
@matched_jet.dask
def matched_jet(self, dask_array):
return dask_array._events().Jet._apply_global_index(dask_array.jetIdxG)
_set_repr_name("Tau")
@awkward.mixin_class(behavior)
class Photon(candidate.PtEtaPhiMCandidate, base.NanoCollection, base.Systematic):
"""NanoAOD photon object"""
LOOSE = 0
"cutBasedBitmap bit position"
MEDIUM = 1
"cutBasedBitmap bit position"
TIGHT = 2
"cutBasedBitmap bit position"
@property
def mass(self):
return 0.0 * self.pt
@property
def isLoose(self):
"""Returns a boolean array marking loose cut-based photons"""
return (self.cutBasedBitmap & (1 << self.LOOSE)) != 0
@property
def isMedium(self):
"""Returns a boolean array marking medium cut-based photons"""
return (self.cutBasedBitmap & (1 << self.MEDIUM)) != 0
@property
def isTight(self):
"""Returns a boolean array marking tight cut-based photons"""
return (self.cutBasedBitmap & (1 << self.TIGHT)) != 0
@dask_property
def matched_electron(self):
return self._events().Electron._apply_global_index(self.electronIdxG)
@matched_electron.dask
def matched_electron(self, dask_array):
return dask_array._events().Electron._apply_global_index(
dask_array.electronIdxG
)
@dask_property
def matched_gen(self):
return self._events().GenPart._apply_global_index(self.genPartIdxG)
@matched_gen.dask
def matched_gen(self, dask_array):
return dask_array._events().GenPart._apply_global_index(dask_array.genPartIdxG)
@dask_property
def matched_jet(self):
return self._events().Jet._apply_global_index(self.jetIdxG)
@matched_jet.dask
def matched_jet(self, dask_array):
return dask_array._events().Jet._apply_global_index(dask_array.jetIdxG)
_set_repr_name("Photon")
@awkward.mixin_class(behavior)
class FsrPhoton(candidate.PtEtaPhiMCandidate, base.NanoCollection):
"""NanoAOD fsr photon object"""
@dask_property
def matched_muon(self):
return self._events().Muon._apply_global_index(self.muonIdxG)
@matched_muon.dask
def matched_muon(self, dask_array):
return dask_array._events().Jet._apply_global_index(dask_array.muonIdxG)
_set_repr_name("FsrPhoton")
@awkward.mixin_class(behavior)
class Jet(vector.PtEtaPhiMLorentzVector, base.NanoCollection, base.Systematic):
"""NanoAOD narrow radius jet object"""
LOOSE = 0
"jetId bit position"
TIGHT = 1
"jetId bit position"
TIGHTLEPVETO = 2
"jetId bit position"
@property
def isLoose(self):
"""Returns a boolean array marking loose jets according to jetId index"""
return (self.jetId & (1 << self.LOOSE)) != 0
@property
def isTight(self):
"""Returns a boolean array marking tight jets according to jetId index"""
return (self.jetId & (1 << self.TIGHT)) != 0
@property
def isTightLeptonVeto(self):
"""Returns a boolean array marking tight jets with explicit lepton veto according to jetId index"""
return (self.jetId & (1 << self.TIGHTLEPVETO)) != 0
@dask_property
def matched_electrons(self):
return self._events().Electron._apply_global_index(self.electronIdxG)
@matched_electrons.dask
def matched_electrons(self, dask_array):
return dask_array._events().Electron._apply_global_index(
dask_array.electronIdxG
)
@dask_property
def matched_muons(self):
return self._events().Muon._apply_global_index(self.muonIdxG)
@matched_muons.dask
def matched_muons(self, dask_array):
return dask_array._events().Muon._apply_global_index(dask_array.muonIdxG)
@dask_property
def matched_gen(self):
return self._events().GenJet._apply_global_index(self.genJetIdxG)
@matched_gen.dask
def matched_gen(self, dask_array):
return dask_array._events().GenJet._apply_global_index(dask_array.genJetIdxG)
@dask_property
def constituents(self):
if "pFCandsIdxG" not in self.fields:
raise RuntimeError("PF candidates are only available for PFNano")
return self._events().JetPFCands._apply_global_index(self.pFCandsIdxG)
@constituents.dask
def constituents(self, dask_array):
if "pFCandsIdxG" not in self.fields:
raise RuntimeError("PF candidates are only available for PFNano")
return dask_array._events().JetPFCands._apply_global_index(
dask_array.pFCandsIdxG
)
_set_repr_name("Jet")
@awkward.mixin_class(behavior)
class FatJet(vector.PtEtaPhiMLorentzVector, base.NanoCollection, base.Systematic):
"""NanoAOD large radius jet object"""
LOOSE = 0
"jetId bit position"
TIGHT = 1
"jetId bit position"
TIGHTLEPVETO = 2
"jetId bit position"
@property
def isLoose(self):
"""Returns a boolean array marking loose jets according to jetId index"""
return (self.jetId & (1 << self.LOOSE)) != 0
@property
def isTight(self):
"""Returns a boolean array marking tight jets according to jetId index"""
return (self.jetId & (1 << self.TIGHT)) != 0
@property
def isTightLeptonVeto(self):
"""Returns a boolean array marking tight jets with explicit lepton veto according to jetId index"""
return (self.jetId & (1 << self.TIGHTLEPVETO)) != 0
@dask_property
def subjets(self):
return self._events().SubJet._apply_global_index(self.subJetIdxG)
@subjets.dask
def subjets(self, dask_array):
return dask_array._events().SubJet._apply_global_index(dask_array.subJetIdxG)
@dask_property
def matched_gen(self):
return self._events().GenJetAK8._apply_global_index(self.genJetAK8IdxG)
@matched_gen.dask
def matched_gen(self, dask_array):
return dask_array._events().GenJetAK8._apply_global_index(
dask_array.genJetAK8IdxG
)
@dask_property
def constituents(self):
if "pFCandsIdxG" not in self.fields:
raise RuntimeError("PF candidates are only available for PFNano")
return self._events().FatJetPFCands._apply_global_index(self.pFCandsIdxG)
@constituents.dask
def constituents(self, dask_array):
if "pFCandsIdxG" not in self.fields:
raise RuntimeError("PF candidates are only available for PFNano")
return dask_array._events().FatJetPFCands._apply_global_index(
dask_array.pFCandsIdxG
)
_set_repr_name("FatJet")
@awkward.mixin_class(behavior)
class MissingET(vector.PolarTwoVector, base.NanoCollection, base.Systematic):
"""NanoAOD Missing transverse energy object"""
@property
def r(self):
return self["pt"]
_set_repr_name("MissingET")
@awkward.mixin_class(behavior)
class Vertex(base.NanoCollection):
"""NanoAOD vertex object"""
@property
def pos(self):
"""Vertex position as a three vector"""
return awkward.zip(
{
"x": self["x"],
"y": self["y"],
"z": self["z"],
},
with_name="ThreeVector",
behavior=self.behavior,
)
_set_repr_name("Vertex")
@awkward.mixin_class(behavior)
class SecondaryVertex(Vertex):
"""NanoAOD secondary vertex object"""
@property
def p4(self):
"""4-momentum vector of tracks associated to this SV"""
return awkward.zip(
{
"pt": self["pt"],
"eta": self["eta"],
"phi": self["phi"],
"mass": self["mass"],
},
with_name="PtEtaPhiMLorentzVector",
behavior=self.behavior,
)
_set_repr_name("SecondaryVertex")
@awkward.mixin_class(behavior)
class AssociatedPFCand(base.NanoCollection):
"""PFNano PF candidate to jet association object"""
collection_map = {
"JetPFCands": ("Jet", "PFCands"),
"FatJetPFCands": ("FatJet", "PFCands"),
"GenJetCands": ("GenJet", "GenCands"),
"GenFatJetCands": ("GenJetAK8", "GenCands"),
}
@dask_property
def jet(self):
collection = self.collection_map[self._collection_name()][0]
return self._events()[collection]._apply_global_index(self.jetIdxG)
@jet.dask
def jet(self, dask_array):
collection = self.collection_map[self._collection_name()][0]
return dask_array.events()[collection]._apply_global_index(dask_array.jetIdxG)
@dask_property
def pf(self):
collection = self.collection_map[self._collection_name()][1]
return self._events()[collection]._apply_global_index(self.pFCandsIdxG)
@pf.dask
def pf(self, dask_array):
collection = self.collection_map[self._collection_name()][1]
return dask_array._events()[collection]._apply_global_index(
dask_array.pFCandsIdxG
)
_set_repr_name("AssociatedPFCand")
@awkward.mixin_class(behavior)
class AssociatedSV(base.NanoCollection):
"""PFNano secondary vertex to jet association object"""
collection_map = {
"JetSVs": ("Jet", "SV"),
"FatJetSVs": ("FatJet", "SV"),
# these two are unclear
"GenJetSVs": ("GenJet", "SV"),
"GenFatJetSVs": ("GenJetAK8", "SV"),
}
@dask_property
def jet(self):
collection = self._events()[self.collection_map[self._collection_name()][0]]
return self._events()[collection]._apply_global_index(self.jetIdxG)
@jet.dask
def jet(self, dask_array):
collection = self._events()[self.collection_map[self._collection_name()][0]]
return dask_array._events()[collection]._apply_global_index(dask_array.jetIdxG)
@dask_property
def sv(self):
collection = self.collection_map[self._collection_name()][1]
return self._events()[collection]._apply_global_index(self.sVIdxG)
@sv.dask
def sv(self, dask_array):
collection = self.collection_map[self._collection_name()][1]
return dask_array._events()[collection]._apply_global_index(dask_array.sVIdxG)
_set_repr_name("AssociatedSV")
@awkward.mixin_class(behavior)
class PFCand(candidate.PtEtaPhiMCandidate, base.NanoCollection):
"""PFNano particle flow candidate object"""
pass
_set_repr_name("PFCand")
__all__ = [
"PtEtaPhiMCollection",
"GenParticle",
"GenVisTau",
"Electron",
"Muon",
"Tau",
"Photon",
"FsrPhoton",
"Jet",
"FatJet",
"MissingET",
"Vertex",
"SecondaryVertex",
"AssociatedPFCand",
"AssociatedSV",
"PFCand",
]