Please read tips for efficient and successful posting and posting code
ROOT Version: 6.30.06
Platform: macosxarm64
Compiler: Apple clang version 15.0.0
The following snippet is converted from the RDataFrame Crash Course (section Defining Custom Columns)
void tmp(){
ROOT::RDataFrame d(10); // an RDF that will generate 100 entries (currently empty)
int x = -1;
auto d_with_columns = d.Define("x", [=]()mutable->int { return ++x; })
.Define("xx", [=]()mutable->int { return x*x; });
// d_with_columns.Snapshot("myNewTree", "newfile.root");
d_with_columns.Display()->Print();
std::cout << "Original x is " << x << std::endl;;
}
The result is, somewhat to my surprise,
root [0]
Processing tmp.C...
+-----+---+----+
| Row | x | xx |
+-----+---+----+
| 0 | 0 | 1 |
+-----+---+----+
| 1 | 1 | 1 |
+-----+---+----+
| 2 | 2 | 1 |
+-----+---+----+
| 3 | 3 | 1 |
+-----+---+----+
| 4 | 4 | 1 |
+-----+---+----+
Original x is -1
I had imagined that capturing by value would make the first column ”x” all 0s.
So my guess is the captured value is static across function calls as we iterate over the rows?
But then if the captured variable is static, how come the second column isn’t affected?
My question is:
When should I expect this “static” behavior? I’d like to avoid surprises.
Many thanks for your time!