{ "cells": [ { "cell_type": "markdown", "id": "928d9361", "metadata": {}, "source": [ "# ntpl001_staff\n", "Write and read tabular data with RNTuple. Adapted from the cernbuild and cernstaff tree tutorials.\n", "Illustrates the type-safe ntuple model interface, which is used to define a data model that is in a second step\n", "taken by an ntuple reader or writer.\n", "\n", "\n", "\n", "\n", "**Author:** The ROOT Team \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:14 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "e743044e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:14:39.278548Z", "iopub.status.busy": "2026-05-19T20:14:39.278434Z", "iopub.status.idle": "2026-05-19T20:14:39.285915Z", "shell.execute_reply": "2026-05-19T20:14:39.285530Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \n", "#include \n", "#include \n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "constexpr char const* kNTupleFileName = \"ntpl001_staff.root\";" ] }, { "cell_type": "markdown", "id": "5336acd3", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "19054038", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:14:39.287447Z", "iopub.status.busy": "2026-05-19T20:14:39.287331Z", "iopub.status.idle": "2026-05-19T20:14:39.522573Z", "shell.execute_reply": "2026-05-19T20:14:39.521824Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void Ingest() {\n", " // The input file cernstaff.dat is a copy of the CERN staff data base from 1988\n", " ifstream fin(gROOT->GetTutorialDir() + \"/io/tree/cernstaff.dat\");\n", " assert(fin.is_open());\n", "\n", " // We create a unique pointer to an empty data model\n", " auto model = ROOT::RNTupleModel::Create();\n", "\n", " // To define the data model, we create fields with a given C++ type and name. Fields are roughly TTree branches.\n", " // MakeField returns a shared pointer to a memory location that we can populate to fill the ntuple with data\n", " auto fldCategory = model->MakeField(\"Category\");\n", " auto fldFlag = model->MakeField(\"Flag\");\n", " auto fldAge = model->MakeField(\"Age\");\n", " auto fldService = model->MakeField(\"Service\");\n", " auto fldChildren = model->MakeField(\"Children\");\n", " auto fldGrade = model->MakeField(\"Grade\");\n", " auto fldStep = model->MakeField(\"Step\");\n", " auto fldHrweek = model->MakeField(\"Hrweek\");\n", " auto fldCost = model->MakeField(\"Cost\");\n", " auto fldDivision = model->MakeField(\"Division\");\n", " auto fldNation = model->MakeField(\"Nation\");\n", "\n", " // We hand-over the data model to a newly created ntuple of name \"Staff\", stored in kNTupleFileName\n", " // In return, we get a unique pointer to an ntuple that we can fill\n", " auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), \"Staff\", kNTupleFileName);\n", "\n", " std::string record;\n", " while (std::getline(fin, record)) {\n", " std::istringstream iss(record);\n", " iss >> *fldCategory >> *fldFlag >> *fldAge >> *fldService >> *fldChildren >> *fldGrade >> *fldStep >> *fldHrweek\n", " >> *fldCost >> *fldDivision >> *fldNation;\n", " writer->Fill();\n", " }\n", "\n", " // The ntuple unique pointer goes out of scope here. On destruction, the ntuple flushes unwritten data to disk\n", " // and closes the attached ROOT file.\n", "}" ] }, { "cell_type": "markdown", "id": "66930d5c", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "90c1af2c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:14:39.524268Z", "iopub.status.busy": "2026-05-19T20:14:39.524139Z", "iopub.status.idle": "2026-05-19T20:14:39.630255Z", "shell.execute_reply": "2026-05-19T20:14:39.629519Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "void Analyze() {\n", " // Get a unique pointer to an empty RNTuple model\n", " auto model = ROOT::RNTupleModel::Create();\n", "\n", " // We only define the fields that are needed for reading\n", " std::shared_ptr fldAge = model->MakeField(\"Age\");\n", "\n", " // Create an ntuple and attach the read model to it\n", " auto reader = ROOT::RNTupleReader::Open(std::move(model), \"Staff\", kNTupleFileName);\n", "\n", " // Quick overview of the ntuple and list of fields.\n", " reader->PrintInfo();\n", "\n", " std::cout << \"The first entry in JSON format:\" << std::endl;\n", " reader->Show(0);\n", "\n", " auto c = new TCanvas(\"c\", \"\", 200, 10, 700, 500);\n", " TH1I h(\"h\", \"Age Distribution CERN, 1988\", 100, 0, 100);\n", " h.SetFillColor(48);\n", "\n", " for (auto entryId : *reader) {\n", " // Populate fldAge\n", " reader->LoadEntry(entryId);\n", " h.Fill(*fldAge);\n", " }\n", "\n", " h.DrawCopy();\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "758e7fba", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:14:39.631705Z", "iopub.status.busy": "2026-05-19T20:14:39.631561Z", "iopub.status.idle": "2026-05-19T20:14:40.350277Z", "shell.execute_reply": "2026-05-19T20:14:40.349573Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "RNTuple : Staff\n", "Entries : 3354\n", "\n", "Field 1 : Category (std::int32_t)\n", "Field 2 : Flag (std::uint32_t)\n", "Field 3 : Age (std::int32_t)\n", "Field 4 : Service (std::int32_t)\n", "Field 5 : Children (std::int32_t)\n", "Field 6 : Grade (std::int32_t)\n", "Field 7 : Step (std::int32_t)\n", "Field 8 : Hrweek (std::int32_t)\n", "Field 9 : Cost (std::int32_t)\n", "Field 10 : Division (std::string)\n", "Field 11 : Nation (std::string)\n", "The first entry in JSON format:\n", "{\n", " \"Category\": 202,\n", " \"Flag\": 15,\n", " \"Age\": 58,\n", " \"Service\": 28,\n", " \"Children\": 0,\n", " \"Grade\": 10,\n", " \"Step\": 13,\n", " \"Hrweek\": 40,\n", " \"Cost\": 11975,\n", " \"Division\": \"PS\",\n", " \"Nation\": \"DE\"\n", "}\n" ] } ], "source": [ "Ingest();\n", "Analyze();" ] }, { "cell_type": "markdown", "id": "bc9dd596", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "db300173", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:14:40.351757Z", "iopub.status.busy": "2026-05-19T20:14:40.351623Z", "iopub.status.idle": "2026-05-19T20:14:40.556264Z", "shell.execute_reply": "2026-05-19T20:14:40.555595Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "gROOT->GetListOfCanvases()->Draw()" ] } ], "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 }