Get, FindObject, FindKey + ReadObj: what's the difference?

Hello,

I’d like to know if such a issue has been already discussed or if it is described somewhere in root documentation.

What are the main differences among the following procedures to get a pointer of an object which is in a TFile?

  1. file.Get(objectname)

  2. file.FindObjectAny(objectname)

  3. key = file.FindKey(objectname) and key->ReadObj()

I am more or less using all of them in a random way choosing the one which is not giving trouble in each case.
For example now I have to get a TTree pointer from a TFile while I have other two TFile opened and if I use method 2) or 3) I have troubles while if I use method 1) it looks like everything is ok.
My feeling is that the main difference is the setting of the working directory after these calls and in the case with three TFile’s opened at the same time it can be relevant.

                              Thanks

                                 Andrea

[quote]1) file.Get(objectname) [/quote]Returns the address of the object whose name is “objectname” on the file. If the object has not yet been read, it is read and created using the information in the file. Note that this is superseeded by file.GetObject(objectname,objptr) which insuses that objptr is non-zero only if the object is of a compatible type (with the type of objptr).

[quote]2) file.FindObjectAny(objectname)[/quote] Find object by name in the list of memory objects of the current directory or its sub-directories. This does NOT read the object from the file if it has not yet been read.

[quote]3) key = file.FindKey(objectname) and key->ReadObj() [/quote]Will always read and create the object using the information on the file. Using this syntax, the same object may be created more than once.

Conclusion, use GetObject :slight_smile:

Cheers,
Philippe.

Thanks Philippe, I will follow your suggestion.

Still related to the same subject: if I want/have to get rid of the object that I got with “GetObject” to save memory space what is the best way to do it?

  1. delete obj
  2. obj->Delete()
  3. none of them

Still related to the garbage collection: I had the impression that for object NOT created with “new” if I use obj.Delete() in a compiled piece of code I can/will have troubles when the object goes out of scope since it is is going to be deleted twice or someting like that. I am not sure about that but for sure in some cases removing Delete() fixed my problems.

                              cheers

                                Andrea

Always use "delete obj"
You should use the form obj.Delete() when object is a collection, to delete
the objects in the collection.

Rene

Hello,

on the same subject, for a confirmation about your suggestions. The use of Get(name) (or GetObject) to retrieve the pointer to an object in a TFile and the use of "delete " to get rid of it when it is no more needed to save some space is ok also when the object is a TTree in a TFile?
I have to read the data from many TTree’s in a file but once I read them I do not need the TTree any more. Is a set of statements like these in a loop:

TTree* tr = (TTree*)file.Get(treename);
/*
read the TTree *tr
*/
delete tr;

safe and efficient to save memory?

                              Thanks

                             Andrea

yes

Rene