Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
timeSeriesFromCSV.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook -js
4/// 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///
8/// \macro_image
9/// \macro_code
10///
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("/graphs/");
20 dir.ReplaceAll("/./", "/");
21 FILE *f = fopen(Form("%sSWAN2017.dat", dir.Data()), "r");
22
23 // Create the time graph
24 auto g = new TGraph();
25 g->SetTitle("SWAN Users during July 2017;Time;Number of Sessions");
26
27 // Read the data and fill the graph with time along the X axis and number
28 // of users along the Y axis
29 char line[80];
30 float v;
31 char dt[20];
32 int i = 0;
33 while (fgets(line, 80, f)) {
34 sscanf(&line[20], "%f", &v);
35 strncpy(dt, line, 18);
36 dt[19] = '\0';
37 g->SetPoint(i, TDatime(dt).Convert(), v);
38 i++;
39 }
40 fclose(f);
41
42 // Draw the graph
43 auto c = new TCanvas("c", "c", 950, 500);
44 c->SetLeftMargin(0.07);
45 c->SetRightMargin(0.04);
46 c->SetGrid();
47 g->SetLineWidth(3);
48 g->SetLineColor(kBlue);
49 g->Draw("al");
50 g->GetYaxis()->CenterTitle();
51
52 // Make the X axis labelled with time.
53 auto xaxis = g->GetXaxis();
54 xaxis->SetTimeDisplay(1);
55 xaxis->CenterTitle();
56 xaxis->SetTimeFormat("%a %d");
57 xaxis->SetTimeOffset(0);
58 xaxis->SetNdivisions(-219);
59 xaxis->SetLimits(TDatime(2017, 7, 3, 0, 0, 0).Convert(), TDatime(2017, 7, 22, 0, 0, 0).Convert());
60 xaxis->SetLabelSize(0.025);
61 xaxis->CenterLabels();
62}
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
@ kBlue
Definition Rtypes.h:66
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
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
TLine * line