Skip to content

Commit

Permalink
Add RNTupleReade to python bindings and file dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Sep 18, 2023
1 parent 37a2789 commit bda5c9a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
33 changes: 27 additions & 6 deletions python/podio/reading.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env python3
"""Module for general reading functionality."""

from enum import IntEnum

from ROOT import TFile

from podio import root_io
Expand All @@ -23,11 +25,26 @@ def _is_frame_sio_file(filename):
'or there is a version mismatch')


def _is_frame_root_file(filename):
"""Peek into the root file to determine whether this is a legacy file or not."""
class RootFileFormat(IntEnum):
"""Enum to specify the ROOT file format"""
TTREE = 0 # Non-legacy TTree based file
RNTUPLE = 1 # RNTuple based file
LEGACY = 2 # Legacy TTree based file


def _determine_root_format(filename):
"""Peek into the root file to determine which flavor we have at hand."""
file = TFile.Open(filename)
# The ROOT Frame writer puts a podio_metadata TTree into the file
return bool(file.Get('podio_metadata'))

metadata = file.Get("podio_metadata")
if not metadata:
return RootFileFormat.LEGACY

md_class = metadata.IsA().GetName()
if "TTree" in md_class:
return RootFileFormat.TTREE

return RootFileFormat.RNTUPLE


def get_reader(filename):
Expand All @@ -50,8 +67,12 @@ def get_reader(filename):
return sio_io.LegacyReader(filename)

if filename.endswith('.root'):
if _is_frame_root_file(filename):
root_flavor = _determine_root_format(filename)
if root_flavor == RootFileFormat.TTREE:
return root_io.Reader(filename)
return root_io.LegacyReader(filename)
elif root_flavor == RootFileFormat.RNTUPLE:
return root_io.RNTupleReader(filename)
elif root_flavor == RootFileFormat.LEGACY:
return root_io.LegacyReader(filename)

raise ValueError('file must end on .root or .sio')
18 changes: 18 additions & 0 deletions python/podio/root_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ def __init__(self, filenames):
super().__init__()


class RNTupleReader(BaseReaderMixin):
"""Reader class for reading podio RNTuple root files."""

def __init__(self, filenames):
"""Create an RNTuple reader that reads from the passed file(s).
Args:
filenames (str or list[str]): file(s) to open and read data from
"""
if isinstance(filenames, str):
filenames = (filenames,)

self._reader = podio.ROOTNTupleReader()
self._reader.openFiles(filenames)

super().__init__()


class LegacyReader(BaseReaderMixin):
"""Reader class for reading legacy podio root files.
Expand Down

0 comments on commit bda5c9a

Please # to comment.