# Filling the branches of a tree in PyROOT

**URL:** <https://root-forum.cern.ch/t/filling-the-branches-of-a-tree-in-pyroot/48702>\
**Category:** Newbie\
**Tags:** tree, python\
**Created:** [February 7, 2022, 5:01pm UTC](https://root-forum.cern.ch/t/filling-the-branches-of-a-tree-in-pyroot/48702 "2022-02-07T17:01:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daumann](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Daumann](https://root-forum.cern.ch/u/Daumann)\
**Post date:** [February 7, 2022, 5:01pm UTC](https://root-forum.cern.ch/t/filling-the-branches-of-a-tree-in-pyroot/48702/1 "2022-02-07T17:01:50Z")

</div>

Hi,

I wrote a simple script in python where I tried to fill a tree branch with an array that has two entries, but only the first entry in filled into the tree branch. The code is the following:

```auto
import ROOT as root
import numpy as np
from array import array

file = root.TFile.Open("tree_tester.root", "RECREATE")

tree = root.TTree("tree","tree")

myvar = array('i')

myvar.append(2)
myvar.append(4)

tree.Branch('myvar', myvar, 'myvar/I')

tree.Fill()
tree.Write()

```

So, how can I create and fill a tree branch with all the array elements?

---

<div class="post-metadata">

**Author:** ![Daumann](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Daumann](https://root-forum.cern.ch/u/Daumann)\
**Post date:** [February 7, 2022, 7:02pm UTC](https://root-forum.cern.ch/t/filling-the-branches-of-a-tree-in-pyroot/48702/2 "2022-02-07T19:02:08Z")

</div>

Hi i solved this with:

```auto
myvar = array('d', [0])
tree.Branch('myvar', myvar, 'myvar/D')

for i in range(10):
    myvar[0] = float(i**2)
    tree.Fill()

```

But I have another question, if a wanted to fill the tree entry with an array with like two entries, how could i do it?

---

<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:** [February 9, 2022, 10:40am UTC](https://root-forum.cern.ch/t/filling-the-branches-of-a-tree-in-pyroot/48702/3 "2022-02-09T10:40:54Z")

</div>

Hello,

You can proceed in a similar way, you create an array of size two and you define it as a branch.

You can find examples here:  
[https://root.cern.ch/doc/master/classTTree.html](https://root.cern.ch/doc/master/classTTree.html)

If you look for the PyROOT box, it will tell you how you need to call `Branch` to achieve what you want.
