Logo ROOT  
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
15Int_t nlines = 0;
16Int_t nfiles = 0;
17Int_t ndirs = 0;
18Int_t nh = 0;
19Int_t nc = 0;
20Int_t nC = 0;
21Int_t npy = 0;
22void 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
47void 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}
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:43
#define gDirectory
Definition: TDirectory.h:229
virtual Int_t GetEntries() const
Definition: TCollection.h:177
Describe directory structure in memory.
Definition: TDirectory.h:40
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:166
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
Definition: TDirectory.cxx:498
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:28
Bool_t IsFolder() const
Check if object referenced by the key is a folder.
Definition: TKey.cxx:664
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:738
Class supporting a collection of lines with C++ code.
Definition: TMacro.h:31
TList * GetListOfLines() const
Definition: TMacro.h:51
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47