Problem with a TTree holding an user defined object

Hi all,
I have a problem with a TTree having a branch which holds an object. The class (ParticleInfo) of those objects is derived from TObject and essentially contains a TClonesArray holding TParticle objects and a set of member functions which give access to the variables of the objects in the array.
I have attached the source here (ParticleInfo.cc and ParticleInfo.hh).
In my analyses I fill the tree with no problems and the output root file is just OK. The problem arise when I try to inspect the TTree with the TreeViewer or drawing histograms (with TTree::Draw()) using the class’s member functions. However if I don’t use these tools and I make analisys just retrieving the objects with SetBranchAddress and GetEntry, the member functions work perfect.
I have spent several days trying to solve this kind of problems and I would need a little help.

I have also attached a macro called analysis.C and the root file with the tree for an easy inspecting. Everything is compressed in the file test.tar.gz

Thanks in advance.
test.tar.gz (43.1 KB)

Hi,

This is actually an issue with the lifetime of a C++ variable. You have:

void analysis() { .... ParticleInfo *HadronInfo; tree->SetBranchAddress("HadronInfo", &HadronInfo); ... }and hence at the end of the function the TTree still hold the address to the (now defunct) variable ‘HadronInfo’. To solve the problem you simply need to call ResetBranchAddresses:

void analysis() { .... ParticleInfo *HadronInfo; tree->SetBranchAddress("HadronInfo", &HadronInfo); ... tree->ResetBranchAddresses(); }

Cheers,
Philippe.[/i]

Oh!
It helps… Thank you! :smiley: