{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# mrt\n",
    "Build ROOT Ntuple from other source.\n",
    "This program reads the `aptuple.txt' file row by row, then creates\n",
    "the Ntuple by adding row by row.\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, August 16, 2022 at 09:39 AM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome to JupyROOT 6.27/01\n",
      "opening file /home/sftnight/build/workspace/root-makedoc-master/rootspi/rdoc/src/master.build/tutorials/pyroot/aptuple.txt ...\n",
      "writing file aptuple.root ...\n",
      "done\n"
     ]
    }
   ],
   "source": [
    "import sys, os\n",
    "from ROOT import TFile, TNtuple, TROOT\n",
    "\n",
    "\n",
    "ifn = os.path.join(str(TROOT.GetTutorialDir()), 'pyroot', 'aptuple.txt')\n",
    "ofn = 'aptuple.root'\n",
    "\n",
    "print('opening file %s ...' % ifn)\n",
    "infile = open( ifn, 'r' )\n",
    "lines  = infile.readlines()\n",
    "title  = lines[0]\n",
    "labels = lines[1].split()\n",
    "\n",
    "print('writing file %s ...' % ofn)\n",
    "outfile = TFile( ofn, 'RECREATE', 'ROOT file with an NTuple' )\n",
    "ntuple  = TNtuple( 'ntuple', title, ':'.join( labels ) )\n",
    "\n",
    "for line in lines[2:]:\n",
    "    words = line.split()\n",
    "    row = map( float, words )\n",
    "    ntuple.Fill(*row)\n",
    "\n",
    "outfile.Write()\n",
    "\n",
    "print('done')"
   ]
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
