# Pyroot fill ttree

**URL:** <https://root-forum.cern.ch/t/pyroot-fill-ttree/43129>\
**Category:** Newbie\
**Created:** [January 16, 2021, 11:49pm UTC](https://root-forum.cern.ch/t/pyroot-fill-ttree/43129 "2021-01-16T23:49:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ROOTer1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@ROOTer1](https://root-forum.cern.ch/u/ROOTer1)\
**Post date:** [January 16, 2021, 11:49pm UTC](https://root-forum.cern.ch/t/pyroot-fill-ttree/43129/1 "2021-01-16T23:49:19Z")

</div>

Hello,

Here is my code to fill a tree in pyroot

> get\_var = ROOT.vector(‘float’)(0)  
> subtree = ROOT.TTree(“subtree”,“subtree”)  
> subtree.Branch(“var”, “std::vector\< float \>”, get\_var)  
> for iev in chain:  
> get\_var.push\_back(iev.var\_value)  
> subtree.Fill()  
> subtree.Write()

This works but I want to separate the values in events, meaning I can read the subtree with TChain and loop over events, what should I change here in this case?

Cheers!

---

<div class="post-metadata">

**Author:** ![jblomer](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jblomer](https://root-forum.cern.ch/u/jblomer)\
**Post date:** [January 18, 2021, 7:46am UTC](https://root-forum.cern.ch/t/pyroot-fill-ttree/43129/2 "2021-01-18T07:46:51Z")

</div>

Instead of creating a branch of type `std::vector<float>`, you’d create a branch of type `float`. You’d then loop over all the values and call `Fill()` for each and everyone.

---

<div class="post-metadata">

**Author:** ![ROOTer1](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/977dab/32.png) [@ROOTer1](https://root-forum.cern.ch/u/ROOTer1)\
**Post date:** [January 24, 2021, 10:45pm UTC](https://root-forum.cern.ch/t/pyroot-fill-ttree/43129/3 "2021-01-24T22:45:29Z")

</div>

Thanks a lot Jakob!
