# Access data across event/row in RDataFrame

**URL:** <https://root-forum.cern.ch/t/access-data-across-event-row-in-rdataframe/56945>\
**Category:** ROOT\
**Tags:** tree, rdataframe\
**Created:** [November 1, 2023, 1:06am UTC](https://root-forum.cern.ch/t/access-data-across-event-row-in-rdataframe/56945 "2023-11-01T01:06:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhangdanyi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/58956e/32.png) [@zhangdanyi](https://root-forum.cern.ch/u/zhangdanyi)\
**Post date:** [November 1, 2023, 1:06am UTC](https://root-forum.cern.ch/t/access-data-across-event-row-in-rdataframe/56945/1 "2023-11-01T01:06:46Z")

</div>

Hi,

I am trying to use RDataFrame to calculate the distance between every two events in a tree. For example, the distance is defined as the absolute value of the difference between two momentum `p1` and `p2` from two different events.

Assume the tree is read into an RDataFrame. I want to define a new column `distance`, for each row it will be an array of the distances between this row and other rows.

However, from what I know the function `Define` can only access the columns inside each event/row, and do one event loop in action. But calculating the distances requires two event loops, accessing data across events.

I tried to search for possible strategies on the RDataFrame documentation page but found nothing useful so far. Is this possible in RDataFrame or am I missing some method already exists?

Thank you for your help,  
Danyi

---

<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:** [November 1, 2023, 1:27pm UTC](https://root-forum.cern.ch/t/access-data-across-event-row-in-rdataframe/56945/2 "2023-11-01T13:27:42Z")

</div>

Dear @zhangdanyi ,

Thank you for asking your question on the forum. You are totally right in that the native way of execution of RDataFrame is per-event (soon it will be per-group-of-events), but in general there is no direct way in the API to establish relations between the current event and others.

There have been similar discussions on the forum, one way to achieve what you want is implementing an helper to act as a “sliding window” on the values of your dataset. See a concrete example [in this forum post](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/5). One potential issue I see is regarding this specific statement

> for each row it will be an array of the distances between this row and other rows.

This means that somehow you need to store _all_ the values of the column in memory, and for each event compare the current value against all the others. It can be done, but it surely has a cost.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![zhangdanyi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/58956e/32.png) [@zhangdanyi](https://root-forum.cern.ch/u/zhangdanyi)\
**Post date:** [November 1, 2023, 4:33pm UTC](https://root-forum.cern.ch/t/access-data-across-event-row-in-rdataframe/56945/3 "2023-11-01T16:33:25Z")

</div>

Thank you! It might be tricky/unsafe to do that with multi-threading. We’ll go back to the normal event loop with ROOT.

---

<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:** [November 15, 2023, 4:34pm UTC](https://root-forum.cern.ch/t/access-data-across-event-row-in-rdataframe/56945/4 "2023-11-15T16:34:12Z")

</div>

Closed on November 15, 2023, 4:34pm UTC

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