Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
importCode.C
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
9 ///
10 /// \author Rene Brun
11 
12 #include "TFile.h"
13 #include "TSystem.h"
14 #include "TMacro.h"
15 
16 void 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  }
52  gSystem->FreeDirectory(dirp);
53  savdir->cd();
54 }
55 void 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;
60 }