Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
gr109_timeSeriesFromCSV_RDF.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook -js
4/// \preview This macro illustrates the use of the time axis on a TGraph
5/// with data read from a text file containing the SWAN usage
6/// statistics during July 2017.
7/// We exploit the TDataFrame for reading from the file. See the [RDataFrame documentation](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) and [RDataFrame tutorials](https://root.cern/doc/master/group__tutorial__dataframe.html)
8///
9/// \macro_image
10/// \macro_code
11/// \authors Danilo Piparo, Olivier Couet
12
14{
15 // Open the data file. This csv contains the usage statistics of a CERN IT
16 // service, SWAN, during two weeks. We would like to plot this data with
17 // ROOT to draw some conclusions from it.
18 TString dir = gROOT->GetTutorialDir();
19 dir.Append("/visualisation/graphs/");
20 dir.ReplaceAll("/./", "/");
21
22 // Read the data from the file using TDataFrame. We do not have headers and
23 // we would like the delimiter to be a space
24 auto rdf = ROOT::RDF::FromCSV(Form("%sSWAN2017.dat", dir.Data()), false, ' ');
25
26 // We now prepare the graph input
27 auto d = rdf.Define("TimeStamp", "auto s = string(Col0) + ' ' + Col1; return (float) TDatime(s.c_str()).Convert();")
28 .Define("Value", "(float)Col2");
29 auto timeStamps = d.Take<float>("TimeStamp");
30 auto values = d.Take<float>("Value");
31
32 // Create the time graph. In this example, we provide to the TGraph constructor
33 // the number of (pairs of) points and all the x and y values
34 auto g = new TGraph(values->size(), timeStamps->data(), values->data());
35 g->SetTitle("SWAN Users during July 2017;Time;Number of Sessions");
36
37 // Draw the graph
38 auto c = new TCanvas("c", "c", 950, 500);
39 c->SetLeftMargin(0.07);
40 c->SetRightMargin(0.04);
41 c->SetGrid();
42 g->SetLineWidth(3);
43 g->SetLineColor(kBlue);
44 g->Draw("al");
45 g->GetYaxis()->CenterTitle();
46
47 // Make the X axis labelled with time
48 auto xaxis = g->GetXaxis();
49 xaxis->SetTimeDisplay(1);
50 xaxis->CenterTitle();
51 xaxis->SetTimeFormat("%a %d");
52 xaxis->SetTimeOffset(0);
53 xaxis->SetNdivisions(-219);
54 xaxis->SetLimits(TDatime(2017, 7, 3, 0, 0, 0).Convert(), TDatime(2017, 7, 22, 0, 0, 0).Convert());
55 xaxis->SetLabelSize(0.025);
56 xaxis->CenterLabels();
57}
#define d(i)
Definition RSha256.hxx:102
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
@ kBlue
Definition Rtypes.h:66
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
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:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
TString & Append(const char *cs)
Definition TString.h:572
RDataFrame FromCSV(std::string_view fileName, const RCsvDS::ROptions &options)
Factory method to create a CSV RDataFrame.
Definition RCsvDS.cxx:671