Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tree200_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
19tree = 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
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)
42 day = xhb;
43 TString temp = TString::Format(" %5.1f #circC", hDay->GetBinContent(day));
44 if (hDay->GetBinEntries(day) == 0)
45 temp = " ";
46 TString m = " ";
47 if (month > 0)
48 m = TString::Format("-%02d", month);
49 TString d = " ";
50 if (day > 0)
51 d = TString::Format("-%02d", day);
52 info->SetText(2.0, hDay->GetMinimum() * 0.8, TString::Format("%4d%s%s%s", year, m.Data(), d.Data(), temp.Data()));
53 Canvas->GetPad(3)->Modified();
54}
55
57{
58 if (!hDay) {
59 hDay = new TProfile("hDay", "; day; temp, #circC", rDay[0], rDay[1], rDay[2]);
60 hDay->SetMinimum(rTemp[1]);
61 hDay->SetMaximum(rTemp[2]);
62 hDay->GetYaxis()->SetNdivisions(410);
63 hDay->SetFillColor(kGray);
64 hDay->SetMarkerStyle(kFullDotMedium);
65 Canvas->cd(3);
66 hDay->Draw("HIST, CP");
67 gPad->Update();
68 hDay->SetHighlight();
69 }
70
71 if (xhb != customhb)
72 month = xhb;
73 year), "goff");
74 hDay->SetTitle(TString::Format("temperature by day (month = %02d, year = %d)", month, year));
75 Canvas->GetPad(3)->Modified();
76
77 HighlightDay(customhb); // custom call HighlightDay
78}
79
81{
82 if (!hMonth) {
83 hMonth = new TProfile("hMonth", "; month; temp, #circC", rMonth[0], rMonth[1], rMonth[2]);
84 hMonth->SetMinimum(rTemp[1]);
85 hMonth->SetMaximum(rTemp[2]);
86 hMonth->GetXaxis()->SetNdivisions(112);
87 hMonth->GetXaxis()->CenterLabels();
88 hMonth->GetYaxis()->SetNdivisions(410);
89 hMonth->SetFillColor(kGray+1);
90 hMonth->SetMarkerStyle(kFullDotMedium);
91 Canvas->cd(2)->SetGridx();
92 hMonth->Draw("HIST, CP");
93 gPad->Update();
94 hMonth->SetHighlight();
95 }
96
97 year = xhb - 1 + rYear[1];
98 year), "goff");
99 hMonth->SetTitle(TString::Format("temperature by month (year = %d)", year));
100 Canvas->GetPad(2)->Modified();
101
102 HighlightMonth(customhb); // custom call HighlightMonth
103}
104
106{
107 if (obj == hYear)
109 if (obj == hMonth)
111 if (obj == hDay)
113 Canvas->Update();
114}
115
117{
118 // Read file (data from Global Historical Climatology Network)
119 TTree("tree", "GHCN-Daily");
120 // data format: YEAR/I:MONTH/I:DAY/I:T/F
121
122 // Read file $ROOTSYS/tutorials/io/tree/temperature_Prague.dat
123 auto dir = gROOT->GetTutorialDir();
124 dir.Append("/io/tree/");
125 dir.ReplaceAll("/./","/");
126 if (Form("%stemperature_Prague.dat", dir.Data())) == 0)
127 return;
128
129 // Compute range of years
130 tree->GetEntry(0);
131 tree->GetLeaf("YEAR")->GetValue(); // first year
132 tree->GetEntries() - 1);
133 tree->GetLeaf("YEAR")->GetValue(); // last year
134 rYear[2] = rYear[2] + 1;
135 rYear[0] = rYear[2] - rYear[1];
136
137 // Create a TProfile for the average temperature by years
138 hYear = new TProfile("hYear", "temperature (average) by year; year; temp, #circC", rYear[0], rYear[1], rYear[2]);
139 tree->Draw("T:YEAR>>hYear", "", "goff");
140 hYear->SetMaximum(hYear->GetMean(2)*1.50);
141 hYear->SetMinimum(hYear->GetMean(2)*0.50);
142 hYear->GetXaxis()->SetNdivisions(410);
143 hYear->GetYaxis()->SetNdivisions(309);
144 hYear->SetLineColor(kGray+2);
145 hYear->SetMarkerStyle(8);
146 hYear->SetMarkerSize(0.75);
147
148 // Draw the average temperature by years
149 gStyle->SetOptStat("em");
150 Canvas = new TCanvas("Canvas", "Canvas", 0, 0, 700, 900);
151 Canvas->HighlightConnect("HighlightTemp(TVirtualPad*,TObject*,Int_t,Int_t)");
152 Canvas->Divide(1, 3, 0.001, 0.001);
153 Canvas->cd(1);
154 hYear->Draw("HIST, LP");
155 gPad->Update();
156
157 // Connect the highlight procedure to the temperature profile
158 hYear->SetHighlight();
159}
#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:57
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gROOT
Definition TROOT.h:414
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
#define gPad
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:1631
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition TCanvas.cxx:719
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2488
To draw Mathematical Formula.
Definition TLatex.h:18
Mother of all ROOT objects.
Definition TObject.h:41
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:1260
TVirtualPad * GetPad(Int_t subpadnumber) const override
Get a pointer to subpadnumber of this pad.
Definition TPad.cxx:3016
Profile Histogram.
Definition TProfile.h:32
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
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:2378
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:1642
A TTree represents a columnar dataset.
Definition TTree.h:84
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual void Modified(Bool_t flag=1)=0
virtual void SetGridx(Int_t value=1)=0
TMarker m
Definition textangle.C:8