TChain and histograms

Hi,
I am using root 4.04/02b
I notice a strange problem while running this :
{

gROOT->Reset();
gStyle->SetOptStat(1111);

TChain ch(“cnt”);
ch.Add("/projects/903/R1.18/mc_files/n13_n14/reducedL010185");

TH2F *shw_0_1_energy = new TH2F(“shw_0_1_energy”,“shw energy”,200,0,50,200,0,50);
TH2F *shw_0_1_vtxz = new TH2F(“vtxz”,“abs(trk vtx z - shw vtx z)”,200,0,7,200,0,7);

TCanvas c1(“c1”,"",0,0,900,900);
c1.Divide(2,2);

c1.cd(1);
ch.Draw(“shwl[0].ph:shwl[1].ph>>shw_0_1_energy”,“evsum.ntracks==1 && evsum.nshowers==2”,"");
shw_0_1_energy->GetXaxis()->SetTitle(“2ndary shower energy (GeV)”);
shw_0_1_energy->GetYaxis()->SetTitle(“primary shower energy (GeV)”);

c1.cd(2);
ch.Draw(“abs(trkl.beg.z-shwl[0].vtx.z):abs(trkl.beg.z-shwl[1].vtx.z)>>shw_0_1_vtxz”,“evsum.ntracks==1 && evsum.nshowers==2”,"");
shw_0_1_vtxz->GetXaxis()->SetTitle(“2ndary shower delz (GeV)”);
shw_0_1_vtxz->GetYaxis()->SetTitle(“primary shower delz (GeV)”);

}
If I run this file twice in the same root session, I see a warning message:
Warning in TH1::Build: Replacing existing histogram: shw_0_1_energy (Potential memory leak)

But I dont see this message when I use the exact same script with a TFile instead of a TChain.

                                       -thanks Debdatta.

}

This is the expected result.
When you run with a TFile and you create the histogram after opening the file, the histogram is registered to the TFile directory. When you close
the file, the corresponding histogram is deleted.
In case of a TChain, the histogram is registered to the default directory gROOT. Next time you run the script, the histogram is still there.
Because you are recreating it, we print a warning message to indicate
that the previous histogram is automatically deleted.
You can avoid the message by doing
-TH1::AddDirectory(kFALSE); histo will not be registered to any dir
or
-shw_0_1_energy->SetDirectory(0);

Rene