TBranchClones - What am I doing wrong?

From: Christian Holm Christensen (cholm@hehi03.nbi.dk)
Date: Thu Nov 25 1999 - 14:58:56 MET


Hi ROOT'ers


I have a peculiar problem. I'm trying to write objects to a TTree, via
a TClonesArray. However, it seems, that the array isn't written to
file, and the TBranchClones objects fList pointer points to zero. What
am I doing wrong? 

Here's the code:

// file Foo.C //////////////////////////////////////// 
TFile* file;
TNamed* obj;
TClonesArray* array;
TTree* tree;
TBranch* branch;

int
Foo(void)
{
  file   = new TFile("foo.root", "RECREATE");
  tree   = new TTree("tree", "A tree");
  array  = new TClonesArray("TNamed",2);
  branch = tree->Branch("branch",&array);

  (*array)[0] = new TNamed("foo", "Foo object");
  tree->Fill();
  (*array)[1] = new TNamed("bar", "Bar object");
  tree->Fill();

  file->Write();
  file->Close();

  delete file;
     
  return 0;
}

int
Bar(void)
{
  file = new TFile("foo.root", "READ");
  file->ls();

  tree = (TTree*)file->Get("tree");
  if (!tree) {
    cout << "Bad!" << endl;
    return 1;
  }

  branch = tree->GetBranch("branch");

  if (!branch) {
    cout << "Bad!" << endl;
    return 2;
  }

  cout << "Object `branch' is a "
       << branch->IsA()->GetName() << " object" 
       << " with " << branch->GetEntries() << " entries" << endl;


  array = ((TBranchClones*)branch)->GetList();
  if (!array) {
    cout << "Bad! (address of array is " << array << ")" << endl;
    return 4;
  }
  
  int n = array->GetEntriesFast();
  for (int i = 0; i < n ; i++) {
    if (!(*array)[i]) {
      cout << "Bad! (No object at position " << i 
	   << " in array)" << endl;
      continue;
    }
    cout << "Entry " << i << " in Clones array in tree is " 
	 << ((TNamed*)(*array)[i])->GetName() << " "
	 << ((TNamed*)(*array)[i])->GetTitle() << endl;
  }
  
  return 0;
}
// End of file Foo.C ///////////////////////////////// 
 
And the output:

// Output from ROOT ////////////////////////////////// 
root [0] .L Foo.C 
root [1] Foo()
(int)0
root [2] Bar()
TFile**         foo.root
 TFile*         foo.root
  KEY: TTree    tree;1  A tree
Object `branch' is a TBranchClones object with 0 entries
Bad! (address of array is (nil))
(int)4
// End of Output from ROOT ///////////////////////////
   
If any of you have an idea why this fails, please notice that I'm not
on the roottalk mailing list (Too much trafic I'm afraid - how about a
rootdigest list?), so please respond to me (cholm@nbi.dk) also. 

Thanks

Christian Holm
----------------------------------------------------------------
Christian Holm Christensen       Phone:        (+45) 35 35 96 91
  Sankt Hansgade 23. 1, th       Office:       (+45) 353  25 307
  DK-2200 Copenhagen N           Email:             cholm@nbi.dk
  Denmark                        Homepage:     www.nbi.dk/~cholm

    
    
 



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