[ROOT] Copying/Moving TObjects

From: Michael Kordosky (kordosky@dell4.hep.utexas.edu)
Date: Sun Mar 11 2001 - 21:34:24 MET


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 : Fri Jun 08 2001 - 11:51:20 MEST