Re: [ROOT] problem with read of TNamed objects from ROOT TTree

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Jan 18 2002 - 10:56:56 MET


Hi Sue,

This problem happens when you set a small value for the buffer size.
I have protected in CVS the TBranchElement constructor to automatically set
the buffer size to the minimum of buffersize and 1000+buffersize.
In your case, set the buffer size to at least 1100. Note that it is very 
innefficient to set small buffer sizes.
Also in your read program, it is more efficient to set the branch address only
once outside the loop, as shown below.

{
  TFile *file = new TFile("test.root","READ");
  TTree *tree = dynamic_cast<TTree*>(file -> Get("testTree"));

  Int_t nent = (Int_t)tree -> GetEntries();
  TNamed *name = 0;
  tree -> SetBranchAddress("TNamed",&name);

  for (Int_t ient=0; ient < nent; ient++) {
    Int_t nb = tree -> GetEntry(ient);
    cout << "Retrieved entry " << ient << " with " << nb << " bytes." << endl;
  }

  // Finished all entries
  cout << "Total entries in tree = " << nent << endl;
}

Rene Brun


Susan Kasahara wrote:
> 
> Hi rooters,
> I am having trouble reading TNamed objects from a ROOT TTree.  I attach two
> simple programs, one which writes the TNamed objects to a tree,
> and one which reads the objects back.   The program which writes the
> TNamed objects to tree works successfully, but the program which reads
> the objects core dumps after the first 20 entries or so.
>   This problem seems to be tied to the frequency of branch basket dumps,
> and so I have set the basket size quite small in this example when creating
> the TNamed branch to illustrate the problem.  I do not have trouble reading
> and writing other classes that I've tested, only TNamed or a class
> that inherits from TNamed.
>   I am working with cvs ROOT (upgraded last on 13-Jan-2002) on rh
> linux using gcc version 2.95.2.
>   Any ideas?
> -Sue
> 
> **************** program to fill and write TNamed objects to TTree ***********
> 
> #include <iostream.h>
> #include "TROOT.h"
> #include "TFile.h"
> #include "TTree.h"
> #include "TNamed.h"
> 
> TROOT test("test","test program");
> 
> int main(int argc, char **argv) {
> 
>   TFile* file = new TFile("test.root","RECREATE","test root file");
>   TTree* tree = new TTree("testTree","test tree");
> 
>   TNamed* name = 0;
>   // basket buffer size is set to 100 to enforce frequent basket dumps
>   tree -> Branch("TNamed","TNamed",&name,100,99);
> 
>   // Begin entry loop
>   for (Int_t ient=0; ient < 100; ient++) {
>     name = new TNamed("Sue","Kasahara");
>     tree -> Fill();
>     delete name; name = 0;
>   }
> 
>   file -> Write();
>   tree -> Print();
>   file -> Close();
> 
>   return 0;
> }
> 
> **************** program to retrieve TNamed objects from ROOT TTree ***********
> 
> #include <iostream.h>
> #include "TROOT.h"
> #include "TFile.h"
> #include "TTree.h"
> 
> TROOT test("test","Test program");
> 
> int main(int argc, char **argv) {
> 
>   TFile *file = new TFile("test.root","READ");
>   TTree *tree = dynamic_cast<TTree*>(file -> Get("testTree"));
> 
>   Int_t nent = (Int_t)tree -> GetEntries();
> 
>   for (Int_t ient=0; ient < nent; ient++) {
>     TNamed *name = 0;
>     tree -> SetBranchAddress("TNamed",&name);
> 
>     Int_t nb = tree -> GetEntry(ient);
>     cout << "Retrieved entry " << ient << " with " << nb << " bytes." << endl;
>     if(name)delete name; name = 0;
>   }
> 
>   // Finished all entries
>   cout << "Total entries in tree = " << nent << endl;
> 
>   return 0;
> 
> }



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:50:38 MET