{ "cells": [ { "cell_type": "markdown", "id": "e34f01ef", "metadata": {}, "source": [ "# importCode\n", "Example of script showing how to create a ROOT file with subdirectories.\n", "The script scans a given directory tree and recreates the same structure in the ROOT file.\n", "All source files of type .h,cxx,c,dat,py are imported as TMacro objects.\n", "See also the other tutorial readCode.C\n", "\n", "\n", "\n", "**Author:** Rene Brun \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:06 PM." ] }, { "cell_type": "markdown", "id": "5900e485", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 1, "id": "4cfe4911", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:06:10.315853Z", "iopub.status.busy": "2026-05-19T20:06:10.315705Z", "iopub.status.idle": "2026-05-19T20:06:10.357111Z", "shell.execute_reply": "2026-05-19T20:06:10.356311Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "\n", "#include \"TFile.h\"\n", "#include \"TSystem.h\"\n", "#include \"TMacro.h\"\n", "\n", "void importdir(const char *dirname) {\n", " char *slash = (char*)strrchr(dirname,'/');\n", " char *locdir;\n", " if (slash) locdir = slash+1;\n", " else locdir = (char*)dirname;\n", " printf(\"processing dir %s\\n\",dirname);\n", " TDirectory *savdir = gDirectory;\n", " TDirectory *adir = savdir->mkdir(locdir);\n", " adir->cd();\n", " void *dirp = gSystem->OpenDirectory(dirname);\n", " if (!dirp) return;\n", " char *direntry;\n", " Long_t id, size,flags,modtime;\n", " //loop on all entries of this directory\n", " while ((direntry=(char*)gSystem->GetDirEntry(dirp))) {\n", " TString afile = Form(\"%s/%s\",dirname,direntry);\n", " gSystem->GetPathInfo(afile,&id,&size,&flags,&modtime);\n", " if (direntry[0] == '.') continue; //forget the \".\" and \"..\" special cases\n", " if (!strcmp(direntry,\"CVS\")) continue; //forget some special directories\n", " if (!strcmp(direntry,\"htmldoc\")) continue;\n", " if (strstr(dirname,\"root/include\")) continue;\n", " if (strstr(direntry,\"G__\")) continue;\n", " if (strstr(direntry,\".c\") ||\n", " strstr(direntry,\".h\") ||\n", " strstr(direntry,\".dat\") ||\n", " strstr(direntry,\".py\") ||\n", " strstr(direntry,\".C\")) {\n", " TMacro *m = new TMacro(afile);\n", " m->Write(direntry);\n", " delete m;\n", " } else {\n", " if (flags != 3) continue; //must be a directory\n", " //we have found a valid sub-directory. Process it\n", " importdir(afile);\n", " }\n", " }\n", " gSystem->FreeDirectory(dirp);\n", " savdir->cd();\n", "}" ] }, { "cell_type": "code", "execution_count": 2, "id": "de0ee929", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:06:10.359215Z", "iopub.status.busy": "2026-05-19T20:06:10.359075Z", "iopub.status.idle": "2026-05-19T20:06:11.326551Z", "shell.execute_reply": "2026-05-19T20:06:11.324504Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "processing dir /github/home/ROOT-CI/build/tutorials\n", "processing dir /github/home/ROOT-CI/build/tutorials/analysis\n", "processing dir /github/home/ROOT-CI/build/tutorials/analysis/dataframe\n", "processing dir /github/home/ROOT-CI/build/tutorials/analysis/parallel\n", "processing dir /github/home/ROOT-CI/build/tutorials/analysis/tree\n", "processing dir /github/home/ROOT-CI/build/tutorials/analysis/unfold\n", "processing dir /github/home/ROOT-CI/build/tutorials/evegen\n", "processing dir /github/home/ROOT-CI/build/tutorials/geom\n", "processing dir /github/home/ROOT-CI/build/tutorials/heterogeneous\n", "processing dir /github/home/ROOT-CI/build/tutorials/hist\n", "processing dir /github/home/ROOT-CI/build/tutorials/hist/data\n", "processing dir /github/home/ROOT-CI/build/tutorials/hist/histv7\n", "processing dir /github/home/ROOT-CI/build/tutorials/http\n", "processing dir /github/home/ROOT-CI/build/tutorials/io\n", "processing dir /github/home/ROOT-CI/build/tutorials/io/experimental\n", "processing dir /github/home/ROOT-CI/build/tutorials/io/fitsio\n", "processing dir /github/home/ROOT-CI/build/tutorials/io/ntuple\n", "processing dir /github/home/ROOT-CI/build/tutorials/io/sql\n", "processing dir /github/home/ROOT-CI/build/tutorials/io/tree\n", "processing dir /github/home/ROOT-CI/build/tutorials/io/tree/dictionary\n", "processing dir /github/home/ROOT-CI/build/tutorials/io/xml\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/cont\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/g3d\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/hist\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/math\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/mlp\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/multicore\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/net\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/regexp\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/spectrum\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/splot\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/thread\n", "processing dir /github/home/ROOT-CI/build/tutorials/legacy/tree\n", "processing dir /github/home/ROOT-CI/build/tutorials/machine_learning\n", "processing dir /github/home/ROOT-CI/build/tutorials/machine_learning/data\n", "processing dir /github/home/ROOT-CI/build/tutorials/machine_learning/envelope\n", "processing dir /github/home/ROOT-CI/build/tutorials/machine_learning/keras\n", "processing dir /github/home/ROOT-CI/build/tutorials/machine_learning/pytorch\n", "processing dir /github/home/ROOT-CI/build/tutorials/math\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/fft\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/fit\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/foam\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/matrix\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/pdf\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/quadp\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/r\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/unuran\n", "processing dir /github/home/ROOT-CI/build/tutorials/math/vecops\n", "processing dir /github/home/ROOT-CI/build/tutorials/roofit\n", "processing dir /github/home/ROOT-CI/build/tutorials/roofit/histfactory\n", "processing dir /github/home/ROOT-CI/build/tutorials/roofit/roofit\n", "processing dir /github/home/ROOT-CI/build/tutorials/roofit/roostats\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/cocoa\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/eve\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/eve7\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/geom\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/geom/gdml\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/gl\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/graphics\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/graphs\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/gui\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/image\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/rcanvas\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webcanv\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/bootstrap\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/bootstrap/dist\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/bootstrap/dist/css\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/bootstrap/dist/js\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/browserv7\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/fitpanelv7\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/geom\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/panel\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/panel/controller\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/panel/view\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/ping\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/qtweb\n", "processing dir /github/home/ROOT-CI/build/tutorials/visualisation/webgui/webwindow\n" ] } ], "source": [ "TFile *f = new TFile(\"code.root\",\"recreate\");\n", "TString dir = gROOT->GetTutorialDir();\n", "importdir(gSystem->UnixPathName(dir.Data())); //change the directory as you like\n", "delete f;" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }