Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_io
3/// \notebook -nodraw
4/// Example of script showing how to create a ROOT file with subdirectories.
5/// The script scans a given directory tree and recreates the same structure in the ROOT file.
6/// All source files of type .h,cxx,c,dat,py are imported as TMacro objects.
7/// See also the other tutorial readCode.C
8/// \macro_code
10/// \author Rene Brun
12#include "TFile.h"
13#include "TSystem.h"
14#include "TMacro.h"
16void importdir(const char *dirname) {
17 char *slash = (char*)strrchr(dirname,'/');
18 char *locdir;
19 if (slash) locdir = slash+1;
20 else locdir = (char*)dirname;
21 printf("processing dir %s\n",dirname);
22 TDirectory *savdir = gDirectory;
23 TDirectory *adir = savdir->mkdir(locdir);
24 adir->cd();
25 void *dirp = gSystem->OpenDirectory(dirname);
26 if (!dirp) return;
27 char *direntry;
28 Long_t id, size,flags,modtime;
29 //loop on all entries of this directory
30 while ((direntry=(char*)gSystem->GetDirEntry(dirp))) {
31 TString afile = Form("%s/%s",dirname,direntry);
32 gSystem->GetPathInfo(afile,&id,&size,&flags,&modtime);
33 if (direntry[0] == '.') continue; //forget the "." and ".." special cases
34 if (!strcmp(direntry,"CVS")) continue; //forget some special directories
35 if (!strcmp(direntry,"htmldoc")) continue;
36 if (strstr(dirname,"root/include")) continue;
37 if (strstr(direntry,"G__")) continue;
38 if (strstr(direntry,".c") ||
39 strstr(direntry,".h") ||
40 strstr(direntry,".dat") ||
41 strstr(direntry,".py") ||
42 strstr(direntry,".C")) {
43 TMacro *m = new TMacro(afile);
44 m->Write(direntry);
45 delete m;
46 } else {
47 if (flags != 3) continue; //must be a directory
48 //we have found a valid sub-directory. Process it
49 importdir(afile);
50 }
51 }
53 savdir->cd();
55void importCode() {
56 TFile *f = new TFile("code.root","recreate");
57 TString dir = gROOT->GetTutorialDir();
58 importdir(gSystem->UnixPathName(dir.Data())); //change the directory as you like
59 delete f;
#define f(i)
Definition RSha256.hxx:104
long Long_t
Definition RtypesCore.h:54
#define gDirectory
Definition TDirectory.h:290
XFontStruct * id
Definition TGX11.cxx:109
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TDirectory * mkdir(const char *name, const char *title="", Bool_t returnExistingDirectory=kFALSE)
Create a sub-directory "a" or a hierarchy of sub-directories "a/b/c/...".
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
Class supporting a collection of lines with C++ code.
Definition TMacro.h:31
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition TSystem.cxx:844
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition TSystem.cxx:835
int GetPathInfo(const char *path, Long_t *id, Long_t *size, Long_t *flags, Long_t *modtime)
Get info about a file: id, size, flags, modification time.
Definition TSystem.cxx:1396
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition TSystem.cxx:852
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1061
TCanvas * slash()
Definition slash.C:1
auto * m
Definition textangle.C:8