Re: TClonesArray & TIter

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Nov 19 1998 - 03:30:12 MET


Hi Paul,
I am pretty sure that your problem is due to the fact that you do
not set the event pointer to zero. I have made the relevant mods
commented in your code below.
Let me know.

Rene Brun

Paul M. Eugenio wrote:
> 
> Dear Rooter,
> 
> I'm having trouble reading and iterating over a TClonesArray. I have
> a program that writes events to a root file. Each event contains 3
> objects  where on one object contains a TClonesArray of a fourth
> object (all objects inherit from TOject). I believe that this program
> is working.
> 
> Using a macro, I read these events in CINT. But I cannot access the first
> event's TClonesArray. If I do not skip it, the macro dies with this error:
> 
> Error: illegal pointer to class object particle 0x0 358  FILE:/tmp_mnt/home/halld/source/root/./macro2.C LINE:71
> *** Interpreter error recovered ***
> 
> Below is a watered down version of my macro that doesworks. Maybe I'm not
> iterating properly or I have missed something important.
> 
> Any help is greatly appreciated.
> 
>                                                 Paul Eugenio
> {
> gROOT->Reset();
> gSystem.Load("libTMCFast.so");
> 
> TFile f("Event.rdt");
> TTree *tree = (TTree*)f.Get("T");
> TBranch *b1 = tree->GetBranch("hepevt");
> TBranch *b2 = tree->GetBranch("trace");
> TBranch *b3 = tree->GetBranch("offtrk");
> 
> TMCFastHepEvt *hepevt = 0;  // always preset to 0
> TMCFastTOF *trace = 0;
> TMCFastOfflineTrack *offtrk = 0;
> hepevt = new TMCFastHepEvt();
> trace = new TMCFastTOF();
> offtrk = new TMCFastOfflineTrack();
> 
> b1->SetAddress(&hepevt);
> b2->SetAddress(&trace);
> b3->SetAddress(&offtrk);
> TH1F *h1 = new TH1F("h1", "x momentum",100,0.0,1.0);
> TCanvas *c1;
> c1 = new TCanvas("c1","MCFast histos",200,10,700,900);
> 
> Int_t nentries = (Int_t)tree->GetEntries();
> for (Int_t ev = 0; ev < nentries; ev++) {
>   tree->GetEvent(ev);        //read  event in memory
> 
>   TIter next(hepevt->GetHepParticles());
>   TMCFastHepParticle *particle = (TMCFastHepParticle *)next();
> 
>   if(ev != 0){ // SKIP THIS EVENT OR IT DIES!!!
>     h1->Fill(particle->GetPx());
>   }
>   hepevt->Clear();
>   trace->Clear();
>   offtrk->Clear();
> }
> h1->Draw();
> }
> 
> --
> Paul M. Eugenio
> Dept. of Physics
> Carnegie Mellon University
> (412) 268 6949
> eugenio@ernest.phys.cmu.edu
> 
>                 _____________________________________________________
>                / The scholar should know; one builds science with    \
>         o O 0 / the facts just like a house can be built of stones;   \
>        o     | but an accumulation of facts is not a science just like |
>   `\|||/      \           that a pile of stones is not a house.       /
>    (o o)       \_____________________________________________________/
> ooO_(_)_Ooo___________________________________________Henri Poincare'_
> _____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|____
> __|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_
> _____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|____



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:40 MET