# RDataFrame in python loop

**URL:** <https://root-forum.cern.ch/t/rdataframe-in-python-loop/46905>\
**Category:** Newbie\
**Created:** [September 20, 2021, 1:57pm UTC](https://root-forum.cern.ch/t/rdataframe-in-python-loop/46905 "2021-09-20T13:57:00Z")\
**Posts on this page:** 4\
**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:** [September 20, 2021, 1:57pm UTC](https://root-forum.cern.ch/t/rdataframe-in-python-loop/46905/1 "2021-09-20T13:57:00Z")

</div>

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:

```auto
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:** ![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:** [September 20, 2021, 2:57pm UTC](https://root-forum.cern.ch/t/rdataframe-in-python-loop/46905/2 "2021-09-20T14:57:43Z")

</div>

From your example, it’s not clear to understand what you’re trying to do, but maybe @etejedor can help you

---

<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:** [September 20, 2021, 3:33pm UTC](https://root-forum.cern.ch/t/rdataframe-in-python-loop/46905/3 "2021-09-20T15:33:52Z")

</div>

Hi Bertrand! I’m trying to fill a histogram with a calculated transverse momentum from the sum of two vectors

---

<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:** [September 20, 2021, 3:48pm UTC](https://root-forum.cern.ch/t/rdataframe-in-python-loop/46905/4 "2021-09-20T15:48:27Z")

</div>

Hello,

Please have a look at the RDataFrame docs first:

[https://root.cern/doc/master/classROOT\_1\_1RDataFrame.html](https://root.cern/doc/master/classROOT_1_1RDataFrame.html)

With RDataFrame, you don’t iterate over the events explictly (this is hidden inside RDataFrame and done much more efficiently in C++). You proceed by applying per-event transformations to your dataset and finally obtaining some results from it.

In your code, you would construct an RDataFrame from your dataset, then for example define a new column `pt` from a function that does the per-event calculation you posted here. After that, you would call a Histo1D action on that column.
