From this simple reproducer:
#include <TCanvas.h>
#include <TFile.h>
#include <TTree.h>
#include <ROOT/RDataFrame.hxx>
#include <iostream>
#include <string>
#include <vector>
void generateData(const char *filename, int n, double value)
{
TFile f{filename, "RECREATE", "file for testing"};
double var1;
TTree tree{"AnalysisTree", "AnalysisTree"};
tree.Branch("column1", &var1, "column1/D");
for (unsigned int i = 0; i < n; i++) {
var1 = value;
tree.Fill();
}
tree.Write();
f.Write();
f.Close();
}
int main()
{
std::vector<std::string> fileNames{"test1.root", "test2.root"};
std::vector<double> weights{2, 0.5};
// Create two files with trees for testing
// 10 entries of 0.5
generateData("test1.root", 10, 0.5);
// 10 entries of 2
generateData("test2.root", 10, 2);
// Create Dataframe from files
ROOT::RDataFrame df("AnalysisTree", fileNames);
// Define weights depending on input file
auto df2 =
df.DefinePerSample("weightbysample", [&fileNames, &weights](unsigned int, const ROOT::RDF::RSampleInfo &id) {
for (unsigned int i = 0; i < fileNames.size(); i++)
if (id.Contains(fileNames[i]))
return weights[i];
return -1.;
});
auto s0 = df2.Sum<double>("weightbysample");
auto d0 = df2.Display({"weightbysample"}, 20);
std::cout << "sum of weights: " << *s0 << "\n";
d0->Print();
auto s1 = df2.Sum<double>("weightbysample");
auto d1 = df2.Display({"weightbysample"}, 20);
std::cout << "sum of weights: " << *s1 << "\n";
d1->Print();
}
The DefinePerSample operation defines a column of 20 entries, 10 should have value 2, the following 10 should have value 0.5. The first set of Sum and Display operations show the correct behaviour, then the second set of operations reports a wrong result. All the 20 entries of the column are 0.5:
sum of weights: 25
+-----+----------------+
| Row | weightbysample |
+-----+----------------+
| 0 | 2.0000000 |
+-----+----------------+
| 1 | 2.0000000 |
+-----+----------------+
| 2 | 2.0000000 |
+-----+----------------+
| 3 | 2.0000000 |
+-----+----------------+
| 4 | 2.0000000 |
+-----+----------------+
| 5 | 2.0000000 |
+-----+----------------+
| 6 | 2.0000000 |
+-----+----------------+
| 7 | 2.0000000 |
+-----+----------------+
| 8 | 2.0000000 |
+-----+----------------+
| 9 | 2.0000000 |
+-----+----------------+
| 10 | 0.50000000 |
+-----+----------------+
| 11 | 0.50000000 |
+-----+----------------+
| 12 | 0.50000000 |
+-----+----------------+
| 13 | 0.50000000 |
+-----+----------------+
| 14 | 0.50000000 |
+-----+----------------+
| 15 | 0.50000000 |
+-----+----------------+
| 16 | 0.50000000 |
+-----+----------------+
| 17 | 0.50000000 |
+-----+----------------+
| 18 | 0.50000000 |
+-----+----------------+
| 19 | 0.50000000 |
+-----+----------------+
sum of weights: 10
+-----+----------------+
| Row | weightbysample |
+-----+----------------+
| 0 | 0.50000000 |
+-----+----------------+
| 1 | 0.50000000 |
+-----+----------------+
| 2 | 0.50000000 |
+-----+----------------+
| 3 | 0.50000000 |
+-----+----------------+
| 4 | 0.50000000 |
+-----+----------------+
| 5 | 0.50000000 |
+-----+----------------+
| 6 | 0.50000000 |
+-----+----------------+
| 7 | 0.50000000 |
+-----+----------------+
| 8 | 0.50000000 |
+-----+----------------+
| 9 | 0.50000000 |
+-----+----------------+
| 10 | 0.50000000 |
+-----+----------------+
| 11 | 0.50000000 |
+-----+----------------+
| 12 | 0.50000000 |
+-----+----------------+
| 13 | 0.50000000 |
+-----+----------------+
| 14 | 0.50000000 |
+-----+----------------+
| 15 | 0.50000000 |
+-----+----------------+
| 16 | 0.50000000 |
+-----+----------------+
| 17 | 0.50000000 |
+-----+----------------+
| 18 | 0.50000000 |
+-----+----------------+
| 19 | 0.50000000 |
+-----+----------------+
From this simple reproducer:
The
DefinePerSampleoperation defines a column of 20 entries, 10 should have value2, the following 10 should have value0.5. The first set ofSumandDisplayoperations show the correct behaviour, then the second set of operations reports a wrong result. All the 20 entries of the column are0.5: