Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#include "TAxis.h"
15#include "TGaxis.h"
16#include "TCanvas.h"
17#include "TString.h"
18#include "TLine.h"
19#include "TLatex.h"
20#include <time.h>
21#include <stdio.h>
22
23TString stime(time_t* t, bool utc = false, bool display_time_zone = true) {
24 struct tm* tt;
25 if (utc) tt = gmtime(t);
26 else tt = localtime(t);
27 char buf[256];
28 if (display_time_zone) strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %Z", tt);
29 else strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tt);
30 return TString(buf);
31}
32
33
34TCanvas *timeonaxis3() {
35 double f = 1.8;
36
37 TCanvas* c = new TCanvas;
38
39 TLatex* tex1 = new TLatex;
40 tex1->SetNDC();
41 tex1->SetTextFont(102);
42 tex1->SetTextSize(0.055*f);
43
44 TLatex* tex3 = new TLatex;
45 tex3->SetNDC();
46 tex3->SetTextFont(102);
47 tex3->SetTextSize(0.07*f);
48 tex3->SetTextColor(kBlue+2);
49
50 TLatex* tex2 = new TLatex;
51 tex2->SetNDC();
52 tex2->SetTextFont(102);
53 tex2->SetTextSize(0.07*f);
54 tex2->SetTextColor(kOrange+3);
55
56 time_t offset[] = {0, 0, 1325376000, 1341100800};
57 time_t t[] = {1331150400, 1336417200, 0, 36000};
58
59 c->SetTopMargin(0); c->SetBottomMargin(0);
60 c->SetLeftMargin(0); c->SetRightMargin(0);
61 c->Divide(2, 4, -1, -1);
62 TLine l;
63 l.DrawLine(0.5, 0, 0.5, 1.);
64
65 for(int i = 0; i < 4; ++i){
66 for(int gmt = 0; gmt < 2; ++gmt){
67 const char* opt = (gmt ? "gmt" : "local");
68 TVirtualPad* p = c->cd(2*i + gmt + 1);
69 p->SetTopMargin(0); p->SetBottomMargin(0);
70 p->SetLeftMargin(0); p->SetRightMargin(0);
71 p->SetFillStyle(4000);
72
73 TGaxis* ga = new TGaxis (.4, .25, 5., .25, t[i], t[i] + 1, 1, "t");
74 ga->SetTimeFormat("TGaxis label: #color[2]{%Y-%m-%d %H:%M:%S}");
75 ga->SetLabelFont(102);
76 ga->SetLabelColor(kBlue+2);
77
78 ga->SetTimeOffset(offset[i], opt);
79 ga->SetLabelOffset(0.04*f);
80 ga->SetLabelSize(0.07*f);
81 ga->SetLineColor(0);
82 ga->Draw();
83
84 // Get offset string of axis time format: there is not accessor
85 // to time format in TGaxis.
86 // Assumes TAxis use the same format.
87 TAxis a(10, 0, 1600000000);
88 a.SetTimeOffset(offset[i], opt);
89 const char* offsettimeformat = a.GetTimeFormat();
90
91 char buf[256];
92 if (offset[i] < t[i]) {
93 sprintf(buf, "#splitline{%s, %s}{offset: %ld, option %s}",
94 stime(t+i).Data(), stime(t+i, true).Data(), offset[i], opt);
95 } else {
96 int h = t[i] / 3600;
97 int m = (t[i] - 3600 * h) / 60 ;
98 int s = (t[i] - h * 3600 - m * 60);
99 sprintf(buf, "#splitline{%d h %d m %d s}{offset: %s, option %s}",
100 h, m, s, stime(offset + i, gmt).Data(), opt);
101 }
102 tex1->DrawLatex(.01, .75, buf);
103 tex2->DrawLatex(.01, .50, offsettimeformat);
104 time_t t_ = t[i] + offset[i];
105 sprintf(buf, "Expecting: #color[2]{%s}", stime(&t_, gmt, false).Data());
106 tex3->DrawLatex(.01, .24, buf);
107 if(i > 0) l.DrawLine(0, 0.95, 1, 0.95);
108 }
109 }
110 return c;
111}
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define h(i)
Definition RSha256.hxx:106
@ kOrange
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:66
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:99
virtual void SetLeftMargin(Float_t leftmargin)
Set Pad left margin in fraction of the pad width.
Definition TAttPad.cxx:109
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition TAttPad.cxx:119
virtual void SetTopMargin(Float_t topmargin)
Set Pad top margin in fraction of the pad height.
Definition TAttPad.cxx:129
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
Class to manage histogram axis.
Definition TAxis.h:30
The Canvas class.
Definition TCanvas.h:23
The axis painter class.
Definition TGaxis.h:23
void SetTimeFormat(const char *tformat)
Change the format used for time plotting.
Definition TGaxis.cxx:2776
void SetLabelFont(Int_t labelfont)
Definition TGaxis.h:105
void SetLabelOffset(Float_t labeloffset)
Definition TGaxis.h:106
void SetTimeOffset(Double_t toffset, Option_t *option="local")
Change the time offset. If option = "gmt", set display mode to GMT.
Definition TGaxis.cxx:2800
void SetLabelColor(Int_t labelcolor)
Definition TGaxis.h:104
void SetLabelSize(Float_t labelsize)
Definition TGaxis.h:107
To draw Mathematical Formula.
Definition TLatex.h:18
void DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2, TextSpec_t spec)
Draw a line in a Latex formula.
Definition TLatex.cxx:1965
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:1941
Use the TLine constructor to create a simple line.
Definition TLine.h:22
virtual TLine * DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Draw this line with new coordinates.
Definition TLine.cxx:102
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:267
Basic string class.
Definition TString.h:136
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TText.cxx:813
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
auto * m
Definition textangle.C:8
auto * tt
Definition textangle.C:16
auto * l
Definition textangle.C:4