Last active
March 19, 2017 12:20
-
-
Save graipher/f4f35d792a97c65c6c458c65c3cc9295 to your computer and use it in GitHub Desktop.
TChain context manager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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