Skip to content

Instantly share code, notes, and snippets.

@kratsg
Created December 18, 2019 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kratsg/db184f6be63afb6bf8b8690a15abe31b to your computer and use it in GitHub Desktop.
Save kratsg/db184f6be63afb6bf8b8690a15abe31b to your computer and use it in GitHub Desktop.
import uproot
from pathlib import Path
import numpy as np
cwd = Path('.')
out_branches = ['bjets_n']
for input_fname in cwd.glob('*.root*'):
output_fname = Path(f'out_{input_fname.parts[-1]}')
with uproot.recreate(output_fname) as out_file:
in_file = uproot.open(input_fname)
print(f"Opening {input_fname} and writing to {output_fname}")
for tree_name, tree in in_file.items():
print(f" ... processing {tree_name}")
branches = {b:tree[b].interpretation.todtype for b in out_branches}
out_treename = tree_name.split(b';')[0]
out_file[out_treename] = uproot.newtree(branches)
for events in tree.iterate(branches=out_branches, namedecode='utf-8'):
out_file[out_treename].extend(events)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment