# Referencing the previous/neighbouring event in RDataFrame::Define

**URL:** <https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-define/52724>\
**Category:** ROOT\
**Tags:** python, rdataframe\
**Created:** [December 10, 2022, 12:43pm UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-define/52724 "2022-12-10T12:43:31Z")\
**Posts on this page:** 5\
**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:** [December 10, 2022, 12:43pm UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-define/52724/1 "2022-12-10T12:43:31Z")

</div>

I wonder if it is possible today?

Very similar question:

> [@Referencing the previous/neighbouring event in RDataFrame::Filter](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614):
>
> ROOT Version: 6.16.00 Platform: CentOS 7 Compiler: GCC 7.3.0 Dear experts, I am currently trying to figure out the correct syntax for using the previous event in the filter for RDataFrame. In our use case, the events are ordered in time and it is essential to check the previous event in order to satisfy an analysis cut, e.g. passes = myDF.Filter("x \< 1.0 && x[previous event] \> 0.5") Does such functionality exist?

was a while ago for patch 6.16. And for Filter, not Define.

It would be nice to have:

```python
improt ROOT

df = ROOT.RDataFrame(10)\
        .Define("squares", "rdfentry_*rdfentry_")\
        .Define("prev_squares", "squares_of the previous event")\
        .Define("odds", "squares - prev_squares")

```

Was there any progress on that?

If I ran the proposed answer in that post, or the answer from this post:

> [@Event mixing with RDataFrame](https://root-forum.cern.ch/t/event-mixing-with-rdataframe/48009/5):
>
> Hi @Samuel1 , sorry for the high latency, I was off last week. The most tricky part about processing entries with a sliding window is multi-threading – each entry will process a bunch of entries at a time, and the first and last entries in the bunch will not have a previous/next entry so you’ll miss some statistics. If you are happy with single-thread processing, or you don’t mind losing some of the pairs of consecutive entries, you can use a stateful functor + RDataFrame, something like (hav…

I get the error:

```auto
  ROOT::RDF::RInterface<ROOT::Detail::RDF::RJittedFilter,void> ROOT::RDF::RInterface<ROOT::Detail::RDF::RJittedFilter,void>::Define(basic_string_view<char,char_traits<char> > name, basic_string_view<char,char_traits<char> > expression) =>
    TypeError: takes at most 2 arguments (3 given)
  Failed to instantiate "Define(std::string,std::string,std::initializer_list<std::string>)"

```

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:** [December 12, 2022, 8:55am UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-define/52724/2 "2022-12-12T08:55:30Z")

</div>

Hello @FoxWise ,

there is nothing ad-hoc in RDF to access previous or neighboring events. In general you need a stateful filter, similar to the stateful Define that you linked. For thread-safety you can have per-thread storage (`df.GetNSlots()` tells you how many threads you’ll have and the `rdfslot_` column can be used to index into per-thread storage, e.g. a vector).

As per the error, it looks like you are passing a string as a second argument _and_ a list of columns as third argument. The third argument is only needed if as second argument you pass a function or functor, not a string.

I hope this helps!  
Enrico

---

<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:** [December 12, 2022, 10:02pm UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-define/52724/3 "2022-12-12T22:02:48Z")

</div>

So, then it is impossible to call the method with 3 arguments from pyROOT?

As I did define functor, but inside

`gInterpriter.Declare(''' . . . ''')`

---

<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:** [December 13, 2022, 9:27am UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-define/52724/4 "2022-12-13T09:27:35Z")

</div>

It is totally possible, e.g. this should work:

```python
import ROOT

ROOT.gInterpreter.Declare("""
struct Functor {
    double operator()(double x) { return x*x; }
};
""")

f = ROOT.Functor()
m = ROOT.RDataFrame(10)\
        .Define("x", "3.")\
        .Define("xsq", f, ["x"])\
        .Mean("xsq")
print(m.GetValue())

```

---

<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:** [December 27, 2022, 9:27am UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-define/52724/5 "2022-12-27T09:27:38Z")

</div>

Closed on December 27, 2022, 9:27am UTC

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