Add Friend with RDataFrame

Hello,

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 ?

Cheers,
Abdellah


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi Abdellah,
this is unexpected, of course.

Some information that might help clarify what’s going on:

  1. is this with the latest ROOT version (v6.22/02)?
  2. if not, could you try upgrading or using v6.22/02 from lxplus and see if that resolves the issue?
  3. do both trees have a coskp_coskm branch?
  4. does using "nominal.coskp_coskm" instead of "coskp_coskm" work around the problem?

Cheers,
Enrico

Hi,

Thank you for your responce.

  1. is this with the latest ROOT version (v6.22/02)?
    i am using ROOT Version: 6.20/06

  2. do both trees have a coskp_coskm branch?

yes but with different name : “reco_coskp_coskm” in nominal tree and “coskp_coskm” in truth tree.

  1. does using "nominal.coskp_coskm" instead of "coskp_coskm" work around the problem?

mmm it s gives me an errors that nominal.coskp_coskm is not known

Cheers,
Abdellah

I am confused, you said

i got diffrent plot of variable coskp_coskm that it does nt match what in nominal tree

but then you mentioned that the nominal tree does not have a branch called “coskp_coskm”.

So I’m not sure what

 auto hreco = df.Histo1D("coskp_coskm"); 
 auto htruth = df.Histo1D({"htruth", "", 100, -1.15, 1.15}, "mtruth.coskp_coskm");

should plot. Should the first line be df.Histo1D("reco_coskp_coskm")?

Hi,

My bad, it must be :

auto hreco = df.Histo1D(“reco_coskp_coskm”);
auto htruth = df.Histo1D({“htruth”, “”, 100, -1.15, 1.15}, “mtruth.coskp_coskm”);

And this not resolve my problem ?
Cheers

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?

Hi,

 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 :slight_smile:

I hope i explain well my problem?
Many thanks for your time

Abdellah

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?

Cheers,
Enrico

The file is quit heavy to uplead it here !

So you can acess via this path which i will delete after this problem is solved :

/afs/cern.ch/user/a/atnourji/public/ForEnrico

Thanks in advance
A.Tnourji

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.

Ok, many thanks

I just test using TTree and it works !! So it this mean that TTree somehow doesn t effect by difference of different enteies in both tree !

 // Original file with all hit info
 TFile *ftime  = TFile::Open("../SpinCorrTree/mc/output/SpinCorrTree_all.root");
 TTree *t = (TTree*) ftime->Get("nominal");
 TTree *tf = (TTree*) ftime->Get("truth");
 
 t->AddFriend(tf, "mtruth");   
 
 TH1F *hreco = new TH1F("hreco", "", 150, -1.15, 1.15); 
 TH1F *htruth = new TH1F("htruth", "", 150, -1.15, 1.15); 
 
 t->Draw("reco_cos_k_p >> hreco", "", "goff"); 
 t->Draw("truth.cos_k_p >> htruth", "", "goff"); 
 
 auto c = new TCanvas("c", "", 2000, 1400);
 
 c->Divide(2,1); 
 c->cd(1); 
 hreco->DrawClone(); 
 
 c->Update();

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

This is now reported as issue https://github.com/root-project/root/issues/6518

Cheers,
Enrico

1 Like

Hi,

Ok, Many thanks

Cheers,
A.Tnourji

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.