{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39bd97e0",
   "metadata": {},
   "source": [
    "# staff\n",
    "example of macro to read data from an ascii file and\n",
    "create a root file with a Tree.\n",
    "\n",
    "NOTE: comparing the results of this macro with those of staff.C, you'll\n",
    "notice that the resultant file is a couple of bytes smaller, because the\n",
    "code below strips all white-spaces, whereas the .C version does not.\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "**Author:** Wim Lavrijsen  \n",
    "<i><small>This notebook tutorial was automatically generated with <a href= \"https://github.com/root-project/root/blob/master/documentation/doxygen/converttonotebook.py\">ROOTBOOK-izer</a> from the macro found in the ROOT repository  on Tuesday, May 19, 2026 at 08:17 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "10c5e7ad",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:17:44.210667Z",
     "iopub.status.busy": "2026-05-19T20:17:44.210533Z",
     "iopub.status.idle": "2026-05-19T20:17:45.175211Z",
     "shell.execute_reply": "2026-05-19T20:17:45.174118Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import ROOT\n",
    "from ROOT import TFile, TTree, addressof, gROOT"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84f084cf",
   "metadata": {},
   "source": [
    " A C/C++ structure is required, to allow memory based access"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7119a22e",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:17:45.176843Z",
     "iopub.status.busy": "2026-05-19T20:17:45.176710Z",
     "iopub.status.idle": "2026-05-19T20:17:45.295228Z",
     "shell.execute_reply": "2026-05-19T20:17:45.294693Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gROOT.ProcessLine(\n",
    "    \"\"\"struct staff_t {\n",
    "   Int_t           Category;\n",
    "   UInt_t          Flag;\n",
    "   Int_t           Age;\n",
    "   Int_t           Service;\n",
    "   Int_t           Children;\n",
    "   Int_t           Grade;\n",
    "   Int_t           Step;\n",
    "   Int_t           Hrweek;\n",
    "   Int_t           Cost;\n",
    "   Char_t          Division[4];\n",
    "   Char_t          Nation[3];\n",
    "};\"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "273139f1",
   "metadata": {},
   "source": [
    " Function to read in data from ASCII file and fill the ROOT tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8e77aacd",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:17:45.297195Z",
     "iopub.status.busy": "2026-05-19T20:17:45.297066Z",
     "iopub.status.idle": "2026-05-19T20:17:45.405241Z",
     "shell.execute_reply": "2026-05-19T20:17:45.404711Z"
    }
   },
   "outputs": [],
   "source": [
    "staff = ROOT.staff_t()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb44b18f",
   "metadata": {},
   "source": [
    "The input file cern.dat is a copy of the CERN staff data base\n",
    "from 1988"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ab171025",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:17:45.407156Z",
     "iopub.status.busy": "2026-05-19T20:17:45.407030Z",
     "iopub.status.idle": "2026-05-19T20:17:45.539971Z",
     "shell.execute_reply": "2026-05-19T20:17:45.538807Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<cppyy.gbl.TBranch object at 0x55cfad7da090>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = TFile(\"staff.root\", \"RECREATE\")\n",
    "tree = TTree(\"T\", \"staff data from ascii file\")\n",
    "tree.Branch(\"staff\", staff, \"Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:Cost\")\n",
    "tree.Branch(\"Divisions\", addressof(staff, \"Division\"), \"Division/C\")\n",
    "tree.Branch(\"Nation\", addressof(staff, \"Nation\"), \"Nation/C\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2835752b",
   "metadata": {},
   "source": [
    "note that the branches Division and Nation cannot be on the first branch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "66ce3d33",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:17:45.541655Z",
     "iopub.status.busy": "2026-05-19T20:17:45.541505Z",
     "iopub.status.idle": "2026-05-19T20:17:45.695326Z",
     "shell.execute_reply": "2026-05-19T20:17:45.694784Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "******************************************************************************\n",
      "*Tree    :T         : staff data from ascii file                             *\n",
      "*Entries :     3354 : Total =          174374 bytes  File  Size =      47224 *\n",
      "*        :          : Tree compression factor =   2.91                       *\n",
      "******************************************************************************\n",
      "*Br    0 :staff     : Category/I:Flag:Age:Service:Children:Grade:Step:Hrweek:*\n",
      "*         | Cost                                                             *\n",
      "*Entries :     3354 : Total  Size=     122254 bytes  File Size  =      28926 *\n",
      "*Baskets :        3 : Basket Size=      32000 bytes  Compression=   3.32     *\n",
      "*............................................................................*\n",
      "*Br    1 :Divisions : Division/C                                             *\n",
      "*Entries :     3354 : Total  Size=      27564 bytes  File Size  =       8980 *\n",
      "*Baskets :        1 : Basket Size=      32000 bytes  Compression=   2.38     *\n",
      "*............................................................................*\n",
      "*Br    2 :Nation    : Nation/C                                               *\n",
      "*Entries :     3354 : Total  Size=      24209 bytes  File Size  =       9318 *\n",
      "*Baskets :        1 : Basket Size=      32000 bytes  Compression=   2.19     *\n",
      "*............................................................................*\n"
     ]
    }
   ],
   "source": [
    "fname = os.path.join(str(ROOT.gROOT.GetTutorialDir()), \"io\", \"tree\", \"cernstaff.dat\")\n",
    "with open(fname) as file:\n",
    "    for line in file.readlines():\n",
    "        t = line.split()\n",
    "        staff.Category = int(t[0])  # assign as integers\n",
    "        staff.Flag = int(t[1])\n",
    "        staff.Age = int(t[2])\n",
    "        staff.Service = int(t[3])\n",
    "        staff.Children = int(t[4])\n",
    "        staff.Grade = int(t[5])\n",
    "        staff.Step = int(t[6])\n",
    "        staff.Hrweek = int(t[7])\n",
    "        staff.Cost = int(t[8])\n",
    "        staff.Division = t[9]  # assign as strings\n",
    "        staff.Nation = t[10]\n",
    "\n",
    "        tree.Fill()\n",
    "\n",
    "tree.Print()\n",
    "tree.Write()\n",
    "\n",
    "f.Close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
