Skip to content

Commit

Permalink
[ntuple] Pythonize REntry
Browse files Browse the repository at this point in the history
Make it behave like a dictionary that automatically gets the field type
and uses it internally.
  • Loading branch information
hahnjo committed Nov 27, 2024
1 parent d0355ee commit 046da80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@
from ._pyz_utils import MethodTemplateGetter, MethodTemplateWrapper


def _REntry_GetPtr(self, key):
# key can be either a RFieldToken already or a string. In the latter case, get a token to use it twice.
if (
not hasattr(type(key), "__cpp_name__")
or type(key).__cpp_name__ != "ROOT::Experimental::REntry::RFieldToken"
):
key = self.GetToken(key)
fieldType = self.GetTypeName(key)
return self._GetPtr[fieldType](key)


def _REntry_getitem(self, key):
ptr = self.GetPtr(key)
return ptr.get()[0]


def _REntry_setitem(self, key, value):
ptr = self.GetPtr(key)
ptr.get()[0] = value


@pythonization("REntry", ns="ROOT::Experimental")
def pythonize_REntry(klass):
klass._GetPtr = klass.GetPtr
klass.GetPtr = _REntry_GetPtr

klass.__getitem__ = _REntry_getitem
klass.__setitem__ = _REntry_setitem


def _RNTupleModel_CreateBare(*args):
if len(args) >= 1:
raise ValueError("no support for passing explicit RFieldZero")
Expand Down
4 changes: 4 additions & 0 deletions tree/ntuple/v7/test/ntuple_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ def test_write_read(self):
model.MakeField["int"]("f")
writer = RNTupleWriter.Recreate(model, "ntpl", "test_ntuple_py_write_read.root")
entry = writer.CreateEntry()
entry["f"] = 42
writer.Fill(entry)
del writer

reader = RNTupleReader.Open("ntpl", "test_ntuple_py_write_read.root")
self.assertEqual(reader.GetNEntries(), 1)
entry = reader.GetModel().CreateEntry()
reader.LoadEntry(0, entry)
self.assertEqual(entry["f"], 42)

0 comments on commit 046da80

Please sign in to comment.