Skip to content

Commit

Permalink
add known forth for 2D vector ElementLinks
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoladze committed Aug 30, 2024
1 parent 3e3e716 commit d4acbdc
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/uproot/interpretation/known_forth/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE

"""
This module defines known forth code for types it is known a priori.
"""
from __future__ import annotations


def known_forth_of(branch):
from uproot.interpretation.known_forth.atlas.element_link import (
vector_vector_element_link,
)

if (
# len(branch.branches) == 0 # don't understand why this goes nuts
# and branch.has_member("fClassName")
branch.has_member("fClassName")
):
typename = branch.member("fClassName").replace(" ", "")
if typename.startswith("vector<vector<ElementLink<DataVector<xAOD::"):
return vector_vector_element_link
5 changes: 5 additions & 0 deletions src/uproot/interpretation/known_forth/atlas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE

"""
This module defines ATLAS specific known forth code
"""
57 changes: 57 additions & 0 deletions src/uproot/interpretation/known_forth/atlas/element_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE

"""
This module defines known forth code for some ElementLink data types in ATLAS (D)AODs
"""

# TODO: delay import?
from __future__ import annotations

from awkward.forms import ListOffsetForm, NumpyForm, RecordForm

vector_vector_element_link = (
"""
input stream
input byteoffsets
input bytestops
output node1-offsets int64
output node2-offsets int64
output node3-data uint32
output node4-data uint32
0 node1-offsets <- stack
0 node2-offsets <- stack
0 do
byteoffsets I-> stack
stream seek
6 stream skip
stream !I-> stack
dup node1-offsets +<- stack
0 do
stream !I-> stack
dup node2-offsets +<- stack
0 do
20 stream skip
stream !I-> node3-data
stream !I-> node4-data
loop
loop
loop
""",
ListOffsetForm(
"i64",
ListOffsetForm(
"i64",
RecordForm(
[
NumpyForm("uint32", form_key="node3"),
NumpyForm("uint32", form_key="node4"),
],
["m_persKey", "m_persIndex"],
),
form_key="node2",
),
form_key="node1",
),
)
13 changes: 11 additions & 2 deletions src/uproot/interpretation/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import uproot
import uproot._awkwardforth
from uproot.interpretation.known_forth import known_forth_of


class AsObjects(uproot.interpretation.Interpretation):
Expand All @@ -45,14 +46,20 @@ class AsObjects(uproot.interpretation.Interpretation):
:ref:`uproot.interpretation.objects.AsObjects.simplify` attempts to
replace this interpretation with a faster-to-read equivalent, but not all
data types can be simplified.
# TODO: known_forth can define forth code and forms for special cases that will be picked up here as well
"""

def __init__(self, model, branch=None):
self._model = model
self._branch = branch
self._form = None
self._forth = True
self._complete_forth_code = None
known_forth = known_forth_of(branch)
if known_forth is not None:
self._complete_forth_code, self._form = known_forth
else:
self._complete_forth_code = None
self._form = None
self._forth_lock = threading.Lock()

@property
Expand Down Expand Up @@ -122,6 +129,8 @@ def awkward_form(
tobject_header=False,
breadcrumbs=(),
):
if self._form is not None: # TODO: is this really fine?
return self._form
context = self._make_context(
context, index_format, header, tobject_header, breadcrumbs
)
Expand Down

0 comments on commit d4acbdc

Please sign in to comment.