# Fill existing tree in pyroot

**URL:** <https://root-forum.cern.ch/t/fill-existing-tree-in-pyroot/43254>\
**Category:** Newbie\
**Created:** [January 25, 2021, 11:48am UTC](https://root-forum.cern.ch/t/fill-existing-tree-in-pyroot/43254 "2021-01-25T11:48:58Z")\
**Posts on this page:** 2\
**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 25, 2021, 11:48am UTC](https://root-forum.cern.ch/t/fill-existing-tree-in-pyroot/43254/1 "2021-01-25T11:48:58Z")

</div>

I was wondering how you can fill events in an existing tree, I was trying something like this:

#first make a new one

> subtree = ROOT.TTree(“subtree”,“subtree”)  
> subtree.Branch(“var1” ,get\_var1)  
> subtree.Branch(“var2” ,get\_var2)  
> subtree.Write()

#later in an event loop

> subtree = ROOT.gDirectory.Get(“subtree”)  
> get\_var1 = var1\_value\_for\_event\_i  
> get\_var2 = var2\_value\_for\_event\_i  
> subtree.Fill()

but this gives the following error:  
Error in TBranchElement::Fill: attempt to fill branch var1 while addresss is not set  
Error in TBranchElement::Fill: attempt to fill branch var2 while addresss is not set

* * *

---

<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 25, 2021, 1:01pm UTC](https://root-forum.cern.ch/t/fill-existing-tree-in-pyroot/43254/2 "2021-01-25T13:01:58Z")

</div>

Hello,  
I will answer in the original thread:

> [@Filling different trees in a loop in pyroot](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/2):
>
> Hello, The most efficient way to do this would be to use RDataFrame: With RDataFrame, the loop would happen in C++, and therefore the performance would be better than to have it in Python. As for what you want to do, the way to proceed would be to create an RDataFrame from your initial tree stored in your ROOT file. From that tree, you can define new columns (branches) based on the information contained in the initial tree. Finally, you can Snapshot (store in a file) the derived trees. In …
