Re: [ROOT] Copying/Moving TObjects

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Mar 12 2001 - 15:45:15 MET


Hi Michael,
Only objects like TH1, TTrees, TEventlist are automatically added
to gDirectory->GetList().
You can prevent TH1 objects to be automatically added by calling
  TH1::AddDirectory(kFALSE);
before creating or reading these objects.
For Trees, you should not change the default behaviour unless you
understand all the consequences. A TTree has a header in memory and
the baskets on the file.
Note that you can remove an object from the default list by:
  gDirectory->GetList()->Remove(myObject);

In the same way, objects like TEventList and TCutG are automatically added
to gROOT->GetListOfSpecials();
You can remove an object from this list with:
   gROOT->GetListOfSpecials()->Remove(myObject);

You do not have to Clone an object to add it to another list.

Rene Brun


Michael Kordosky wrote:
> 
> Hi,
> 
> I am attempting to implement a function which opens a file, reads in all
> TObjects in the file (without casting or otherwise discriminating on
> them), and places copies of them into a vector. Later code will then come
> along and select the objects that it wants, using ROOT's RTTI system.  A
> trimmed down sample of my best attempt is shown below. My biggest problem
> is that the opened TFile "owns" its objects in memory and will remove them
> when it closes.  How do I take objects in memory away from a TFile, so
> that those objects persist after the closure of the TFile?  I figured that
> perhaps I had to move or copy the objects into a persistent TDirectory,
> ie:
> 
> gROOT->cd();
> /* copying */
> file.cd();
> 
> After some experimentation, the only method that I can find for
> moving/copying TObject derived classes is TObject::Clone().  This function
> seems to bind correctly to the derived class except that it doesn't get
> the name and title? So, must I insert a test to figure out if a particular
> object inherits from TNamed, then do some casting to allow me to reset the
> names (and titles) to the correct values? Is there a better way to do what
> I am trying to do? Alternatively, am I doing something "immoral" in the
> ROOT sense?
> 
> Thanks!
> 
> Mike Kordosky
> (sample code follows)
> --
> Graduate Research Assistant  // High Energy Physics Lab
> kordosky@hep.utexas.edu     // University of Texas at Austin
> ph: (512) 471-8426 (RLM Lab, Office)
>     (512) 475-8673 (ENS Lab)
> 
> int RunRecord::GetObjects(vector<TObject*>& objvec) const
> {
>         TFile f(fRfile.c_str(), "READ");
> 
>         TList* list = f.GetListOfKeys();
> 
>         TIter next(list);
>         TKey* key;
>         TObject* obj;
> 
>         while (key = (TKey*)next()){
>                 obj = key -> ReadObj(); // reads objects in from disk
> 
>                 // Don't want to deal with sub-directories right now
>                 if(obj->IsA() == TDirectory::Class()){
>                         delete obj;
>                         obj = NULL;
>                         continue;
>                 }
> 
>                 // Will take everything else
> 
>                 gROOT->cd();
>                 TObject* newobj = obj->Clone();
>                 objvec.push_back(newobj);
>                 cout<<"objvec.size() = "<<objvec.size()<<endl;
>                 f.cd();
>         }
>         delete key;
>         f.Close();
>         return objvec.size();
> }



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:39 MET