# Using SetBranchAddress in pyRoot

**URL:** <https://root-forum.cern.ch/t/using-setbranchaddress-in-pyroot/36626>\
**Category:** Newbie\
**Created:** [November 1, 2019, 3:30pm UTC](https://root-forum.cern.ch/t/using-setbranchaddress-in-pyroot/36626 "2019-11-01T15:30:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![diwakar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/2acd7d/32.png) [@diwakar](https://root-forum.cern.ch/u/diwakar)\
**Post date:** [November 1, 2019, 3:30pm UTC](https://root-forum.cern.ch/t/using-setbranchaddress-in-pyroot/36626/1 "2019-11-01T15:30:57Z")

</div>

Hi

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

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

```

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

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

```

But i get the following error:

```auto
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’.

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [November 1, 2019, 8:46pm UTC](https://root-forum.cern.ch/t/using-setbranchaddress-in-pyroot/36626/2 "2019-11-01T20:46:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![diwakar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/2acd7d/32.png) [@diwakar](https://root-forum.cern.ch/u/diwakar)\
**Post date:** [November 4, 2019, 8:56am UTC](https://root-forum.cern.ch/t/using-setbranchaddress-in-pyroot/36626/3 "2019-11-04T08:56:42Z")

</div>

@jfcaron

Thanks for answering.

I am now trying:

```auto
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.

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [November 4, 2019, 11:42am UTC](https://root-forum.cern.ch/t/using-setbranchaddress-in-pyroot/36626/4 "2019-11-04T11:42:21Z")

</div>

what about:

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

```

---

<div class="post-metadata">

**Author:** ![diwakar](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/2acd7d/32.png) [@diwakar](https://root-forum.cern.ch/u/diwakar)\
**Post date:** [November 4, 2019, 1:36pm UTC](https://root-forum.cern.ch/t/using-setbranchaddress-in-pyroot/36626/5 "2019-11-04T13:36:10Z")

</div>

@sbinet

Yes that works. Thanks
