Finding differences between rows from PyROOT

Hi,
I tried to follow the previous post to compute some quantities between rows using PyROOT. I could declare a struct similar to the suggestion, but the output of the RDataFame is not as expected.
My function:

ROOT.gInterpreter.Declare('''
struct diff {
  Double_t prevSeries = -1;
  Double_t firstTime = -1;
  double operator()(Double_t x, Double_t y) {
    if (prevSeries != x) {
      prevSeries = x;
      firstTime = y; 
    }
    Double_t diff = y - firstTime;
    return diff;
  }
};
''')

RdataFrame call:
df = df.Range(10)
df = df.Define(‘livetime’,‘diff()(SeriesNumber,timestamp_corr)’)
df = df.AsNumpy({‘livetime’,‘SeriesNumber’,‘timestamp_corr’})
Output:
image
Note that SeriesNumbers are all the same.
Also, the 3 input overload of Define() didn’t work from PyROOT.

_ROOT Version: 6.22/6.26

I just needed to declare an instance of the diff, and use the instance in the Define().

ROOT.gInterpreter.Declare('''diff diff_instance;''')
df = df.Define('livetime','diff_instance(SeriesNumber,timestamp_corr)')
1 Like