RE: a problem in reading TFile

From: Fine, Valeri <>
Date: Wed, 11 Oct 2006 10:04:25 -0400

Hello Liang,      

From: on behalf of Zhijun Liang Sent: Wed 10/11/2006 9:29 AM
Subject: [ROOT] a problem in reading TFile

hi :
I found a problem in reading TFile
I want to read a object from a root file ,but meet some error . here is the code :
TFile f("hsimple.root")
TH1F *hist=(TH1F *) f->Get("hpx"))
TH1F *hpx1=hpx->Clone("hpx")

and the "hsimple.root " is from tutorial directory. error information appears

do you know what is the problem ?

It is not a problem. It is the ROOT feature.  

The TFile object "owns" its histogram objects including all objects created "somewhere" by your code , Those are not connected "explicitly" with your file are owned also . As soon as you close the file all histogram objects those belong to the file are deleted. As result your "hist" and "hpx1" pointers point to the "dead" objects leading to the program crash.  

To avoid the crash "disconnect" the histogram from the file with SetDirectory method:  


should do the trick. In fact you may not call "Clone" at all.  

hist->SetDirectory(0) ;  

should be sufficient.  

Hope this helps.


thank you
best regards
Liang Received on Wed Oct 11 2006 - 16:06:42 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:32:01 MET