# Speeding up the PyROOT loop over TTree with RDataFrame

**URL:** <https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634>\
**Category:** ROOT\
**Tags:** python, rdataframe\
**Created:** [June 16, 2019, 3:48pm UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634 "2019-06-16T15:48:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![apetukho](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apetukho](https://root-forum.cern.ch/u/apetukho)\
**Post date:** [June 16, 2019, 3:48pm UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/1 "2019-06-16T15:48:15Z")

</div>

* * *

_ROOT Version:_ 6.16  
_Platform:_ lxplus

* * *

Dear experts,

I’ve looked for ways to speed up the looping over TTrees in PyROOT and RDataFrame seemed like the preferred way to do it. But when I’ve tried to implement it in my code the performance was either the same or only slightly better than for the pure python script.  
I’ve attached the basic versions of the looping script written in C++, python with `for event` loop and python with RDataFrame and the example .root file. The approximate time it takes to loop over the tree is:

```auto
C++: 0.2 sec
Python (for event): 6.7 sec
Python (RDF, no MT): 5 sec
Python (RDF, implicit MT): 6.5 sec

```

Is there something I’m doing wrong in the RDataFrame script?

Thanks in advance.  
[ConvertTreeRDF.py](https://root-forum.cern.ch/uploads/default/original/3X/2/e/2e7eea9baa7ac83c2699b8dbf7a0524be440d1fe.py) (1.2 KB)  
[ConvertTree.py](https://root-forum.cern.ch/uploads/default/original/3X/a/9/a94b23a1b954a194319f2f96ce8a73c37eb56c8e.py) (1.3 KB)  
[ConvertTree.cpp](https://root-forum.cern.ch/uploads/default/original/3X/4/1/41a53cecffebe5eabf963c5877f49c05137528ab.cpp) (3.1 KB)  
[Input file (cernbox)](https://cernbox.cern.ch/index.php/s/MKAjf65Onw6lPGw)

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [June 17, 2019, 9:41am UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/2 "2019-06-17T09:41:34Z")

</div>

FYI: we’re investigating! (Yes Python is slow, but for the RDF case, most time should be spent on the C++ side!) Which exact ROOT version do you use?

---

<div class="post-metadata">

**Author:** ![apetukho](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apetukho](https://root-forum.cern.ch/u/apetukho)\
**Post date:** [June 17, 2019, 9:49am UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/3 "2019-06-17T09:49:10Z")

</div>

It’s 6.16.00-x86\_64-slc6-gcc62-opt.

---

<div class="post-metadata">

**Author:** ![apetukho](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apetukho](https://root-forum.cern.ch/u/apetukho)\
**Post date:** [June 20, 2019, 11:22am UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/4 "2019-06-20T11:22:47Z")

</div>

Is there any updates on this topic?

---

<div class="post-metadata">

**Author:** ![jcervant](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jcervant/32/18033_2.png) [@jcervant](https://root-forum.cern.ch/u/jcervant)\
**Post date:** [June 20, 2019, 12:46pm UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/5 "2019-06-20T12:46:46Z")

</div>

Hi @apetukho

As far as we see, your implementation with RDataFrame looks good. Running some performance analyses, we see that most of the overhead comes from fact that ROOT needs to compile some parts of the analysis at runtime, i.e. template code in the internals of RDataFrame and the C++ strings used inside the `Define` and `Filters` in the Python side.

These are the numbers I get on my workstation:

```auto
C++: 0.2 sec
Python (for event): 3.4 sec
Python (RDF, no MT): 2.9 sec

```

The 2.9 seconds running the `Python (RDF, no MT)` version are distributed as follows (in wall-time):

```auto
Compiling JIT code: 0.836 s 
Initialization of RDF Nodes: 0.038 s 
**Event Loop: 0.087 s** 
Snapshot (also compiling): 0.879 s
Other: 1 s

```

As you can see the computation of event loop runs quite fast while the compilation of the code takes a big portion of the total time. I believe the size of the input file also affects on this case, most likely for a bigger input the differences between the Python version based on events and the one based on RDataFrame would be more significant.

Alternatively, should you need to run this analysis faster you can also use this version of the analysis using RDataFrame in cpp:

[ConvertTreeRDF.cpp](https://root-forum.cern.ch/uploads/default/original/3X/1/f/1f3932b6774679b5c7a7996d9d2b10bbf50b0e7f.cpp) (1.7 KB)

In my machine it took 0.182 seconds to run the analysis. The main difference here is that we are not using any JIT code so everything is compiled in advance before running the analysis.

I hope that helps. Let us know if you have any further question.

And **Thank you very much** for reporting this, it has been very useful for us to detect this performance _issue_ on small analyses.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 20, 2019, 1:39pm UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/6 "2019-06-20T13:39:21Z")

</div>

Thank you @jcervant!

To add to this: we are looking into refactoring RDF in order to speedup its compilation (and therefore also jitting) – we might or might not succeed.

The current state of things is that, especially from PyROOT, the startup time of RDF is…important, unfortunately – but the event loop still runs at C++ speed (potentially subdividing batches of entries among multiple threads).

For larger datasets, you should see large improvements w.r.t. a python loop.

---

<div class="post-metadata">

**Author:** ![apetukho](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@apetukho](https://root-forum.cern.ch/u/apetukho)\
**Post date:** [June 25, 2019, 7:39am UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/7 "2019-06-25T07:39:39Z")

</div>

Thank you for your response. I’ve run some more tests by adding more cuts and branches to the _ConvertTree_ function and running it on the real MC data (and swapping _TLorentzVector_ to _ROOT::Math::LorentzVector_ to speed everything up and get rid of the _gInterpreter_). I don’t want to switch to C++ yet, because for me it’s way easier to handle the datasets and normalization coefficients in Python. The script looks like this:

```auto
processPathList = [...]
for processPath in processPathList:
	inputFile = ROOT.TFile(processPath, 'read')
	inputTree = inputFile.Get('output_tree')

	start = time.time()
	ConvertTree(inputTree)
	timeList.append((entryNum, time.time() - start))

```

And the measured times are (for lxplus machines with ROOT 6.16.00)

```auto
# of events in a tree	Python, sec	RDF, sec
167333 21,80 9,26
358497 47,85 7,30
468998 61,18 7,72
6205 0,83 4,93
7986 1,08 5,77
23283 2,71 5,30
13263 2,00 5,27
3729 0,61 5,16
5169 0,62 5,11
6228 0,82 5,83
5903 1,53 5,16
8381 1,19 5,27
10756 1,31 5,45
3342 0,56 5,43
6458 0,62 5,27
671951 58,14 9,19
509784 46,41 7,59
334797 33,33 6,42
580629 64,53 7,68
258548 30,31 7,58
66361 8,54 5,43
196346 22,86 6,42
164301 21,31 6,02
39374 5,76 5,58
206070 26,03 6,35
108375 14,31 7,08
33813 4,84 5,40
1437583 114,05 11,43
900101 84,94 10,57
497204 48,27 7,98
426843 44,61 7,86
2353543 62,37 14,41
3729460 121,76 10,37
1190529 39,71 6,73

Total 996,79 238,32

```

It’s interesting how the RDF is way faster on the big datasets but really slow in comparison even to Python loops for trees with less than ~50k events.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 25, 2019, 8:06am UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/8 "2019-06-25T08:06:14Z")

</div>

Nice, thanks for the measurements!

The event loop is always fast with RDF, but between jitting and PyROOT, on lxplus, there’s a constant 5 second overhead, it’s very clear from your benchmarks.

We’ll try to reduce that constant offset…and there are also plans to introduce some sort of “verbose mode” that would directly tell you how many events per second RDF processed during the event loop, and how much time was spent on setting things up, similarly to what Javi showed.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [June 26, 2019, 11:49am UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/9 "2019-06-26T11:49:38Z")

</div>

Some of your measurements are surprising, e.g. python @ 2353543 is way lower than what a fit would suggest.

For python I get `2s + 1E-4s*N`, for RDF it’s `6s + 2E-6s*N`. The constant time isn’t what it should be - and we’ll improve likely both parameters by factors during the next 18 months: we know exactly what we want to do to fix them. (FYI: bulk processing of `RNTuple`)! ![RDF](https://root-forum.cern.ch/uploads/default/original/3X/f/9/f9b6d1b908a0c97d37483dce27c5f5291ca80718.png)

---

<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:** [July 10, 2019, 11:49am UTC](https://root-forum.cern.ch/t/speeding-up-the-pyroot-loop-over-ttree-with-rdataframe/34634/10 "2019-07-10T11:49:40Z")

</div>

Closed on July 10, 2019, 11:49am UTC

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