Retrieving custom objects from TTrees


Apologies if this question has been answered somewhere previously.

Basically I have written a custom class for objects which I want to store in a TTree, I have built the dictionary and can apparently store the objects successfully. However, it would seem I do not understand how to retrieve them properly. For every entry, I can retrieve all the object data members in turn and rebuild the object one piece at a a time from them, but this is quite laborious. I want to be able to pull a completely reconstructed object at the first attempt as it was when I filled the tree. All my attempts to do so have just resulted in the object taking default values. Clearly I am missing something. Below is some dummy code which illustrates my issues.

TTree * _ntp1 = (TTree*) myFile.Get(“ntp1”)

myClass * ptr=0;
string datamember1;
float datamember2;


_ntp1->SetBranchAddress(“myClass”,&ptr); //I want to write a line like this but figure it is probably wrong


cout<<datamember1<<" "<<datamember2<<endl;
ptr->DisplayDataMember1();//some appropriate handle on member 1
ptr->DisplayDataMember2();// "

The output I get would be something like:

(from show)
======> EVENT:X
myClass = NULL
datamember1 = val1X
datemember2 = val2X

val1X val2X (from cout line)
defaultValue1 (from access methods)

I would want the access methods to return the appropriate values.
Hope this example makes sense…

I appreciate that I am probably being naive in thinking how these objects are stored in the TTree, and I dont understand why the show gives a value NULL next to the myClass pointer (and therefore why do I not get a segv when calling the access methods?). Any help most appreciated.




You should use either_ntp1->SetBranchAddress("myClass",&ptr);

_ntp1->SetMakeClass(true); _ntp1->SetBranchAddress("datamember1",&datamember1); _ntp1->SetBranchAddress("datamember2",&datamember2);but NOT both. The value can be copied into ONE place by the TTree.
SetMakeClass(true) tell the tree that you do NOT want to us real objects.
Doing all 3 SetBranchAddress result in part of the object being properly initialized. All but the datamember1 and datamember2 which you explicitly asked to be loaded not in the object but in the variable.


PS. Note that the code generated by MakeClass/MakeSelector explicitly do NOT support object layout. Instead you would need to use MakeProxy