Re: moving a TDirectory from one TFile to another

From: stefan kluth (skluth@mh1.lbl.gov)
Date: Wed Dec 02 1998 - 00:39:15 MET


Hello,

I finally solved the problem of moving directories. Here is the code
fragment which does the trick:

...

  // Get list of root objects in memory, then create the new file. 
  // Iterate on list and transfer objects from old to new file. 
  TList* list1= gDirectory->GetList();
  TIter iter( list1 );
  TFile* filep= new TFile( file, "RECREATE", 
			   "Created for you again by RooTupleManager" );
  TList* list2= filep->GetList();
  TObject* obj= 0;
  while( obj= iter.Next() ) {
    if( obj->InheritsFrom( "TH1" ) ) {
      // TH1::SetDirectory does all the work for us:
      ((TH1*)obj)->SetDirectory( gDirectory );
    }
    else if( strcmp( obj->ClassName(), "TDirectory" ) == 0 ) {
      // Have to do it by hand for TDirectory:
      list1->Remove( obj );
      list2->Add( obj );
      ((TDirectory*)obj)->SetMother( gDirectory );
    }
  }

...


I hadn't realised that TH1::SetDirectory already does the transfer and
ended up doing it twice, with weird consequences. 

Actually, a similar member function for TDirectory
(TDirectory::SetDirectory( TDirectory* target )) would be nice. This would
help implementing a TDirectory::Mv( TObject* obj, TDirectory* target ) I
guess. 

Could this be done for TTrees too? I think it could, if one takes care to
flush buffers before moving a TTree from one file to another. So, One
would also need a new member function 
TTree::SetDirectory( TDirectory* target ). 

cheers, Stefan

---Stefan Kluth---------------Lynen Fellow----------------|\--|\-------
-  LBNL, MS 50A 2160       -  phone:  +1 510 495 2376  -  |/  |/      -
-  1 Cyclotron Rd.         -  fax:    +1 510 495 2957  -  |\/\|\/\|'  -
---Berkeley, CA94720, USA-----e-mail: SKluth@lbl.gov------|/\/|/\/|----



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:40 MET