How to update a tree

From: lijowski@cosray2.wustl.edu
Date: Tue Mar 21 2000 - 21:43:06 MET


   Hello, 

   I would like to update an existing tree in the existing file 
   with more entries. In the following example after running the
   program twice I would like to have 75000 entries in the ntuple
   instead of 50000 which I get using this version.

   Thank you and best regards,

       Michal Lijowski

// This program tests updating the ROOT ntuple 
#include <TROOT.h>
#include <Rtypes.h>
#include <TSystem.h>
#include <TFile.h>
#include <TNtuple.h>
#include <TRandom.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>

int main(int argc, char **argv)
{

  char  *ProgName;
  TFile *hfile;
  struct stat *statbuf;
  TNtuple *ntuple;

  TROOT test_ROOT("SIMPLE", "Testing Tree Update");

   ProgName = strrchr(argv[0], '/');
   if (ProgName == NULL) {
       /* not running under gdb */
       ProgName = argv[0];
   } else {
       /* running under gdb */
       ProgName++;
   }
   Char_t  outfile[40];
   sprintf(outfile, "%s.root", ProgName);
   Int_t  istat = 1;
   istat = stat(outfile, statbuf);
   Int_t NoEvents;

// test whether the output file exists or not
   if ( (istat < 0) && (errno == ENOENT) ) {
       printf(" %s  file %s does not exist  istat  %d  errno  %d\n", ProgName, outfile, istat, errno);
       hfile = (TFile*)gROOT->FindObject(outfile);
       if (hfile) hfile->Close();
       hfile = new TFile(outfile,"RECREATE","Demo ROOT file with ntuple");
       NoEvents = 25000;
//       return 1;
   } else if ( (istat < 0) && (errno != ENOENT) ) {
       printf(" %s has problem with file  %s  istat  %d  errno  %d\n", ProgName, outfile, istat, errno);
        return 1;
   } else if (istat == 0) {
        printf(" %s  file %s exists  istat  %d  errno  %d\n", ProgName, outfile, istat, errno);
        hfile = new TFile(outfile, "UPDATE");
        NoEvents = 50000;
   }

   ntuple = new TNtuple("ntuple","Demo ntuple","px:py:pz:random:i");

// Fill histograms randomly
  gRandom->SetSeed();
  Float_t px, py, pz;
  for (Int_t i = 0; i < NoEvents; i++) {
     gRandom->Rannor(px,py);
     pz = px*px + py*py;
     Float_t random = gRandom->Rndm(1);
     ntuple->Fill(px, py, pz, random, i);
  }
// Save all objects in this file
  hfile -> Write();
  hfile -> Close();
  return 0;
}

    



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