# How to write a TTree for an existing numpy array using pyroot

**URL:** <https://root-forum.cern.ch/t/how-to-write-a-ttree-for-an-existing-numpy-array-using-pyroot/49777>\
**Category:** Newbie\
**Tags:** python\
**Created:** [April 27, 2022, 10:38pm UTC](https://root-forum.cern.ch/t/how-to-write-a-ttree-for-an-existing-numpy-array-using-pyroot/49777 "2022-04-27T22:38:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Radwan\_Parvez](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/radwan_parvez/32/28684_2.png) [@Radwan\_Parvez](https://root-forum.cern.ch/u/Radwan_Parvez)\
**Post date:** [April 27, 2022, 10:38pm UTC](https://root-forum.cern.ch/t/how-to-write-a-ttree-for-an-existing-numpy-array-using-pyroot/49777/1 "2022-04-27T22:38:34Z")

</div>

Hey Root Experts

```auto
from ROOT import *
import numpy as np

file = TFile("tree.root", 'recreate')
tree = TTree("tree_name", "tree title")

px = np.array([1,2,3.5])

tree.Branch("px", px, 'normal/D')

for i in range(len(px)):
    tree.Fill()

file.Write()
file.Close()

```

I’m new to the ROOT community and I know this is very simple thing. But I’m having a hard time storing a numpy array to a TTree object as one of its branch and access the branch. Right now this code should Fill the tree same px array for 3 times. But I want the values of the array in tree not the whole array 3 times.  
What should I do?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 28, 2022, 9:03am UTC](https://root-forum.cern.ch/t/how-to-write-a-ttree-for-an-existing-numpy-array-using-pyroot/49777/2 "2022-04-28T09:03:20Z")

</div>

Hello,

So if I understand correctly, you want to fill a branch of type double with the values contained in a numpy array of doubles.

One way to do it is via RDataFrame, this tutorial shows exactly how to do it:

[https://root.cern/doc/master/df032\_\_MakeNumpyDataFrame\_8py.html](https://root.cern/doc/master/df032__MakeNumpyDataFrame_8py.html)

Another (more complicated) way is to do the loop in Python as you were doing. But when you define the branch, you need to pass a numpy array of just one position.

```auto
px = np.array([0], dtype=np.float64)

tree.Branch("px", px, 'normal/D')

```

and then inside the loop you would need to assign a new value to that position in every iteration, so that a single double is filled at each iteration:

```auto
for i in range(num_elems):
    px[0] = current_elem
    tree.Fill()

```

---

<div class="post-metadata">

**Author:** ![Radwan\_Parvez](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/radwan_parvez/32/28684_2.png) [@Radwan\_Parvez](https://root-forum.cern.ch/u/Radwan_Parvez)\
**Post date:** [May 25, 2022, 9:04pm UTC](https://root-forum.cern.ch/t/how-to-write-a-ttree-for-an-existing-numpy-array-using-pyroot/49777/3 "2022-05-25T21:04:50Z")

</div>

I forgot to thank you.  
Thank you so much.  
The RDataFrame tutorial didn’t work for me. It throws this error:

 ![Screen Shot 2022-05-25 at 2.39.15 PM](https://root-forum.cern.ch/uploads/default/original/3X/a/7/a717b9b7aa0915a8be29fce35a5debfe97ca5bc5.png)

---

<div class="post-metadata">

**Author:** ![Radwan\_Parvez](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/radwan_parvez/32/28684_2.png) [@Radwan\_Parvez](https://root-forum.cern.ch/u/Radwan_Parvez)\
**Post date:** [May 25, 2022, 9:13pm UTC](https://root-forum.cern.ch/t/how-to-write-a-ttree-for-an-existing-numpy-array-using-pyroot/49777/4 "2022-05-25T21:13:32Z")

</div>

But, I’m trying to work with the other solution. It worked but my problem also changed a bit.  
I need to create a tree with multiple branches, but the branch length is not same for all. To do that, I wrote this:

```auto
a = np.array([1,2,3.2])
b = np.array([1,2,3.2, 5])

file = TFile("tree.root", 'recreate')
tree = TTree("tree_name", "tree title")
# Placeholder variable
ax = np.array([0], dtype=np.float64)
bx = np.array([0], dtype=np.float64)
# Creating branches
tree.Branch("a", ax, 'normal/D')
tree.Branch("b", bx, 'normal/D')

for i in a:
    print(i)
    ax[0] = i
    tree.Fill()

for i in b:
    print(i)
    bx[0] = i
    tree.Fill()

file.Write()
file.Close()

type or paste code here

```

It’s not quite right as I’m using tree.Fill() two times, and I guess the branch ‘a’ also gets filled partially for the second time I call tree.Fill().

 ![Screen Shot 2022-05-25 at 4.12.36 PM](https://root-forum.cern.ch/uploads/default/original/3X/b/8/b81068737e8316ce28b42593e6024f1c1f1f7e62.png)  
 ![Screen Shot 2022-05-25 at 4.12.55 PM](https://root-forum.cern.ch/uploads/default/original/3X/4/3/4370eb6533ba92b27d17eb805f37d26019cd5edd.jpeg)

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 13, 2022, 9:41am UTC](https://root-forum.cern.ch/t/how-to-write-a-ttree-for-an-existing-numpy-array-using-pyroot/49777/5 "2022-06-13T09:41:50Z")

</div>

This is probably due to the ROOT version you are using, could you try updating to a more recent one (e.g. 6.24 or 6.26)?

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 13, 2022, 9:44am UTC](https://root-forum.cern.ch/t/how-to-write-a-ttree-for-an-existing-numpy-array-using-pyroot/49777/6 "2022-06-13T09:44:24Z")

</div>

Yes indeed, if you use this alternative, the second fill will also fill the `a` branch. You need to have one loop where at every iteration you assign the values for `a` and `b` and you fill an entry with those values.
