Logo ROOT  
Reference Guide
readCode.C File Reference

Detailed Description

View in nbviewer Open in SWAN 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

#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;
void readdir(TDirectory *dir) {
ndirs++;
TIter next(dir->GetListOfKeys());
TKey *key;
while ((key = (TKey*)next())) {
if (key->IsFolder()) {
dir->cd(key->GetName());
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;
}
}
void readCode() {
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);
}
Author
Rene Brun

Definition in file readCode.C.

TDirectory::GetListOfKeys
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:168
f
#define f(i)
Definition: RSha256.hxx:122
TCollection::GetEntries
virtual Int_t GetEntries() const
Definition: TCollection.h:177
Int_t
int Int_t
Definition: RtypesCore.h:45
TMacro.h
TDirectory::cd
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
Definition: TDirectory.cxx:498
TFile.h
gDirectory
#define gDirectory
Definition: TDirectory.h:236
TKey::IsFolder
Bool_t IsFolder() const
Check if object referenced by the key is a folder.
Definition: TKey.cxx:664
TFile
Definition: TFile.h:54
TMacro
Definition: TMacro.h:31
TKey
Definition: TKey.h:28
TKey::ReadObj
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:750
TMacro::GetListOfLines
TList * GetListOfLines() const
Definition: TMacro.h:51
TDirectory
Definition: TDirectory.h:40
TIter
Definition: TCollection.h:233
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
TKey.h