Read histogram from file

Dear Rooters,

I cannot read histogram of the file. I tried to repeat the tutorial code, but it does not work.

My histogram name is “LDJ”, which is stored in kq.root; I can browser it from root browser, but I cannot call it in macro.

I includes here two versions of code: one is my real code (test.C), and one is simple code (test2.C) that i tried to repeat the tutorial. Both of them did not give me the result.
test2.C (416 Bytes)
kq.root (3.8 KB)
test.C (2.3 KB)
I used root 5.34.32 on window 7 64 bit.
Thank you,


Don’t write your own main() unless you really have to. You are missing an event loop; you want GUI interaction - all of that is provided if you run your code in ROOT, instead of building your own main().

(You’re missing a TApplication object; its Run() method creates an event loop.)


Thank Axel,

Could you please give me an example. I do not really understand how to fix, I usually use main() without problem, it’s only like a subroutine.

For compiler, I used .L test.C+ then main()

Thank you again,


O I see, you are running this inside ROOT! Good!

So the problem is that the TFile is destructed at the end of the function, and (good old ROOT behavior…) TH1 objects are owned by the TFile - i,e. the file destroys the histogram.

This should fix it:

#include <TH1.h>
#include <TFile.h>

void main()
	TFile f("kq.root");	
	TH1D* h = (TH1D*)f.Get("LDJ");

Does it?


Thanks Axel,

That now runs in example file, but crash in my code. Could you please explain me a little bit about SetDirectory, I think that problem come from the command.

This is my program igg.C (9.2 KB)

Any more info on the crash? Do you have a backtrace?

It’s crashed without error information. (Not responding very long).

Your macro igg,C runs fine for me on Mac using the ROOT master. The only thing I had to do was to change void main() into void igg()

Dear couet, with the same macro.

If I try to
.L igg.C+
Then crash without any information.

If I try:
.L igg.C
this happens:
Error: illegal pointer to class object LDJ 0x0 1430 D:\OneDrive\Espace de travail\00 - Duy Tan 2017\Igg2\igg.C(338)
*** Interpreter error recovered ***

igg.C (9.2 KB)

Could you guess where problem comes from?


I see that the folder name where you execute your macro contains spaces and special characters like minus. Can you try to execute it in a folder without spaces and special characters ?

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