Add a histogram to tree branch in rdataframes

Hi, I am new to rdataframes. I am having trouble in adding this to a tree branch . Here is my code

ROOT::RDataFrame rdf(my_chain);
auto rdf_variables = rdf.Define(“mmSq”, “missing_massSq”);

auto rdf_cut = rdf_variables.Filter(" (mmSq<=-0.04 || mmSq<=0.04) ");

ROOT::RDataFrame d(“tree name”, “rootfilename.root”);
auto myHisto = rdf_cut.Histo1D({“im”, “;M#Lambda);Counts”, 80, 1.00, 1.20}, “Lambda”,“acc_wtg”);

TTree *tree= new TTree (“tree name”,“outfiletree”);
I want to add “myHisto” to my tree. as a branch . I tried Snapshot(), didn’t work.
I appreciate your help

Hello @Chandra_Sekhar_Akond ,

and welcome to the ROOT forum.

I guess what you want to do is store the computed histogram in a ROOT file, for safekeeping. You don’t need a TTree for that, you can just store the histogram in a ROOT file directly (you can think of TTrees are tables, datasets – here you just want to store a single histogram object on disk).

Also note that after auto &actualHistogram = myHisto.GetValue();, the histogram has little to no relation with the RDataFrame, so I think the question is really how to store a ROOT histogram in a ROOT file. The relevant part of the ROOT manual should help, that is ROOT files - ROOT .

I hope this helps!

Hello @eguiraud ,
Thanks for the reply. No I specifically want to add this histogram as a branch to a ROOT file.
myHisto->Write(); works as a histogram , but for my specific analysis I need it in a branch.
I hope I explained the problem clearly. Thanks in advance.

I guess you mean a branch of a TTree. Then the relevant part of the manual is Trees - ROOT – again after RDataFrame has returned the histogram object you cannot use RDataFrame itself to store it into a TTree, you have to create an appropriate branch in a TTree and then write the histogram object to that branch. You can also search this forum and the ROOT tutorials for more examples, writing objects to TTrees is a common enough task that you should find many.


Are you trying to create a single histogram for all entries in your original TTree, or are you trying to create a corresponding histogram for EVERY entry of your original TTree? What you have above is the former (that one histogram will contain entries from EVERY entry that passes your filter cut!). Just want to make sure, because storing histograms per-entry in a TTree is atypical

1 Like