Toggle Branch Status

Hi, I’ve got a seemingly simple goal, but it’s proving difficult.

I would like to write a PyROOT function that takes as an argument a TTree or TChain and a string with a branch name. In the function, I’d like to set all the branch statuses to 0 except for the named branch. Then I’d loop over all the entries in the TTree or TChain to do something (e.g. fill a set or numpy array), restore all the branch statuses to what they were BEFORE the function was called, then return.

My complication is that some of the branches in the TTree may be nontrivial & split. E.g. when I do mytree.Show(0), I get a largeish list of branches, but in mytree.GetListOfBranches() I only get a few, because many of those “Shown” branches are actually just members of a single branch in the List. I’d like my function to work for those member branches and for simple branches.

My real code where the branch is created in the TTree can be seen here: and the composite struct which has the sub-branches is here:

I also tried using TTree::GetBranch to access only a single branch, but again I have trouble with the fact that the branch name is actually for a split member of the struct.


root_numpy [1] enables only the required branches in tree2array [2] and then reverts them all to the original status after creating the numpy array. root_numpy also handles struct branches [3]. If you are using python+numpy and your goal is to make a numpy array from a tree, or some subset of the branches, or even expressions involving branches, then root_numpy might be what you need.

[2] … array.html
[3] …

For my specific purpose, I am actually looking to make a set of the values of a branch. The branch only takes a discrete number of values and I want to count them all. Making a numpy array and then feeding that to a set might take a ton of memory (but that might be ok). I’ll look at how root_numpy does it to see if I can adapt it to a set.


Is that a python set or a std::set?

A python set will probably be much slower than using a numpy array, and it will most definitely consume more memory. You can use a numpy array as a set: