{
"cells": [
{
"cell_type": "markdown",
"id": "76738223",
"metadata": {},
"source": [
"# dirs\n",
"This macro illustrates how to create a hierarchy of directories\n",
"in a Root file.\n",
"Ten directories called plane0, plane1, ..., plane9 are created.\n",
"Each plane directory contains 200 histograms.\n",
"Note that the macro deletes the TFile object at the end!\n",
"Connect the file again in read mode:\n",
"```bash\n",
" Root [0] TFile top(\"top.root\");\n",
"```\n",
"The hierarchy can be browsed by the Root browser as shown below\n",
"```bash\n",
" Root TBrowser b;\n",
"```\n",
"Click on the left pane on one of the plane directories.\n",
"This shows the list of all histograms in this directory.\n",
"Double click on one histogram to draw it (left mouse button).\n",
"Select different options with the right mouse button.\n",
"Instead of using the browser, you can also do:\n",
"```bash\n",
" Root > tof->cd();\n",
" Root > plane3->cd();\n",
" Root > h3_90N->Draw();\n",
"```\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:13 PM."
]
},
{
"cell_type": "markdown",
"id": "b7fd98bc",
"metadata": {},
"source": [
"create a new Root file"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "66a286b3",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:37.659403Z",
"iopub.status.busy": "2026-05-19T20:13:37.659267Z",
"iopub.status.idle": "2026-05-19T20:13:37.976272Z",
"shell.execute_reply": "2026-05-19T20:13:37.975692Z"
}
},
"outputs": [],
"source": [
"TFile *top = new TFile(\"top.root\",\"recreate\");"
]
},
{
"cell_type": "markdown",
"id": "fe115184",
"metadata": {},
"source": [
"create a subdirectory \"tof\" in this file"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "d7c20116",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:37.978221Z",
"iopub.status.busy": "2026-05-19T20:13:37.978097Z",
"iopub.status.idle": "2026-05-19T20:13:38.182623Z",
"shell.execute_reply": "2026-05-19T20:13:38.182026Z"
}
},
"outputs": [],
"source": [
"TDirectory *cdtof = top->mkdir(\"tof\");\n",
"cdtof->cd(); // make the \"tof\" directory the current directory"
]
},
{
"cell_type": "markdown",
"id": "9704bb00",
"metadata": {},
"source": [
"create a new subdirectory for each plane"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "08b1b1e4",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:38.184542Z",
"iopub.status.busy": "2026-05-19T20:13:38.184424Z",
"iopub.status.idle": "2026-05-19T20:13:38.386688Z",
"shell.execute_reply": "2026-05-19T20:13:38.386067Z"
}
},
"outputs": [],
"source": [
"const Int_t nplanes = 10;\n",
"const Int_t ncounters = 100;\n",
"char dirname[50];\n",
"char hname[20];\n",
"char htitle[80];\n",
"Int_t i,j,k;\n",
"TDirectory *cdplane[nplanes];\n",
"TH1F *hn[nplanes][ncounters];\n",
"TH1F *hs[nplanes][ncounters];\n",
"for (i=0;imkdir(dirname);\n",
" cdplane[i]->cd();\n",
" // create counter histograms\n",
" for (j=0;jcd(); // change current directory to top\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "32c22699",
"metadata": {},
"source": [
"Fill histograms"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "cfecae44",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:38.388432Z",
"iopub.status.busy": "2026-05-19T20:13:38.388310Z",
"iopub.status.idle": "2026-05-19T20:13:38.590525Z",
"shell.execute_reply": "2026-05-19T20:13:38.589915Z"
}
},
"outputs": [],
"source": [
"TRandom r;\n",
"for (i=0;icd();\n",
" for (j=0;jFill(100*r.Rndm(),i+j);\n",
" hs[i][j]->Fill(100*r.Rndm(),i+j+k);\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "2392caf8",
"metadata": {},
"source": [
"save histogram hierarchy in the file"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "8d7840eb",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:13:38.592379Z",
"iopub.status.busy": "2026-05-19T20:13:38.592260Z",
"iopub.status.idle": "2026-05-19T20:13:38.796658Z",
"shell.execute_reply": "2026-05-19T20:13:38.796092Z"
}
},
"outputs": [],
"source": [
"top->Write();\n",
"delete top;"
]
}
],
"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
}