Skip to content

Instantly share code, notes, and snippets.

@graipher
Last active March 19, 2017 12:20
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 graipher/f4f35d792a97c65c6c458c65c3cc9295 to your computer and use it in GitHub Desktop.
Save graipher/f4f35d792a97c65c6c458c65c3cc9295 to your computer and use it in GitHub Desktop.
TChain context manager
import ROOT
class TChain(ROOT.TChain):
"""
A contextmanager for TChain, which loads from a file directly.
Can disable all branches except the needed branches (default: all enabled).
"""
def __init__(self, tree_name, file_names=None, active_branches=None):
super(TChain, self).__init__(tree_name)
self.file_names = file_names
if active_branches is not None:
self.SetBranchStatus("*", False)
for branch in active_branches:
self.SetBranchStatus(branch, True)
def __enter__(self):
for file_name in self.file_names:
self.Add(file_name)
return self
def __exit__(self, *args):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment