Closing file and object scope?

Hello,

I’m trying to add several similar histograms from different root files, but I’m facing a problem when closing the input file. It looks the object is being deleted when I close the input file.
Here is an extract of the code to reproduce the problem. It can be run using the input file attached.

[code]#include
#include “TFile.h”
#include “TH2D.h”
#include “TCanvas.h”
#include “TApplication.h”

using namespace std;

int main() {
TApplication app(“test”,0,0);

TH2D *myHisto;

TCanvas *c1=new TCanvas(“c1”,“c1”,800,600);
c1->SetFillColor(0);

TFile myfile(“input.root”,“READ”);
if(!myfile.IsOpen()) {
cerr<<“Error, could not read input file”<<endl;
return 1;
}
TH2D hist_tmp;
myfile.GetObject(“mh2AccAZLambda_00”,hist_tmp);
if(!hist_tmp) return 1;
myHisto=new TH2D(
((TH2D*)hist_tmp->Clone(“mh2AccAZLambda_00_copy”)));
myfile.Close();
myHisto->DrawCopy(“colz”); app.Run();

return 0;
}
[/code]
when I run it I have the following error:

[quote]==> Dumping object at: 0x0000000011652300, name=TObject, class=TObject

fUniqueID 0 object unique identifier
fBits 0x01000208 bit field status word[/quote]

If I exchange the following lines, it runs fine (but I need to encapsulate this in a loop, so this is not a solution for me).

myfile.Close(); myHisto->DrawCopy("colz"); app.Run();

I tried to look in the documentation and the forum without success. Any idea of what I am doing wrong?

EDIT: If this matters, I’m using 5.28/00c on Lyon cluster.

Thanks,
Colas
mini.tar.gz (1.39 MB)

Hi,

At the beginning of your script add the lineTH1::AddDirectory(kFALSE);and read explanation in the chapter about Object Ownership in the Users Guide.

Cheers, Bertrand.

Thanks for the fast answer, it worked without problem.
Cheers, Colas

You’re welcome! Glad to see it solved your problem :slight_smile:
Cheers, Bertrand.