Read Code


Example of script showing how to navigate in a ROOT file with sub-directories and read the objects in each sub-directory. This example uses the file produced by the tutorial importCode.C

Author: Rene Brun
This notebook tutorial was automatically generated with ROOTBOOK-izer (Beta) from the macro found in the ROOT repository on Thursday, January 19, 2017 at 04:31 PM.


In [1]:
%%cpp -d
#include "TFile.h"
#include "TKey.h"
#include "TMacro.h"

Int_t nlines = 0;
Int_t nfiles = 0;
Int_t ndirs = 0;
Int_t nh = 0;
Int_t nc = 0;
Int_t nC = 0;
Int_t npy = 0;



A helper function is created:


In [2]:
%%cpp -d
void readdir(TDirectory *dir) {
   ndirs++;
   TDirectory *dirsav = gDirectory;
   TIter next(dir->GetListOfKeys());
   TKey *key;
   while ((key = (TKey*)next())) {
      if (key->IsFolder()) {
         dir->cd(key->GetName());
         TDirectory *subdir = gDirectory;
         readdir(subdir);
         dirsav->cd();
         continue;
      }
      TMacro *macro = (TMacro*)key->ReadObj();
      nfiles++;
      nlines += macro->GetListOfLines()->GetEntries();
      if (strstr(key->GetName(),".h"))   nh++;
      if (strstr(key->GetName(),".c"))   nc++;
      if (strstr(key->GetName(),".C"))   nC++;
      if (strstr(key->GetName(),".py"))  npy++;
      delete macro;
   }
}

In [3]:
TFile *f = new TFile("code.root");
if (f->IsZombie()) {
   printf("File code.root does not exist. Run tutorial importCode.C first\n");
   return;
}
printf("Reading file ==> code.root\n");
printf("File size in bytes       = %lld\n",f->GetEND());
printf("File compression factor  = %g\n",f->GetCompressionFactor());

readdir(f);

printf("Number of sub-dirs       = %d\n",ndirs);
printf("Number of macro files    = %d\n",nfiles);
printf("Number of lines in mac   = %d\n",nlines);
printf("Number of cxx,c,cc files = %d\n",nc);
printf("Number of C files        = %d\n",nC);
printf("Number of Python files   = %d\n",npy);


Reading file ==> code.root
File size in bytes       = 2503164
File compression factor  = 3.93623
Number of sub-dirs       = 54
Number of macro files    = 775
Number of lines in mac   = 159318
Number of cxx,c,cc files = 18
Number of C files        = 680
Number of Python files   = 46