Accessing data in root file

Hello,
first of all, sorry if I’m not using the proper terminology.
I have a TTree file containing some branches, as shown in the attached image:

For the first branch I managed to draw the histogram but I don’t know how to proceed for the other ones and obtain the correspondant graphs…

Thank you

If you are reading as TTree (not RDataFrame), see the read function in this tutorial:
https://root.cern/doc/master/hvector_8C.html
In short: create vector pointer(s), use SetBranchAddress, loop over the tree to get each event, and within each event, loop over the elements of the vector(s).

Hello @liz, thank you for posting your question.

In general, I would recommend going through the manual to get a better understanding of the TTree interface.

For your specific question: the recommended way to process and/or visualize the data inside a TTree is through RDataFrame.
For example, a simple way to visualize the data inside your TTree’s branches is:

ROOT::RDataFrame d("B5", "your_file.root");
auto myHisto = d.Histo1D("Etot"); // replace with whichever branch you want
myHisto->Draw();

RDataFrame also offers a number of other operations that you may want to perform on your data: refer to the documentation linked above if needed.

Thank you very much!! I’ll defintely look a bit into RDataFrame…
May I ask another question? I tried to follow the same method you told me about in order to plot a 2D histogram; this is what I did:

root [3] ROOT::RDataFrame d("B5", "B5ntuple.root");
root [4] auto myHisto2D = d.Histo2D({"histo", "XV vs YV;XV;YV", 100, 0, 100000000, 100, 0, 100000000}, "XV", "YV");
root [5] myHisto2D->Draw("COLZ")

XV and YV are the names of two branches containing vectors.
However, after the “Draw” instruction, root just crashes (there are no errors apparently, it just makes me go back to my directory and I have to start over)
I don’t know if I’m doing something wrong or if maybe it’s just a lot of data to process…

Hi liz,
the snippet you posted looks ok and it should work in principle. Could you please share the complete reproducer so we can have a look at it?

This is what I do:
First of all,

root -l readNtuple.C

and then I use a loop in this way:


It goes through 100 events and it does take a little while to do it… then I use the instructions I wrote in my previous reply:

For example, drawing a 1D histogram is quite time consuming but at the end I get the graph.
Screenshot 2024-08-07 114556

But for the 2D one, it just crashes :smiling_face_with_tear:
I’m sorry, I don’t think I can attach all the actual files…

Thank you for providing more details.

I imagine you’re filling your TTree branches from n.Loop() and then reading them back with RDataFrame right after it. If that’s the case, there might be some resource management error in the Loop() function which causes errors when reading back the data (e.g. maybe the file doesn’t get closed properly before invoking RDataFrame::Draw).

Unfortunately it’s hard to tell what the problem is without knowing what Loop() does exactly. If you want a more thorough answer, you can send me at least the code for the readNtuple class and a small sample of the data you’re processing (you can do it via private message if you prefer).