Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
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();
54}
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;
60}
#define f(i)
Definition RSha256.hxx:104
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
long Long_t
Definition RtypesCore.h:54
#define gDirectory
Definition TDirectory.h:384
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize id
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
R__EXTERN TSystem * gSystem
Definition TSystem.h:555
Describe directory structure in memory.
Definition TDirectory.h:45
virtual Bool_t cd()
Change current directory to "this" directory.
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/...".
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
Class supporting a collection of lines with C++ code.
Definition TMacro.h:31
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition TObject.cxx:880
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
virtual void FreeDirectory(void *dirp)
Free a directory.
Definition TSystem.cxx:845
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition TSystem.cxx:836
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:1398
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition TSystem.cxx:853
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1063
TCanvas * slash()
Definition slash.C:1
TMarker m
Definition textangle.C:8