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

<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

---

_[View the full topic](https://root-forum.cern.ch/t/rdataframe-apply-method-on-vector-of-lorentzvectors/53534)._
