ROOT histogram in TTree


I have an histogram as a TBranch element in a TTree (“Detector_PV_Detector_PV_depositedEnergy”), this histogram (or the TBranch) is called “depositedEnergy” and I would like to clone it, the following code doesn’t work, It gives me a segmentation error meaning it didn’t find my histogram and I don’t know how to correct it :

    TFile *f=new TFile(name.c_str());
    TDirectory *d = (TDirectory *) f->Get("Detector_PV_Detector_PV_depositedEnergy");
    TTree *T = (TTree*)f->Get("Detector_PV_Detector_PV_depositedEnergy"); //open the tree
    TH1F* hkg = (TH1F*)d->Get("depositedEnergy");
    TH1F* hclone = (TH1F*)hkg->Clone("hclone");

Well I wanted to open the Tree and then to ‘Get()’ the histogram, but the Three doesn’t have the method Get(). Could you help me please ?


_ROOT Version: 6.10
_Platform: macOS

Hi Pierre,

Just to make sure I understand correctly: you have not just one histogram in the ROOT file but a histogram for every entry of the tree, right?

In this case, I’d suggest to use a TTreeReader to get the histogram(s). You can construct the TTreeReader directly with the tree, like TTreeReader reader(T);


1 Like

Hallo @jblomer ,

I think I made a mistake : In my ROOT file, I have TTrees which contain TBranch. So basically it’s not a histogram, is that it ?
I made the mistake because when I double clicked on it, I get a plotted histogram, hence my confusion…
It’s more of a beginner vocabulary question but It will help me to read clearly the root documentation.

It’s a bit of a simplification but assuming your data is a table, the table rows are the “entries” or “events” and the table columns are the “branches”. A tree often has numerical branches. For instance it may have a branch of type float called “missingEnergy”, meaning a column of floats, one float per event. You can use TTree::Print() to get an overview of the branches and their types.

In the ROOT browser, if you double click on a numerical branch, it will show you a histogram of all the values in that branch. That’s a convenience feature of the browser.

1 Like

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