Problems when drawing a TH1F* recoverd from a .root file

Hello,

I have recorded a TH1F in a .root file.
When I open a TBrowser, I can see my histogram, everything is ok, I can see its content.
But when I try to open and draw it from a macro, only a blank canvas opens (code below).

Does anybody have an idea why ?

Thanks,

Marie-Helene

[code]

TFile file1(“PSF.root”);
TH1F* PSF_histo=(TH1F*)file1.Get(“ym_histo”);
gStyle -> SetStatW(0.28);
gStyle -> SetStatH(0.13);
gStyle -> SetStatColor(41);
gStyle -> SetStatX(0.87);
gStyle -> SetStatY(0.85);
gStyle -> SetStatFont(132);
gStyle -> SetOptStat(111);
gStyle -> SetOptFit(0000);
gStyle->SetLabelFont(132,“xyz”);
gStyle->SetLabelSize(0.06,“xyz”);
gStyle->SetTitleFont(132,“xyzpad”);
gStyle->SetTitleSize(0.06,“xyz”);
gStyle->SetPalette(1);

c1 = new TCanvas(“Canvas1”,“1”,1);
c1->SetFillColor(0);
c1->SetBorderMode(0);
PSF_histo->Draw();[/code]

Hi Marie-Helene,

Try to add PSF_histo->SetDirectory(0); or add TH1::AddDirectory(kFALSE); at the top of your script. And read the chapter about Object Ownership in the Users Guide :wink:

Cheers, Bertrand.

Thanks, this works. :smiley:
But I don’t really understand the problem here. :question:
When I create directly a TH1F and fill it, I own it but when I get an histo from one file, ROOT owns it ? That ’ s why it is deleted ?

Yes. If you read the chapter about Object Ownership in the Users Guide:

[quote]When a histogram, tree, or event list (TEventList) is created, it is added to the list of objects in the current directory by default[/quote]When the current directory is a TFile, when the TFile is deleted (e.g. when going out of scope) the objects (the histogram in your case) are deleted.

Ok, Thanks again

You’re welcome :slight_smile: