# Fill TTree in pyroot loop

**URL:** <https://root-forum.cern.ch/t/fill-ttree-in-pyroot-loop/42824>\
**Category:** Newbie\
**Created:** [December 21, 2020, 10:43pm UTC](https://root-forum.cern.ch/t/fill-ttree-in-pyroot-loop/42824 "2020-12-21T22:43:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [December 21, 2020, 10:43pm UTC](https://root-forum.cern.ch/t/fill-ttree-in-pyroot-loop/42824/1 "2020-12-21T22:43:20Z")

</div>

Dear,

I trying to fill a new tree from events using tchain:

```auto
 outfile = ROOT.TFile("Smaller.root",'recreate')
 get_var = ROOT.vector('float')(0)
 subtree = ROOT.TTree("subtree","subtree")
 subtree.Branch("var", get_var,"var/D")
 for iev in chain:
 if (a few cuts):
 get_var = iev.var
 subtree.Fill()
 subtree.Write()
 outfile.Close()

```

This is running but the values are all 0, is there something I’m doing wrong?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [December 22, 2020, 8:17am UTC](https://root-forum.cern.ch/t/fill-ttree-in-pyroot-loop/42824/2 "2020-12-22T08:17:59Z")

</div>

@etejedor can correct me, but I think your code should be something like that:

```auto
outfile = ROOT.TFile("Smaller.root",'recreate')
get_var = ROOT.vector('float')(0)
subtree = ROOT.TTree("subtree","subtree")
subtree.Branch("var", "vector", get_var)
for iev in chain:
if (a few cuts):
get_var.push_back(iev.var)
subtree.Fill()
subtree.Write()
outfile.Close()

```

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [December 22, 2020, 8:50am UTC](https://root-forum.cern.ch/t/fill-ttree-in-pyroot-loop/42824/3 "2020-12-22T08:50:46Z")

</div>

Hi Bertrand,

Many thanks for your help, it works fine!  
I just got an error saying:

> Error in TTree::Bronch: Cannot find class:vector

which for some reason I could fix by removing “vector” 🙂

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [December 22, 2020, 8:54am UTC](https://root-forum.cern.ch/t/fill-ttree-in-pyroot-loop/42824/4 "2020-12-22T08:54:21Z")

</div>

Well, I was not sure about the syntax, try with

```auto
subtree.Branch("var", "std::vector<float>", get_var)

```

---

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [December 22, 2020, 8:58am UTC](https://root-forum.cern.ch/t/fill-ttree-in-pyroot-loop/42824/5 "2020-12-22T08:58:00Z")

</div>

Yes this works too 🙂

Once again, many thanks for your help!

---

<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:** [January 4, 2021, 9:54am UTC](https://root-forum.cern.ch/t/fill-ttree-in-pyroot-loop/42824/6 "2021-01-04T09:54:20Z")

</div>

Thanks @bellenot for the answer, I just wanted to add that the TTree documentation now includes information about PyROOT (you need to look for the PyROOT box at the end of the C++ docs):

> **[ROOT: TTree Class Reference](https://root.cern.ch/doc/master/classTTree.html)**

Among others, it includes documentation for the pythonization of `Branch`. Here you can even do `subtree.Branch("var", get_var)` (without the type).
