SetBranchAddress in PyROOT

I am wondering what would be the proper equivalent in PyROOT for the following code,

    Double_t mcWeightOrg = 0;
    TBranch *b_mcWeightOrg;
    tree->SetBranchAddress("mcWeightOrg", &mcWeightOrg, &b_mcWeightOrg );

In particular, I am more concerned how can I pass pointer-to-pointer in the SetBranchAddress method.

In PyROOT the branches are generally already set for you, as data members of the TTree object.

So for example:

# Assume TFile myrootfile contains a tree called mytree
# Assume the tree called mytree has a branch called foo
t = myrootfile.Get("mytree")
print # This will print the value of foo in entry 0.

This is not the most optimal in terms of performance way. Especially on large root files. For example, [url]TTree::Draw vs loop

And because of that I want to find out how to do SetBranchAddress in PyROOT.


problem with builtin types is that the python ones are not addressable (well, technically they are, but common numbers are cached and so you risk e.g. changing the value of 0., so that isn’t recommended). You either need a double member variable in a struct (using ROOT.AddressOf; see example A.2.2) or array.array(‘d’, [0.]) from module array. I forget whether ROOT.Double works (which is an addressable type derived from the builtin Python float), but don’t think so.

Beyond that, SetBranchAddress() has been pythonized and should be able to figure out most of what you hand to it.

(Oh, and in PyPy, the neat python access runs at native C++ speed, but no-one is interested in that.)


I’m interested in using PyPyROOT (I tried it out when it was new) but I haven’t seen any news recently. Maybe I’ll check it out again.



yes, I know. And that makes one. :slight_smile: More to the point, mgmt higher-up is not interested. So as-is and as far as I’m concerned, the message is to use C++, rather than twiddling details in python, if TTree looping performance is paramount.