Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
18 Int_t year, month, day;
19 TTree *tree;
20 TProfile *hYear = 0, *hMonth = 0, *hDay = 0;
21 TCanvas *Canvas;
22 Int_t customhb = -2;
23 TLatex *info = 0;
24 
25 // Ranges for year, month, day and temperature
26 Int_t rYear[3]; // from tree/data
27 Int_t rMonth[3] = { 12, 1, 13 };
28 Int_t rDay[3] = { 31, 1, 32 };
29 Double_t rTemp[3] = { 55.0, -20.0, 35.0 };
30 
31 void HighlightTemp(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
32 void HighlightYear(Int_t xhb);
33 void HighlightMonth(Int_t xhb);
34 void HighlightDay(Int_t xhb);
35 
36 
37 void temperature()
38 {
39  // Read file (data from Global Historical Climatology Network)
40  tree = new TTree("tree", "GHCN-Daily");
41  // data format: YEAR/I:MONTH/I:DAY/I:T/F
42 
43  // Read file $ROOTSYS/tutorials/tree/temperature_Prague.dat
44  auto dir = gROOT->GetTutorialDir();
45  dir.Append("/tree/");
46  dir.ReplaceAll("/./","/");
47  if (tree->ReadFile(Form("%stemperature_Prague.dat",dir.Data())) == 0) return;
48 
49  // Compute range of years
50  tree->GetEntry(0);
51  rYear[1] = (Int_t)tree->GetLeaf("YEAR")->GetValue(); // first year
52  tree->GetEntry(tree->GetEntries() - 1);
53  rYear[2] = (Int_t)tree->GetLeaf("YEAR")->GetValue(); // last year
54  rYear[2] = rYear[2] + 1;
55  rYear[0] = rYear[2] - rYear[1];
56 
57  // Create a TProfile for the average temperature by years
58  hYear = new TProfile("hYear", "temperature (average) by year; year; temp, #circC", rYear[0], rYear[1], rYear[2]);
59  tree->Draw("T:YEAR>>hYear", "", "goff");
60  hYear->SetMaximum(hYear->GetMean(2)*1.50);
61  hYear->SetMinimum(hYear->GetMean(2)*0.50);
62  hYear->GetXaxis()->SetNdivisions(410);
63  hYear->GetYaxis()->SetNdivisions(309);
64  hYear->SetLineColor(kGray+2);
65  hYear->SetMarkerStyle(8);
66  hYear->SetMarkerSize(0.75);
67 
68  // Draw the average temperature by years
69  gStyle->SetOptStat("em");
70  Canvas = new TCanvas("Canvas", "Canvas", 0, 0, 700, 900);
71  Canvas->Divide(1, 3, 0.001, 0.001);
72  Canvas->cd(1);
73  hYear->Draw("HIST, LP");
74  gPad->Update();
75 
76  // Connect the highlight procedure to the temperature profile
77  hYear->SetHighlight();
78  Canvas->HighlightConnect("HighlightTemp(TVirtualPad*,TObject*,Int_t,Int_t)");
79 }
80 
81 
82 void HighlightTemp(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
83 {
84  if (obj == hYear) HighlightYear(xhb);
85  if (obj == hMonth) HighlightMonth(xhb);
86  if (obj == hDay) HighlightDay(xhb);
87  Canvas->Update();
88 }
89 
90 
91 void HighlightYear(Int_t xhb)
92 {
93  if (!hMonth) {
94  hMonth = new TProfile("hMonth", "; month; temp, #circC", rMonth[0], rMonth[1], rMonth[2]);
95  hMonth->SetMinimum(rTemp[1]);
96  hMonth->SetMaximum(rTemp[2]);
97  hMonth->GetXaxis()->SetNdivisions(112);
98  hMonth->GetXaxis()->CenterLabels();
99  hMonth->GetYaxis()->SetNdivisions(410);
100  hMonth->SetFillColor(kGray+1);
101  hMonth->SetMarkerStyle(kFullDotMedium);
102  Canvas->cd(2)->SetGridx();
103  hMonth->Draw("HIST, CP");
104  gPad->Update();
105  hMonth->SetHighlight();
106  }
107 
108  year = xhb - 1 + rYear[1];
109  tree->Draw("T:MONTH>>hMonth", TString::Format("YEAR==%d", year), "goff");
110  hMonth->SetTitle(TString::Format("temperature by month (year = %d)", year));
111  Canvas->GetPad(2)->Modified();
112 
113  HighlightMonth(customhb); // custom call HighlightMonth
114 }
115 
116 
117 void HighlightMonth(Int_t xhb)
118 {
119  if (!hDay) {
120  hDay = new TProfile("hDay", "; day; temp, #circC", rDay[0], rDay[1], rDay[2]);
121  hDay->SetMinimum(rTemp[1]);
122  hDay->SetMaximum(rTemp[2]);
123  hDay->GetYaxis()->SetNdivisions(410);
124  hDay->SetFillColor(kGray);
125  hDay->SetMarkerStyle(kFullDotMedium);
126  Canvas->cd(3);
127  hDay->Draw("HIST, CP");
128  gPad->Update();
129  hDay->SetHighlight();
130  }
131 
132  if (xhb != customhb) month = xhb;
133  tree->Draw("T:DAY>>hDay", TString::Format("MONTH==%d && YEAR==%d", month, year), "goff");
134  hDay->SetTitle(TString::Format("temperature by day (month = %02d, year = %d)", month, year));
135  Canvas->GetPad(3)->Modified();
136 
137  HighlightDay(customhb); // custom call HighlightDay
138 }
139 
140 
141 void HighlightDay(Int_t xhb)
142 {
143  if (!info) {
144  info = new TLatex();
145  info->SetTextSizePixels(25);
146  Canvas->cd(3);
147  info->Draw();
148  gPad->Update();
149  }
150 
151  if (xhb != customhb) day = xhb;
152  TString temp = TString::Format(" %5.1f #circC", hDay->GetBinContent(day));
153  if (hDay->GetBinEntries(day) == 0) temp = " ";
154  TString m = " ";
155  if (month>0) m = TString::Format("-%02d",month);
156  TString d = " ";
157  if (day>0) d = TString::Format("-%02d",day);
158  info->SetText(2.0, hDay->GetMinimum()*0.8, TString::Format("%4d%s%s%s", year, m.Data(), d.Data(), temp.Data()));
159  Canvas->GetPad(3)->Modified();
160 }