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: branch slimming selection #102

Merged
merged 2 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 5 additions & 12 deletions src/hepconvert/copy_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def copy_root(
tqdm = _utils.check_tqdm()
progress_bar = tqdm.tqdm(desc="Trees copied")
progress_bar.reset(total=number_of_items)
for t in trees:
for t in trees: # pylint: disable=too-many-nested-blocks
tree = f[t]
count_branches = get_counter_branches(tree)
kb = filter_branches(tree, keep_branches, drop_branches, count_branches)
Expand Down Expand Up @@ -251,19 +251,12 @@ def copy_root(
)
}
)
for key in group:
if key in kb:
del chunk[key]
for key in group:
if key in kb:
del chunk[key]
if first:
first = False
if drop_branches:
branch_types = {
name: array.type
for name, array in chunk.items()
if name not in drop_branches
}
else:
branch_types = {name: array.type for name, array in chunk.items()}
branch_types = {name: array.type for name, array in chunk.items()}
of.mktree(
tree.name,
branch_types,
Expand Down
4 changes: 3 additions & 1 deletion tests/test_copy_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ def test_keep_branches(tmp_path):
hepconvert.copy_root(
Path(tmp_path) / "drop_branches.root",
skhep_testdata.data_path("uproot-HZZ.root"),
drop_branches=["Jet_*", "MClepton_*"],
keep_branches="MClepton_*",
counter_name=lambda counted: "N" + counted,
force=True,
)
original = uproot.open(skhep_testdata.data_path("uproot-HZZ.root"))

file = uproot.open(Path(tmp_path) / "drop_branches.root")
file["events"].show()
for key in original["events"].keys():
if key.startswith("MClepton_"):
assert key in file["events"].keys()
Expand All @@ -81,6 +82,7 @@ def test_keep_branches(tmp_path):
)
else:
assert key not in file["events"].keys()
file.close()


def test_hepdata_example(tmp_path):
Expand Down
Loading