Re: [ROOT] Updating existing tree.

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Feb 02 2001 - 18:41:51 MET


Hi Nicholai,

You can add new data to an existing Tree:

 - open the file in "Update" mode
 - get the Tree from the file
 - set the branch(es) address
 - continue your fill loop
 - save the new Tree header mytree->Write()
 - close the file

This works OK if you simply want to add new entries.
If you want to add one or more branches, you can proceeed like above
for the first 3 steps, then
 - create your new branch
 - fill ONLY this branch, making sure that you have the same number of entries
   at the end in all your branches.
 - write the header, etc

Rene Brun


 Rene Brun

Nicholai Kuropatkin wrote:
> 
>  Dear rooters.
>  In my data analyses I need to process a list of gziped data files creating
>  a root tree. After a while another list should be processed and selected events
>  should be added to the existing tree on the same file increasing statistics.
>  I have solved the problem reading existing tree, adding new events and writing
>   a new temporary file that will replace the old one.
>   It works, but looks complicated.
>   I just wonder if there is a better way to achieve the same goal.
> 
>  Extraction from my program follows.
> 
>                         Thanks for any suggestion.
> 
>                                                         Nikolai
> 
> #include <stdlib.h>
> #include "TROOT.h"
> #include "TFile.h"
> #include "TRandom.h"
> #include "TTree.h"
> #include "TBranch.h"
> #include "TClonesArray.h"
> #include "TStopwatch.h"
> #include "TUnixSystem.h"
> #include "TRecon.h"
> 
> //_____________________________________________________________________________
> int main(int argc, char **argv)
> {
>    TROOT vt2rtup("vt2rtup","Create root tree from vtuple");
> 
>    char vtup_file[256]=" ";
>    char vtup_list[256]=" ";
>    char out_file[256]=" ";
>    char tmp_file[256]=" ";
>    const char mode[2]="r";
>    const char lmod[2]="r";
>    Int_t comp   = 2;       // by default file is compressed
> 
> 
> //
>    TUnixSystem *os = new TUnixSystem();
>    if ( os->Init()) { cout<<"error creating os interface"<<endl; }
>    char command[180] = "gunzip -c ";
> 
>     FILE *pipe;
>    TTree *Stup=0;
>    TFile *hfile=0;
>     FILE *fp;
> 
>    TRecon *event= new TRecon();
> 
> //
> //   Create temporary root file
> //
> 
>           hfile = new TFile(tmp_file,"recreate","ntuple");
>           hfile->SetCompressionLevel(comp);
> //
> 
>      // Create a ROOT tree with one branch
> 
>       Stup = new TTree(name,title);
>       Stup->Branch("RN",&RN.run,varlist);
> 
> // Try to open an old file if any
> 
>      TFile *f1 = new TFile(out_file);
>      Bool_t stat = f1->IsOpen();
>      if ( stat ) {                    // if the file is opend
> //
> //  read an old tree
> //
>        TTree *oldTree = (TTree*)f1->Get(name);
>        oldTree->SetBranchAddress("RN",&RN.run);
> 
>       Int_t nentries = oldTree->GetEntries();
> 
>    for (Int_t i=0;i<nentries;i++) {      // copy it to a new tree
>       oldTree->GetEntry(i);
>       Stup->Fill();
>    }
>      f1->Close();
> 
>                  }
> 
>      hfile->cd();
> //
> //   Read the list of vtuple files and process them one by one
> //
>      cout<<" list of vtuples file="<<vtup_list<<endl;
>      if( (fp = fopen(vtup_list,lmod)) == NULL)
>     {
>     cout<<" Error opening vtup_list"<<endl;
>    fclose(fp);
>    hfile->Close();
>    return -1;
>     } else  {                                 // if list open -  OK
>      while ( !feof(fp) ) {                    // read file loop
>      fscanf(fp," %s ",vtup_file);
>      cout<<" Processing "<<vtup_file<<endl;
> 
> 
>      strcpy(command,"gunzip -c  ");
>      strcat(command,vtup_file);
> 
>       pipe = os->OpenPipe(command,mode);
>       if( ! pipe )
>     {
>     cout<<"Error opening vtup file "<<endl;
>      return -1;
>      } else {                                    // if pipe is opened
> 
> //
>     while ( !feof(pipe) ) {                      // read file loop
> //
> //
> //   File processing loop
> //
>       Stup->Fill();                                   // Fill new tree
> //
> 
>      }                                                // if read OK
>           else {                                      // if error in the file
>      cout<<" Error reading the vtuple"<<endl;
> //
>      os->ClosePipe(pipe);                               // close pipe
>      break;
>    }                                                  //
> 
>           }                                           // end read vtup
>    os->ClosePipe(pipe);                               // close pipe
>             }                                         // end if pipe open
>           }                                           // end of read list
>         }                                             // end if list open
> //
>    Stup->Write();                                     // Write temporary file
>    hfile->Close();
> //
>    strcpy(command,"mv ");                             // Replace the old one
>    strcat(command,tmp_file);
>    strcat(command," ");
>    strcat(command,out_file);
>    os->Exec(command);
> //
> 
>    return 0;
> }



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