Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
timeonaxis.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 mode on the axis
5/// with different time intervals and time formats.
6/// Through all this script, the time is expressed in UTC. some
7/// information about this format (and others like GPS) may be found at
8/// <a href="http://tycho.usno.navy.mil/systime.html">http://tycho.usno.navy.mil/systime.html</a>
9/// or
10/// <a href="http://www.topology.org/sci/time.html">http://www.topology.org/sci/time.html</a>
11///
12/// The start time is : almost NOW (the time at which the script is executed)
13/// actually, the nearest preceding hour beginning.
14/// The time is in general expressed in UTC time with the C time() function
15/// This will obviously most of the time not be the time displayed on your watch
16/// since it is universal time. See the C time functions for converting this time
17/// into more useful structures.
18///
19/// \macro_image
20/// \macro_code
21///
22/// \author Damir Buskulic
23
24#include <time.h>
25
26TCanvas *timeonaxis()
27{
28
29 time_t script_time;
30 script_time = time(0);
31 script_time = 3600*(int)(script_time/3600);
32
33// The time offset is the one that will be used by all graphs.
34// If one changes it, it will be changed even on the graphs already defined
35 gStyle->SetTimeOffset(script_time);
36
37 TCanvas *ct = new TCanvas("ct","Time on axis",10,10,700,900);
38 ct->Divide(1,3);
39
40 int i;
41
42//### Build a signal : noisy damped sine
43// Time interval : 30 minutes
44
45 gStyle->SetTitleH(0.08);
46 float noise;
47 TH1F *ht = new TH1F("ht","Love at first sight",3000,0.,2000.);
48 for (i=1;i<3000;i++) {
49 noise = gRandom->Gaus(0,120);
50 if (i>700) {
51 noise += 1000*sin((i-700)*6.28/30)*exp((double)(700-i)/300);
52 }
53 ht->SetBinContent(i,noise);
54 }
55 ct->cd(1);
56 ht->SetLineColor(2);
57 ht->GetXaxis()->SetLabelSize(0.05);
58 ht->Draw();
59// Sets time on the X axis
60// The time used is the one set as time offset added to the value
61// of the axis. This is converted into day/month/year hour:min:sec and
62// a reasonable tick interval value is chosen.
63 ht->GetXaxis()->SetTimeDisplay(1);
64
65//### Build a simple graph beginning at a different time
66// Time interval : 5 seconds
67
68 float x[100], t[100];
69 for (i=0;i<100;i++) {
70 x[i] = sin(i*4*3.1415926/50)*exp(-(double)i/20);
71 t[i] = 6000+(double)i/20;
72 }
73 TGraph *gt = new TGraph(100,t,x);
74 gt->SetTitle("Politics");
75 ct->cd(2);
76 gt->SetLineColor(5);
77 gt->SetLineWidth(2);
78 gt->Draw("AL");
79 gt->GetXaxis()->SetLabelSize(0.05);
80// Sets time on the X axis
81 gt->GetXaxis()->SetTimeDisplay(1);
82 gPad->Modified();
83
84//### Build a second simple graph for a very long time interval
85// Time interval : a few years
86
87 float x2[10], t2[10];
88 for (i=0;i<10;i++) {
89 x2[i] = gRandom->Gaus(500,100)*i;
90 t2[i] = i*365*86400;
91 }
92 TGraph *gt2 = new TGraph(10,t2,x2);
93 gt2->SetTitle("Number of monkeys on the moon");
94 ct->cd(3);
95 gt2->SetMarkerColor(4);
96 gt2->SetMarkerStyle(29);
97 gt2->SetMarkerSize(1.3);
98 gt2->Draw("AP");
99 gt2->GetXaxis()->SetLabelSize(0.05);
100// Sets time on the X axis
101 gt2->GetXaxis()->SetTimeDisplay(1);
102//
103// One can choose a different time format than the one chosen by default
104// The time format is the same as the one of the C strftime() function
105// It's a string containing the following formats :
106//
107// for date :
108// %a abbreviated weekday name
109// %b abbreviated month name
110// %d day of the month (01-31)
111// %m month (01-12)
112// %y year without century
113// %Y year with century
114//
115// for time :
116// %H hour (24-hour clock)
117// %I hour (12-hour clock)
118// %p local equivalent of AM or PM
119// %M minute (00-59)
120// %S seconds (00-61)
121// %% %
122// The other characters are output as is.
123
124 gt2->GetXaxis()->SetTimeFormat("y. %Y %F2000-01-01 00:00:00");
125 gPad->Modified();
126 return ct;
127}
Option_t Option_t TPoint TPoint const char x2
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
#define gPad
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels.
Definition TAttAxis.cxx:203
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:45
virtual void SetTimeDisplay(Int_t value)
Definition TAxis.h:171
virtual void SetTimeFormat(const char *format="")
Change the format used for time plotting.
Definition TAxis.cxx:1157
The Canvas class.
Definition TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:716
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition TGraph.cxx:809
TAxis * GetXaxis() const
Get x axis of the graph.
Definition TGraph.cxx:1544
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2374
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
TAxis * GetXaxis()
Definition TH1.h:324
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3062
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition TH1.cxx:9186
void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0) override
Automatic pad generation by division.
Definition TPad.cxx:1153
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:275
void SetTimeOffset(Double_t toffset)
Change the time offset for time plotting.
Definition TStyle.cxx:1909
void SetTitleH(Float_t h=0)
Definition TStyle.h:413
RVec< PromoteType< T > > exp(const RVec< T > &v)
Definition RVec.hxx:1800
RVec< PromoteType< T > > sin(const RVec< T > &v)
Definition RVec.hxx:1814
Double_t x[n]
Definition legend1.C:17