{
 "cells": [
  {
   "cell_type": "markdown",
   "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 Wednesday, March 03, 2021 at 09:43 AM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome to JupyROOT 6.23/01\n"
     ]
    }
   ],
   "source": [
    "import re, array, os\n",
    "import ROOT\n",
    "from ROOT import TFile, TTree, gROOT, addressof"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " A C/C++ structure is required, to allow memory based access"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "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",
    "};\" );"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " Function to read in data from ASCII file and fill the ROOT tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "staff = ROOT.staff_t()"
   ]
  },
  {
   "cell_type": "markdown",
   "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,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<cppyy.gbl.TBranch object at 0x568d6d0>"
      ]
     },
     "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",
   "metadata": {},
   "source": [
    "note that the branches Division and Nation cannot be on the first branch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "958"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "******************************************************************************\n",
      "*Tree    :T         : staff data from ascii file                             *\n",
      "*Entries :     3354 : Total =          172140 bytes  File  Size =      47342 *\n",
      "*        :          : Tree compression factor =   2.89                       *\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  =      32334 *\n",
      "*Baskets :        3 : Basket Size=      32000 bytes  Compression=   2.97     *\n",
      "*............................................................................*\n",
      "*Br    1 :Divisions : Division/C                                             *\n",
      "*Entries :     3354 : Total  Size=      25330 bytes  File Size  =       8328 *\n",
      "*Baskets :        1 : Basket Size=      32000 bytes  Compression=   2.49     *\n",
      "*............................................................................*\n",
      "*Br    2 :Nation    : Nation/C                                               *\n",
      "*Entries :     3354 : Total  Size=      24209 bytes  File Size  =       6680 *\n",
      "*Baskets :        1 : Basket Size=      32000 bytes  Compression=   3.05     *\n",
      "*............................................................................*\n"
     ]
    }
   ],
   "source": [
    "fname = os.path.join(str(ROOT.gROOT.GetTutorialDir()), 'tree', 'cernstaff.dat')\n",
    "for line in open(fname).readlines():\n",
    "    t = list(filter( lambda x: x, re.split( '\\s+', line ) ) )\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()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
