Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hlHisto1.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
15void HighlightTitle(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
16
17TText *info;
18
19
20void hlHisto1()
21{
22 auto Canvas = new TCanvas();
23 auto h2 = new TH2F("h2", "", 50, -5.0, 5.0, 50, -5.0, 5.0);
24 for (Int_t i = 0; i < 10000; i++) h2->Fill(gRandom->Gaus(), gRandom->Gaus());
25 h2->Draw();
26
27 info = new TText(0.0, -4.0, "please move the mouse over the frame");
28 info->SetTextAlign(22);
29 info->SetTextColor(kRed+1);
30 info->SetBit(kCannotPick);
31 info->Draw();
32 Canvas->Update();
33
34 h2->SetHighlight();
35 Canvas->HighlightConnect("HighlightTitle(TVirtualPad*,TObject*,Int_t,Int_t)");
36}
37
38
39void HighlightTitle(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
40{
41 auto h2 = (TH2F *)obj;
42 if (!h2) return;
43 if (!h2->IsHighlight()) { // after highlight disabled
44 h2->SetTitle("");
45 return;
46 }
47 info->SetTitle("");
48 TString t;
49 t.Form("bin[%02d, %02d] (%5.2f, %5.2f) content %g", xhb, yhb,
50 h2->GetXaxis()->GetBinCenter(xhb), h2->GetYaxis()->GetBinCenter(yhb),
51 h2->GetBinContent(xhb, yhb));
52 h2->SetTitle(t.Data());
53 pad->Update();
54}
int Int_t
Definition RtypesCore.h:45
@ kRed
Definition Rtypes.h:66
@ kCannotPick
Definition TObject.h:359
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:41
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:43
The Canvas class.
Definition TCanvas.h:23
2-D histogram with a float per channel (see TH1 documentation)}
Definition TH2.h:251
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
Mother of all ROOT objects.
Definition TObject.h:37
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:696
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:197
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:274
Basic string class.
Definition TString.h:136
const char * Data() const
Definition TString.h:369
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2309
Base class for several text objects.
Definition TText.h:22
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual void Update()=0