Re: How to add records in TFile

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Mar 02 2000 - 17:59:26 MET


Andrei,
In your second session, you have to open the file in "update" mode.
See the two examples below;
The first example test2.C creates the file with 10 entries.
The second example opens the previous file in update mode and continues
to fill.

Rene Brun

//-----------------file test2.C
void test2()
{
   int i,j;
   struct {
     unsigned short s_buf[8192];
   } cbuf;

   TFile *ff = new TFile("test2.root", "recreate", "Test of root file");

   TTree *T = new TTree("T", "List of arrays");

   TBranch *B = T->Branch("B", &cbuf, "s_buf[8192]/s");

   for(i = 0; i < 10; ++i) {
      cbuf.s_buf[0] = i;
      cbuf.s_buf[100] = i+10;
      T->Fill();
   }
   T->Print();
   T->Write();
   ff->Close();   
}


//-------------------file test2a.C
void test2a()
{
   int i,j;
   struct {
     unsigned short s_buf[8192];
   } cbuf;

   TFile *ff = new TFile("test2.root", "update");

   TTree *T = (TTree*)ff->Get("T");

   T->SetBranchAddress("B", &cbuf);

   for(i = 0; i < 10; ++i) {
      cbuf.s_buf[0] = i;
      cbuf.s_buf[100] = i+10;
      T->Fill();
   }
   T->Print();
   T->Write();
   ff->Close();   
}


A.V. Daniel wrote:
> 
> Hi Rooters,
> 
> Who can help me? I well know how to add new
> records to the old file using C++ or fortran.
> But how to do the similar using ROOT. I checked
> the next code to write a simple TTree (Thanks
> to Rene Brun who explained my error, it works now).
> But I could not find description how to reopen
> old file and to add new records in the same TTree.
> May be it's very simple but I am not experience
> with ROOT. Thanks in advance.
> 
> Andrei
> 
> void test2()
> {
>    int i,j;
>    struct {
>      unsigned short s_buf[8192];
>    } cbuf;
> 
>    TFile *ff = new TFile("ff.dat", "NEW", "Test of root file");
> 
>    TTree *T = new TTree("T", "List of arrays");
> 
>    TBranch *B = T->Branch("B", &cbuf, "s_buf[8192]/s");
> 
>    for(i = 0; i < 10; ++i)
>    {
>       cbuf.s_buf[0] = i;
>       cbuf.s_buf[100] = i+10;
>       T->Fill();
>    }
> 
>    T->Write();  // <=========== new line (From Rene Brun)
>    T->MakeClass("test4");
> 
>    ff->Close();
> 
> }
> 
> --
> 
> *******************************************************
> * Andrei V. Daniel
> * Department of Physics & Astronomy, P.O. Box 1807-B
> * Vanderbilt University             Tel: (615) 322 2646
> * Nashville, TN 37235, USA          Fax: (615) 343 7263
> *******************************************************



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:20 MET