Logo ROOT  
Reference Guide
importCode.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Example of script showing how to create a ROOT file with subdirectories.

The script scans a given directory tree and recreates the same structure in the ROOT file. All source files of type .h,cxx,c,dat,py are imported as TMacro objects. See also the other tutorial readCode.C

#include "TFile.h"
#include "TSystem.h"
#include "TMacro.h"
void importdir(const char *dirname) {
char *slash = (char*)strrchr(dirname,'/');
char *locdir;
if (slash) locdir = slash+1;
else locdir = (char*)dirname;
printf("processing dir %s\n",dirname);
TDirectory *adir = savdir->mkdir(locdir);
void *dirp = gSystem->OpenDirectory(dirname);
if (!dirp) return;
char *direntry;
Long_t id, size,flags,modtime;
//loop on all entries of this directory
while ((direntry=(char*)gSystem->GetDirEntry(dirp))) {
TString afile = Form("%s/%s",dirname,direntry);
if (direntry[0] == '.') continue; //forget the "." and ".." special cases
if (!strcmp(direntry,"CVS")) continue; //forget some special directories
if (!strcmp(direntry,"htmldoc")) continue;
if (strstr(dirname,"root/include")) continue;
if (strstr(direntry,"G__")) continue;
if (strstr(direntry,".c") ||
strstr(direntry,".h") ||
strstr(direntry,".dat") ||
strstr(direntry,".py") ||
strstr(direntry,".C")) {
TMacro *m = new TMacro(afile);
delete m;
} else {
if (flags != 3) continue; //must be a directory
//we have found a valid sub-directory. Process it
void importCode() {
TFile *f = new TFile("code.root","recreate");
TString dir = gROOT->GetTutorialDir();
importdir(gSystem->UnixPathName(dir.Data())); //change the directory as you like
delete f;
#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:2468
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
Describe directory structure in memory.
Definition: TDirectory.h:45
virtual Bool_t cd()
Change current directory to "this" directory.
Definition: TDirectory.cxx:533
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 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
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:874
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:848
virtual void * OpenDirectory(const char *name)
Open a directory. Returns 0 if directory does not exist.
Definition: TSystem.cxx:839
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:1401
virtual const char * GetDirEntry(void *dirp)
Get a directory entry. Returns 0 if no more entries.
Definition: TSystem.cxx:856
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1066
TCanvas * slash()
Definition: slash.C:1
TMarker m
Definition: textangle.C:8
Rene Brun

Definition in file importCode.C.