Can't access Histograms from a ROOT file


I’m having issues accessing Histograms in a ROOT file (example attached). I created this .root file myself from an ASCII file with my own program. When I do

$ root histos.root
root[0] TBrowser b 

I can see all 6 histograms in the browser. However, doing

root[1] _file->ls()

returns only

TFile**		histos.root	
 TFile*		histos.root

Without any of the objects listed. Likewise doing

root[2] _file->Print()

returns only

TFile: name=histos.root, title=, option=READ

With no objects, unless I double click on a histo in the TBrowser. In which case it returns

TFile: name=histos.root, title=, option=READ
TH1.Print Name  = dsigma/dpT:pT, Entries= 175, Total sum= 0.134773

but no change for ls(). Ultimately, I have several of these .root files and wish to superimpose histograms of the same name from each one. I would hope to be able to access the histograms using something like

root[4] TH1F *hist1 = (TH1F*)_file0.Get("dsigma/dpT:pT")
root[4] hist1->Draw()
Error: illegal pointer to class object hist1 0x0 157  (tmpfile):1:
*** Interpreter error recovered ***

I am not convinced that the histograms are being stored correctly, but confused that they appear to be healthy in the TBrowser. Can anyone tell just by examining the .root file what the issue may be? I can also upload the code I used to output the .root if required.

Thanks in Advance for your help,
histos.root (13.6 KB)

Hi Declan,

You shouldn’t use the ‘/’ and ‘:’ characters in the histogram name, they are separators used for directories and cycles (versions). For example, if you rename “dsigma/dpT:pT” into “dsigma_dpT_pT” (from the browser), then it will be displayed when typing “.ls”:

root [15] .ls TFile** histos.root TFile* histos.root OBJ: TH1D dsigma_dpT_pT : 0 at: 004DFED8
And so you can call:

root [16] dsigma_dpT_pT->Draw()
root [17]

Cheers, Bertrand.

Hi Bertrand,

Solved. Thanks very much. I’d been tearing my hair out over that one.



Would it be possible to throw an exception, to prevent someone from writing a histogram with these forbidden characters in the histogram name? This would help prevent people from developing code that causes others problems.

For example, RIVET 2.2.x (which is used for MC validation) writes histograms using YODA, which can then be converted to ROOT. However, YODA allows “/” in the histogram name and happily writes them to the output ROOT file. The result is that one cannot read the histograms from the validation tool. If ROOT threw an exception when the histogram was written the YODA/RIVET developers would immediately know that something was wrong and change their code.

Thanks and best regards,


Hi Will,

I came across this thread trying to read in root files from Rivet 2.2.X and I figure others here might also need a non TBrowser based solution to use these. Even with the bad names you can grab the histograms from the directory’s keylist and rename them. Afterwards you can use them like normal.

i.e. in python.

in_folder_name="MC_EXT" in_folder=root_file.Get(in_folder_name) for key in in_folder.GetListOfKeys(): out_name=key.GetName().replace("/","").replace(in_folder_name,"") key.SetName(out_name) hist=in_folder.Get(out_name) hist.SetName(out_name)