Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist043_Graphics_highlight.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3///
4/// This tutorial demonstrates how the highlight mechanism can be used on an histogram.
5/// A 2D histogram is booked an filled with a random gaussian distribution.
6/// Then an highlight method is connected to the histogram. Moving the mouse
7/// on the histogram will update the histogram title in real time according to
8/// the highlighted bin.
9///
10/// \macro_code
11///
12/// \date March 2018
13/// \author Jan Musinsky
14
15TText *info = nullptr;
16
17void HighlightTitle(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
18{
19 auto h2 = dynamic_cast<TH2F *>(obj);
20 if (!h2)
21 return;
22 if (!h2->IsHighlight()) { // after highlight disabled
23 h2->SetTitle("Disable highlight");
24 return;
25 }
26 if (info)
27 info->SetTitle("");
28 TString t;
29 t.Form("bin[%02d, %02d] (%5.2f, %5.2f) content %g", xhb, yhb, h2->GetXaxis()->GetBinCenter(xhb),
30 h2->GetYaxis()->GetBinCenter(yhb), h2->GetBinContent(xhb, yhb));
31 h2->SetTitle(t.Data());
32 pad->Update();
33}
34
35void hist043_Graphics_highlight()
36{
37 auto c1 = new TCanvas();
38 c1->HighlightConnect("HighlightTitle(TVirtualPad*,TObject*,Int_t,Int_t)");
39
40 auto h2 = new TH2F("h2", "", 50, -5.0, 5.0, 50, -5.0, 5.0);
41 for (Int_t i = 0; i < 10000; i++)
42 h2->Fill(gRandom->Gaus(), gRandom->Gaus());
43 h2->Draw();
44
45 info = new TText(0.0, -4.0, "please move the mouse over the frame");
46 info->SetTextAlign(22);
47 info->SetTextColor(kRed + 1);
48 info->SetBit(kCannotPick);
49 info->Draw();
50 c1->Update();
51
52 // call after update to apply changes in the histogram painter
53 h2->SetHighlight();
54}
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:67
@ kCannotPick
Definition TObject.h:380
externTRandom * gRandom
Definition TRandom.h:62
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:48
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:50
The Canvas class.
Definition TCanvas.h:23
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
Mother of all ROOT objects.
Definition TObject.h:42
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:888
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2363
Definition TText.h:22
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual void Update()=0
return c1
Definition legend1.C:41