Referencing the previous/neighbouring event in RDataFrame::Filter

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?

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:

// This template keeps a state which could be the 
// value of a variable in the previous event
template<typename T>
class PrevValProducer {
   T fVal;
   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;



1 Like

Hi Danilo,

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

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


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.


And DefineDefineSlot 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)?


Yes it is.


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