I would like to create, for a nominal tree a friend tree ( particleLevel tree ) usiing RDataFrame, the problem is that i got a different distrubtion of var1 from nominal tree that it doesn’t match with what i have before adding particleLevel tree as friend ?
Here is my code :
TFile *f = TFile::Open("file.root");
auto t = f->Get<TTree>("nominal");
auto ft = f->Get<TTree>("truth");
t->AddFriend(ft, "mtruth");
ROOT::RDataFrame df (*t);
auto hreco = df.Histo1D("coskp_coskm");
auto htruth = df.Histo1D({"htruth", "", 100, -1.15, 1.15}, "mtruth.coskp_coskm");
auto c = new TCanvas("c", "", 2000, 1400);
hreco->DrawClone();
c->Update();
Here i got diffrent plot of variable coskp_coskm that it does nt match what in nominal tree ?
Sorry, I don’t understand what the problem is.
Do you get different histograms for the same variable using RDataFrame and another method?
Or do you expect hreco and htruth to look the same but they do not?
TFile *f = TFile::Open("file.root");
auto t = f->Get<TTree>("nominal");
auto ft = f->Get<TTree>("truth");
t->AddFriend(ft, "mtruth");
ROOT::RDataFrame df (*t);
auto hreco = df.Histo1D("reco_coskp_coskm");
auto htruth = df.Histo1D({"htruth", "", 100, -1.15, 1.15}, "mtruth.coskp_coskm");
auto c = new TCanvas("c", "", 2000, 1400);
hreco->DrawClone();
c->Update();
Here When I just plot “reco_coskp_coskm” which is a variable stored in nominal tree, i got a different distrubtion form what i can see using nominal->Draw(“reco_coskp_coskm”) ?
when i delete the part responsible of addign truth tree as friend :
TFile *f = TFile::Open("file.root");
auto t = f->Get<TTree>("nominal");
ROOT::RDataFrame df (*t);
auto hreco = df.Histo1D("reco_coskp_coskm");
auto c = new TCanvas("c", "", 2000, 1400);
hreco->DrawClone();
c->Update();
I got the same distribution
I hope i explain well my problem?
Many thanks for your time
Ok, so adding the tree "truth" (which does not contain a branch called reco_coskp_coskm) as friend of tree "nominal" changes how RDataFrame plots variable reco_coskp_coskm. Sounds like a bug!
Can you share file.root with me so I can investigate?
That’s perfect, thanks!
What’s causing the problem is that “nominal” and “truth” are breaking the assumption that friended trees must have the same number of entries. “nominal” has 475002 entries while “truth” has 720000, and RDF gets confused and when the friend is present it loops over 720000 entries, filling the histogram with the same number 720000 - 475002 times.
This usecase is formally unsupported, but the nicer behavior on the part of RDataFrame would be to respect the number of entries in the main tree when running the event loop. I’ll see what can be done about that.
Yes, TTree does the more sensible thing and it truncates the event loop to the amount of entries in “nominal”.
However, you will probably get the “wrong” number of entries if you try to plot a variable in “truth”. In the end the assumption is typically that main tree and friend tree have the same number of entries.
RDataFrame’s problem seems to be due to TTreeReader, which RDataFrame uses internally to read the data.
A workaround for now seems to be adding ROOT::EnableImplicitMT(); at the beginning of your code: it activates ROOT’s implicit multi-threading, so it changes the way RDataFrame runs the event loop in such a way that the bug is fixed problematic behavior is not triggered.
A reproducer that depends only on TTreeReader:
TFile f("/afs/cern.ch/user/a/atnourji/public/ForEnrico/SpinCorrTree_all.root");
auto nominal = f.Get<TTree>("nominal");
auto truth = f.Get<TTree>("truth");
nominal->AddFriend(truth);
int counter = 0;
TTreeReader r(nominal);
while (r.Next())
++counter;
std::cout << counter << std::endl; // prints 720000 instead of 475002