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

fix: Speed-up readxml by caching key lookup instead of using try/except #1691

Merged
merged 3 commits into from
Nov 10, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/pyhf/readxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,21 @@ def import_root_histogram(rootdir, filename, path, name, filecache=None):
fullpath = str(Path(rootdir).joinpath(filename))
if fullpath not in filecache:
f = uproot.open(fullpath)
filecache[fullpath] = f
keys = set(f.keys(cycle=False))
filecache[fullpath] = (f, keys)
else:
f = filecache[fullpath]
try:
f, keys = filecache[fullpath]

fullname = "/".join([path, name])

if name in keys:
hist = f[name]
except (KeyError, uproot.deserialization.DeserializationError):
fullname = "/".join([path, name])
try:
hist = f[fullname]
except KeyError:
raise KeyError(
f'Both {name} and {fullname} were tried and not found in {fullpath}'
)
elif fullname in keys:
hist = f[fullname]
kratsg marked this conversation as resolved.
Show resolved Hide resolved
else:
raise KeyError(
f'Both {name} and {fullname} were tried and not found in {fullpath}'
)
return hist.to_numpy()[0].tolist(), extract_error(hist)


Expand Down