# RDataFrame loop over vector to test all possible combinations

**URL:** <https://root-forum.cern.ch/t/rdataframe-loop-over-vector-to-test-all-possible-combinations/45392>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [June 14, 2021, 3:32pm UTC](https://root-forum.cern.ch/t/rdataframe-loop-over-vector-to-test-all-possible-combinations/45392 "2021-06-14T15:32:17Z")\
**Posts on this page:** 2\
**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:** [June 14, 2021, 3:32pm UTC](https://root-forum.cern.ch/t/rdataframe-loop-over-vector-to-test-all-possible-combinations/45392/1 "2021-06-14T15:32:17Z")

</div>

Hello,

I would like to write a simple Kzero finder in RDataFrame. I have a vector of all tracks and I need to compute the invariant mass of all pairs.  
Something like  
for i in tracks:  
for j in track:  
mass=Mass(i,j)  
push mass in vector of all Kzero candidates

I can not figure out how this can be efficiently done in RDataFrame.  
Do you have an example ?

I saw a post where it was proposed to use the  
`Take` action and then do a nested for loop over these vectors.

How is this done ? Does anybody have an example ?

I am using the python setup.

Thank you for your help.

Regards,  
Tancredi

---

<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:** [June 14, 2021, 3:39pm UTC](https://root-forum.cern.ch/t/rdataframe-loop-over-vector-to-test-all-possible-combinations/45392/2 "2021-06-14T15:39:03Z")

</div>

Hi Tancredi,  
if you want to perform an operation on a vector of tracks _for each event_, see [#working with collections](https://root.cern/doc/master/classROOT_1_1RDataFrame.html#collections) in the RDF users’ guide: you can inject your logic (expressed as a helper C++ function or a numba-compiled Python function) through a `Define`. There are many examples of this in the [tutorials](https://root.cern.ch/doc/master/group __tutorial__ dataframe.html), see e.g. [df103\_NanoAODHiggsAnalysis.py](https://root.cern.ch/doc/master/df103__NanoAODHiggsAnalysis_8py.html).

An example of numba-compiled RDF helper functions is [ROOT: tutorials/pyroot/pyroot004\_NumbaDeclare.py File Reference](https://root.cern.ch/doc/master/pyroot004__NumbaDeclare_8py.html) .

If you want to have _all tracks from the whole dataset_ in a single vector, in Python I recommend using `AsNumpy` and then flattening + possibly convert std::vectors to numpy arrays.

Cheers,  
Enrico
