-
-
Save AndreasAlbert/c4de903c5e547171dc0181377ff1c7d5 to your computer and use it in GitHub Desktop.
Code to demonstrate a feature of the RDataFrame::Alias function I do not understand.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include<TTree.h> | |
#include <TROOT.h> | |
#include <ROOT/RDataFrame.hxx> | |
using namespace std; | |
// Helper function adapted from | |
// https://root.cern.ch/doc/master/df001__introduction_8C.html | |
// Creates an input example tree | |
void fill_tree(const char *treeName, const char *fileName) | |
{ | |
ROOT::RDataFrame d(10); | |
int i(0); | |
d.Define("oldVar", [&i]() { return (double)i++; }) | |
.Snapshot(treeName, fileName); | |
} | |
int main() { | |
// Preparation | |
auto fileName = "demo.root"; | |
auto treeName = "demotree"; | |
fill_tree(treeName, fileName); | |
// Read input | |
ROOT::RDataFrame rdf(treeName, fileName,{"oldVar"}); | |
cout << "The mean value of oldVar is: " << *rdf.Mean("oldVar") << endl; | |
// On one subnode of the original data frame, we simply alias | |
// "newVar" to be identical to "oldVar" | |
auto r1 = rdf.Alias("newVar","oldVar"); | |
auto mean_r1 = *r1.Mean("newVar"); | |
cout << "The mean value in r1 is: " << mean_r1 << ", expected: 4.5" << endl; | |
// On a second subnode, we define newVar to be twice oldVar | |
// This is where the problem appears | |
auto r2 = rdf.Define("newVar","2*oldVar"); | |
auto mean_r2 = *r2.Mean("newVar"); | |
cout << "The mean value in r2 is: " << mean_r2 << ", expected: 9.0"<< endl; | |
// The problem goes away if we use another name for our variable | |
auto r3 = rdf.Define("otherVar","2*oldVar"); | |
auto mean_r3 = *r3.Mean("otherVar"); | |
cout << "The mean value in r3 is: " << mean_r3 << ", expected: 9.0"<< endl; | |
} |
Output:
$ ./alias_demo.o
The mean value of oldVar is: 4.5
The mean value in r1 is: 4.5, expected: 4.5 # Good
The mean value in r2 is: 4.5, expected: 9.0 # Bad
The mean value in r3 is: 9, expected: 9.0 # Good
The problem can be solved by using replacing Alias
-> Define
on Line 32.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compiled with
g++ alias_demo.cc `root-config --cflags --glibs` -std=c++11 -o alias_demo.o
and ROOT version
6.16/00