Skip to content

Commit 70262f5

Browse files
authored
feat: add CorrT1METJet to NanoAOD schema and also add zero TrigObj mass (#1480)
* add CorrT1METJet to nanoaod and add zero trigobj mass * make tracing work with form changes? * better implementation using awkward internals * move report def * remove commented out code * add key assertion in tracing * raise warning in case TrigObj mass is present * alias rawmass -> mass only if present
1 parent f58e914 commit 70262f5

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/coffea/nanoevents/schemas/nanoaod.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class NanoAODSchema(BaseSchema):
7474
"LHEPart": "PtEtaPhiMCollection",
7575
"SubGenJetAK8": "PtEtaPhiMCollection",
7676
"SubJet": "PtEtaPhiMCollection",
77+
"CorrT1METJet": "PtEtaPhiMCollection",
7778
# Candidate: lorentz + charge
7879
"Electron": "Electron",
7980
"LowPtElectron": "LowPtElectron",
@@ -314,6 +315,26 @@ def _build_collections(self, field_names, input_contents):
314315
if "Photon_energy" in branch_forms:
315316
branch_forms["Photon_regrEnergy"] = branch_forms.pop("Photon_energy")
316317

318+
# Alias CorrT1METJet_rawPt to CorrT1METJet_pt and CorrT1METJet_rawMass to CorrT1METJet_mass
319+
if "oCorrT1METJet" in branch_forms:
320+
if "CorrT1METJet_pt" not in branch_forms:
321+
branch_forms["CorrT1METJet_pt"] = branch_forms["CorrT1METJet_rawPt"]
322+
if (
323+
"CorrT1METJet_mass" not in branch_forms
324+
and "CorrT1METJet_rawMass" in branch_forms
325+
):
326+
branch_forms["CorrT1METJet_mass"] = branch_forms["CorrT1METJet_rawMass"]
327+
328+
# Add zero mass to the trigger objects
329+
if "oTrigObj" in branch_forms:
330+
if "TrigObj_mass" in branch_forms:
331+
warnings.warn(
332+
"TrigObj_mass branch is present but will be replaced with zeros"
333+
)
334+
branch_forms["TrigObj_mass"] = transforms.zeros_from_offsets_form(
335+
branch_forms["oTrigObj"]
336+
)
337+
317338
output = {}
318339
for name in collections:
319340
mixin = self.mixins.get(name, "NanoCollection")

src/coffea/nanoevents/trace.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,39 @@ def _make_length_zero_one_tracer(
3636
) -> tuple[ak.Array, list]:
3737
form = ak.forms.from_dict(events.attrs["@form"])
3838
buffer_key = events.attrs["@buffer_key"]
39-
buffer_keys = form.expected_from_buffers(buffer_key=buffer_key).keys()
39+
expected_buffer_keys = form.expected_from_buffers(buffer_key=buffer_key).keys()
4040

4141
if length == 0:
42-
buffers = ak.to_buffers(
43-
form.length_zero_array(),
44-
byteorder=ak._util.native_byteorder,
45-
)[2].values()
42+
tmp_array = form.length_zero_array()
4643
elif length == 1:
47-
buffers = ak.to_buffers(
48-
form.length_one_array(),
49-
byteorder=ak._util.native_byteorder,
50-
)[2].values()
44+
tmp_array = form.length_one_array()
5145
else:
5246
raise ValueError("length must be 0 or 1")
5347

48+
def getkey(layout, form, attribute):
49+
return buffer_key(
50+
form_key=form.form_key,
51+
attribute=attribute,
52+
form=form,
53+
)
54+
55+
container = {}
56+
tmp_array._to_buffers(
57+
form, getkey, container, events._layout.backend, ak._util.native_byteorder
58+
)
59+
5460
report = []
5561

5662
def generate(buffer, report, buffer_key):
5763
report.append(buffer_key)
5864
return buffer
5965

60-
container = {}
61-
for key, buffer in zip(buffer_keys, buffers):
66+
for key, buffer in container.items():
6267
container[key] = partial(generate, buffer=buffer, report=report, buffer_key=key)
68+
69+
assert list(container.keys()) == list(
70+
expected_buffer_keys
71+
), "length zero/one array buffer keys do not match the expected ones"
6372
array = ak.from_buffers(
6473
form=form,
6574
length=length,

0 commit comments

Comments
 (0)