General way to find class stored in TClonesArray on file

Hi,

      Is there a general way to find out what class was stored in a TClonesArray , in a branch of a TTtree in a TFile. I can get the leaf names so for example if the array contains a TVector3, then the leaf names can be found, terminating in .px,.py, .pz etc., but I can't find a way to discover that it was created from a TVector3. Can it be done, if so how?

     Alex

Hi Alex,

For a complete example see the implementation of TTree::MakeSelector or TTree::MakeProxy.

Roughly, the answer is to call GetClassName on the branch.TBranchElement *br = (TBranchElement*)tree->GetBranch(branchname); br->GetClassName();Cheers,
Philippe.

Actually I found the answer elsewhere on this forum, here it is with explanatory comments added:

Assuming branch is a TBranch*

if(TString(branch->GetClassName()).EqualTo("TClonesArray")){

char *oldBuffer=branch->GetAddress(); // current pointer to branch
Long64_t oldReadEntry=branch->GetReadEntry();// current entry number
TClonesArray *buffer=NULL; // pointer to an empty TClonesArray
branch->SetAddress(&buffer); // tell branch to go here!
branch->GetEntry(0); // read in the 0th entry into buffer
branch->SetAddress(oldBuffer); // return branch to original position
if(oldReadEntry>-1){
				branch->GetEntry(oldReadEntry);//re-read original entry
}

Now you have an instance of the TClonesArray object stored in “buffer”, you can find out what TClass it is like this:
buffer->GetClass();
e.g.
cout<<buffer->GetClass()->GetName()<<endl;