Re: [ROOT] unfriendly trees

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Apr 27 2001 - 10:53:36 MEST


Hi Eddy,

Your program crashes because you try to read a non-existing Tree.
If you replace the line :

  TTree *tr = (TTree*)gDirectory->Get("trades");
by
  TTree *tr = (TTree*)gDirectory->Get("tree");

your loop reading the Tree will run correctly.

However when running your program, I see that you are hitting a particular case
where the friend Tree is defined in the creation program but not used at all
in the reading program. In this particular case, there was a problem
when deleting the Tree with friends at the end of your example.

I have fixed this problem in the development version (source and binaries).

Rene Brun

Eddy Offermann wrote:
> 
> Hi Rene,
> 
> I seem not to be able to get the AddFriend() in tree going.
> In the example below, I create two tree's. In the first
> one I make a friend of the second one. Later when opening
> the seconf the program segv.
> 
> I ran this on SunOS 2.6 with gcc2.9.5 with a CVS version of a week
> ago.
> 
> Could you please have a look,
> 
> Best Regards, Eddy
> 
> Starting program: /work/eddy/kk3/src/tree/doit
> Filling tree
> Filling tree
> ******************************************************************************
> *Tree    :tree      : test                                                   *
> *Entries :       10 : Total =               0 bytes  File  Size =          0 *
> *        :          : Tree compression factor =   1.00                       *
> ******************************************************************************
> *Br    0 :keylen    : keylen/I                                               *
> *Entries :       10 : Total  Size=          0 bytes  File Size  =          0 *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> *Br    1 :key       : key[keylen]/C                                          *
> *Entries :       10 : Total  Size=          0 bytes  File Size  =          0 *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> *Br    2 :id        : id[2]/I                                                *
> *Entries :       10 : Total  Size=          0 bytes  File Size  =          0 *
> *Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
> *............................................................................*
> 
> Program received signal SIGSEGV, Segmentation fault.
> LoadOutput (file=@0x0) at main.C:73
> 73        tr->SetBranchAddress("keylen",&keylen);
> (gdb) where
> #0  LoadOutput (file=@0x0) at main.C:73
> #1  0x10b20 in main (argc=-4262088, argv=0xffbef8bc) at main.C:18
> 
> //---------------------------------------------------------
> #include <TROOT.h>
> #include <TFile.h>
> #include <TTree.h>
> #include <Rtypes.h>
> 
> void CreateOutput(TString &file, Bool_t mkFriends);
> void LoadOutput(TString &file);
> 
> int main(Int_t argc, Char_t **argv)
> {
>   TROOT showit("showit","showit");
> 
>   TString file1 = "sample1.root";
>   CreateOutput(file1,0);
>   TString file2 = "sample2.root";
>   CreateOutput(file2,1);
> 
>   LoadOutput(file2);
> 
>   return 0;
> }
> 
> //__________________________________________________________________________________
> void CreateOutput(
> TString  &file,
> Bool_t    mkFriends)
> {
>   TFile *tfile = new TFile(file.Data(),"RECREATE","file");
>   TTree *tree  = new TTree("tree","test");
> 
>   Int_t keylen;
>   Char_t *key = new Char_t[1024];
>   Int_t id[2];
> 
>   TBranch *b1 = tree->Branch("keylen",&keylen,"keylen/I");
>   TBranch *b2 = tree->Branch("key",key,"key[keylen]/C");
>   TBranch *b3 = tree->Branch("id",id,"id[2]/I");
> 
>   Char_t *funny = "aap noot mies";
>   printf("Filling tree\n");
>   for (Int_t i = 0; i < 10; i++)
>   {
>     id[0] = i; id[1] = i+1;
>     keylen = 10;
>     strcpy(key,funny);
>     keylen = strlen(key)+1;
>     tree->Fill();
>   }
> 
>   if (mkFriends)
>   {
>     tree->AddFriend("alias = tree","sample1.root");
>     tree->Print();
>   }
> 
>   delete [] key;
> 
>   tfile->Write();
>   tfile->Close();
> }
> 
> //__________________________________________________________________________________
> void LoadOutput(
> TString &file)
> {
>   TFile tfile(file.Data());
>   TTree *tr = (TTree*)gDirectory->Get("trades");
> 
>   Int_t keylen;
>   Char_t *key = new Char_t[1024];
>   Int_t id[2];
> 
>   tr->SetBranchAddress("keylen",&keylen);
>   tr->SetBranchAddress("key",key);
>   tr->SetBranchAddress("id",id);
> 
>   printf("Reading tree\n");
>   Int_t nrBytes = 0;
>   for (Int_t j = 0; j < tr->GetEntries(); j++)
>   {
>     nrBytes += tr->GetEvent(j);
>     printf("%d %d %s\n",id[0],id[1],key);
>   }
> 
>   delete tr;
> }



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:43 MET