Check found in SetBranchStatus

I’m trying to make a pyroot script quit when I branch is missing. In c++ I’d do

unsigned found = 0; 
tree.SetBranchStatus(branch_name, 1, &found); 
if (found != 1) { 
  throw std::runtime_error('bla'); 
}

But this doesn’t seem to work in PyROOT v5-34-05. When I try

found = array.array('I',[0])
tree.SetBranchStatus(branch_name, 1, found)
if (found[0] != 1): 
    raise RuntimeError('bla')

the script runs fine, but it doesn’t suppress the printed warning, and found always gets set to 1.

Hi,

does this do the job:if not hasattr(tree, 'branch_name'): raise RuntimeError('bla')or even:tree.branch_namewhich will raise an AttributeError on non-existence?

I’ll have a look to see why the value isn’t set in the array.

Cheers,
Wim

Hi,

so I don’t observe the same thing as you have, and I don’t notice any difference in behavior between C++ and python. In fact, the code is straightforward (from TTree): if (!nb && !foundInFriend) { if (found==0) Error("SetBranchStatus", "unknown branch -> %s", bname); return; } if (found) *found = nb + foundInFriend;
So the value of “found” is not touched unless some branch is found (i.e. you can’t rely on it being 0 either, unless you input a 0, so be careful using this in a loop). Arguably, the interface would be nicer (I think) if the code set *found = 0 in case of failure. Of course, it most certainly can not and set it to 1 and report the error.

Cheers,
Wim