Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for reading free floating std::vector #1180

Merged
merged 12 commits into from
Mar 22, 2024
Merged
12 changes: 11 additions & 1 deletion src/uproot/reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,16 @@ def class_named(self, classname, version=None):
if len(streamers) == 0 and self._custom_classes is not None:
cls = uproot.classes.get(classname)

if (
cls is None
and re.match(r"(std\s*::\s*)?(vector|map|set|bitset)\s*<", classname)
is not None
):
cls = uproot.interpretation.identify.parse_typename(classname)
cls._header = False

return cls

if cls is None:
if len(streamers) == 0:
unknown_cls = uproot.unknown_classes.get(classname)
Expand Down Expand Up @@ -2494,7 +2504,7 @@ def get(self):
start_cursor = cursor.copy()
context = {"breadcrumbs": (), "TKey": self}

if self._fClassName == "string":
if re.match(r"(std\s*::\s*)?string", self._fClassName):
return cursor.string(chunk, context)

cls = self._file.class_named(self._fClassName)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_1180_read_free_floating_vector_issue_1179.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/uproot5/blob/main/LICENSE

import pytest
import uproot
import os
import numpy as np

ROOT = pytest.importorskip("ROOT")


def test_read_free_floating_vector(tmp_path):
newfile = os.path.join(tmp_path, "test_freevec.root")
f = ROOT.TFile(newfile, "recreate")
a = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float64)
c = np.array([0, 1, 4, 5, 6, 7, 8, 9], dtype=np.uint32)
avec = ROOT.std.vector("double")(a)
bvec = ROOT.std.vector("unsigned int")(c)
f.WriteObject(avec, "avec")
f.WriteObject(bvec, "bvec")
f.Write()
f.Close()

with uproot.open(newfile) as f:
assert f["avec"].tolist() == [1.0, 2.0, 3.0, 4.0]
assert f["bvec"].tolist() == [0, 1, 4, 5, 6, 7, 8, 9]