-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
TGaxis.cxx
2951 lines (2600 loc) · 102 KB
/
TGaxis.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/graf:$Id$
// Author: Rene Brun, Olivier Couet 12/12/94
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <iostream>
#include "TROOT.h"
#include "TBuffer.h"
#include "TGaxis.h"
#include "TAxisModLab.h"
#include "TVirtualPad.h"
#include "TVirtualX.h"
#include "TLine.h"
#include "TLatex.h"
#include "TStyle.h"
#include "TF1.h"
#include "TAxis.h"
#include "THashList.h"
#include "TObject.h"
#include "TMath.h"
#include "THLimitsFinder.h"
#include "TColor.h"
#include "TTimeStamp.h"
#include "strlcpy.h"
#include "snprintf.h"
Int_t TGaxis::fgMaxDigits = 5;
Float_t TGaxis::fXAxisExpXOffset = 0.;
Float_t TGaxis::fXAxisExpYOffset = 0.;
Float_t TGaxis::fYAxisExpXOffset = 0.;
Float_t TGaxis::fYAxisExpYOffset = 0.;
const Int_t kHori = BIT(9);
ClassImp(TGaxis);
/** \class TGaxis
\ingroup BasicGraphics
The axis painter class.
Instances of this class are generated by the histograms and graphs painting
classes when `TAxis` are drawn. `TGaxis` is the "painter class" of
`TAxis`. Therefore it is mainly used via `TAxis`, even if is some
occasion it can be used directly to draw an axis which is not part of a graph
or an instance. For instance to draw an extra scale on a plot.
- [Basic definition](\ref GA00)
- [Definition with a function](\ref GA01)
- [Logarithmic axis](\ref GA02)
- [Blank axis](\ref GA03)
- [Tick marks' orientation](\ref GA04)
- [Tick marks' size](\ref GA05)
- [Labels' positioning](\ref GA06)
- [Labels' orientation](\ref GA07)
- [Labels' position on tick marks](\ref GA08)
- [Labels' format](\ref GA09)
- [Alphanumeric labels](\ref GA10)
- [Changing axis labels](\ref GA10a)
- [Number of divisions optimisation](\ref GA11)
- [Maximum Number of Digits for the axis labels](\ref GA12)
- [Optional grid](\ref GA13)
- [Time axis](\ref GA14)
\anchor GA00
## Basic definition
A `TGaxis` is defined the following way:
~~~ {.cpp}
TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
Double_t wmin, Double_t wmax, Int_t ndiv, Option_t *chopt,
Double_t gridlength)
~~~
Where:
- xmin : X origin coordinate in user's coordinates space.
- xmax : X end axis coordinate in user's coordinates space.
- ymin : Y origin coordinate in user's coordinates space.
- ymax : Y end axis coordinate in user's coordinates space.
- wmin : Lowest value for the tick mark labels written on the axis.
- wmax : Highest value for the tick mark labels written on the axis.
- ndiv : Number of divisions.
- ndiv=N1 + 100*N2 + 10000*N3
- N1=number of 1st divisions.
- N2=number of 2nd divisions.
- N3=number of 3rd divisions. e.g.:
- ndiv=0 --> no tick marks.
- ndiv=2 --> 2 divisions, one tick mark in the middle of the axis.
- chopt : Drawing options (see below).
- gridlength: grid length on main tick marks.
The example below generates various kind of axis.
Begin_Macro(source)
{
TCanvas *c1 = new TCanvas("c1","Examples of TGaxis",10,10,700,500);
c1->Range(-10,-1,10,1);
TGaxis *axis1 = new TGaxis(-4.5,-0.2,5.5,-0.2,-6,8,510,"");
axis1->SetName("axis1");
axis1->Draw();
TGaxis *axis2 = new TGaxis(-4.5,0.2,5.5,0.2,0.001,10000,510,"G");
axis2->SetName("axis2");
axis2->Draw();
TGaxis *axis3 = new TGaxis(-9,-0.8,-9,0.8,-8,8,50510,"");
axis3->SetName("axis3");
axis3->Draw();
TGaxis *axis4 = new TGaxis(-7,-0.8,-7,0.8,1,10000,50510,"G");
axis4->SetName("axis4");
axis4->Draw();
TGaxis *axis5 = new TGaxis(-4.5,-0.6,5.5,-0.6,1.2,1.32,80506,"-+");
axis5->SetName("axis5");
axis5->SetLabelSize(0.03);
axis5->SetTextFont(72);
axis5->SetLabelOffset(0.025);
axis5->Draw();
TGaxis *axis6 = new TGaxis(-4.5,0.6,5.5,0.6,100,900,50510,"-");
axis6->SetName("axis6");
axis6->Draw();
TGaxis *axis7 = new TGaxis(8,-0.8,8,0.8,0,9000,50510,"+L");
axis7->SetName("axis7");
axis7->SetLabelOffset(0.01);
axis7->Draw();
//one can make axis going top->bottom. However because of a long standing
//problem, the two x values should not be equal
TGaxis *axis8 = new TGaxis(6.5,0.8,6.499,-0.8,0,90,50510,"-");
axis8->SetName("axis8");
axis8->Draw();
}
End_Macro
\anchor GA01
## Definition with a function
Instead of the wmin,wmax arguments of the normal definition, the
name of a `TF1` function can be specified. This function will be used to
map the user coordinates to the axis values and ticks.
A `TGaxis` is defined the following way:
~~~ {.cpp}
TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
const char *func, Int_t ndiv, Option_t *chopt,
Double_t gridlength)
~~~
Where:
- xmin : X origin coordinate in user's coordinates space.
- xmax : X end axis coordinate in user's coordinates space.
- ymin : Y origin coordinate in user's coordinates space.
- ymax : Y end axis coordinate in user's coordinates space.
- func : function defining axis labels and tick marks.
- ndiv : Number of divisions.
- ndiv=N1 + 100*N2 + 10000*N3
- N1=number of 1st divisions.
- N2=number of 2nd divisions.
- N3=number of 3rd divisions. e.g.:
- ndiv=0 --> no tick marks.
- ndiv=2 --> 2 divisions, one tick mark in the middle of the axis.
- chopt : Drawing options (see below).
- gridlength: grid length on main tick marks.
It should be noted that `func` is not defined in the user's coordinate space,
but in the new TGaxis space. If `x` is the original axis, `w` the new axis,
and `w = f(x)` (for example, `f` is a calibration function converting ADC
channels `x` to energy `w`), then `func` must be supplied as `f^{-1}(w)`.
Examples:
Begin_Macro(source)
{
TCanvas *c2 = new TCanvas("c2","c2",10,10,700,500);
gPad->DrawFrame(0.,-2.,10.,2);
TF1 *f1=new TF1("f1","-x",-10,10);
TGaxis *A1 = new TGaxis(0,2,10,2,"f1",510,"-");
A1->SetTitle("axis with decreasing values");
A1->Draw();
TF1 *f2=new TF1("f2","exp(x)",0,2);
TGaxis *A2 = new TGaxis(1,1,9,1,"f2");
A2->SetTitle("exponential axis");
A2->SetLabelSize(0.03);
A2->SetTitleSize(0.03);
A2->SetTitleOffset(1.2);
A2->Draw();
TF1 *f3=new TF1("f3","log10(x)",1,1000);
TGaxis *A3 = new TGaxis(2,-2,2,0,"f3",505,"");
A3->SetTitle("logarithmic axis");
A3->SetLabelSize(0.02);
A3->SetTitleSize(0.03);
A3->SetTitleOffset(0.); // Axis title automatically placed
A3->Draw();
}
End_Macro
Note that this functionality has some limitations and does not follow all the TGaxis setting.
In particular the number of divisions or the maximum number digits do not apply.
\anchor GA02
## Logarithmic axis
By default axis are linear. To define a `TGaxis` as logarithmic, it is
enough to create it with the option `"G"`.
When plotting an histogram or a graph the logarithmic scale can be set using:
- `gPad->SetLogx(1);` set the logarithmic scale on the X axis
- `gPad->SetLogy(1);` set the logarithmic scale on the Y axis
When the `SetMoreLogLabels()` method is called more labels are drawn
when in logarithmic scale and there is a small number of decades (less than 3).
\anchor GA03
## Blank axis
To draw only the axis tick marks without the axis body, it is enough to specify
the option `"B"`. It useful to superpose axis.
\anchor GA04
## Tick marks' orientation
By default tick marks are drawn on the positive side of the axis, except for
vertical axis for which the default is negative. The `chop` parameter
allows to control the tick marks orientation:
- `chopt = "+"`: tick marks are drawn on Positive side. (default)
- `chopt ="-"`: tick mark are drawn on the negative side.
- `chopt = "+-"`: tick marks are drawn on both sides of the axis.
- `chopt = "U"`: Unlabelled axis, default is labeled.
\anchor GA05
## Tick marks' size
By default, tick marks have a length equal to 3 per cent of the axis length.
When the option "S" is specified, the length of the tick marks is equal to
`fTickSize*axis_length`, where `fTickSize` may be set via
`TGaxis::SetTickSize`.
When plotting an histogram `h` the tick marks size can be changed using:
- `h->GetXaxis()->SetTickLength(0.02);` set the tick length for the X axis
- `gStyle->SetTickLength(0.02,"x");` set the tick length for the X axis
of all histograms drawn after this instruction.
A good way to remove tick marks on an axis is to set the tick length to 0:
`h->GetXaxis()->SetTickLength(0.);`
\anchor GA06
## Labels' positioning
Labels are normally drawn on side opposite to tick marks. However the option
`"="` allows to draw them on the same side. The distance between the labels and
the axis body can be changed with `SetLabelOffset`.
\anchor GA07
## Labels' orientation
By default axis labels are drawn parallel to the axis. However if the axis is vertical
then are drawn perpendicular to the axis.
\anchor GA08
## Labels' position on tick marks
By default axis labels are centered on tick marks. However, for vertical axis,
they are right adjusted. The `chop` parameter allows to control the labels'
position on tick marks:
- `chopt = "R"`: labels are Right adjusted on tick mark.(default is centered)
- `chopt = "L"`: labels are Left adjusted on tick mark.
- `chopt = "C"`: labels are Centered on tick mark.
- `chopt = "M"`: In the Middle of the divisions.
\anchor GA09
## Labels' format
Blank characters are stripped, and then the label is correctly aligned. the dot,
if last character of the string, is also stripped, unless the option `"."`
(a dot, or period) is specified. if `SetDecimals(kTRUE)` has been called
all labels have the same number of decimals after the `"."`
The same is true if `gStyle->SetStripDecimals(kFALSE)` has been called.
In the following, we have some parameters, like tick marks length and characters
height (in percentage of the length of the axis (user's coordinates))
The default values are as follows:
- Primary tick marks: 3.0 %
- Secondary tick marks: 1.5 %
- Third order tick marks: .75 %
- Characters height for labels: 4%
- Labels offset: 1.0 %
By default, an exponent of the form 10^N is used when the label values are either
all very small or very large. One can disable the exponent by calling
`axis.SetNoExponent(kTRUE)`.
`TGaxis::SetExponentOffset(Float_t xoff, Float_t yoff, Option_t *axis)` is
static function to set X and Y offset of the axis 10^n notation. It is in % of
the pad size. It can be negative. `axis` specifies which axis
(`"x"` or/and `"y"`), default is `"x"` if `axis = "xz"`
set the two axes
\anchor GA10
## Alphanumeric labels
Axis labels can be any alphanumeric character strings. Such axis can be produced
only with histograms because the labels'definition is stored in `TAxis`.
The following example demonstrates how to create such labels.
Begin_Macro(source)
../../../tutorials/hist/hlabels2.C
End_Macro
Because the alphanumeric labels are usually longer that the numeric labels, their
size is by default equal to `0.66666 * the_numeric_labels_size`.
\anchor GA10a
## Changing axis labels
\since **ROOT version 6.07/07:**
After an axis has been created, TGaxis::ChangeLabel allows to define new text
attributes for a given label. A fine tuning of the labels can be done. All the
attributes can be changed as well as the text label itself.
When plotting an histogram or a graph the labels can be changed like in the
following example which shows a way to produce \f$\pi\f$-axis :
Begin_Macro(source)
{
Double_t pi = TMath::Pi();
TF1* f = new TF1("f","TMath::Cos(x/TMath::Pi())", -pi, pi);
TAxis* a = f->GetXaxis();
a->SetNdivisions(-502);
a->ChangeLabel(1,-1,-1,-1,-1,-1,"-#pi");
a->ChangeLabel(-1,-1,-1,-1,-1,-1,"#pi");
f->Draw();
}
End_Macro
\anchor GA11
## Number of divisions optimisation
By default the number of divisions on axis is optimised to show a coherent
labelling of the main tick marks. The number of division (`ndiv`) is a
composite integer given by:
` ndiv = N1 + 100*N2 + 10000*N3`
- `N1` = number of 1st divisions.
- `N2` = number of 2nd divisions.
- `N3` = number of 3rd divisions.
by default the value of `N1`, `N2` and `N3` are maximum
values. After optimisation the real number of divisions will be smaller or
equal to these value. If one wants to bypass the optimisation, the option `"N"`
should be given when the `TGaxis` is created. The option `"I"`
also act on the number of division as it will force an integer labelling of
the axis.
On an histogram pointer `h` the number of divisions can be set in different ways:.
- Directly on the histogram. The following will set the number of division
to 510 on the X axis of `h`. To avoid optimization the number of divisions
should be negative (ie: -510);
~~~ {.cpp}
h->SetNdivisions(510, "X");
~~~
- On the axis itself:
~~~ {.cpp}
h->GetXaxis()->SetNdivisions(510, kTRUE);
~~~
The first parameter is the number of division. If it is negative of if the
second parameter is kFALSE then the number of divisions is not optimised.
And other signature is also allowed:
~~~ {.cpp}
h->GetXaxis()->SetNdivisions(10, 5, 0, kTRUE);
~~~
\anchor GA12
## Maximum Number of Digits for the axis labels
The static function `TGaxis::SetMaxDigits` sets the maximum number of
digits permitted for the axis labels above which the notation with 10^N is used.
For example, to accept 6 digits number like 900000 on an axis call
`TGaxis::SetMaxDigits(6)`. The default value is 5.
`fgMaxDigits` must be greater than 0.
Warning: even when called on a particular TGaxis* instance, this static function
changes globally the number of digits for all axes (X, Y, ...) in the canvas.
If you want to change the maximum number of digits N only of the current TGaxis*,
and not all the others, use axis->SetNdivisions(N*1000000 + (axis->GetNdiv()%1000000))
instead of axis->SetMaxDigits(N).
\anchor GA13
## Optional grid
The option `"W"` allows to draw a grid on the primary tick marks. In case
of a log axis, the grid is only drawn for the primary tick marks if the number
of secondary and tertiary divisions is 0. `SetGridLength()` allows to define
the length of the grid.
When plotting an histogram or a graph the grid can be set ON or OFF using:
- `gPad->SetGridy(1);` set the grid on the X axis
- `gPad->SetGridx(1);` set the grid on the Y axis
- `gPad->SetGrid(1,1);` set the grid on both axis.
\anchor GA14
## Time axis
Histograms' axis can be defined as "time axis". To do that it is enough to activate
the TAxis::SetTimeDisplay attribute on a given axis. If `h` is an histogram, it is
done the following way:
~~~ .cpp
h->GetXaxis()->SetTimeDisplay(1); // The X axis is a time axis
~~~
Two parameters can be adjusted in order to define time axis:
### The time format:
Defines the format of the labels along the time axis. It can be changed using the TAxis
TAxis::SetTimeFormat. The time format is the one used by the C function **strftime()**.
It's a string containing the following formatting characters:
- for date :
- **%a** abbreviated weekday name
- **%b** abbreviated month name
- **%d** day of the month (01-31)
- **%m** month (01-12)
- **%y** year without century
- **%Y** year with century
- for time :
- **%H** hour (24-hour clock)
- **%I** hour (12-hour clock)
- **%p** local equivalent of AM or PM
- **%M** minute (00-59)
- **%S** seconds (00-61)
- **%%** %
The other characters are output as is. For example to have a format like
`dd/mm/yyyy` one should do:
~~~ .cpp
h->GetXaxis()->SetTimeFormat("%d\/%m\/%Y");
~~~
### The time offset:
This is a time in seconds in the UNIX standard UTC format (this is an universal
time, not the local time), defining the starting date of an histogram axis.
This date should be greater than 01/01/95 and is given in seconds. There are
three ways to define the time offset:
#### By setting the global default time offset:
~~~ .cpp
TDatime da(2003,02,28,12,00,00);
gStyle->SetTimeOffset(da.Convert());
~~~
If no time offset is defined for a particular axis, the default time offset
will be used. In the example above, notice the usage of TDateTime to translate
an explicit date into the time in seconds required by TAxis::SetTimeFormat.
#### By setting a time offset to a particular axis:
~~~ .cpp
TDatime dh(2001,09,23,15,00,00);
h->GetXaxis()->SetTimeOffset(dh.Convert());
~~~
#### Together with the time format using TAxis::SetTimeFormat:
The time offset can be specified using the control character `%F` after
the normal time format. **%F** is followed by the date in the format:
`yyyy-mm-dd hh:mm:ss`.
Example:
~~~ .cpp
h->GetXaxis()->SetTimeFormat("%d\/%m\/%y%F2000-02-28 13:00:01");
~~~
Notice that this date format is the same used by the TDateString function
`AsSQLString`. If needed, this function can be used to translate a time in
seconds into a character string which can be appended after `%F`. If the time
format is not specified (before `%F), the automatic one will be used.
If a time axis has no specified time offset, the global time offset will be
stored in the axis data structure.
The following example illustrates the various possibilities.
Begin_Macro(source)
{
gStyle->SetTitleH(0.08);
TDatime da(2003,2,28,12,00,00);
gStyle->SetTimeOffset(da.Convert());
auto ct = new TCanvas("ct","Time on axis",0,0,600,600);
ct->Divide(1,3);
auto ht1 = new TH1F("ht1","ht1",30000,0.,200000.);
auto ht2 = new TH1F("ht2","ht2",30000,0.,200000.);
auto ht3 = new TH1F("ht3","ht3",30000,0.,200000.);
for (Int_t i=1;i<30000;i++) {
auto noise = gRandom->Gaus(0,120);
ht1->SetBinContent(i,noise);
ht2->SetBinContent(i,noise*noise);
ht3->SetBinContent(i,noise*noise*noise);
}
ct->cd(1);
ht1->GetXaxis()->SetLabelSize(0.06);
ht1->GetXaxis()->SetTimeDisplay(1);
ht1->GetXaxis()->SetTimeFormat("%d/%m/%y%F2000-02-28 13:00:01");
ht1->Draw();
ct->cd(2);
ht2->GetXaxis()->SetLabelSize(0.06);
ht2->GetXaxis()->SetTimeDisplay(1);
ht2->GetXaxis()->SetTimeFormat("%d/%m/%y");
ht2->Draw();
ct->cd(3);
ht3->GetXaxis()->SetLabelSize(0.06);
TDatime dh(2001,9,23,15,00,00);
ht3->GetXaxis()->SetTimeDisplay(1);
ht3->GetXaxis()->SetTimeOffset(dh.Convert());
ht3->Draw();
}
End_Macro
The histogram limits times in seconds. If `wmin` and `wmax` are the histogram
limits, the time axis will spread around the time offset value from `TimeOffset+wmin`
to `TimeOffset+wmax`. Until now all the examples had a lowest value equal to 0.
The following example demonstrates how to define the histogram limits relatively
to the time offset value.
Begin_Macro(source)
{
// Define the time offset as 2003, January 1st
TDatime T0(2003,1,1,0,0,0);
auto X0 = T0.Convert();
gStyle->SetTimeOffset(X0);
// Define the lowest histogram limit as 2002, September 23rd
TDatime T1(2002,9,23,0,0,0);
auto X1 = T1.Convert()-X0;
// Define the highest histogram limit as 2003, March 7th
TDatime T2(2003,3,7,0,0,0);
auto X2 = T2.Convert(1)-X0;
auto h1 = new TH1F("h1","test",100,X1,X2);
TRandom r;
for (Int_t i=0;i<30000;i++) {
Double_t noise = r.Gaus(0.5*(X1+X2),0.1*(X2-X1));
h1->Fill(noise);
}
h1->GetXaxis()->SetTimeDisplay(1);
h1->GetXaxis()->SetLabelSize(0.03);
h1->GetXaxis()->SetTimeFormat("%Y/%m/%d");
h1->Draw();
}
End_Macro
Usually time axis are created automatically via histograms, but one may also want
to draw a time axis outside an "histogram context". Therefore it is useful to
understand how TGaxis works for such axis.
The time offset can be defined using one of the three methods described before.
The time axis will spread around the time offset value. Actually it will go from
`TimeOffset+wmin` to `TimeOffset+wmax` where `wmin` and `wmax` are the minimum
and maximum values (in seconds) of the axis. Let's take again an example. Having
defined "2003, February 28 at 12h" we would like to see the axis a day before and
a day after. A TGaxis can be created the following way (a day has 86400 seconds):
~~~ .cpp
TGaxis *axis = new TGaxis(x1,y1,x2,y2,-100000,150000,2405,"t");
~~~
the `t` option (in lower case) means it is a "time axis". The axis goes form
100000 seconds before `TimeOffset` and 150000 seconds after.
So the complete macro is:
Begin_Macro(source)
{
c1 = new TCanvas("c1","Examples of TGaxis",10,10,700,100);
c1->Range(-10,-1,10,1);
TGaxis *axis = new TGaxis(-8,0.,8,0.,-100000,150000,2405,"tS");
axis->SetLabelSize(0.2);
axis->SetTickSize(0.2);
TDatime da(2003,02,28,12,00,00);
axis->SetTimeOffset(da.Convert());
axis->SetTimeFormat("%d-%m-%Y");
axis->Draw();
return c1;
}
End_Macro
Thanks to the TLatex directive `#splitline` it is possible to write the time
labels on two lines. In the previous example changing the `SetTimeFormat` line by
~~~ .cpp
axis->SetLabelOffset(0.15);
axis->SetTimeFormat("#splitline{%Y}{%d\/%m}");
~~~
will produce the following axis:
Begin_Macro
{
c1 = new TCanvas("c1","Examples of TGaxis",10,10,700,100);
c1->Range(-10,-1,10,1);
TGaxis *axis = new TGaxis(-8,0.,8,0.,-100000,150000,2405,"tS");
axis->SetLabelSize(0.2);
axis->SetTickSize(0.2);
TDatime da(2003,02,28,12,00,00);
axis->SetTimeOffset(da.Convert());
axis->SetLabelOffset(0.15);
axis->SetTimeFormat("#splitline{%Y}{%d/%m}");
axis->Draw();
return c1;
}
End_Macro
The following example shows time axis on a TGraph:
Begin_Macro(source)
{
TDatime da1(2008,02,28,15,52,00);
TDatime da2(2008,02,28,15,53,00);
double x[2],y[2];
y[0] = 1.;
y[1] = 2.;
x[0] = da1.Convert();
x[1] = da2.Convert();
TGraph mgr(2,x,y);
mgr.SetMarkerStyle(20);
mgr.Draw("apl");
mgr.GetXaxis()->SetTimeDisplay(1);
mgr.GetXaxis()->SetNdivisions(-503);
mgr.GetXaxis()->SetTimeFormat("%Y-%m-%d %H:%M");
mgr.GetXaxis()->SetTimeOffset(0,"gmt");
}
End_Macro
The following example compares what the system time function `gmtime`
and `localtime` give with what gives `TGaxis`. It can be used
as referenced test to check if the time option of `TGaxis` is working properly.
Begin_Macro(source)
../../../tutorials/graphs/timeonaxis3.C
End_Macro
The following macro illustrates the use, with histograms axis, of the time mode on the axis
with different time intervals and time formats.
Begin_Macro(source)
../../../tutorials/graphs/timeonaxis.C
End_Macro
*/
////////////////////////////////////////////////////////////////////////////////
/// TGaxis default constructor.
TGaxis::TGaxis(): TLine(), TAttText(11,0,1,62,0.040)
{
fGridLength = 0.;
fLabelOffset = 0.005;
fLabelSize = 0.040;
fLabelFont = 62;
fLabelColor = 1;
fTickSize = 0.030;
fTitleOffset = 1;
fTitleSize = fLabelSize;
fChopt = "";
fName = "";
fTitle = "";
fTimeFormat = "";
fFunctionName= "";
fFunction = nullptr;
fAxis = nullptr;
fNdiv = 0;
fNModLabs = 0;
fModLabs = nullptr;
fWmin = 0.;
fWmax = 0.;
}
////////////////////////////////////////////////////////////////////////////////
/// TGaxis normal constructor.
TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
Double_t wmin, Double_t wmax, Int_t ndiv, Option_t *chopt,
Double_t gridlength)
: TLine(xmin,ymin,xmax,ymax), TAttText(11,0,1,62,0.040)
{
fWmin = wmin;
fWmax = wmax;
fNdiv = ndiv;
fNModLabs = 0;
fModLabs = nullptr;
fGridLength = gridlength;
fLabelOffset = 0.005;
fLabelSize = 0.040;
fLabelFont = 62;
fLabelColor = 1;
fTickSize = 0.030;
fTitleOffset = 1;
fTitleSize = fLabelSize;
fChopt = chopt;
fName = "";
fTitle = "";
fTimeFormat = "";
fFunctionName= "";
fFunction = nullptr;
fAxis = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
/// Constructor with a `TF1` to map axis values.
///
/// \note The function `func` (with name `funcname`) is not defined in the user's
/// coordinate space, but in the new TGaxis space. If `x` is the original axis,
/// `w` the new axis, and `w = f(x)` (for example, `f` is a calibration function
/// converting ADC channels `x` to energy `w`), then `func` must be supplied as
/// `f^{-1}(w)`.
TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
const char *funcname, Int_t ndiv, Option_t *chopt,
Double_t gridlength)
: TLine(xmin,ymin,xmax,ymax), TAttText(11,0,1,62,0.040)
{
fFunction = (TF1*)gROOT->GetFunction(funcname);
if (!fFunction) {
Error("TGaxis", "calling constructor with an unknown function: %s", funcname);
fWmin = 0;
fWmax = 1;
} else {
fWmin = fFunction->GetXmin();
fWmax = fFunction->GetXmax();
}
fFunctionName= funcname;
fNdiv = ndiv;
fNModLabs = 0;
fModLabs = nullptr;
fGridLength = gridlength;
fLabelOffset = 0.005;
fLabelSize = 0.040;
fLabelFont = 62;
fLabelColor = 1;
fTickSize = 0.030;
fTitleOffset = 1;
fTitleSize = fLabelSize;
fChopt = chopt;
fName = "";
fTitle = "";
fTimeFormat = "";
fAxis = nullptr;
}
////////////////////////////////////////////////////////////////////////////////
/// Copy constructor.
TGaxis::TGaxis(const TGaxis& ax) :
TLine(ax),
TAttText(ax),
fWmin(ax.fWmin),
fWmax(ax.fWmax),
fGridLength(ax.fGridLength),
fTickSize(ax.fTickSize),
fLabelOffset(ax.fLabelOffset),
fLabelSize(ax.fLabelSize),
fTitleOffset(ax.fTitleOffset),
fTitleSize(ax.fTitleSize),
fNdiv(ax.fNdiv),
fLabelColor(ax.fLabelColor),
fLabelFont(ax.fLabelFont),
fNModLabs(ax.fNModLabs),
fChopt(ax.fChopt),
fName(ax.fName),
fTitle(ax.fTitle),
fTimeFormat(ax.fTimeFormat),
fFunctionName(ax.fFunctionName),
fFunction(ax.fFunction),
fAxis(ax.fAxis)
{
if (ax.IsOwnedModLabs())
fModLabs = (TList *) ax.fModLabs->Clone();
else
fModLabs = ax.fModLabs;
}
////////////////////////////////////////////////////////////////////////////////
/// Assignment operator.
TGaxis& TGaxis::operator=(const TGaxis& ax)
{
if(this!=&ax) {
TLine::operator=(ax);
TAttText::operator=(ax);
fWmin=ax.fWmin;
fWmax=ax.fWmax;
fGridLength=ax.fGridLength;
fTickSize=ax.fTickSize;
fLabelOffset=ax.fLabelOffset;
fLabelSize=ax.fLabelSize;
fTitleOffset=ax.fTitleOffset;
fTitleSize=ax.fTitleSize;
fNdiv=ax.fNdiv;
fLabelColor=ax.fLabelColor;
fLabelFont=ax.fLabelFont;
fChopt=ax.fChopt;
fName=ax.fName;
fTitle=ax.fTitle;
fTimeFormat=ax.fTimeFormat;
fFunctionName=ax.fFunctionName;
fFunction=ax.fFunction;
fAxis=ax.fAxis;
fNModLabs=ax.fNModLabs;
fModLabs = ax.IsOwnedModLabs() ? (TList *) ax.fModLabs->Clone() : ax.fModLabs;
}
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// TGaxis default destructor.
TGaxis::~TGaxis()
{
CleanupModLabs();
}
////////////////////////////////////////////////////////////////////////////////
/// Returns kTRUE when fModLabs owned by TGaxis and should be cleaned up
Bool_t TGaxis::IsOwnedModLabs() const
{
if (!fModLabs) return kFALSE;
if (fAxis && (fAxis->GetModifiedLabels() == fModLabs)) return kFALSE;
// TList created by TGaxis configured with owner flag
// If TGaxis object from old ROOT file will be read, memory will be leaked
return fModLabs->IsOwner();
}
////////////////////////////////////////////////////////////////////////////////
/// Correctly cleanup fModLabs - delete content when owned by TGaxis
void TGaxis::CleanupModLabs()
{
if (IsOwnedModLabs()) {
fModLabs->Delete();
delete fModLabs;
}
fModLabs = nullptr;
fNModLabs = 0;
}
////////////////////////////////////////////////////////////////////////////////
/// If center = kTRUE axis labels are centered in the center of the bin.
/// The default is to center on the primary tick marks.
/// This option does not make sense if there are more bins than tick marks.
void TGaxis::CenterLabels(Bool_t center)
{
if (center) SetBit(TAxis::kCenterLabels);
else ResetBit(TAxis::kCenterLabels);
}
////////////////////////////////////////////////////////////////////////////////
/// If center = kTRUE axis title will be centered. The default is right adjusted.
void TGaxis::CenterTitle(Bool_t center)
{
if (center) SetBit(TAxis::kCenterTitle);
else ResetBit(TAxis::kCenterTitle);
}
////////////////////////////////////////////////////////////////////////////////
/// Draw this axis with new attributes.
TGaxis *TGaxis::DrawAxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
Double_t wmin, Double_t wmax, Int_t ndiv, Option_t *chopt,
Double_t gridlength)
{
TGaxis *newaxis = new TGaxis(xmin,ymin,xmax,ymax,wmin,wmax,ndiv,chopt,gridlength);
newaxis->SetLineColor(fLineColor);
newaxis->SetLineWidth(fLineWidth);
newaxis->SetLineStyle(fLineStyle);
newaxis->SetTextAlign(fTextAlign);
newaxis->SetTextAngle(fTextAngle);
newaxis->SetTextColor(fTextColor);
newaxis->SetTextFont(fTextFont);
newaxis->SetTextSize(fTextSize);
newaxis->SetTitleSize(fTitleSize);
newaxis->SetTitleOffset(fTitleOffset);
newaxis->SetLabelFont(fLabelFont);
newaxis->SetLabelColor(fLabelColor);
newaxis->SetLabelSize(fLabelSize);
newaxis->SetLabelOffset(fLabelOffset);
newaxis->SetTickSize(fTickSize);
newaxis->SetBit(kCanDelete);
newaxis->SetTitle(GetTitle());
newaxis->SetBit(TAxis::kCenterTitle,TestBit(TAxis::kCenterTitle));
newaxis->AppendPad();
return newaxis;
}
////////////////////////////////////////////////////////////////////////////////
/// Static function returning fgMaxDigits (See SetMaxDigits).
Int_t TGaxis::GetMaxDigits()
{
return fgMaxDigits;
}
////////////////////////////////////////////////////////////////////////////////
/// Internal method to import TAxis attributes to this TGaxis.
void TGaxis::ImportAxisAttributes(TAxis *axis)
{
// cleanup previously configured custom labels
CleanupModLabs();
fAxis = axis;
SetLineColor(axis->GetAxisColor());
SetTextColor(axis->GetTitleColor());
SetTextFont(axis->GetTitleFont());
SetLabelColor(axis->GetLabelColor());
SetLabelFont(axis->GetLabelFont());
SetLabelSize(axis->GetLabelSize());
SetLabelOffset(axis->GetLabelOffset());
SetTickSize(axis->GetTickLength());
SetTitle(axis->GetTitle());
SetTitleOffset(axis->GetTitleOffset());
SetTitleSize(axis->GetTitleSize());
SetBit(TAxis::kCenterTitle, axis->TestBit(TAxis::kCenterTitle));
SetBit(TAxis::kCenterLabels, axis->TestBit(TAxis::kCenterLabels));
SetBit(TAxis::kRotateTitle, axis->TestBit(TAxis::kRotateTitle));
SetBit(TAxis::kNoExponent, axis->TestBit(TAxis::kNoExponent));
SetBit(TAxis::kTickPlus, axis->TestBit(TAxis::kTickPlus));
SetBit(TAxis::kTickMinus, axis->TestBit(TAxis::kTickMinus));
SetBit(TAxis::kMoreLogLabels, axis->TestBit(TAxis::kMoreLogLabels));
if (axis->GetDecimals()) SetBit(TAxis::kDecimals); //the bit is in TAxis::fAxis2
SetTimeFormat(axis->GetTimeFormat());
}
////////////////////////////////////////////////////////////////////////////////
/// Draw this axis with its current attributes.
void TGaxis::Paint(Option_t *)
{