Using SetBranchAddress in pyRoot

Hi

In C++ ,in order to assign the value of a branch to a variable i would write something like this:

float data_tau1_pt;
data_tree->SetBranchAddress("pt_1",&data_tau1_pt);

I am now using pyroot and wrote the corresponding code like this:

data_tau1_pt = float()
data_tree.SetBranchAddress("pt_1",data_tau1_pt)

But i get the following error:

TypeError: none of the 3 overloaded methods succeeded. Full details:
  Int_t TTree::SetBranchAddress(const char* bname, void** add, TBranch** ptr = 0) =>
    could not convert argument 2
  Int_t TTree::SetBranchAddress(const char* bname, void* add, TClass* realClass, EDataType datatype, Bool_t isptr) =>
    takes at least 5 arguments (2 given)
  Int_t TTree::SetBranchAddress(const char* bname, void* add, TBranch** ptr, TClass* realClass, EDataType datatype, Bool_t isptr) =>
    takes at least 6 arguments (2 given)

As per my understanding passing TBranch is optional. So the error is with the variable to which the value is to be assigned, in my case with ‘data_tau1_pt’.

In python “float” is a builtin python function to convert stuff to float, it’s not a type like in C++. I don’t know exactly what float() (with no argument) means, but it just returns the value 0.0.

You probably want to use something like ROOT.Double() or an array.array(“d”,[0]) or a numpy array in order to properly use SetBranchAddress.

Jean-François

@jfcaron

Thanks for answering.

I am now trying:

w_tau1_pt = array('f',[0])
b_w_tau1_pt = ROOT.TBranch()
w_tree.SetBranchAddress("pt_1",w_tau1_pt[0], b_w_tau1_pt)

Removing the third argument of TBranch has no effect, as i expected.But i still get the same error as before.

what about:

w_tau1_pt = array('f',[0])
w_tree.SetBranchAddress("pt_1",w_tau1_pt)

@sbinet

Yes that works. Thanks