Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist046_Graphics_highlight1D.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 1D histogram is created.
6/// Then an highlight method is connected to the histogram. Moving the mouse
7/// on the histogram will open a new canvas showing in real time a zoom around
8/// the highlighted bin.
9///
10/// \macro_code
11///
12/// \date March 2018
13/// \author Jan Musinsky
14
15TText *info = nullptr;
16TH1 *hz = nullptr;
17
18void HighlightZoom(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
19{
20 auto h = dynamic_cast<TH1F *>(obj);
21 if (!h)
22 return;
23
24 auto Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
25 if (!h->IsHighlight()) { // after highlight disabled
26 if (Canvas2)
27 delete Canvas2;
28 if (hz) {
29 delete hz;
30 hz = nullptr;
31 }
32 return;
33 }
34
35 if (info)
36 info->SetTitle("");
37
38 if (!Canvas2) {
39 Canvas2 = new TCanvas("Canvas2", "Canvas2", 605, 0, 400, 400);
40 Canvas2->SetGrid();
41 if (hz)
42 hz->Draw(); // after reopen this canvas
43 }
44 if (!hz) {
45 hz = (TH1 *)h->Clone("hz");
46 hz->SetTitle(TString::Format("%s (zoomed)", hz->GetTitle()));
47 hz->SetStats(kFALSE);
48 hz->Draw();
49 Canvas2->Update();
51 }
52
53 Int_t zf = hz->GetNbinsX() * 0.05; // zoom factor
54 hz->GetXaxis()->SetRange(xhb - zf, xhb + zf);
55
56 Canvas2->Modified();
57 Canvas2->Update();
58}
59
60void hist046_Graphics_highlight1D()
61{
62 auto Canvas1 = new TCanvas("Canvas1", "", 0, 0, 600, 400);
63 Canvas1->HighlightConnect("HighlightZoom(TVirtualPad*,TObject*,Int_t,Int_t)");
64
65 auto f1 = new TF1("f1", "x*gaus(0) + [3]*abs(sin(x)/x)", -50.0, 50.0);
66 f1->SetParameters(20.0, 4.0, 1.0, 20.0);
67 auto h1 = new TH1F("h1", "Test random numbers", 200, -50.0, 50.0);
68 h1->FillRandom("f1", 100000);
69 h1->Draw();
70 h1->Fit(f1, "Q");
71 gStyle->SetGridColor(kGray);
72 Canvas1->SetGrid();
73
74 info = new TText(0.0, h1->GetMaximum() * 0.7, "please move the mouse over the frame");
75 info->SetTextSize(0.04);
76 info->SetTextAlign(22);
77 info->SetTextColor(kRed - 1);
78 info->SetBit(kCannotPick);
79 info->Draw();
80
81 Canvas1->Update();
82
83 // configure highlight at the end when histogram is already painted
84 h1->SetHighlight();
85}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
@ kGray
Definition Rtypes.h:66
@ kRed
Definition Rtypes.h:67
@ kCannotPick
Definition TObject.h:380
#define gROOT
Definition TROOT.h:417
externTStyle * gStyle
Definition TStyle.h:442
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
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:53
virtual void SetRange(Int_t first=0, Int_t last=0)
Set the viewing range for the axis using bin numbers.
Definition TAxis.cxx:1061
The Canvas class.
Definition TCanvas.h:23
Definition TF1.h:182
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6836
TAxis * GetXaxis()
Definition TH1.h:571
virtual Int_t GetNbinsX() const
Definition TH1.h:541
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3097
virtual void SetHighlight(Bool_t set=kTRUE)
Set highlight (enable/disable) mode for the histogram by default highlight mode is disable.
Definition TH1.cxx:4537
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9127
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:173
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
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
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:2385
Definition TText.h:22
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
TH1F * h1
Definition legend1.C:5
TF1 * f1
Definition legend1.C:11