Logo ROOT  
Reference Guide
temperature.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_tree
3///
4/// This tutorial illustrates how to use the highlight mode with trees.
5/// It first creates a TTree from a temperature data set in Prague between 1775
6/// and 2004. Then it defines three pads representing the temperature per year,
7/// month and day. Thanks to the highlight mechanism it is possible to explore the
8/// data set only by moving the mouse on the plots. Movements on the years' plot
9/// will update the months' and days' plot. Movements on the months plot will update
10/// the days plot. Movements on the days' plot will display the exact temperature
11/// for a given day.
12///
13/// \macro_code
14///
15/// \date March 2018
16/// \author Jan Musinsky
17
18Int_t year, month, day;
19TTree *tree = nullptr;
20TProfile *hYear = nullptr, *hMonth = nullptr, *hDay = nullptr;
21TCanvas *Canvas = nullptr;
22Int_t customhb = -2;
23TLatex *info = nullptr;
24
25// Ranges for year, month, day and temperature
26Int_t rYear[3]; // from tree/data
27Int_t rMonth[3] = { 12, 1, 13 };
28Int_t rDay[3] = { 31, 1, 32 };
29Double_t rTemp[3] = { 55.0, -20.0, 35.0 };
30
31void HighlightDay(Int_t xhb)
32{
33 if (!info) {
34 info = new TLatex();
35 info->SetTextSizePixels(25);
36 Canvas->cd(3);
37 info->Draw();
38 gPad->Update();
39 }
40
41 if (xhb != customhb) day = xhb;
42 TString temp = TString::Format(" %5.1f #circC", hDay->GetBinContent(day));
43 if (hDay->GetBinEntries(day) == 0) temp = " ";
44 TString m = " ";
45 if (month>0) m = TString::Format("-%02d",month);
46 TString d = " ";
47 if (day>0) d = TString::Format("-%02d",day);
48 info->SetText(2.0, hDay->GetMinimum()*0.8, TString::Format("%4d%s%s%s", year, m.Data(), d.Data(), temp.Data()));
49 Canvas->GetPad(3)->Modified();
50}
51
52void HighlightMonth(Int_t xhb)
53{
54 if (!hDay) {
55 hDay = new TProfile("hDay", "; day; temp, #circC", rDay[0], rDay[1], rDay[2]);
56 hDay->SetMinimum(rTemp[1]);
57 hDay->SetMaximum(rTemp[2]);
58 hDay->GetYaxis()->SetNdivisions(410);
59 hDay->SetFillColor(kGray);
60 hDay->SetMarkerStyle(kFullDotMedium);
61 Canvas->cd(3);
62 hDay->Draw("HIST, CP");
63 gPad->Update();
64 hDay->SetHighlight();
65 }
66
67 if (xhb != customhb) month = xhb;
68 tree->Draw("T:DAY>>hDay", TString::Format("MONTH==%d && YEAR==%d", month, year), "goff");
69 hDay->SetTitle(TString::Format("temperature by day (month = %02d, year = %d)", month, year));
70 Canvas->GetPad(3)->Modified();
71
72 HighlightDay(customhb); // custom call HighlightDay
73}
74
75void HighlightYear(Int_t xhb)
76{
77 if (!hMonth) {
78 hMonth = new TProfile("hMonth", "; month; temp, #circC", rMonth[0], rMonth[1], rMonth[2]);
79 hMonth->SetMinimum(rTemp[1]);
80 hMonth->SetMaximum(rTemp[2]);
81 hMonth->GetXaxis()->SetNdivisions(112);
82 hMonth->GetXaxis()->CenterLabels();
83 hMonth->GetYaxis()->SetNdivisions(410);
84 hMonth->SetFillColor(kGray+1);
85 hMonth->SetMarkerStyle(kFullDotMedium);
86 Canvas->cd(2)->SetGridx();
87 hMonth->Draw("HIST, CP");
88 gPad->Update();
89 hMonth->SetHighlight();
90 }
91
92 year = xhb - 1 + rYear[1];
93 tree->Draw("T:MONTH>>hMonth", TString::Format("YEAR==%d", year), "goff");
94 hMonth->SetTitle(TString::Format("temperature by month (year = %d)", year));
95 Canvas->GetPad(2)->Modified();
96
97 HighlightMonth(customhb); // custom call HighlightMonth
98}
99
100void HighlightTemp(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
101{
102 if (obj == hYear) HighlightYear(xhb);
103 if (obj == hMonth) HighlightMonth(xhb);
104 if (obj == hDay) HighlightDay(xhb);
105 Canvas->Update();
106}
107
108void temperature()
109{
110 // Read file (data from Global Historical Climatology Network)
111 tree = new TTree("tree", "GHCN-Daily");
112 // data format: YEAR/I:MONTH/I:DAY/I:T/F
113
114 // Read file $ROOTSYS/tutorials/tree/temperature_Prague.dat
115 auto dir = gROOT->GetTutorialDir();
116 dir.Append("/tree/");
117 dir.ReplaceAll("/./","/");
118 if (tree->ReadFile(Form("%stemperature_Prague.dat",dir.Data())) == 0) return;
119
120 // Compute range of years
121 tree->GetEntry(0);
122 rYear[1] = (Int_t)tree->GetLeaf("YEAR")->GetValue(); // first year
123 tree->GetEntry(tree->GetEntries() - 1);
124 rYear[2] = (Int_t)tree->GetLeaf("YEAR")->GetValue(); // last year
125 rYear[2] = rYear[2] + 1;
126 rYear[0] = rYear[2] - rYear[1];
127
128 // Create a TProfile for the average temperature by years
129 hYear = new TProfile("hYear", "temperature (average) by year; year; temp, #circC", rYear[0], rYear[1], rYear[2]);
130 tree->Draw("T:YEAR>>hYear", "", "goff");
131 hYear->SetMaximum(hYear->GetMean(2)*1.50);
132 hYear->SetMinimum(hYear->GetMean(2)*0.50);
133 hYear->GetXaxis()->SetNdivisions(410);
134 hYear->GetYaxis()->SetNdivisions(309);
135 hYear->SetLineColor(kGray+2);
136 hYear->SetMarkerStyle(8);
137 hYear->SetMarkerSize(0.75);
138
139 // Draw the average temperature by years
140 gStyle->SetOptStat("em");
141 Canvas = new TCanvas("Canvas", "Canvas", 0, 0, 700, 900);
142 Canvas->HighlightConnect("HighlightTemp(TVirtualPad*,TObject*,Int_t,Int_t)");
143 Canvas->Divide(1, 3, 0.001, 0.001);
144 Canvas->cd(1);
145 hYear->Draw("HIST, LP");
146 gPad->Update();
147
148 // Connect the highlight procedure to the temperature profile
149 hYear->SetHighlight();
150}
#define d(i)
Definition: RSha256.hxx:102
int Int_t
Definition: RtypesCore.h:45
double Double_t
Definition: RtypesCore.h:59
@ kGray
Definition: Rtypes.h:65
@ kFullDotMedium
Definition: TAttMarker.h:50
#define gROOT
Definition: TROOT.h:404
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2452
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
#define gPad
Definition: TVirtualPad.h:288
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition: TAttAxis.cxx:236
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
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:41
virtual void SetTextSizePixels(Int_t npixels)
Set the text size in pixel.
Definition: TAttText.cxx:424
The Canvas class.
Definition: TCanvas.h:23
virtual void HighlightConnect(const char *slot)
This is "simplification" for function TCanvas::Connect with Highlighted signal for specific slot.
Definition: TCanvas.cxx:1629
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition: TCanvas.cxx:706
void Update() override
Update canvas pad buffers.
Definition: TCanvas.cxx:2492
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Definition: TH1.cxx:7446
TAxis * GetXaxis()
Definition: TH1.h:319
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:397
TAxis * GetYaxis()
Definition: TH1.h:320
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3060
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:398
virtual void SetHighlight(Bool_t set=kTRUE)
Set highlight (enable/disable) mode for the histogram by default highlight mode is disable.
Definition: TH1.cxx:4455
To draw Mathematical Formula.
Definition: TLatex.h:18
Mother of all ROOT objects.
Definition: TObject.h:37
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
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:1152
TVirtualPad * GetPad(Int_t subpadnumber) const override
Get a pointer to subpadnumber of this pad.
Definition: TPad.cxx:2870
Profile Histogram.
Definition: TProfile.h:32
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2341
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1589
virtual void SetText(Double_t x, Double_t y, const char *text)
Definition: TText.h:74
A TTree represents a columnar dataset.
Definition: TTree.h:79
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:51
virtual void Modified(Bool_t flag=1)=0
void Draw(Option_t *option="") override=0
Default Draw method for all objects.
virtual void SetGridx(Int_t value=1)=0
Definition: tree.py:1
TMarker m
Definition: textangle.C:8