Logo ROOT   6.18/05
Reference Guide
timeonaxis3.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// This example compares what the system time function gmtime and localtime give
5/// with what gives TGaxis. It can be used as referenced test to check if TGaxis
6/// is working properly.
7/// The original code was developed by Philippe Gras (CEA Saclay. IRFU/SEDI)
8///
9/// \macro_image
10/// \macro_code
11///
12/// \authors Philippe Gras, Bertrand Bellenot, Olivier Couet
13
14#if defined(G__WIN32) && defined(__CINT__) && !defined(__MAKECINT__)
15{
16 // timeonaxis3.C has to be run in compiled mode on Windows.
17 // the following code does it.
18
19 gSystem->CompileMacro("timeonaxis3.C");
20 timeonaxis3();
21}
22#else
23
24#include "TAxis.h"
25#include "TGaxis.h"
26#include "TCanvas.h"
27#include "TString.h"
28#include "TLine.h"
29#include "TLatex.h"
30#include <time.h>
31#include <stdio.h>
32
33TString stime(time_t* t, bool utc = false, bool display_time_zone = true) {
34 struct tm* tt;
35 if (utc) tt = gmtime(t);
36 else tt = localtime(t);
37 char buf[256];
38 if (display_time_zone) strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", tt);
39 else strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tt);
40 return TString(buf);
41}
42
43
44TCanvas *timeonaxis3() {
45 double f = 1.8;
46
47 TCanvas* c = new TCanvas;
48
49 TLatex* tex1 = new TLatex;
50 tex1->SetNDC();
51 tex1->SetTextFont(102);
52 tex1->SetTextSize(0.055*f);
53
54 TLatex* tex3 = new TLatex;
55 tex3->SetNDC();
56 tex3->SetTextFont(102);
57 tex3->SetTextSize(0.07*f);
58 tex3->SetTextColor(kBlue+2);
59
60 TLatex* tex2 = new TLatex;
61 tex2->SetNDC();
62 tex2->SetTextFont(102);
63 tex2->SetTextSize(0.07*f);
64 tex2->SetTextColor(kOrange+3);
65
66 time_t offset[] = {0, 0, 1325376000, 1341100800};
67 time_t t[] = {1331150400, 1336417200, 0, 36000};
68
69 c->SetTopMargin(0); c->SetBottomMargin(0);
70 c->SetLeftMargin(0); c->SetRightMargin(0);
71 c->Divide(2, 4, -1, -1);
72 TLine l;
73 l.DrawLine(0.5, 0, 0.5, 1.);
74
75 for(int i = 0; i < 4; ++i){
76 for(int gmt = 0; gmt < 2; ++gmt){
77 const char* opt = (gmt ? "gmt" : "local");
78 TVirtualPad* p = c->cd(2*i + gmt + 1);
79 p->SetTopMargin(0); p->SetBottomMargin(0);
80 p->SetLeftMargin(0); p->SetRightMargin(0);
81 p->SetFillStyle(4000);
82
83 TGaxis* ga = new TGaxis (.4, .25, 5., .25, t[i], t[i] + 1, 1, "t");
84 ga->SetTimeFormat("TGaxis label: #color[2]{%Y-%m-%d %H:%M:%S}");
85 ga->SetLabelFont(102);
86 ga->SetLabelColor(kBlue+2);
87
88 ga->SetTimeOffset(offset[i], opt);
89 ga->SetLabelOffset(0.04*f);
90 ga->SetLabelSize(0.07*f);
91 ga->SetLineColor(0);
92 ga->Draw();
93
94 // Get offset string of axis time format: there is not accessor
95 // to time format in TGaxis.
96 // Assumes TAxis use the same format.
97 TAxis a(10, 0, 1600000000);
98 a.SetTimeOffset(offset[i], opt);
99 const char* offsettimeformat = a.GetTimeFormat();
100
101 char buf[256];
102 if (offset[i] < t[i]) {
103 sprintf(buf, "#splitline{%s, %s}{offset: %ld, option %s}",
104 stime(t+i).Data(), stime(t+i, true).Data(), offset[i], opt);
105 } else {
106 int h = t[i] / 3600;
107 int m = (t[i] - 3600 * h) / 60 ;
108 int s = (t[i] - h * 3600 - m * 60);
109 sprintf(buf, "#splitline{%d h %d m %d s}{offset: %s, option %s}",
110 h, m, s, stime(offset + i, gmt).Data(), opt);
111 }
112 tex1->DrawLatex(.01, .75, buf);
113 tex2->DrawLatex(.01, .50, offsettimeformat);
114 time_t t_ = t[i] + offset[i];
115 sprintf(buf, "Expecting: #color[2]{%s}", stime(&t_, gmt, false).Data());
116 tex3->DrawLatex(.01, .24, buf);
117 if(i > 0) l.DrawLine(0, 0.95, 1, 0.95);
118 }
119 }
120 return c;
121}
122#endif
#define f(i)
Definition: RSha256.hxx:104
#define c(i)
Definition: RSha256.hxx:101
#define h(i)
Definition: RSha256.hxx:106
@ kOrange
Definition: Rtypes.h:65
@ kBlue
Definition: Rtypes.h:64
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition: TAttFill.h:39
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void SetBottomMargin(Float_t bottommargin)
Set Pad bottom margin in fraction of the pad height.
Definition: TAttPad.cxx:100
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition: TAttPad.cxx:110
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:120
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition: TAttPad.cxx:130
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:45
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
Class to manage histogram axis.
Definition: TAxis.h:30
The Canvas class.
Definition: TCanvas.h:31
The axis painter class.
Definition: TGaxis.h:24
void SetTimeFormat(const char *tformat)
Change the format used for time plotting.
Definition: TGaxis.cxx:2723
void SetLabelFont(Int_t labelfont)
Definition: TGaxis.h:106
void SetLabelOffset(Float_t labeloffset)
Definition: TGaxis.h:107
void SetTimeOffset(Double_t toffset, Option_t *option="local")
Change the time offset. If option = "gmt", set display mode to GMT.
Definition: TGaxis.cxx:2747
void SetLabelColor(Int_t labelcolor)
Definition: TGaxis.h:105
void SetLabelSize(Float_t labelsize)
Definition: TGaxis.h:108
To draw Mathematical Formula.
Definition: TLatex.h:18
TLatex * DrawLatex(Double_t x, Double_t y, const char *text)
Make a copy of this object with the new parameters And copy object attributes.
Definition: TLatex.cxx:1914
A simple line.
Definition: TLine.h:23
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:195
Basic string class.
Definition: TString.h:131
virtual int CompileMacro(const char *filename, Option_t *opt="", const char *library_name="", const char *build_dir="", UInt_t dirmode=0)
This method compiles and loads a shared library containing the code from the file "filename".
Definition: TSystem.cxx:2873
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition: TText.cxx:812
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:50
static constexpr double s
auto * m
Definition: textangle.C:8
auto * tt
Definition: textangle.C:16
auto * l
Definition: textangle.C:4
auto * a
Definition: textangle.C:12