Passing directory object

Hello all,

Is there a way to get a directory pointer from a file … something like

TDirectory wdir = (TDirectory)OutputFile->GetObject(initialDirectory);!!!

In my case, a certain directory has lost association with the output file and I just need to ‘re-attach’ them. Here is a sample run time error message:

Error in <TDirectory::WriteTObject>: The current directory (TimeHistograms) is not associated with a file. The object (hTimeDiff_on) has not been written.

A simple gDirectory->pwd() shows that the file is still active and I am in the correct directory right before I attempt to write out my histograms.

erdos
Root: 5.17/05

ps I know its extremely frustrating to not have a sample code but it wouldn’t make any sense unless I post the entire thing (!)… sorry! Here is the gist though:

[code]
TFile *foutfile=new TFile(…);
foutfile->cd();
TDirectory *idir = new TDirectory(name,title);
idir->cd();
MethodA(idir);

void MethodA(TDirectory *idir){

idir->cd();
TH1F *h=new TH1F(…);
h->FillRandom(…);
h->Write();

}[/code]

EDIT: Here is a more concrete example. It seems that idir->GetFile() is returning NULL but I don’t know how to set it to the the file pointer f. [Incidentally, there is no such error messages in my old version of root(v5.06)].

root [0] TFile *f = new TFile("mohaha.root","RECREATE")     
root [1] TDirectory *idir = new TDirectory("junk","name","")
root [2] idir->cd()                                         
(Bool_t)(1)
root [3] gDirectory->pwd()
mohaha.root:/junk
root [4] idir->GetFile()
(const class TFile*)0x0
root [5] TH1F h("h","sd",100,0,1000);                       
root [6]  h.FillRandom("gaus",1000)                         
root [7] h.Write()                                          
Error in <TDirectory::WriteTObject>: The current directory (junk) is not associated with a file. The object (h) has not been written.
(Int_t)(0)
root [8] .q

Replace lines like:

TDirectory *idir = new TDirectory(name,title); by

TDirectoryFile *idir = new TDirectoryFile(name,title); or simply

TDirectory *idir = foutfile->mkdir(name,title); idir->cd();
Rene