Cant create a branch with a TObjArray

Hi,
Using root v 4.04/02b and g++ v3.4.3 on Linux

I am trying to create a branch with a TObjArray using this TTree method:

Int_t Branch(TCollection *li, Int_t bufsize, Int_t splitlevel, const char *name)

Here is the file with the main function:

//FILE: test_array_imp.cpp
#include "test_array.hpp"
#include "TFile.h"
#include "TTree.h"
#include "TObject.h"
#include <iostream>
 
using namespace std;
 
int main()
{
   TFile * f = new TFile("test_array.root","RECREATE");
   TTree * tree = new TTree("mytree","mytree");
    
   TObjArray s(10);
   tree->Branch(&s,64000,4,"stuff");
    
   for( int i = 0; i<100 ; ++i) {
      for ( int j = 0; j<7; ++j) {
         s.Add(new MyCand(i,j));
         cout << s.GetEntriesFast() << endl;
      }
      tree->Fill();
      s.Delete();
      cout << s.GetEntriesFast() << endl;
   }
   tree->Print();
   tree->Write();
   delete f;
   return 0;
}

Here is the class implementation:

//FILE: test_array.cpp
#include "test_array.hpp"
#include "TObject.h"
 
ClassImp(MyCand);
 
MyCand::MyCand()
{
   _i=0;
   _j=0;
}
 
MyCand::MyCand(Int_t i, Int_t j)
{
   _i=i;
   _j=j;
}
//end test_array.cpp
//FILE: test_array.hpp
#include "TObject.h"
#include "Rtypes.h"
 
class MyCand : public TObject {
  
   Int_t _i;
   Int_t _j;
 
 public:
   MyCand();
   MyCand(Int_t i, Int_t j);
    
   ClassDef(MyCand,1)
};
//end test_array.hpp
//File: test_array_linkdef.h
#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;
 
#ifdef __CINT__
#ifndef INCLUDE_CAND_CLASSES_LINKDEF
#define INCLUDE_CAND_CLASSES_LINKDEF
 
#pragma link C++ class MyCand+;
 
 
#endif
#endif // __CINT__

I compile this with

> rootcint -f test_arrayDict.cpp -c -I${ROOTSYS}/include est_array.hpp test_array_linkdef.h

> g++$(root-config --cflags) -c test_array.cpp test_arrayDict.cpp test_array_imp.cpp

> g+ -O -Wl $(root-config --ldflags) test_array.o test_array_imp.o test_arrayDict.o $(root-config --libs) -o test_array

The program runs and TObjArray::GetEntriesFast() returns the right number but there is no data in the tree

What can I do to put a TObjArray into a branch?

Thanks for your help,
Burair

I read the example in the source code ( not visible in the html class reference to TTree ) and understand why this method does not work for what I want to do…

What I want to do is create a branch with an array of class objects which contain other objects as members. I would like to be able to read back these objects later:

class MyCandidate: public TObject {
....
Vertex vtx;
Track t1;
Track t2;

ClassDef(MyCandidate,1)
};

then create:

TTree t("mytree.root","RECREATE");
TClonesArray * candidates = new TClonesArray("MyCandidate", 10);
t.Branch("Candidates",&candidates,64000,2);

and add for every event:

new((*candidates)[count++]) MyCandidate(vtx,t1,t2);

fill and write the tree
then read the objects back for every event

TClonesArray * c = new TClonesArray("MyCandidate",10);
t->SetBranchAddress("Candidates",&c);
while(t->GetEntry(i)) {
Track t1 = (MyCandidate*) (c->At(1))->Track1();
H->Fill(t1->Pt());
++i;
}

Guess I should use TRefs?

Thanks,
Burair

see an example in tutorial jets.C

Rene