Change existing column in RDataFrame::Foreach

I’m looking to be able to use the Foreach method of RDataFrame to be able to modify a preexisting column in a dataframe.

As a test, I have tried:

ROOT::RDataFrame df(10);
auto d_new = df.Define("x","0");
d_new.Foreach([](int x){ cout << x << endl; },{"x"}); //prints 0s
d_new.Foreach([](int &x){ x = 7; },{"x"});
d_new.Foreach([](int x){ cout << x << endl; },{"x"}); //still prints 0s

but this does not give the desired behaviour, the columns remain unchanged.

Is there any way of modifying columns like this?

My use case is to be able to define multiple columns at once, for quantities that are correlated and will share much of the same calculation. The best way I have found to be able to do this is to have a function which returns multiple values, and then call that from lambdas inside define statements, selecting the values that are necessary for that particular definition. The obvious issue with this is the code which calculates all the values is run every time a new quantity is defined. Also, I feel it makes the code rather messy.

IIRC columns are read-only.

quantities that are correlated and will share much of the same calculation

I believe you could return a struct from a Define(). Think of a Define() of a ROOT::Math::LorentzVector. For an example see how genTracks is used in the tutorial dataframe/df002_dataModel.C


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