# Filling different trees in a loop in pyroot

**URL:** <https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239>\
**Category:** ROOT\
**Tags:** tree, python, root\
**Created:** [January 24, 2021, 4:16pm UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239 "2021-01-24T16:16:59Z")\
**Posts on this page:** 12\
**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 24, 2021, 4:16pm UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/1 "2021-01-24T16:16:59Z")

</div>

I have a rootfile with one tree having a lot of events.

I want to have these events classified in different trees defined as bins of certain variables

What I do is making a new rootfile and define the empty trees with empty leaves. Now in the loop of the original events, I want to be able to fill the events in the leaves of the new trees, based on which bins they belong.

My question is, how do you fill leaves of existing trees in pyroot, and is there a more efficient way to do this?

---

<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, 8:46am UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/2 "2021-01-25T08:46:06Z")

</div>

Hello,

The most efficient way to do this would be to use RDataFrame:

> **[ROOT: ROOT::RDataFrame Class Reference](https://root.cern/doc/master/classROOT_1_1RDataFrame.html)**

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 particular, this RDataFrame tutorial:

> **[ROOT: tutorials/dataframe/df007\_snapshot.py File Reference](https://root.cern.ch/doc/master/df007__snapshot_8py.html)**

might be useful.

Please let us know if you need further assistance to develop this code with RDataFrame.

@eguiraud

---

<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, 10:33am UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/3 "2021-01-25T10:33:31Z")

</div>

Hi, thank you very much for the info!

I will definitely consider using RDataFrame in the future, but 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:08pm UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/4 "2021-01-25T13:08:09Z")

</div>

Hello,

When you do:

```python
 subtree.Branch(“var1” ,get_var1)

```

you are creating a new branch whose address will be taken from `get_var1`. If later you do:

```python
get_var1 = var1_value_for_event_i

```

You are binding `get_var1` to a new object, and the previous one (the one you associated with `var1` in `Branch`) will be garbage collected. What you need to do is modify the original `get_var1` object instead of reassigning the variable to a new object. So it should be something like:

Here’s some documentation about using `Branch` from Python (how to use it with different types of branches):

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

---

<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, 1:37pm UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/5 "2021-01-25T13:37:20Z")

</div>

Hi, thanks a lot!

The problem is that I have a lot of these trees and I define them the same way, so get\_var1 is just an example, but I don’t know how to specifically fill the var1 of a specific tree among all the others.

So if I know how to fill a existing branch of any existing tree from scratch, and put this in the loop, it would solve my problem I think 🙂 in the documentation they start by defining a new branch before filling 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:** [January 25, 2021, 3:30pm UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/6 "2021-01-25T15:30:54Z")

</div>

> but I don’t know how to specifically fill the var1 of a specific tree among all the others.

If you defined `var1` as the `Branch` of a tree `mytree`, and then you call `mytree.Fill`, a new entry for `var1` in `mytree` will be filled.

> So if I know how to fill a existing branch of any existing tree from scratch, and put this in the loop, it would solve my problem I think 🙂 in the documentation they start by defining a new branch before filling it

Just to be sure, you don’t mean _updating_ the values of an existing branch of a tree, right? You want to fill up new trees, whose branches you create with `Branch`.

---

<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, 7:31pm UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/7 "2021-01-25T19:31:26Z")

</div>

Hi @etejedor, I’m sorry to be not very clear here.

Basically, in need 3k trees that are constructed from 1 tree, but just classified based on their values of certain variables, i.e if combining all the 3k trees I would get back the same original tree.

Now I define these 3k trees as following:

```auto
outfile = ROOT.TFile("rootfile",'recreate')
for i in range(0,len(bins)-1):
             subtree = ROOT.TTree("name_with_bin_info","name_with_bin_info")
             subtree.Branch("var1" ,get_var1)
             subtree.Branch("var2" ,get_var2)
             subtree.Write()

```

So after having these trees, in an event loop I want to fill them:

```auto
for iev in chain:
      (decision to which bin the event belongs):
      fill to the corresponding tree

```

But in this case, I don’t have the variable pointing to corresponding tree, so I have to somehow open the tree from scratch, get address of var1 and var2, then add the event with the values of var1 and var2. And this is the part I’m missing

---

<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 26, 2021, 9:35am UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/8 "2021-01-26T09:35:31Z")

</div>

I see, so you need to make sure the trees you create are stored in a Python list, so you keep all their references and they are not garbage collected as the first loop runs, plus the same for the `vars1` and `vars2` - everything needs to be stored and survive the first loop:

```python
outfile = ROOT.TFile("rootfile",'recreate')
trees = []
vars1 = []
vars2 = []
for i in range(0,len(bins)-1):
             trees.append(ROOT.TTree("name_with_bin_info","name_with_bin_info"))
             vars1.append(get_var1_for_this_subtree)
             vars2.append(get_var2_for_this_subtree)
             trees[-1].Branch("var1" , vars1[-1])
             trees[-1].Branch("var2" , vars2[-1])
             trees[-1].Write()

```

Then in the second loop you don’t have to open any tree from scratch, since you have all of them stored in `trees`, and the same goes for `var1` and `var2`.

```python
for iev in chain:
      (decision to which bin the event belongs):
      assign value to the right var1 and var2 in vars1 and vars2
      fill the corresponding tree from trees

```

---

<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 26, 2021, 5:21pm UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/9 "2021-01-26T17:21:00Z")

</div>

Hi @etejedor, thank you very much, I think I’m almost there, just one last question:

For this part, how do I generate different variables without initialising 3k vars per variable?

```auto
vars1.append(get_var1_for_this_subtree)
vars2.append(get_var2_for_this_subtree)

```

My real struggle is really inside the eventloop to be honest, because there you need to fill the right var to the right tree

---

<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 26, 2021, 10:49pm UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/10 "2021-01-26T22:49:24Z")

</div>

I think I solved the problem 🙂 Can’t thank you enough @etejedor !!!

---

<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 27, 2021, 8:04am UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/11 "2021-01-27T08:04:54Z")

</div>

Hello,

Good to hear!

That’s right, actually you don’t strictly need as many `vars1` and `vars2` as trees, you could just have one of each (the same for all the trees), as long as you change its value inside the event loop and fill one tree per iteration.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [February 10, 2021, 8:05am UTC](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239/12 "2021-02-10T08:05:24Z")

</div>

Closed on February 10, 2021, 8:05am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
