# I have a large root file, but when using pyroot, the for loop is too slow, is there any good way to deal with it?

**URL:** <https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928>\
**Category:** ROOT\
**Tags:** python, root\
**Created:** [January 23, 2024, 6:26am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928 "2024-01-23T06:26:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esword618](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esword618/32/35063_2.png) [@Esword618](https://root-forum.cern.ch/u/Esword618)\
**Post date:** [January 23, 2024, 6:26am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/1 "2024-01-23T06:26:53Z")

</div>

I have a large root file, but when using pyroot, the for loop is too slow, is there any good way to deal with it?

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 23, 2024, 7:44am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/2 "2024-01-23T07:44:18Z")

</div>

> [@RDataFrame in python loop](https://root-forum.cern.ch/t/rdataframe-in-python-loop/46905):
>
> Dear, I heard that RDataFrame is much efficient than classic loops, but I’m not sure how to use it for a simple code as in python: chain = ROOT.TChain() chain.Add(Dataset) h = ROOT.TH1D(“h”,"", n, x\_min, x\_max) lvmu1=ROOT.TLorentzVector() lvmu2 =ROOT.TLorentzVector() for ev in chain: lvmu1.SetPtEtaPhiE(ev.pt\_muon[0], ev.eta\_muon[0], ev.phi\_muon[0], ev.E\_muon[0]) lvmu2.SetPtEtaPhiE(ev.pt\_muon[1], ev.eta\_muon[1], ev.phi\_muon[1], ev.E\_muon[1]) lvmu3 = lvmu1+ lvmu2 h.Fill(lvmu3.Pt())

---

<div class="post-metadata">

**Author:** ![Esword618](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esword618/32/35063_2.png) [@Esword618](https://root-forum.cern.ch/u/Esword618)\
**Post date:** [January 23, 2024, 8:22am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/3 "2024-01-23T08:22:36Z")

</div>

I don’t know. Who can help us? A sample code would be good.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 23, 2024, 8:25am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/4 "2024-01-23T08:25:05Z")

</div>

If you didn’t find anything useful on this Forum, maybe @vpadulan can help

---

<div class="post-metadata">

**Author:** ![Esword618](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esword618/32/35063_2.png) [@Esword618](https://root-forum.cern.ch/u/Esword618)\
**Post date:** [January 23, 2024, 8:53am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/5 "2024-01-23T08:53:03Z")

</div>

@vpadulan Can you help me?

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [January 23, 2024, 12:28pm UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/6 "2024-01-23T12:28:52Z")

</div>

Dear @Esword618 ,

Thanks for reaching out to the forum!

A Python for loop is slow and there’s really no way around this. Your best chance to get good performance is to move the loop (and heavy computations) to C or C++, that’s what practically all scientific Python libraries do.

Thus, I suggest you move away from running a Python for loop over the entries of your TTree as soon as possible. ROOT offers since a long while a much superior alternative, [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html). You can find many usage tutorials at [ROOT: Dataframe tutorials](https://root.cern.ch/doc/master/group __tutorial__ dataframe.html) .

Feel free to post here any example code you already have with your existing loop over the entries of the TTree. We then might start working together on a path towards using RDataFrame.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![Esword618](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esword618/32/35063_2.png) [@Esword618](https://root-forum.cern.ch/u/Esword618)\
**Post date:** [January 23, 2024, 12:31pm UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/7 "2024-01-23T12:31:39Z")

</div>

@vpadulan However, I need to use the for loop to judge and filter the data in TTree, but RDataFrame cannot meet my requirements.

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [January 23, 2024, 1:58pm UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/8 "2024-01-23T13:58:43Z")

</div>

Dear @Esword618 ,

Generally speaking, RDataFrame can be used to judge and filter data of a TTree. Could you be more explicit as to which requirements do not fit in RDataFrame? Maybe a code example of what you are trying to do with TTree?

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![Esword618](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esword618/32/35063_2.png) [@Esword618](https://root-forum.cern.ch/u/Esword618)\
**Post date:** [January 23, 2024, 2:19pm UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/9 "2024-01-23T14:19:01Z")

</div>

Dear @vpadulan  
But TTree’s python loop is too slow, so there is no way, can only use C++ to handle it?

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [January 23, 2024, 2:36pm UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/10 "2024-01-23T14:36:46Z")

</div>

Dear @Esword618 ,

Yes, the loop over TTree entries in Python is slow, that is clear. I would like to help you speed it up, and the best way we can do that is using RDataFrame (in Python!) so that we remove the for loop.

Now, I believe you already have some code of your own that is running some kind of analysis. It would help quite a lot if you could share a snippet of code that you already have right now. From that snippet, I could show you how to move your analysis (in Python) to use RDataFrame (still in Python).

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![Esword618](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esword618/32/35063_2.png) [@Esword618](https://root-forum.cern.ch/u/Esword618)\
**Post date:** [January 24, 2024, 2:07am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/11 "2024-01-24T02:07:08Z")

</div>

Dear @vpadulan ,  
Below is the root file I’m processing with pyroot, I want to process mu, a TTree, and get the elements in the branches that satisfy my conditions through some judgment conditions, how do I facilitate the elements through the RDataFrame, because this judgment condition is more complicated and I have to use a for loop.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/b/0/b0267902d57ec89c3f7e8d3dfe7af074b5199bac.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:** [February 7, 2024, 2:07am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/12 "2024-02-07T02:07:30Z")

</div>

Closed on February 7, 2024, 2:07am UTC

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

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [November 25, 2024, 6:42am UTC](https://root-forum.cern.ch/t/i-have-a-large-root-file-but-when-using-pyroot-the-for-loop-is-too-slow-is-there-any-good-way-to-deal-with-it/57928/13 "2024-11-25T06:42:08Z")

</div>

Dear @Esword618 ,

Apologies for the very late reply, your last post has slipped from my sight. Your best starting point (as always) is from the documentation: [ROOT: ROOT::RDataFrame Class Reference](https://root.cern/doc/master/classROOT_1_1RDataFrame.html)

Regarding your specific request

> I want to process mu, a TTree, and get the elements in the branches that satisfy my conditions through some judgment conditions,

I could imagine a very simple script such as

```python
import ROOT

df = ROOT.RDataFrame("mu", "mufile.root")
df_filter = df.Filter("judgment_condition(some_branch_name)")
h = df_filter.Histo1D("mybranch")
c = ROOT.TCanvas()
h.Draw()
c.SaveAs("myhisto.png")

```

You can find many similar examples in the documentation and tutorials [ROOT: Dataframe tutorials](https://root.cern.ch/doc/master/group __tutorial__ dataframe.html)

Cheers,  
Vincenzo
