Re: [ROOT] merging directories

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri May 16 2003 - 10:13:01 MEST


Hi Michael,

When reading the directory fileDir, its objects are added
to the input->GetList() automatically.
In your statement
   dbDir->Append(subDir);
you also put a rreference to some of these objects in your new directory.
However, when deleting input, you will get dangling pointers in your new
directory because they are deleted with input.
You must remove the reference to these objects from input.
Add the line marked with //<======== in your function readDb.

Rene Brun

Michael Gold wrote:
> 
> Dear rooters:
> 
> here is my problem.  I have files which all have a certain
> directory ( here called dbDir ).  This directory has a
> number of subdirectories, each subdirectory contains some
> TObjects.  I'd like to collect all the subdirectories from
> multiple files and put them into a single dbDir, then
> append this to an output file.  below is my attempt
> to do this.  the program will seg violate at the end
> when I try to write the output file.  If I remove the
> line "delete input", then the program does not seg violate,
> but then the output file directory dbDir is empty.
> 
> suggestions?
> 
> here is the basic loop in the main code:
> 
> int main( int argc, char *argv[] ) {
> 
> ....
>   // create directory for db files in fout
>   TDirectory *dbDir =  new TDirectory("db"," data base stuff");
> 
>   // Recreate output file
>   TFile *fout = new TFile(outFileName.c_str(),"recreate");
> 
> ....
> 
>   //loop over files
>   while(getline(fileList,inFile,'\n')) {
>     ++ nFiles;
>     // read db files
>     readDb(inFile,dbDir);
>   }// end loop over files
>   dbDir->ls();
>   fout->Append(dbDir);
> ......
>   fout->Write();
> }
> 
> here is the subroutine that attempts to
> read from input file into dbDir:
> 
> void readDb(string inFile, TDirectory *dbDir)
> {
>     TFile *input = new TFile(inFile.c_str());
>     TDirectory  *fileDir  = (TDirectory *) input->GetKey("db")->ReadObj();
>     assert(fileDir);
>     TIter next( fileDir->GetListOfKeys());
>     TKey *kdir;
>     while (( kdir = (TKey *)next())) {
>       if(kdir->ReadObj()->InheritsFrom("TDirectory")) {
>         TDirectory *subDir =  (TDirectory *) kdir->ReadObj() ;
>         dbDir->Append(subDir);
>         subDir->ReadAll();
          kdir->GetList()->Remove(subDir); //<========
>       }
>     }
>     dbDir->ls();
>     delete input;
> }



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:11 MET