Re: TBranchClones - What am I doing wrong?

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Nov 26 1999 - 17:25:22 MET


Hi Christian,
yesterday, I answered a similar question from Rolf.Dubitzky. I said:


Two problems in your small test:
 - TClonesArray must reference objects with a fix structure.
   TNamed or like are not authorized because each class instance
   has a different length.
 - Up to version 2.23/08 included, it was not possible to have
   a TClonesArray branch in non-split mode. I am supporting
   this feature in 2.23/09 to be released tomorrow.

Your program will work if you set split=1. However, as I said above,
you cannot use classes like TNamed or any class where instances
have a different size in a TClonesArray.

Rene Brun

Christian Holm Christensen wrote:
> 
> 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