[ROOT] Tree holding objects : explanations needed...

From: Mignon Alexis (mignon@ganil.fr)
Date: Tue Jan 21 2003 - 17:49:46 MET


Hi,

I've tried to follow the exemples given with root to create a class 
event, and it seems i can't do it properly. I f anyone could explain me 
what's wrong i would be very gratefull.

File IEvent.H:<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#ifndef IEVENT_H
#define IEVENT_H

#include <TObject.h>
#include <TClonesArray.h>


class IParticle;
class IEvent: public TObject{
protected :
  UInt_t fMult;               //Multiplicity of particles
  TClonesArray *fParticles;   //->Array of Particles in the event

  static TClonesArray* fgParticles;

public :
  IEvent();
  ~IEvent();

  UInt_t GetMult()const{return fMult;};
  TClonesArray* GetParticles()const{return fParticles;};
  IParticle * AddParticle(UInt_t z,UInt_t a,Double_t energy,Double_t 
theta,Double_t phi);
  virtual void Clear(Option_t *option="");
  
  ClassDef(IEvent,1) // Event class;
};

class IParticle: public TObject{
protected:
  UInt_t fZ;
  UInt_t fA;
  Double_t fEnergy;
  Double_t fTheta;
  Double_t fPhi;

  IEvent *fEvent; // Event the particle belongs to

public:
  IParticle();
  IParticle(UInt_t z,UInt_t a,Double_t energy,Double_t theta,Double_t 
phi,IEvent*event=0);
  ~IParticle();

  UInt_t GetZ()const{return fZ;};
  UInt_t GetA()const{return fA;};
  Double_t GetEnergy()const{return fEnergy;};
  Double_t GetTheta()const{return fTheta;};
  Double_t GetPhi()const{return fPhi;};

  ClassDef(IParticle,1) // Particle class

}
;

#endif

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

File IEvent.cxx : <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#include "IEvent.H"

#define MAX_PART 120

ClassImp(IParticle)
ClassImp(IEvent)

TClonesArray *IEvent::fgParticles=0;

//_____________________________________________________________
IEvent::IEvent(){
  if (!fgParticles) fgParticles = new 
TClonesArray("IParticle",MAX_PART);
  fParticles = fgParticles;
  fMult=0;
}
//_____________________________________________________________
IEvent::~IEvent()
{
  Clear();
}
//_____________________________________________________________
void IEvent::Clear(Option_t *option){
  fParticles->Clear();
  fMult=0;
}

//_____________________________________________________________
IParticle* IEvent::AddParticle(UInt_t z,UInt_t a,Double_t 
energy,Double_t theta,Double_t phi){
  TClonesArray &particles=*fParticles;
  IParticle*part=new(particles[fMult++])  
IParticle(z,a,energy,theta,phi,this);
  return part; 
}
//_____________________________________________________________
IParticle::IParticle(){
  fZ=fA=0;
  fEnergy=fTheta=fPhi=0;
  fEvent=0;
}
//_____________________________________________________________
IParticle::IParticle(UInt_t z,UInt_t a,Double_t energy,Double_t 
theta,Double_t phi,IEvent*event){
  fZ=z;
  fA=a;
  fEnergy=energy;
  fTheta=theta;
  fPhi=phi;
  fEvent=event;
}
//_____________________________________________________________
IParticle::~IParticle(){
  fEvent=0;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


I create the tree with :
#include <TFile.h>
#include <TTree.h>
#include "IEvent.H"

int main(){

  TFile*f=new TFile("IEventTree.root","recreate");
  TTree*tree=new TTree("Tree","IEvent Tree");
  IEvent *event=new IEvent();
  tree->Branch("IEventBranch","IEvent",&event,32000,2);
  
  event->AddParticle(2,4,90,3,110);
  event->AddParticle(3,7,50,62,10);
  event->AddParticle(24,50,10,30,54);
  event->AddParticle(1,1,200,4,79);
  tree->Fill();

  event->Clear();

  event->AddParticle(12,21,30,7,24);
  event->AddParticle(23,52,23,12,130);
  event->AddParticle(5,11,60,22,10);
  event->AddParticle(82,161,5,10,7);
  event->AddParticle(32,64,21,5,35);
  event->AddParticle(2,4,120,78,110);
  event->AddParticle(1,3,105,31,23);
  event->AddParticle(1,1,168,3,21);
  
  tree->Fill();
  event->Clear();

  event->AddParticle(12,21,30,7,24);
  event->AddParticle(23,52,23,12,130);
  event->AddParticle(1,1,200,4,79);
  event->AddParticle(24,50,10,30,54);
  event->AddParticle(5,11,60,22,10);
  event->AddParticle(82,5,10,7,32);
  event->AddParticle(32,2,21,5,35);
  event->AddParticle(1,1,200,4,79);
  event->AddParticle(24,50,10,30,54);
  event->AddParticle(2,4,120,78,110);
  event->AddParticle(1,3,105,31,23);
  event->AddParticle(1,1,168,3,21);
  
  tree->Fill();

  tree->Write();
  f->Close();
}

Then in CINT i can open the 
.L libIEvent.so
TFile*f=new TFile("IEventTree.root","read");
TTree*tree=(TTree*)f->Get("Tree");
IEvent *event=new IEvent();
tree->SetBranchAddress("IEventBranch",&event);
tree->GetEntry(2);

i get a segmentation fault error
 *** Break *** segmentation violation
 Generating stack trace...
 0x40196943 in TUnixSystem::DispatchSignals(ESignals) + 0xb5 from 
/usr/local/root/lib/libCore.so
 0x40195960 in <unknown> from /usr/local/root/lib/libCore.so
 0x40199a2a in <unknown> from /usr/local/root/lib/libCore.so
 0x40d29e55 in <unknown> from /lib/i686/libpthread.so.0
 tho i can use 

tree->Show()

======> EVENT:2
 IEventBranch    = NULL
 fUniqueID       = 0
 fBits           = 50331648
 fMult           = 12
 fParticles      = 12
 fParticles.fUniqueID = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 fParticles.fBits = 50331648, 50331648, 50331648, 50331648, 50331648, 
50331648, 50331648, 50331648, 50331648, 50331648
 fParticles.fZ   = 12, 23, 1, 24, 5, 82, 32, 1, 24, 2
 fParticles.fA   = 21, 52, 1, 50, 11, 5, 2, 1, 50, 4
 fParticles.fEnergy = 30, 23, 200, 10, 60, 10, 21, 200, 10, 120
 fParticles.fTheta = 7, 12, 4, 30, 22, 7, 5, 4, 30, 78
 fParticles.fPhi = 24, 130, 79, 54, 10, 32, 35, 79, 54, 110
 fParticles.fEvent = (IEvent*)8f134b8, (IEvent*)8f134b8, 
(IEvent*)8f134b8, (IEvent*)8f134b8, (IEvent*)0, (IEvent*)0, (IEvent*)0, 
(IEvent*)0, (IEvent*)0, (IEvent*)0

the event seems more or less correctly filled

so i'm puzzled, what did i do wrong ?


thanks in advance

	Alexis Mignon

PS : i use the 3.05/00 version of root on a linux platform (g++-3.2)


-- 
Alexis Mignon
GANIL
Bd Henri Becqurel
14076 CAEN Cedex 5
France
tel: +(33) (0)231454680
e-mail : mignon@ganil.fr



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:08 MET