{ "cells": [ { "cell_type": "markdown", "id": "e20d041d", "metadata": {}, "source": [ "# readCode\n", "Example of script showing how to navigate in a ROOT file\n", "with sub-directories and read the objects in each sub-directory.\n", "This example uses the file produced by the tutorial importCode.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:15 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "289f314e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:47.976349Z", "iopub.status.busy": "2026-05-19T20:15:47.976241Z", "iopub.status.idle": "2026-05-19T20:15:47.984127Z", "shell.execute_reply": "2026-05-19T20:15:47.983717Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"TFile.h\"\n", "#include \"TKey.h\"\n", "#include \"TMacro.h\"\n", "\n", "Int_t nlines = 0;\n", "Int_t nfiles = 0;\n", "Int_t ndirs = 0;\n", "Int_t nh = 0;\n", "Int_t nc = 0;\n", "Int_t nC = 0;\n", "Int_t npy = 0;" ] }, { "cell_type": "markdown", "id": "086a599a", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "774f0309", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:47.985646Z", "iopub.status.busy": "2026-05-19T20:15:47.985520Z", "iopub.status.idle": "2026-05-19T20:15:47.993844Z", "shell.execute_reply": "2026-05-19T20:15:47.993438Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void readdir(TDirectory *dir) {\n", " ndirs++;\n", " TDirectory *dirsav = gDirectory;\n", " TIter next(dir->GetListOfKeys());\n", " TKey *key;\n", " while ((key = (TKey*)next())) {\n", " if (key->IsFolder()) {\n", " dir->cd(key->GetName());\n", " TDirectory *subdir = gDirectory;\n", " readdir(subdir);\n", " dirsav->cd();\n", " continue;\n", " }\n", " TMacro *macro = (TMacro*)key->ReadObj();\n", " nfiles++;\n", " nlines += macro->GetListOfLines()->GetEntries();\n", " if (strstr(key->GetName(),\".h\")) nh++;\n", " if (strstr(key->GetName(),\".c\")) nc++;\n", " if (strstr(key->GetName(),\".C\")) nC++;\n", " if (strstr(key->GetName(),\".py\")) npy++;\n", " delete macro;\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "id": "24282357", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:47.995336Z", "iopub.status.busy": "2026-05-19T20:15:47.995228Z", "iopub.status.idle": "2026-05-19T20:15:48.459030Z", "shell.execute_reply": "2026-05-19T20:15:48.458443Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Reading file ==> code.root\n", "File size in bytes = 3966049\n", "File compression factor = 4.28322\n", "Number of sub-dirs = 81\n", "Number of macro files = 1224\n", "Number of lines in mac = 304179\n", "Number of cxx,c,cc files = 55\n", "Number of C files = 861\n", "Number of Python files = 279\n" ] } ], "source": [ "TFile *f = new TFile(\"code.root\");\n", "if (f->IsZombie()) {\n", " printf(\"File code.root does not exist. Run tutorial importCode.C first\\n\");\n", " return;\n", "}\n", "printf(\"Reading file ==> code.root\\n\");\n", "printf(\"File size in bytes = %lld\\n\",f->GetEND());\n", "printf(\"File compression factor = %g\\n\",f->GetCompressionFactor());\n", "\n", "readdir(f);\n", "\n", "printf(\"Number of sub-dirs = %d\\n\",ndirs);\n", "printf(\"Number of macro files = %d\\n\",nfiles);\n", "printf(\"Number of lines in mac = %d\\n\",nlines);\n", "printf(\"Number of cxx,c,cc files = %d\\n\",nc);\n", "printf(\"Number of C files = %d\\n\",nC);\n", "printf(\"Number of Python files = %d\\n\",npy);" ] } ], "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 }