{ "cells": [ { "cell_type": "markdown", "id": "0f12b6cf", "metadata": {}, "source": [ "# gr109_timeSeriesFromCSV_RDF\n", "with data read from a text file containing the SWAN usage\n", "statistics during July 2017.\n", "We exploit the RDataFrame for reading from the file. See the [RDataFrame\n", "documentation](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) and [RDataFrame\n", "tutorials](https://root.cern/doc/master/group__tutorial__dataframe.html)\n", "\n", "\n", "\n", "**Author:** Danilo Piparo, Olivier Couet \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:38 PM." ] }, { "cell_type": "markdown", "id": "95aaebc3", "metadata": {}, "source": [ "Open the data file. This csv contains the usage statistics of a CERN IT\n", "service, SWAN, during two weeks. We would like to plot this data with\n", "ROOT to draw some conclusions from it." ] }, { "cell_type": "code", "execution_count": 1, "id": "96d014e3", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:26.653392Z", "iopub.status.busy": "2026-05-19T20:38:26.653253Z", "iopub.status.idle": "2026-05-19T20:38:26.979744Z", "shell.execute_reply": "2026-05-19T20:38:26.979055Z" } }, "outputs": [], "source": [ "TString dir = gROOT->GetTutorialDir();\n", "dir.Append(\"/visualisation/graphs/\");\n", "dir.ReplaceAll(\"/./\", \"/\");" ] }, { "cell_type": "markdown", "id": "96e05c27", "metadata": {}, "source": [ "Read the data from the file using RDataFrame. We do not have headers and\n", "we would like the delimiter to be a space" ] }, { "cell_type": "code", "execution_count": 2, "id": "6b46ee1b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:26.981767Z", "iopub.status.busy": "2026-05-19T20:38:26.981626Z", "iopub.status.idle": "2026-05-19T20:38:27.183648Z", "shell.execute_reply": "2026-05-19T20:38:27.183260Z" } }, "outputs": [], "source": [ "auto rdf = ROOT::RDF::FromCSV(Form(\"%sSWAN2017.dat\", dir.Data()), false, ' ');" ] }, { "cell_type": "markdown", "id": "59a7dbb9", "metadata": {}, "source": [ "We now prepare the graph input" ] }, { "cell_type": "code", "execution_count": 3, "id": "d97eb7d2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:27.185683Z", "iopub.status.busy": "2026-05-19T20:38:27.185546Z", "iopub.status.idle": "2026-05-19T20:38:28.260346Z", "shell.execute_reply": "2026-05-19T20:38:28.259755Z" } }, "outputs": [], "source": [ "auto d = rdf.Define(\"TimeStamp\", \"auto s = string(Col0) + ' ' + Col1; return (float) TDatime(s.c_str()).Convert();\")\n", " .Define(\"Value\", \"(float)Col2\");\n", "auto timeStamps = d.Take(\"TimeStamp\");\n", "auto values = d.Take(\"Value\");" ] }, { "cell_type": "markdown", "id": "bb2aa8c6", "metadata": {}, "source": [ "Create the time graph. In this example, we provide to the TGraph constructor\n", "the number of (pairs of) points and all the x and y values" ] }, { "cell_type": "code", "execution_count": 4, "id": "0d5ff072", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:28.262342Z", "iopub.status.busy": "2026-05-19T20:38:28.262221Z", "iopub.status.idle": "2026-05-19T20:38:28.672537Z", "shell.execute_reply": "2026-05-19T20:38:28.671779Z" } }, "outputs": [], "source": [ "auto g = new TGraph(values->size(), timeStamps->data(), values->data());\n", "g->SetTitle(\"SWAN Users during July 2017;Time;Number of Sessions\");" ] }, { "cell_type": "markdown", "id": "51e060b9", "metadata": {}, "source": [ "Draw the graph" ] }, { "cell_type": "code", "execution_count": 5, "id": "ad740d4b", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:28.674337Z", "iopub.status.busy": "2026-05-19T20:38:28.674211Z", "iopub.status.idle": "2026-05-19T20:38:28.886362Z", "shell.execute_reply": "2026-05-19T20:38:28.885974Z" } }, "outputs": [], "source": [ "auto c = new TCanvas(\"c\", \"c\", 950, 500);\n", "c->SetLeftMargin(0.07);\n", "c->SetRightMargin(0.04);\n", "c->SetGrid();\n", "g->SetLineWidth(3);\n", "g->SetLineColor(kBlue);\n", "g->Draw(\"al\");\n", "g->GetYaxis()->CenterTitle();" ] }, { "cell_type": "markdown", "id": "7e3da043", "metadata": {}, "source": [ "Make the X axis labelled with time" ] }, { "cell_type": "code", "execution_count": 6, "id": "4f962d21", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:28.898399Z", "iopub.status.busy": "2026-05-19T20:38:28.898253Z", "iopub.status.idle": "2026-05-19T20:38:29.111462Z", "shell.execute_reply": "2026-05-19T20:38:29.110865Z" } }, "outputs": [], "source": [ "auto xaxis = g->GetXaxis();\n", "xaxis->SetTimeDisplay(1);\n", "xaxis->CenterTitle();\n", "xaxis->SetTimeFormat(\"%a %d\");\n", "xaxis->SetTimeOffset(0);\n", "xaxis->SetNdivisions(-219);\n", "xaxis->SetLimits(TDatime(2017, 7, 3, 0, 0, 0).Convert(), TDatime(2017, 7, 22, 0, 0, 0).Convert());\n", "xaxis->SetLabelSize(0.025);\n", "xaxis->CenterLabels();" ] }, { "cell_type": "markdown", "id": "1ddbffe7", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 7, "id": "d0ac7caf", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:38:29.113100Z", "iopub.status.busy": "2026-05-19T20:38:29.112978Z", "iopub.status.idle": "2026-05-19T20:38:29.349614Z", "shell.execute_reply": "2026-05-19T20:38:29.349129Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%jsroot on\n", "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 }