Import Code

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

Author: Rene Brun
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Thursday, June 17, 2021 at 04:24 PM.

In [1]:
%%cpp -d
#include "TFile.h"
#include "TSystem.h"
#include "TMacro.h"

A helper function is created:

In [2]:
%%cpp -d
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 *savdir = gDirectory;
   TDirectory *adir = savdir->mkdir(locdir);
   adir->cd();
   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);
      gSystem->GetPathInfo(afile,&id,&size,&flags,&modtime);
      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);
         m->Write(direntry);
         delete m;
      } else {
         if (flags != 3)                     continue; //must be a directory
         //we have found a valid sub-directory. Process it
         importdir(afile);
     }
  }
  gSystem->FreeDirectory(dirp);
  savdir->cd();
}
In [3]:
TFile *f = new TFile("code.root","recreate");
TString dir = gROOT->GetTutorialDir();
importdir(gSystem->UnixPathName(dir.Data())); //change the directory as you like
delete f;
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/graphs
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/graphics
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/unuran
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/unfold
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/spectrum
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/tmva
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/tmva/envelope
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/tmva/pytorch
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/tmva/data
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/tmva/keras
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/matrix
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/gl
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/r
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/io
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/eve
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/fit
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/roostats
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/mc
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/math
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/splot
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/multicore
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/dataframe
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/html
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/histfactory
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/hist
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/hist/data
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/cocoa
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/pythia
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/physics
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/foam
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/gui
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/fft
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/http
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/doc
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/doc/v530
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/doc/v534
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/doc/v600
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/doc/v602
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/doc/v528
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/doc/v532
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/roofit
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/xml
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/vecops
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/webgui
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/webgui/webwindow
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/webgui/qt5web
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/webgui/ping
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/webgui/panel
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/webgui/panel/controller
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/webgui/panel/view
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/legacy
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/legacy/thread
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/legacy/mlp
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/legacy/regexp
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/legacy/pyroot
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/legacy/cont
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/quadp
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/v7
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/v7/ntuple
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/geom
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/geom/gdml
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/fitsio
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/sql
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/eve7
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/tree
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/net
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/pyroot
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/proof
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/cont
processing dir /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/image