Is it possible compute quantities between entries in RDF?

I would like to know if it is possible to compute quantities between entries with RDF Define method.

For example, if each entrie is a particle, and I want to compute the pT difference between them.

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Unfortunately I don’t think this is possible but @eguiraud can confirm.

There is not an ID-number or something to identificate the entries ?

Hi @imanol097 ,

there are a few ways in which you can “compute quantities between entries”, which one is simpler in your case depends on the actual use case.

For example in a case where you know you have no Filters and, for simplicity, you process all entries in the same thread, you can calculate, for each event, the difference between x[entry] and x[entry - 1] with a stateful Define helper:

#include <ROOT/RDataFrame.hxx>

struct DiffEvaluator {
  int prevX = -1;
  int operator()(int x) {
    if (prevX == -1) {
      prevX = x;
      return 999; // do something special if there was no "previous x"
    const int diff = x - prevX;
    prevX = x;
    return diff;

void foo() {
  auto df = ROOT::RDataFrame(10).Define("x", "int(rdfentry_)");

  auto h = df.Define("xdiff", DiffEvaluator(), {"x"})
             .Filter("xdiff != 999")

I hope this gives you the idea: you can inject any arbitrary logic in RDF, so you can do pretty much what you want as part of the loop. With multi-threading activated you’ll have to have a different state for each “slot” (in this case a different “prevX” for each processing slot), see also DefineSlot.


Thanks @eguiraud , I will try this one.

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