{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2c05e135",
   "metadata": {},
   "source": [
    "# csv2tntuple\n",
    "Build ROOT Ntuple from other source.\n",
    "This program reads the `aptuple.txt' file row by row, then creates\n",
    "the TNtuple 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, May 19, 2026 at 08:16 PM.</small></i>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e869c5ce",
   "metadata": {
    "collapsed": false,
    "execution": {
     "iopub.execute_input": "2026-05-19T20:16:52.390969Z",
     "iopub.status.busy": "2026-05-19T20:16:52.390838Z",
     "iopub.status.idle": "2026-05-19T20:16:53.432496Z",
     "shell.execute_reply": "2026-05-19T20:16:53.431897Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "opening file /github/home/ROOT-CI/build/tutorials/io/tree/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()), 'io', 'tree', '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.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
