Logo ROOT  
Reference Guide
temperature.C File Reference

Detailed Description

This tutorial illustrates how to use the highlight mode with trees.

It first creates a TTree from a temperature data set in Prague between 1775 and 2004. Then it defines three pads representing the temperature per year, month and day. Thanks to the highlight mechanism it is possible to explore the data set only by moving the mouse on the plots. Movements on the years' plot will update the months' and days' plot. Movements on the months plot will update the days plot. Movements on the days' plot will display the exact temperature for a given day.

Int_t year, month, day;
TProfile *hYear = 0, *hMonth = 0, *hDay = 0;
TCanvas *Canvas;
Int_t customhb = -2;
TLatex *info = 0;
// Ranges for year, month, day and temperature
Int_t rYear[3]; // from tree/data
Int_t rMonth[3] = { 12, 1, 13 };
Int_t rDay[3] = { 31, 1, 32 };
Double_t rTemp[3] = { 55.0, -20.0, 35.0 };
void HighlightTemp(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
void HighlightYear(Int_t xhb);
void HighlightMonth(Int_t xhb);
void HighlightDay(Int_t xhb);
void temperature()
{
// Read file (data from Global Historical Climatology Network)
tree = new TTree("tree", "GHCN-Daily");
// data format: YEAR/I:MONTH/I:DAY/I:T/F
// Read file $ROOTSYS/tutorials/tree/temperature_Prague.dat
auto dir = gROOT->GetTutorialDir();
dir.Append("/tree/");
dir.ReplaceAll("/./","/");
if (tree->ReadFile(Form("%stemperature_Prague.dat",dir.Data())) == 0) return;
// Compute range of years
tree->GetEntry(0);
rYear[1] = (Int_t)tree->GetLeaf("YEAR")->GetValue(); // first year
tree->GetEntry(tree->GetEntries() - 1);
rYear[2] = (Int_t)tree->GetLeaf("YEAR")->GetValue(); // last year
rYear[2] = rYear[2] + 1;
rYear[0] = rYear[2] - rYear[1];
// Create a TProfile for the average temperature by years
hYear = new TProfile("hYear", "temperature (average) by year; year; temp, #circC", rYear[0], rYear[1], rYear[2]);
tree->Draw("T:YEAR>>hYear", "", "goff");
hYear->SetMaximum(hYear->GetMean(2)*1.50);
hYear->SetMinimum(hYear->GetMean(2)*0.50);
hYear->GetXaxis()->SetNdivisions(410);
hYear->GetYaxis()->SetNdivisions(309);
hYear->SetLineColor(kGray+2);
hYear->SetMarkerStyle(8);
hYear->SetMarkerSize(0.75);
// Draw the average temperature by years
Canvas = new TCanvas("Canvas", "Canvas", 0, 0, 700, 900);
Canvas->Divide(1, 3, 0.001, 0.001);
Canvas->cd(1);
hYear->Draw("HIST, LP");
gPad->Update();
// Connect the highlight procedure to the temperature profile
hYear->SetHighlight();
Canvas->HighlightConnect("HighlightTemp(TVirtualPad*,TObject*,Int_t,Int_t)");
}
void HighlightTemp(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
{
if (obj == hYear) HighlightYear(xhb);
if (obj == hMonth) HighlightMonth(xhb);
if (obj == hDay) HighlightDay(xhb);
Canvas->Update();
}
void HighlightYear(Int_t xhb)
{
if (!hMonth) {
hMonth = new TProfile("hMonth", "; month; temp, #circC", rMonth[0], rMonth[1], rMonth[2]);
hMonth->SetMinimum(rTemp[1]);
hMonth->SetMaximum(rTemp[2]);
hMonth->GetXaxis()->SetNdivisions(112);
hMonth->GetXaxis()->CenterLabels();
hMonth->GetYaxis()->SetNdivisions(410);
hMonth->SetFillColor(kGray+1);
hMonth->SetMarkerStyle(kFullDotMedium);
Canvas->cd(2)->SetGridx();
hMonth->Draw("HIST, CP");
gPad->Update();
hMonth->SetHighlight();
}
year = xhb - 1 + rYear[1];
tree->Draw("T:MONTH>>hMonth", TString::Format("YEAR==%d", year), "goff");
hMonth->SetTitle(TString::Format("temperature by month (year = %d)", year));
Canvas->GetPad(2)->Modified();
HighlightMonth(customhb); // custom call HighlightMonth
}
void HighlightMonth(Int_t xhb)
{
if (!hDay) {
hDay = new TProfile("hDay", "; day; temp, #circC", rDay[0], rDay[1], rDay[2]);
hDay->SetMinimum(rTemp[1]);
hDay->SetMaximum(rTemp[2]);
hDay->GetYaxis()->SetNdivisions(410);
hDay->SetFillColor(kGray);
hDay->SetMarkerStyle(kFullDotMedium);
Canvas->cd(3);
hDay->Draw("HIST, CP");
gPad->Update();
hDay->SetHighlight();
}
if (xhb != customhb) month = xhb;
tree->Draw("T:DAY>>hDay", TString::Format("MONTH==%d && YEAR==%d", month, year), "goff");
hDay->SetTitle(TString::Format("temperature by day (month = %02d, year = %d)", month, year));
Canvas->GetPad(3)->Modified();
HighlightDay(customhb); // custom call HighlightDay
}
void HighlightDay(Int_t xhb)
{
if (!info) {
info = new TLatex();
info->SetTextSizePixels(25);
Canvas->cd(3);
info->Draw();
gPad->Update();
}
if (xhb != customhb) day = xhb;
TString temp = TString::Format(" %5.1f #circC", hDay->GetBinContent(day));
if (hDay->GetBinEntries(day) == 0) temp = " ";
TString m = " ";
if (month>0) m = TString::Format("-%02d",month);
TString d = " ";
if (day>0) d = TString::Format("-%02d",day);
info->SetText(2.0, hDay->GetMinimum()*0.8, TString::Format("%4d%s%s%s", year, m.Data(), d.Data(), temp.Data()));
Canvas->GetPad(3)->Modified();
}
Date
March 2018
Author
Jan Musinsky

Definition in file temperature.C.

TAttMarker::SetMarkerSize
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition: TAttMarker.h:41
m
auto * m
Definition: textangle.C:8
TH1::SetMinimum
virtual void SetMinimum(Double_t minimum=-1111)
Definition: TH1.h:397
TString::Data
const char * Data() const
Definition: TString.h:369
TVirtualPad::Draw
virtual void Draw(Option_t *option="")=0
Default Draw method for all objects.
tree
Definition: tree.py:1
Form
char * Form(const char *fmt,...)
TTree
A TTree represents a columnar dataset.
Definition: TTree.h:79
Int_t
int Int_t
Definition: RtypesCore.h:45
TVirtualPad::SetGridx
virtual void SetGridx(Int_t value=1)=0
TLatex
To draw Mathematical Formula.
Definition: TLatex.h:18
TPad::Divide
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:1175
TAttLine::SetLineColor
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
TString::Format
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:2311
TString
Basic string class.
Definition: TString.h:136
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TText::SetText
virtual void SetText(Double_t x, Double_t y, const char *text)
Definition: TText.h:74
TH1::GetMean
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:7305
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TH1::GetYaxis
TAxis * GetYaxis()
Definition: TH1.h:319
TCanvas::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:708
TPad::GetPad
virtual TVirtualPad * GetPad(Int_t subpadnumber) const
Get a pointer to subpadnumber of this pad.
Definition: TPad.cxx:2900
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
TH1::SetHighlight
virtual void SetHighlight(Bool_t set=kTRUE)
Set highlight (enable/disable) mode for the histogram by default highlight mode is disable.
Definition: TH1.cxx:4399
TH1::SetMaximum
virtual void SetMaximum(Double_t maximum=-1111)
Definition: TH1.h:396
TStyle::SetOptStat
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
TProfile
Profile Histogram.
Definition: TProfile.h:32
TObject::Draw
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
Double_t
double Double_t
Definition: RtypesCore.h:59
TVirtualPad
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:51
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TAttText::SetTextSizePixels
virtual void SetTextSizePixels(Int_t npixels)
Set the text size in pixels.
Definition: TAttText.cxx:393
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TCanvas::Update
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2504
d
#define d(i)
Definition: RSha256.hxx:102
TAttAxis::SetNdivisions
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition: TAttAxis.cxx:228
TVirtualPad::Modified
virtual void Modified(Bool_t flag=1)=0
gPad
#define gPad
Definition: TVirtualPad.h:287
kGray
@ kGray
Definition: Rtypes.h:65
TAttMarker::SetMarkerStyle
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:318
TCanvas::HighlightConnect
virtual void HighlightConnect(const char *slot)
This is "simplification" for function TCanvas::Connect with Highlighted signal for specific slot.
Definition: TCanvas.cxx:1633
kFullDotMedium
@ kFullDotMedium
Definition: TAttMarker.h:50
gROOT
#define gROOT
Definition: TROOT.h:406
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050