Help understand tree reading

Hi

(I don’t think my root version matters but - v4-00-04)

I have a simple Event class:

#ifndef ROOT_Event
#define ROOT_Event
#include "TObject.h"
class Event : public TObject
{
 private:
  UInt_t        nTracks;                        // daf
  Int_t         nPosCharged;                    // daf
  Int_t         nNegCharged;                    // daf
 public:
  Event();
  Event(UInt_t t1, Int_t t2, Int_t t3);
  void SetTracks(UInt_t temp) {nTracks = temp;}
  void SetPos(Int_t temp) {nPosCharged = temp;}
  void SetNeg(Int_t temp) {nNegCharged = temp;}
  ClassDef(Event,1) //Simple class
};

#endif
#include "Event.h"
ClassImp(Event)
Event::Event()
{
        nTracks = 0;
        nPosCharged = 0;
        nNegCharged = 0;
}

Event::Event(UInt_t t1, Int_t t2, Int_t t3)
{
        nTracks = t1;
        nPosCharged = t2;
        nNegCharged = t3;
}

and I fill it into a tree:

{
        gSystem->Load("libEvent.so");
        TRandom rd;
        TClonesArray* arr = new TClonesArray("Event",1);
        TTree* mytree = new TTree("mytree","Title");
        TBranch* b = mytree->Branch("Events",&arr,256000,99);
        for(Int_t i=0;i<20;i++)
        {
             new((*arr)[0])  Event(gRandom->Integer(50),gRandom->Integer(50),gRandom->Integer(50));
                mytree->Fill();
        }

        TFile f("testPaul.root","RECREATE");
        mytree->Write();
        f.Close();
}

What I don’t understand is why the following code ( selected lines from mytree->MakeClass() ) doesn’t work. The full code works but I am missing something as to why this doesn’t work (or why I always get 0):

root4star.1 [0] const Int_t kMaxEvents = 1;
root4star.1 [1] TFile f("testPaul.root")
Warning in <TClass::TClass>: no dictionary for class Event is available
root4star.1 [2] UInt_t          Events_nTracks[kMaxEvents];   //[Events_]
root4star.1 [3]    TBranch        *b_Events_nTracks;   //!
root4star.1 [5] TTree* tree = f.Get("mytree")
root4star.1 [6] tree->SetBranchAddress("Events.nTracks",Events_nTracks);
root4star.1 [7] tree->GetEntries()
(const Double_t)2.00000000000000000e+01
root4star.1 [8] tree->GetEntry(1)
(Int_t)24
root4star.1 [9] Events_nTracks
(UInt_t*)0xa1f28f0
root4star.1 [10] Events_nTracks[0]
(unsigned int)0
root4star.1 [15] for (Int_t i=0;i<20;i++){tree->GetEntry(i);cout<<Events_nTracks[0]<<endl;}
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

Thanks,

Mark[/code]

Hi

so the problem comes down to just the branches I don’t read - I MUST set their status to
zero otherwise I get garbage back.

Is this a bug? Couldn’t it be that if a branch address hasn’t been set when the first GetEntry/GetEvent is called then the status is automatically set to 0?

Thanks,

Mark

If you are going to set the address like:tree->SetBranchAddress("Events.nTracks",Events_nTracks);
you also must do

Cheers,
Philippe.