# RDataFrame apply method on vector of LorentzVectors

**URL:** <https://root-forum.cern.ch/t/rdataframe-apply-method-on-vector-of-lorentzvectors/53534>\
**Category:** Newbie\
**Tags:** rdataframe, python\
**Created:** [February 8, 2023, 3:18pm UTC](https://root-forum.cern.ch/t/rdataframe-apply-method-on-vector-of-lorentzvectors/53534 "2023-02-08T15:18:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tancredi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tancredi/32/20395_2.png) [@tancredi](https://root-forum.cern.ch/u/tancredi)\
**Post date:** [February 8, 2023, 3:18pm UTC](https://root-forum.cern.ch/t/rdataframe-apply-method-on-vector-of-lorentzvectors/53534/1 "2023-02-08T15:18:09Z")

</div>

* * *

I can not figure out what is the correct syntax to apply a method on a RVecROOT::Math::PtEtaPhiEVector\> in python.  
I made an example  
“”  
import ROOT  
import os,sys

# 

if **name** == “ **main** ”:

path = “root://eospublic.cern.ch//eos/opendata/atlas/OutreachDatasets/2020-01-22/”  
filename=path+‘GamGam/Data/data\_A.GamGam.root’  
print ('filename= ',filename)  
d = ROOT.RDataFrame(“mini”, filename)

# 

d=d.Filter(‘photon\_n\>0’,‘At least 1 photon’)  
d = d.Define(“photons”, ’ ROOT::VecOps::ConstructROOT::Math::PtEtaPhiEVector(photon\_pt , photon\_eta, photon\_phi, photon\_E )')  
d = d.Define(“ph\_pt”,‘ROOT::VecOps::Map(photons, photons.Pt())’)  
hnom = d.Histo1D(“ph\_pt”)  
h1.Draw()  
“”  
What is the correct syntax to call the Map in python ?  
Thank you for the help.  
Tancredi

* * *

---

<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:** [February 8, 2023, 3:30pm UTC](https://root-forum.cern.ch/t/rdataframe-apply-method-on-vector-of-lorentzvectors/53534/2 "2023-02-08T15:30:27Z")

</div>

Hi @tancredi ,

I don’t think you are forced to use helpers like `Construct` or even `Map`. With this example I think I am doing the same thing you are trying to do, correct me if I am wrong:

```python
import ROOT

if __name__ == " __main__":
    df = (
        ROOT.RDataFrame(5)
            .Define("photon_pt", "1.")
            .Define("photon_eta", "2.")
            .Define("photon_phi", "3.")
            .Define("photon_E", "4.")
            .Define("photons", "ROOT::Math::PtEtaPhiEVector(photon_pt , photon_eta, photon_phi, photon_E)")
            .Define("ph_pt", "photons.Pt()")
    )
    df.Display(("ph_pt",)).Print()

```

---

<div class="post-metadata">

**Author:** ![tancredi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tancredi/32/20395_2.png) [@tancredi](https://root-forum.cern.ch/u/tancredi)\
**Post date:** [February 8, 2023, 3:33pm UTC](https://root-forum.cern.ch/t/rdataframe-apply-method-on-vector-of-lorentzvectors/53534/3 "2023-02-08T15:33:49Z")

</div>

Hello,  
yes, but in my case the photon is a RVec (many photons in the event) and I need to apply a method from ROOT::Math::PtEtaPhiEVector on all elements in the RVec. I saw that the Map is needed for that.  
Regards,  
Tancredi

---

<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:** [February 8, 2023, 4:30pm UTC](https://root-forum.cern.ch/t/rdataframe-apply-method-on-vector-of-lorentzvectors/53534/4 "2023-02-08T16:30:10Z")

</div>

Right,

The following snippet should then be closer to your case:

```python
import ROOT

if __name__ == " __main__":
    df = (
        ROOT.RDataFrame(5)
            .Define("photon_pt", "ROOT::RVecD(3, 1.)")
            .Define("photon_eta", "ROOT::RVecD(3, 2.)")
            .Define("photon_phi", "ROOT::RVecD(3, 3.)")
            .Define("photon_E", "ROOT::RVecD(3, 4.)")
            .Define("photons", "ROOT::VecOps::Construct<ROOT::Math::PtEtaPhiEVector>(photon_pt, photon_eta, photon_phi, photon_E)")
            .Define("ph_pt", "return ROOT::VecOps::Map(photons, [](const ROOT::Math::PtEtaPhiEVector &v){return v.Pt();}); ")
    )
    df.Display(("photons",)).Print()

```

First off, with respect to your original example which you said doesn’t work, I don’t think `photons.Pt()` is doing what you think. The [docs](https://root.cern.ch/doc/master/group__vecops.html#gaca1b9783b8a603b7a145d8d8d5c8df6a) of the `Map` function state that you should pass a callable that will be applied to all the elements of the collection. In your case, the collection is `photons` (i.e. an `RVec<ROOT::Math::PtEtaPhiEVector>`). The other argument you are passing, i.e. `photons.Pt()`, is not a callable. Logically, that would mean the result of the method `Pt`, which is a `double`. Not even `photons.Pt` would work since that is a member function of the `ROOT::Math::PtEtaPhiEVector` class, and not the `RVec` class which is the type of `photons`. So, practically, you have to pass a callable that takes a `ROOT::Math::PtEtaPhiEVector` as input argument and calls its `Pt` method (that is what you see in my example above).

That being said, pay extra attention in this particular case: passing a C++ lambda to the `Map` call inside the string hits a non-ideal behaviour which is documented in [the docs](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a4698601205a55ac49279150d56fc904f) (incidentally with exactly your use case as an example) and has also been explained in the forum in other posts ([here](https://root-forum.cern.ch/t/possible-bug-in-rdataframe-define-with-lambdas/51132/3) for example). Make sure to always explicitly use `return` in the strings you pass to `Define` and other operations whenever another `return` keyword is already present in the string.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![tancredi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tancredi/32/20395_2.png) [@tancredi](https://root-forum.cern.ch/u/tancredi)\
**Post date:** [February 8, 2023, 5:25pm UTC](https://root-forum.cern.ch/t/rdataframe-apply-method-on-vector-of-lorentzvectors/53534/5 "2023-02-08T17:25:07Z")

</div>

Hello Vincenzo,  
thank you for the detailed explaination. I did not know how to construct the lambda function.  
It works now !  
Regards,  
Tancredi
