TFolder structure -> TDirectory structure

Hi all,

I have a TFolder structure in memory that I’d like to save to a file while translating the folder hierarchy into a TDirectory heirarchy. I feel that my users are more used to accessing histograms in the TDirectory structure.

Is there a preexisting feature that does the translation? I looked but did not find one and so I wrote this little bit of void trans(TFolder* f, TDirectory* bd){ // translate the folder heirarchy hanging off f // into a directory heirarcy inside the directory pointed to by bd // if bd is null, translate into the current directory (gDirectory) // also append any objects held by the folders (e.g. histograms) // to the new directory structure if(!bd) bd=gDirectory; // make a directory for this folder TDirectory* dir = bd->mkdir(f->GetName()); dir->cd(); // look for subfolders of this one TCollection* col = f->GetListOfFolders(); TIter it(col->MakeIterator()); TObject* obj; while ( obj=it() ){ if(obj->InheritsFrom("TFolder")){ TFolder* fol = (TFolder*) obj; // I'm recursive! trans(fol); dir->cd(); } else{ dir->Append(obj); } } }

This appears to do what I want. Could it be added as a static method in either TFolder or TDirectory? By the way, the TFolder::GetListOfFolders() call is very poorly named since it returns a list of objects only some of which are folders. This really calls for a comment in the html documentation.[/code]

Regards,

mike kordosky

Mike,

This is not a safe solution because the same objects will be owned by two different parents.
May be what you want to do is simply write to the current directory objects from a folder?

Rene

Hi Rene,

[quote=“brun”]Mike,

This is not a safe solution because the same objects will be owned by two different parents.
May be what you want to do is simply write to the current directory objects from a folder?

Rene[/quote]

In my specific case calling:

obj->Write()

rather than

dir->Append(obj)

is indeed what I ultimately did when implementing the function in my code. I thought that simply appending to a directory would perhaps be a little better for general use. However, isn’t it possible to remove the non-folder objects from ownership by the folders? I my case, the folders do not own the objects contained therein and I haven’t played around much with cases in which they do. What about calling:

bf->Remove(obj);

before the call to TDirectory::Append(). My only question is if this invalidates my iterator.

mike

OK Mike,

It was my guess that you just want to write the contents of a TFolder to a file where each object is a key and subfolders become directories.
If this is the case, you just need a function TFolder::Write. Right?

Rene

Hi Rene,

Yes. The function above does what I want but it could certainly be made a TFolder member function. However, I think TFolder::Write() may be already implemented and writes the folder itself to disk. So, pehaps TFolder::WriteAsDir() or some such?

mike