{ "cells": [ { "cell_type": "markdown", "id": "453fa5ee", "metadata": {}, "source": [ "# df010_trivialDataSource\n", "Use the \"trivial data source\", an example data source implementation.\n", "\n", "This tutorial illustrates how use the RDataFrame in combination with a\n", "RDataSource. In this case we use a RTrivialDS, which is nothing more\n", "than a simple generator: it does not interface to any existing dataset.\n", "The RTrivialDS has a single column, col0, which has value n for entry n.\n", "The code for RTrivialDS is available at these links (header and source):\n", "- https://github.com/root-project/root/blob/master/tree/dataframe/src/RTrivialDS.cxx\n", "- https://github.com/root-project/root/blob/master/tree/dataframe/inc/ROOT/RTrivialDS.hxx\n", "\n", "Note that RTrivialDS is only a demo data source implementation and superior alternatives\n", "typically exist for production use (e.g. constructing an empty RDataFrame as `RDataFrame(nEntries)`).\n", "\n", "\n", "\n", "\n", "**Author:** Danilo Piparo (CERN) \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:09 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "f6447921", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:38.840088Z", "iopub.status.busy": "2026-05-19T20:09:38.839965Z", "iopub.status.idle": "2026-05-19T20:09:39.283875Z", "shell.execute_reply": "2026-05-19T20:09:39.283482Z" } }, "outputs": [], "source": [ "auto nEvents = 128U;\n", "auto d_s = ROOT::RDF::MakeTrivialDataFrame(nEvents);" ] }, { "cell_type": "markdown", "id": "d92edf35", "metadata": {}, "source": [ "Now we have a regular RDataFrame: the ingestion of data is delegated to\n", "the RDataSource. At this point everything works as before." ] }, { "cell_type": "code", "execution_count": 2, "id": "72ede287", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:39.296329Z", "iopub.status.busy": "2026-05-19T20:09:39.296193Z", "iopub.status.idle": "2026-05-19T20:09:39.661007Z", "shell.execute_reply": "2026-05-19T20:09:39.660630Z" } }, "outputs": [], "source": [ "auto h_s = d_s.Define(\"x\", \"1./(1. + col0)\").Histo1D({\"h_s\", \"h_s\", 128, 0, .6}, \"x\");" ] }, { "cell_type": "markdown", "id": "8c572baa", "metadata": {}, "source": [ "Now we redo the same with a RDF from scratch and we draw the two histograms" ] }, { "cell_type": "code", "execution_count": 3, "id": "12a05bae", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:39.679365Z", "iopub.status.busy": "2026-05-19T20:09:39.679228Z", "iopub.status.idle": "2026-05-19T20:09:39.881971Z", "shell.execute_reply": "2026-05-19T20:09:39.881437Z" } }, "outputs": [], "source": [ "ROOT::RDataFrame d(nEvents);" ] }, { "cell_type": "markdown", "id": "2328a4f6", "metadata": {}, "source": [ "This lambda redoes what the RTrivialDS provides" ] }, { "cell_type": "code", "execution_count": 4, "id": "38018211", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:39.885142Z", "iopub.status.busy": "2026-05-19T20:09:39.885020Z", "iopub.status.idle": "2026-05-19T20:09:41.319223Z", "shell.execute_reply": "2026-05-19T20:09:41.318684Z" } }, "outputs": [], "source": [ "auto g = []() {\n", " static ULong64_t i = 0;\n", " return i++;\n", "};\n", "auto h = d.Define(\"col0\", g).Define(\"x\", \"1./(1. + col0)\").Histo1D({\"h\", \"h\", 128, 0, .6}, \"x\");\n", "\n", "auto c_s = new TCanvas();\n", "c_s->SetLogy();\n", "h_s->DrawClone();\n", "\n", "auto c = new TCanvas();\n", "c->SetLogy();\n", "h->DrawClone();\n", "\n", "return 0;" ] }, { "cell_type": "markdown", "id": "1b06c1e4", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "d7b488c5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:41.326420Z", "iopub.status.busy": "2026-05-19T20:09:41.326293Z", "iopub.status.idle": "2026-05-19T20:09:41.537489Z", "shell.execute_reply": "2026-05-19T20:09:41.536440Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "