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

**URL:** <https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614>\
**Category:** ROOT\
**Tags:** rdataframe\
**Created:** [April 15, 2019, 4:37pm UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614 "2019-04-15T16:37:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_K](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luke_k/32/16909_2.png) [@Luke\_K](https://root-forum.cern.ch/u/Luke_K)\
**Post date:** [April 15, 2019, 4:37pm UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614/1 "2019-04-15T16:37:28Z")

</div>

* * *

_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.

```auto
passes = myDF.Filter("x < 1.0 && x[previous event] > 0.5")

```

Does such functionality exist?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 15, 2019, 7:18pm UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614/2 "2019-04-15T19:18:21Z")

</div>

Hi Luke,

presently data frame can only process one event at the time but you can implement the behaviour you are after. Below you find a macro, `example.C`, which shows how to offer for a certain variable at event `n` the value of event `n-1`. I made some assumptions, but it proves the point I hope:

```auto
// This template keeps a state which could be the 
// value of a variable in the previous event
template<typename T>
class PrevValProducer {
   T fVal;
  public:
   explicit PrevValProducer(const T& v) : fVal(v) {}
   T operator()(T v)
   {
   std::swap(v, fVal);
   return v;
   }

};

void example()
{

PrevValProducer<int> prod(0);
auto foo = 100;

ROOT::RDataFrame r(4);
auto r2 = r.Define("v", [&foo](){return foo++;})
           .Define("prev_v", prod, {"v"});        

// This just to take out the values and print them
auto vals = r2.Take<int, ROOT::RVec<int>>("prev_v");

cout << *vals << endl;

}

```

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Luke\_K](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luke_k/32/16909_2.png) [@Luke\_K](https://root-forum.cern.ch/u/Luke_K)\
**Post date:** [April 16, 2019, 8:36am UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614/3 "2019-04-16T08:36:45Z")

</div>

Hi Danilo,

Thank you for the example. That’s exactly what we need :).

I completely forgot that functors can be used to define variables.

Cheers,  
L

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 16, 2019, 8:49am UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614/4 "2019-04-16T08:49:28Z")

</div>

Hi Luke,

one last caveat, which I am sure you already spot: the suggested procedure is not thread safe. It could be made such using some thread\_local qualifier, or, even better, the slot index provided by dataframe. In this case the fVal datamember would become a std::vector with as many elements as slots.

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Luke\_K](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/luke_k/32/16909_2.png) [@Luke\_K](https://root-forum.cern.ch/u/Luke_K)\
**Post date:** [April 16, 2019, 8:59am UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614/5 "2019-04-16T08:59:21Z")

</div>

And `Define` → `DefineSlot` and `T operator()(T v)` → `T operator()(unsigned int slot, T v)`, right?

Is the number of slots available globally before the DataFrame is created (without calling `ROOT::EnableImplicitMT (numthreads)` with a specific number)?

Cheers,  
L

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [April 16, 2019, 9:11am UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614/6 "2019-04-16T09:11:44Z")

</div>

Yes it is.

Cheers,  
D

---

<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 30, 2019, 9:11am UTC](https://root-forum.cern.ch/t/referencing-the-previous-neighbouring-event-in-rdataframe-filter/33614/7 "2019-04-30T09:11:47Z")

</div>

Closed on April 30, 2019, 9:11am UTC

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