Filling Tree with My event class

Dear Rooters,

I’m fighting with tree and branch. I’m running version 3.10 under MacOSX Panther on a 1GHz PowerPC G4 with 512 MB SDRAM.

I have a rootple with simple data and want to produce a new one with object.
I ran MakeClass and obtained a MyClass.h and MyClass.C
I defined 2 new classes: RecMuon and MCPart.
They are basically a TLorentzVector plus some integer.

I then wrote my own event class ZEvent, which has 2 TClonesArray member one for the MCpart and another for the RecMuon.

While looping on the first tree with MyClass::Loop
I’d like to size the ZEvent, event by event and fill the tree.

I Wrote something like

TFile *fp = new TFile(“my.root”,“recreate”);
fp->SetCompressionLevel(9);

TTree *Ztree = new TTree(“Ztree”, “Tree for Z analisys”);
Ztree->SetBranchStyle(1);

ZEvent *event =0;
TBranch *branch = Ztree->Branch(“event”,“ZEvent”,&event, bufsize,split);

for (jentry=0; jentry<nentries;jentry++) {
cout<<"####### “<<jentry<<”##########"<<endl;
nb = fChain->GetEntry(jentry); nbytes += nb;
//Npar number of MCPart and NMUO number of RecMuon
event = new ZEvent(jentry,NPar,NMUO);
Ztree->SetBranchAddress(“event”,&event);
for(loop over MCpart){ … }
for(loop over NMUO){ … }
event->Print();
Ztree->Fill();
delete event;
}

After 4 event the program crashes on the Ztree->Fill(), and gdb says something like

Program received signal EXC_BAD_ACCESS, Could not access memory.
0x018b92fc in TClass::Streamer(void*, TBuffer&) ()

I’m Sure that the probolem is the uncorrect pointing of the branch to the event object, but I do I handle such a situation??

Thank for your help

PS. here find Attached a tar file with the sources+Makefile

Hi,

I see nothing obviously wrong in the code (note that your forgot the attachement). What is the complete stack trace?

You may want to try out valgrind it might give you more insight on what’s wrong (valgrind.kde.org)

Cheers,
Philippe.

Hi,

looks like a memory leak. Do you clear the clone arrays correctly before filling?
An easy way to look for memory leaks is to print an table of root-derived objects event-wise by

and comparing the numbers.

Please find a working example of such a mc particle class at
http://www-zeus.desy.de/~kind/analysis/htmldoc/ZParticle.html
The corresponding event class description is at
http://www-zeus.desy.de/~kind/analysis/htmldoc/ZEvent.html
The event tree is fillled by the class
http://www-zeus.desy.de/~kind/analysis/htmldoc/ZEaze.html
in particular
http://www-zeus.desy.de/~kind/analysis/htmldoc/ZEaze.html#ZEaze:BuildEvent

Hope this helps,
Oliver