Passing variable arguments to Define in RDataFrame

Hi all,

I’m a bit new to using RDataFrame, so I was wondering if someone had some insight on how to implement this. I have a function (let’s say a simple linear polynomial) that I wish to operate on every element in a tree, and want to change the coefficients in that function to see how the result changes. But it’s not clear to me how to pass those coefficients as arguments to Define, since it only seems to accept columns.

To make it a bit more clear, say I have some function:

double polyFunc(p0, p1, x) { return p0 + p1*x; }

And I am trying to do something like this:

double par0 = 0.3;
double par1 = 5.7;
auto rdfPoly = rdf.Define(“polyn1d”, polyFunc, {par0, par1, “xVals”});

Which doesn’t work since the third argument in Define only seems to accept columns.

My goal is to have some more complicated function whose parameters I can tweak, but I’m stuck on how to do this basic thing, if it’s possible. Any feedback would be greatly appreciated!



Thanks for the this question.
I see two rough options to accomplish what I understand you are after (please correct me if I am wrong!).

The first one is really to Define columns with the function parameters values and use them as arguments of the function.
The second one, is to wrap the function in a C++ lambda and capture the parameters by reference, like

double p0 = 0.1;
double p1 = 0.3;
auto myFunc = [&p0, &p1] (double x) {return polyFunc(p0, p1 * x);};

I insist on the capture by reference to be able to change the value of the parameters at a later stage and have these values percolated inside the lambda.

I hope this helps.

1 Like

The capture method works perfectly–thank you!