Logo ROOT  
Reference Guide
timeSeriesFromCSV_TDF.C File Reference

Detailed Description

View in nbviewer Open in SWAN
This macro illustrates the use of the time axis on a TGraph with data read from a text file containing the SWAN usage statistics during July 2017.

We exploit the TDataFrame for reading from the file

void timeSeriesFromCSV_TDF()
// Open the data file. This csv contains the usage statistics of a CERN IT
// service, SWAN, during two weeks. We would like to plot this data with
// ROOT to draw some conclusions from it.
TString dir = gROOT->GetTutorialDir();
dir.ReplaceAll("/./", "/");
// Read the data from the file using TDataFrame. We do not have headers and
// we would like the delimiter to be a space
auto tdf = ROOT::RDF::MakeCsvDataFrame(Form("%sSWAN2017.dat", dir.Data()), false, ' ');
// We now prepare the graph input
auto d = tdf.Define("TimeStamp", "auto s = string(Col0) + ' ' + Col1; return (float) TDatime(s.c_str()).Convert();")
.Define("Value", "(float)Col2");
auto timeStamps = d.Take<float>("TimeStamp");
auto values = d.Take<float>("Value");
// Create the time graph
auto g = new TGraph(values->size(), timeStamps->data(), values->data());
g->SetTitle("SWAN Users during July 2017;Time;Number of Sessions");
// Draw the graph
auto c = new TCanvas("c", "c", 950, 500);
// Make the X axis labelled with time.
auto xaxis = g->GetXaxis();
xaxis->SetTimeFormat("%a %d");
xaxis->SetLimits(TDatime(2017, 7, 3, 0, 0, 0).Convert(), TDatime(2017, 7, 22, 0, 0, 0).Convert());
#define d(i)
Definition: RSha256.hxx:102
#define c(i)
Definition: RSha256.hxx:101
@ kBlue
Definition: Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t g
#define gROOT
Definition: TROOT.h:404
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2456
The Canvas class.
Definition: TCanvas.h:23
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:37
A TGraph is an object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TString & Append(const char *cs)
Definition: TString.h:564
RDataFrame MakeCsvDataFrame(std::string_view fileName, bool readHeaders=true, char delimiter=',', Long64_t linesChunkSize=-1LL, std::unordered_map< std::string, char > &&colTypes={})
Factory method to create a CSV RDataFrame.
Definition: RCsvDS.cxx:556
Danilo Piparo, Olivier Couet

Definition in file timeSeriesFromCSV_TDF.C.