{ "cells": [ { "cell_type": "markdown", "id": "12b1ba6a", "metadata": {}, "source": [ "# df015_LazyDataSource\n", "Use the lazy RDataFrame data source to concatenate computation graphs.\n", "\n", "This tutorial illustrates how to take advantage of a *lazy data source*\n", "creating a data frame from columns of one or multiple parent dataframe(s),\n", "delaying the creation of the columns to the actual usage of the daughter\n", "data frame.\n", "Dataset Reference:\n", "McCauley, T. (2014). Dimuon event information derived from the Run2010B\n", "public Mu dataset. CERN Open Data Portal.\n", "DOI: [10.7483/OPENDATA.CMS.CB8H.MFFA](http://opendata.cern.ch/record/700).\n", "From the ROOT website: https://root.cern/files/tutorials/tdf014_CsvDataSource_MuRun2010B.csv\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": "27d28ea1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:42.857275Z", "iopub.status.busy": "2026-05-19T20:09:42.857112Z", "iopub.status.idle": "2026-05-19T20:09:43.060171Z", "shell.execute_reply": "2026-05-19T20:09:43.059597Z" } }, "outputs": [], "source": [ "using namespace ROOT::RDF;" ] }, { "cell_type": "markdown", "id": "0bc0c202", "metadata": {}, "source": [ "Let's first create a RDF that will read from the CSV file.\n", "See the tutorial (https://root.cern/doc/master/df014__CSVDataSource_8C.html) on CSV data sources for more details!" ] }, { "cell_type": "code", "execution_count": 2, "id": "35fa6b29", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:43.067249Z", "iopub.status.busy": "2026-05-19T20:09:43.067110Z", "iopub.status.idle": "2026-05-19T20:09:43.406634Z", "shell.execute_reply": "2026-05-19T20:09:43.405682Z" } }, "outputs": [], "source": [ "auto fileUrl = \"http://root.cern/files/tutorials/df014_CsvDataSource_MuRun2010B.csv\";\n", "auto csv_rdf = FromCSV(fileUrl);" ] }, { "cell_type": "markdown", "id": "77757b40", "metadata": {}, "source": [ "Now we take out two columns: px and py of the first muon in the muon pair" ] }, { "cell_type": "code", "execution_count": 3, "id": "2a9c3d5d", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:43.408242Z", "iopub.status.busy": "2026-05-19T20:09:43.408115Z", "iopub.status.idle": "2026-05-19T20:09:44.450696Z", "shell.execute_reply": "2026-05-19T20:09:44.449757Z" } }, "outputs": [], "source": [ "std::string px1Name = \"px1\";\n", "auto px1 = csv_rdf.Take(px1Name);\n", "std::string py1Name = \"py1\";\n", "auto py1 = csv_rdf.Take(py1Name);" ] }, { "cell_type": "markdown", "id": "4386ca47", "metadata": {}, "source": [ "Now we create a new dataframe built on top of the columns above. Note that up to now, no event loop\n", "has been carried out!" ] }, { "cell_type": "code", "execution_count": 4, "id": "3b121ca1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:44.452315Z", "iopub.status.busy": "2026-05-19T20:09:44.452182Z", "iopub.status.idle": "2026-05-19T20:09:45.020335Z", "shell.execute_reply": "2026-05-19T20:09:45.018649Z" } }, "outputs": [], "source": [ "auto df = MakeLazyDataFrame(std::make_pair(px1Name, px1), std::make_pair(py1Name, py1));" ] }, { "cell_type": "markdown", "id": "96bba011", "metadata": {}, "source": [ "We build a histogram of the transverse momentum of the muons." ] }, { "cell_type": "code", "execution_count": 5, "id": "53b7d3f0", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:45.021976Z", "iopub.status.busy": "2026-05-19T20:09:45.021838Z", "iopub.status.idle": "2026-05-19T20:09:47.325199Z", "shell.execute_reply": "2026-05-19T20:09:47.324636Z" } }, "outputs": [], "source": [ "auto ptFormula = [](double px, double py) { return sqrt(px * px + py * py); };\n", "auto pt_h = df.Define(\"pt\", ptFormula, {\"px1\", \"py1\"})\n", " .Histo1D({\"pt\", \"Muon p_{T};p_{T} [GeV/c];\", 128, 0, 128}, \"pt\");\n", "\n", "auto can = new TCanvas();\n", "can->SetLogy();\n", "pt_h->DrawCopy();\n", "\n", "return 0;" ] }, { "cell_type": "markdown", "id": "d19aa64f", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 6, "id": "f20b8a00", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:09:47.327109Z", "iopub.status.busy": "2026-05-19T20:09:47.326979Z", "iopub.status.idle": "2026-05-19T20:09:47.530428Z", "shell.execute_reply": "2026-05-19T20:09:47.529422Z" } }, "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 }