I am trying to fill a RDataframe with some vectors. To improve the code organization, I have defined a function that takes as input an RDataFrame, and fill it:
void FillTry(ROOT::RDataFrame &d1, TString datafileName){
int j(0); // Variable for loop
d1.Define("id", [&j](){ // Id of the events
j++;
return j;
});
}
Something like that. I tried to run the function like this:
Please fill also the fields below. Note that root -b -q will tell you this info, and starting from 6.28/06 upwards, you can call .forum bug from the ROOT prompt to pre-populate a topic.
The issue is that Define returns a node of the processing graph enriched with the new column, it does not add the column to the node it is called from (see here)
Here I tried to propose a slightly modified version of your code that works:
auto FillTry(ROOT::RDataFrame &d1){
int j = 0; // Variable for loop
return d1.Define("id", [&j](){return j++;}); // Id of the events
}
void test() {
ROOT::RDataFrame d(100); // PDF2
auto filledFrame = FillTry(d);
filledFrame.Snapshot("myTree", "a.root");
// Print the content of the file, for the sake of the example
TFile f("a.root");
f.ls();
}
A final small remark about performance: you pass by value a TString in your original FillTry function. This triggers a copy and should be perhaps replaced by a const reference for performance reasons, i.e. void FillTry(ROOT::RDataFrame &d1, const TString &datafileName)
Don’t hesitate to write back in case of further questions!