{ "cells": [ { "cell_type": "markdown", "id": "d694596c", "metadata": {}, "source": [ "# tree101_basic\n", " Read data from an ascii file and create a root file with an histogram and an ntuple.\n", " See a variant of this macro in tree102_basic.C.\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:17 PM." ] }, { "cell_type": "markdown", "id": "49d3341d", "metadata": {}, "source": [ "read file $ROOTSYS/tutorials/io/tree/basic.dat\n", "this file has 3 columns of float data" ] }, { "cell_type": "code", "execution_count": 1, "id": "adaaa473", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:17:46.210442Z", "iopub.status.busy": "2026-05-19T20:17:46.210314Z", "iopub.status.idle": "2026-05-19T20:17:46.685455Z", "shell.execute_reply": "2026-05-19T20:17:46.685055Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = -1.102279, y = -1.799389, z = +4.452822\n", "x = +1.867178, y = -0.596622, z = +3.842313\n", "x = -0.524181, y = +1.868521, z = +3.766139\n", "x = -0.380611, y = +0.969128, z = +1.084074\n", "x = +0.552454, y = -0.212309, z = +0.350281\n", " found 1000 points\n" ] } ], "source": [ "TString dir = gROOT->GetTutorialDir();\n", "dir.Append(\"/io/tree/\");\n", "dir.ReplaceAll(\"/./\", \"/\");\n", "ifstream in;\n", "in.open(TString::Format(\"%sbasic.dat\", dir.Data()));\n", "\n", "Float_t x, y, z;\n", "Int_t nlines = 0;\n", "auto f = TFile::Open(\"tree101.root\", \"RECREATE\");\n", "TH1F h1(\"h1\", \"x distribution\", 100, -4, 4);\n", "TNtuple ntuple(\"ntuple\",\"data from ascii file\", \"x:y:z\");\n", "\n", "while (1) {\n", " in >> x >> y >> z;\n", " if (!in.good())\n", " break;\n", " if (nlines < 5)\n", " printf(\"x = %+8.6f, y = %+8.6f, z = %+8.6f\\n\", x, y, z);\n", " h1.Fill(x);\n", " ntuple.Fill(x, y, z);\n", " nlines++;\n", "}\n", "printf(\" found %d points\\n\", nlines);\n", "in.close();\n", "f->Write();" ] } ], "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 }