{ "cells": [ { "cell_type": "markdown", "id": "c2b30358", "metadata": {}, "source": [ "# ntpl010_skim\n", "Example creating a derived RNTuple\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:15 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "79879f6b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:11.573914Z", "iopub.status.busy": "2026-05-19T20:15:11.573800Z", "iopub.status.idle": "2026-05-19T20:15:11.581639Z", "shell.execute_reply": "2026-05-19T20:15:11.581047Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \n", "#include \n", "#include \n", "\n", "#include \n", "#include \n", "#include \n", "\n", "#include \n", "\n", "constexpr char const *kNTupleInputName = \"ntpl\";\n", "constexpr char const *kNTupleInputFileName = \"ntpl010_input.root\";\n", "constexpr char const *kNTupleOutputName = \"ntpl_skim\";\n", "constexpr char const *kNTupleOutputFileName = \"ntpl010_skim.root\";\n", "constexpr int kNEvents = 25000;" ] }, { "cell_type": "markdown", "id": "b546c415", "metadata": {}, "source": [ " Input and output.\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "df4fc81e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:11.582977Z", "iopub.status.busy": "2026-05-19T20:15:11.582862Z", "iopub.status.idle": "2026-05-19T20:15:11.661489Z", "shell.execute_reply": "2026-05-19T20:15:11.660826Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "static void Write()\n", "{\n", " auto model = ROOT::RNTupleModel::Create();\n", "\n", " auto fldVpx = model->MakeField>(\"vpx\");\n", " auto fldVpy = model->MakeField>(\"vpy\");\n", " auto fldVpz = model->MakeField>(\"vpz\");\n", " auto fldN = model->MakeField(\"n\");\n", "\n", " auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), kNTupleInputName, kNTupleInputFileName);\n", "\n", " gRandom->SetSeed();\n", " for (int i = 0; i < kNEvents; i++) {\n", " *fldN = static_cast(gRandom->Rndm(1) * 15);\n", "\n", " fldVpx->clear();\n", " fldVpy->clear();\n", " fldVpz->clear();\n", "\n", " for (int j = 0; j < *fldN; ++j) {\n", " float px, py, pz;\n", " gRandom->Rannor(px, py);\n", " pz = px * px + py * py;\n", "\n", " fldVpx->emplace_back(px);\n", " fldVpy->emplace_back(py);\n", " fldVpz->emplace_back(pz);\n", " }\n", "\n", " writer->Fill();\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "id": "97c97789", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:11.663199Z", "iopub.status.busy": "2026-05-19T20:15:11.663071Z", "iopub.status.idle": "2026-05-19T20:15:12.563324Z", "shell.execute_reply": "2026-05-19T20:15:12.562658Z" } }, "outputs": [], "source": [ "Write();\n", "\n", "auto reader = ROOT::RNTupleReader::Open(kNTupleInputName, kNTupleInputFileName);\n", "\n", "auto skimModel = ROOT::RNTupleModel::Create();" ] }, { "cell_type": "markdown", "id": "d0da38b7", "metadata": {}, "source": [ "Loop through the top-level fields of the input RNTuple" ] }, { "cell_type": "code", "execution_count": 4, "id": "308d2e8e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:12.565567Z", "iopub.status.busy": "2026-05-19T20:15:12.565417Z", "iopub.status.idle": "2026-05-19T20:15:12.767703Z", "shell.execute_reply": "2026-05-19T20:15:12.767077Z" } }, "outputs": [], "source": [ "for (const auto &value : reader->GetModel().GetDefaultEntry()) {\n", " // Drop \"n\" field from skimmed dataset\n", " if (value.GetField().GetFieldName() == \"n\")\n", " continue;\n", "\n", " // Add a renamed clone of the other fields to the skim model\n", " const std::string newName = \"skim_\" + value.GetField().GetFieldName();\n", " skimModel->AddField(value.GetField().Clone(newName));\n", " // Connect input and output field\n", " skimModel->GetDefaultEntry().BindValue(newName, value.GetPtr());\n", "}" ] }, { "cell_type": "markdown", "id": "e739a4fa", "metadata": {}, "source": [ "Add an additional field to the skimmed dataset" ] }, { "cell_type": "code", "execution_count": 5, "id": "a9aa3cba", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:12.772088Z", "iopub.status.busy": "2026-05-19T20:15:12.771958Z", "iopub.status.idle": "2026-05-19T20:15:13.147755Z", "shell.execute_reply": "2026-05-19T20:15:13.146964Z" } }, "outputs": [], "source": [ "auto ptrSkip = skimModel->MakeField(\"skip\");\n", "\n", "auto writer = ROOT::RNTupleWriter::Recreate(std::move(skimModel), kNTupleOutputName, kNTupleOutputFileName);\n", "\n", "auto hSkip = new TH1F(\"h\", \"distribution of skipped entries\", 10, 0, 10);\n", "auto ptrN = reader->GetModel().GetDefaultEntry().GetPtr(\"n\");\n", "for (auto numEntry : *reader) {\n", " reader->LoadEntry(numEntry);\n", " if (*ptrN <= 7) {\n", " (*ptrSkip)++;\n", " continue;\n", " }\n", " writer->Fill();\n", " hSkip->Fill(*ptrSkip);\n", " *ptrSkip = 0;\n", "}\n", "\n", "TCanvas *c1 = new TCanvas(\"\", \"Skimming Example\", 200, 10, 700, 500);\n", "hSkip->DrawCopy();" ] }, { "cell_type": "markdown", "id": "7c98f7de", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 6, "id": "5312b1cb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:15:13.149653Z", "iopub.status.busy": "2026-05-19T20:15:13.149510Z", "iopub.status.idle": "2026-05-19T20:15:13.354175Z", "shell.execute_reply": "2026-05-19T20:15:13.353690Z" } }, "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 }