Re: [ROOT] adding object to a directory?

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Dec 06 2002 - 08:33:25 MET


Hi,

Your histograms are stored in the top directory because you
put them explicitly there with:
       f->Append(h1);
       f->Append(h2);

You can simplify your program like shown below

void testDir(void){

        TBrowser *b = new TBrowser();

        TH1F *h1 = new TH1F("h1", "h1", 100, -10, 10);
        TH1F *h2 = new TH1F("h2", "h2", 100, -10, 10);

        h1->FillRandom("gaus",1000);
        h2->FillRandom("gaus",1000);

        TFile *f = new TFile("xxxDir.root", "Recreate");

        TDirectory *d1 = f->mkdir("d1");
        TDirectory *d2 = f->mkdir("d2");

        //d1->cd(); //not needed
        d1->Append(h1);

        //d2->cd();
        d2->Append(h2);

        f->Write();
        f->Close();

        f = new TFile("xxxDir.root");

}

Rene Brun

On Thu, 5 Dec 2002, Yiqun Wang wrote:

> Hi,
> 
> I am using root 3.03.09b (compiled myself). I want to add objects to
> certain directories of a root file, after the objects are created (not
> before). Here is my script:
> 
> void testDir(void){
> 
> 	TBrowser *b = new TBrowser();
> 
> 	TH1F *h1 = new TH1F("h1", "h1", 100, -10, 10);
> 	TH1F *h2 = new TH1F("h2", "h2", 100, -10, 10);
> 
> 	h1->FillRandom("gaus",1000);
> 	h2->FillRandom("gaus",1000);
> 
> 	TFile *f;
> 
> 	f = new TFile("xxxDir.root", "Recreate");
> 
> 	f->mkdir("d1");
> 	f->mkdir("d2");
> 
> 	f->cd("d1");
> 	f->Append(h1);
> 
> 	f->cd();
> 
> 	f->cd("d2");
> 	f->Append(h2);
> 
> 	f->Write();
> 	f->Close();
> 
> 	f = new TFile("xxxDir.root");
> 
> }
> 
> 
> When I run to the following script, in the root file created, objects "h1"
> and "h2" are listed along side folders "d1" and "d2", not in them.
> 
> Please tell me what I did wrong! Thanks!
> 
> Yiqun Wang
> 
> 
> 



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:22 MET