ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
timeonaxis.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphs
3 /// This macro illustrates the use of the time mode on the axis
4 /// with different time intervals and time formats.
5 /// Through all this script, the time is expressed in UTC. some
6 /// information about this format (and others like GPS) may be found at
7 /// <a href="http://tycho.usno.navy.mil/systime.html">http://tycho.usno.navy.mil/systime.html</a>
8 /// or
9 /// <a href="http://www.topology.org/sci/time.html">http://www.topology.org/sci/time.html</a>
10 ///
11 /// The start time is : almost NOW (the time at which the script is executed)
12 /// actually, the nearest preceding hour beginning.
13 /// The time is in general expressed in UTC time with the C time() function
14 /// This will obviously most of the time not be the time displayed on your watch
15 /// since it is universal time. See the C time functions for converting this time
16 /// into more useful structures.
17 ///
18 /// \macro_image
19 /// \macro_code
20 ///
21 /// \author Damir Buskulic
22 
23 #include <time.h>
24 
25 TCanvas *timeonaxis()
26 {
27 
28 //Author:
29 
30  time_t script_time;
31  script_time = time(0);
32  script_time = 3600*(int)(script_time/3600);
33 
34 // The time offset is the one that will be used by all graphs.
35 // If one changes it, it will be changed even on the graphs already defined
36  gStyle->SetTimeOffset(script_time);
37 
38  TCanvas *ct = new TCanvas("ct","Time on axis",10,10,700,900);
39  ct->Divide(1,3);
40  ct->SetFillColor(28);
41 
42  int i;
43 
44 //======= Build a signal : noisy damped sine ======
45 // Time interval : 30 minutes
46 
47  gStyle->SetTitleH(0.08);
48  float noise;
49  TH1F *ht = new TH1F("ht","Love at first sight",3000,0.,2000.);
50  for (i=1;i<3000;i++) {
51  noise = gRandom->Gaus(0,120);
52  if (i>700) {
53  noise += 1000*sin((i-700)*6.28/30)*exp((double)(700-i)/300);
54  }
55  ht->SetBinContent(i,noise);
56  }
57  ct->cd(1);
58  gPad->SetFillColor(41);
59  gPad->SetFrameFillColor(33);
60  ht->SetLineColor(2);
61  ht->GetXaxis()->SetLabelSize(0.05);
62  ht->Draw();
63 // Sets time on the X axis
64 // The time used is the one set as time offset added to the value
65 // of the axis. This is converted into day/month/year hour:min:sec and
66 // a reasonable tick interval value is chosen.
67  ht->GetXaxis()->SetTimeDisplay(1);
68 
69 //======= Build a simple graph beginning at a different time ======
70 // Time interval : 5 seconds
71 
72  float x[100], t[100];
73  for (i=0;i<100;i++) {
74  x[i] = sin(i*4*3.1415926/50)*exp(-(double)i/20);
75  t[i] = 6000+(double)i/20;
76  }
77  TGraph *gt = new TGraph(100,t,x);
78  gt->SetTitle("Politics");
79  ct->cd(2);
80  gPad->SetFillColor(41);
81  gPad->SetFrameFillColor(33);
82  gt->SetFillColor(19);
83  gt->SetLineColor(5);
84  gt->SetLineWidth(2);
85  gt->Draw("AL");
86  gt->GetXaxis()->SetLabelSize(0.05);
87 // Sets time on the X axis
88  gt->GetXaxis()->SetTimeDisplay(1);
89  gPad->Modified();
90 
91 //======= Build a second simple graph for a very long time interval ======
92 // Time interval : a few years
93 
94  float x2[10], t2[10];
95  for (i=0;i<10;i++) {
96  x2[i] = gRandom->Gaus(500,100)*i;
97  t2[i] = i*365*86400;
98  }
99  TGraph *gt2 = new TGraph(10,t2,x2);
100  gt2->SetTitle("Number of monkeys on the moon");
101  ct->cd(3);
102  gPad->SetFillColor(41);
103  gPad->SetFrameFillColor(33);
104  gt2->SetFillColor(19);
105  gt2->SetMarkerColor(4);
106  gt2->SetMarkerStyle(29);
107  gt2->SetMarkerSize(1.3);
108  gt2->Draw("AP");
109  gt2->GetXaxis()->SetLabelSize(0.05);
110 // Sets time on the X axis
111  gt2->GetXaxis()->SetTimeDisplay(1);
112 //
113 // One can choose a different time format than the one chosen by default
114 // The time format is the same as the one of the C strftime() function
115 // It's a string containing the following formats :
116 // for date :
117 // %a abbreviated weekday name
118 // %b abbreviated month name
119 // %d day of the month (01-31)
120 // %m month (01-12)
121 // %y year without century
122 // %Y year with century
123 //
124 // for time :
125 // %H hour (24-hour clock)
126 // %I hour (12-hour clock)
127 // %p local equivalent of AM or PM
128 // %M minute (00-59)
129 // %S seconds (00-61)
130 // %% %
131 // The other characters are output as is.
132 
133  gt2->GetXaxis()->SetTimeFormat("y. %Y %F2000-01-01 00:00:00");
134  gPad->Modified();
135  return ct;
136 }
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void SetTimeFormat(const char *format="")
Change the format used for time plotting.
Definition: TAxis.cxx:943
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:235
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2153
void SetTimeOffset(Double_t toffset)
Change the time offset for time plotting.
Definition: TStyle.cxx:1463
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:740
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
double sin(double)
virtual void SetTimeDisplay(Int_t value)
Definition: TAxis.h:161
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
TAxis * GetXaxis() const
Get x axis of the graph.
Definition: TGraph.cxx:1563
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
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:8543
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
void SetTitleH(Float_t h=0)
Definition: TStyle.h:410
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:187
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
The Canvas class.
Definition: TCanvas.h:48
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
#define gPad
Definition: TVirtualPad.h:288
tuple ct
Definition: tornado.py:53
double exp(double)
TAxis * GetXaxis()
Definition: TH1.h:319