Logo ROOT   6.12/07
Reference Guide
readCode.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_io
3 /// \notebook -nodraw
4 /// Example of script showing how to navigate in a ROOT file
5 /// with sub-directories and read the objects in each sub-directory.
6 /// This example uses the file produced by the tutorial importCode.C
7 /// \macro_code
8 ///
9 /// \author Rene Brun
10 
11 #include "TFile.h"
12 #include "TKey.h"
13 #include "TMacro.h"
14 
15 Int_t nlines = 0;
16 Int_t nfiles = 0;
17 Int_t ndirs = 0;
18 Int_t nh = 0;
19 Int_t nc = 0;
20 Int_t nC = 0;
21 Int_t npy = 0;
22 void readdir(TDirectory *dir) {
23  ndirs++;
24  TDirectory *dirsav = gDirectory;
25  TIter next(dir->GetListOfKeys());
26  TKey *key;
27  while ((key = (TKey*)next())) {
28  if (key->IsFolder()) {
29  dir->cd(key->GetName());
30  TDirectory *subdir = gDirectory;
31  readdir(subdir);
32  dirsav->cd();
33  continue;
34  }
35  TMacro *macro = (TMacro*)key->ReadObj();
36  nfiles++;
37  nlines += macro->GetListOfLines()->GetEntries();
38  if (strstr(key->GetName(),".h")) nh++;
39  if (strstr(key->GetName(),".c")) nc++;
40  if (strstr(key->GetName(),".C")) nC++;
41  if (strstr(key->GetName(),".py")) npy++;
42  delete macro;
43  }
44 }
45 
46 
47 void readCode() {
48  TFile *f = new TFile("code.root");
49  if (f->IsZombie()) {
50  printf("File code.root does not exist. Run tutorial importCode.C first\n");
51  return;
52  }
53  printf("Reading file ==> code.root\n");
54  printf("File size in bytes = %lld\n",f->GetEND());
55  printf("File compression factor = %g\n",f->GetCompressionFactor());
56 
57  readdir(f);
58 
59  printf("Number of sub-dirs = %d\n",ndirs);
60  printf("Number of macro files = %d\n",nfiles);
61  printf("Number of lines in mac = %d\n",nlines);
62  printf("Number of cxx,c,cc files = %d\n",nc);
63  printf("Number of C files = %d\n",nC);
64  printf("Number of Python files = %d\n",npy);
65 }
TList * GetListOfLines() const
Definition: TMacro.h:51
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:150
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual Int_t GetEntries() const
Definition: TCollection.h:177
Class supporting a collection of lines with C++ code.
Definition: TMacro.h:31
int Int_t
Definition: RtypesCore.h:41
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:134
Describe directory structure in memory.
Definition: TDirectory.h:34
Float_t GetCompressionFactor()
Return the file compression factor.
Definition: TFile.cxx:1154
virtual Long64_t GetEND() const
Definition: TFile.h:202
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
Definition: TDirectory.cxx:497
#define gDirectory
Definition: TDirectory.h:213