Hi Yiqun, This should work. Which version of ROOT are you using? Please read: http://root.cern.ch/root/roottalk/RoottalkRules.html Rene Brun On Tue, 17 Dec 2002, Yiqun Wang wrote: > ROOTers: > > I am writing a program that reads TTree in a root file, does something, > and then writes to another root file. I want to write histograms in > seperate directories of the output file. Below is an example: > > > void dirCd(void){ > > TBrowser *b = new TBrowser(); > > TH1F *h0 = new TH1F("h0", "h0", 100, -10, 10); > h0->FillRandom("gaus",1000); > > TFile *f1 = new TFile("xxx.root"); > > TFile *f2; > > f2 = new TFile("cdDir.root", "Recreate"); > > TDirectory *d1 = f2->mkdir("d1"); > TDirectory *d2 = f2->mkdir("d2"); > > d1->cd(); > > TH1F *h1 = new TH1F("h1", "h1", 100, -10, 10); > > d2->cd(); > > TH1F *h2 = new TH1F("h2", "h2", 100, -10, 10); > > h1->FillRandom("gaus",1000); > h2->FillRandom("gaus",1000); > > > f2->Write(); > f2->Close(); > > f1->Close(); > > f2 = new TFile("cdDir.root"); > > } > > > This macro fails at the two places when TDirectory::cd() is called. If you > comment out both "d1->cd();" and "d2->cd();", the code will run (but "h1" > and "h2" are created in "/" of file "f2". > > What is the problem? Or any suggestion to accomplish my goal in another way? > > Thanks! > > Yiqun Wang > >
This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:24 MET