Skip to content

Instantly share code, notes, and snippets.

@AndreasAlbert
Last active March 18, 2019 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreasAlbert/c4de903c5e547171dc0181377ff1c7d5 to your computer and use it in GitHub Desktop.
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.
#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;
}
@AndreasAlbert
Copy link
Author

Compiled with

g++ alias_demo.cc `root-config --cflags --glibs` -std=c++11 -o alias_demo.o

and ROOT version 6.16/00

@AndreasAlbert
Copy link
Author

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

@AndreasAlbert
Copy link
Author

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