Re: [ROOT] TClonesArray

From: C. Bernet-2001 (bernet@phnx7.saclay.cea.fr)
Date: Fri Jul 07 2000 - 20:27:13 MEST


Hi Paolo,

TClonesArray::AddrAt(int) returns a TObject*. Maybe you should specify
that this TObject is actually a Track with a cast : 

Track *prova=(Track*) tracks->AddrAt(2);   

cheers,

	Colin



On Fri, 7 Jul 2000, Paolo MAESTRO wrote:

> 
> Hello,
> my problem is the following.
> I have a class Event so defined (just like your example):
> 
> ------------------------------
> #ifndef ROOT_Event
> #define ROOT_Event
> 
> #include "TObject.h"
> #include "TClonesArray.h"
> #include "TMath.h"
> 
> #include <iostream.h>
> 
> class TDirectory;
> 
> 
> class EventHeader {
> 
> private:
>    Int_t   fEvtNum;
>    Int_t   fRun;
>    Int_t   fDate;
> 
> public:
>    EventHeader() : fEvtNum(0), fRun(0), fDate(0) { }
>    virtual ~EventHeader() { }
>    void   Set(Int_t i, Int_t r, Int_t d) { fEvtNum = i; fRun = r; fDate
> = d; }
>    Int_t  GetEvtNum() const { return fEvtNum; }
>    Int_t  GetRun() const { return fRun; }
>    Int_t  GetDate() const { return fDate; }
> 
>    ClassDef(EventHeader,1)  //Event Header
> };
> 
> class Track;
> 
> class Event : public TObject {
> 
> private:
>    Int_t          necht;
>    EventHeader    fEvtHdr;
>    TClonesArray  *fTracks;
> 
>    static TClonesArray *fgTracks;
> 
> public:
>    Event();
>    virtual ~Event();
>    void          Clear(Option_t *option ="");
>    static void   Reset(Option_t *option ="");
>    void          SetNecht(Int_t n) { necht = n; };
>    void          SetHeader(Int_t i, Int_t run, Int_t date, Float_t
> random);
>    void          AddTrack(Float_t energy,Int_t cell,Int_t plane,Int_t
> proj,Int_t status);
>    Int_t         GetNecht() const { return necht; }
>    EventHeader  *GetHeader() { return &fEvtHdr; }
>    TClonesArray *GetTracks() const { return fTracks; }
> 
> 
> 
>    ClassDef(Event,1)  //Event structure
> };
> 
> 
> class Track : public TObject {
> 
> private:
>    Float_t      Echtedep;
>    Int_t        Echtcell;
>    Int_t        Echtplane;
>    Int_t        Echtproj;
>    Int_t        Echtstatus;
> 
> 
> public:
>    Track() { }
>    Track(Float_t energy,Int_t cell,Int_t plane,Int_t proj,Int_t status);
> 
>    virtual ~Track() { }
>    void Stampa() {cout << Echtedep<<endl;};
>    Float_t       GetEnergy() const { return Echtedep; };
>    Float_t       GetCell() const { return Echtcell; }
>    Float_t       GetPlane() const { return Echtplane; }
>    Float_t       GetProj() const { return Echtproj; }
> 
> 
>    ClassDef(Track,1)  //A track segment
> };
> 
> #endif
> ----------------------
> 
> 
> When I use eventa.c to get information from the tree, I would like to
> get each tracks in each event.
> I try with:
> {
>    gROOT->Reset();
>    gSystem.Load("libEvent.so");
>  TFile f("Event.root");
>   TTree *T = (TTree*)f.Get("T");
> 
>    Event *event = new Event();
> 
>    TClonesArray *tracks=event->GetTracks();
>    TBranch *branch=T->GetBranch("event");
>    T->SetBranchAddress("event",&event);
> 
>   Int_t nevent = T->GetEntries();
>    Int_t nb = 0;
>   for (Int_t i=0;i<nevent;i++)
>     {
>         nb += T->GetEntry(i);
> 
> 
>     // get data from the second track of the i-event
>         Track *prova=tracks->AddrAt(2);    //THIS LINE
> 
>         cout <<prova->GetEnergy()<<endl;
>         cout <<prova->GetCell()<<endl;
>         cout <<prova->GetPlane()<<endl;
> 
>      event->Clear();
>    }
> 
> printf("%d events and %d bytes read.\n",nevent,nb);
> 
> f.Close();
> }
> 
> ----
> This macro works well when I execute with .x eventa.c. Futhermore when I
> 
> try to compile it (out of ROOT,as I would like to do), including all the
> 
> required library, the compiler  gives the error:
>  THIS LINE:         type `Track' is not a base type for type `TObject'
> 
> Why this behaviour? Are There other methods to extract data from a
> TClonesArray?
> Thank you in advance for your help.
> 
> ********************************************
> * Paolo Maestro
> * Istituto Nazionale di Fisica Nucleare
> * INFN - V.Vecchia Livornese,1291
> * 56010 S.Piero a Grado (Pisa) Italy
> * tel :   +39-050-880309/245
> * fax :   +39-050-880317
> ********************************************
> 
> 
> 
> 
> 



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:29 MET