# Call method for every object in RVec in a single line

**URL:** <https://root-forum.cern.ch/t/call-method-for-every-object-in-rvec-in-a-single-line/49428>\
**Category:** ROOT\
**Tags:** rdataframe, vecops\
**Created:** [March 31, 2022, 2:49pm UTC](https://root-forum.cern.ch/t/call-method-for-every-object-in-rvec-in-a-single-line/49428 "2022-03-31T14:49:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![FoxWise](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/foxwise/32/37588_2.png) [@FoxWise](https://root-forum.cern.ch/u/FoxWise)\
**Post date:** [March 31, 2022, 2:49pm UTC](https://root-forum.cern.ch/t/call-method-for-every-object-in-rvec-in-a-single-line/49428/1 "2022-03-31T14:49:22Z")

</div>

Hi all,

I have a `std::vector<XYZVector>` stored in TTree.

Is there an **easy** way to get absolute momentum `XYZVector.R()` for each element of the array within RDataFrame?

The one I am currently using is:

```auto
ROOT.gInterpreter.Declare('''
using namespace ROOT::Math;
using namespace ROOT::VecOps;

RVec <double> getAbsOfVectors(const RVec<XYZVector>& vecs){
    auto getAbs = [&](const XYZVector& vec) { return vec.R(); };
    return Map(vecs, getAbs);
}
''')

    df = ROOT.RDataFrame("tree", "file.root")
    df = df.Define("abs_mom", "getAbsOfVectors(mom)")

```

not very convenient if I need to predefine separate function for evey `getPhi()`, `getTheta()`, `getX()`, `getY()`, `getZ()`… Then the advantage of using XYZVector rather than plain `px`, `py`, `pz` vanishes.

I have tried this one-liner, which I was expecting to work, but it doesn’t…

```auto
    df = df.Define("abs_mom", "Map(mom, [&](const XYZVector& vec) { return vec.R(); })")

```

Any advice?

ROOT version: 6.24/06

cheers,  
Bohdan

---

<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:** [March 31, 2022, 3:48pm UTC](https://root-forum.cern.ch/t/call-method-for-every-object-in-rvec-in-a-single-line/49428/2 "2022-03-31T15:48:14Z")

</div>

Hello Bohdan,  
very annoyingly, to make it work you need `return Map` instead of just `Map`, because there is a `return` in the expression so RDF does not insert another one. This should work:

```python
.Define("absv", "return Map(v, [&](const XYZVector& vec) { return vec.R(); })")

```

Minimal repro that works for me:

```python
import ROOT

ROOT.RDataFrame(2)
   .Define("v", "ROOT::RVec<ROOT::Math::XYZVector>(3)")
   .Define("absv", "return Map(v, [](ROOT::Math::XYZVector& vec) { return vec.R(); })")
   .Display().Print()

```

I agree that’s more characters than one would like to write, but at least you don’t need the extra `Declare` stuff. However if you do these operations over and over again you might want to just put some helper functions in a library that you load at the beginning. Write them once, use them many times.

For ROOT, we could:

- have a bunch of RVec helper functions that work on RVecs of XYZVectors (or 4-vectors in general)
- allow `Map(vec, &XYZVector::R)`, to invoke a method on each element of the RVec. unfortunately C++ forces us to have that `&` in there

I’m open to better ideas 🙂

Cheers,  
Enrico

---

<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:** [April 14, 2022, 3:49pm UTC](https://root-forum.cern.ch/t/call-method-for-every-object-in-rvec-in-a-single-line/49428/3 "2022-04-14T15:49:12Z")

</div>

Closed on April 14, 2022, 3:49pm UTC

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