# 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:** 1\
**Showing post:** 2

<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

---

_[View the full topic](https://root-forum.cern.ch/t/filling-different-trees-in-a-loop-in-pyroot/43239)._
