Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hlHisto4.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
15void HighlightZoom(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
16
17TText *info;
18
19
20void hlHisto4()
21{
22 auto Canvas1 = new TCanvas("Canvas1", "", 0, 0, 600, 400);
23 auto f1 = new TF1("f1", "x*gaus(0) + [3]*abs(sin(x)/x)", -50.0, 50.0);
24 f1->SetParameters(20.0, 4.0, 1.0, 20.0);
25 auto h1 = new TH1F("h1", "Test random numbers", 200, -50.0, 50.0);
26 h1->FillRandom("f1", 100000);
27 h1->Draw();
28 h1->Fit(f1, "Q");
30 Canvas1->SetGrid();
31
32 info = new TText(0.0, h1->GetMaximum()*0.7, "please move the mouse over the frame");
33 info->SetTextSize(0.04);
34 info->SetTextAlign(22);
35 info->SetTextColor(kRed-1);
36 info->SetBit(kCannotPick);
37 info->Draw();
38 Canvas1->Update();
39
41 Canvas1->HighlightConnect("HighlightZoom(TVirtualPad*,TObject*,Int_t,Int_t)");
42}
43
44
45void HighlightZoom(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
46{
47 auto h = (TH1F *)obj;
48 if(!h) return;
49
50 auto Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
51 static TH1 *hz = 0;
52 if (!h->IsHighlight()) { // after highlight disabled
53 if (Canvas2) delete Canvas2;
54 if (hz) { delete hz; hz = 0; }
55 return;
56 }
57
58 info->SetTitle("");
59
60 if (!Canvas2) {
61 Canvas2 = new TCanvas("Canvas2", "Canvas2", 605, 0, 400, 400);
62 Canvas2->SetGrid();
63 if (hz) hz->Draw(); // after reopen this canvas
64 }
65 if (!hz) {
66 hz = (TH1 *)h->Clone("hz");
67 hz->SetTitle(TString::Format("%s (zoomed)", hz->GetTitle()));
68 hz->SetStats(kFALSE);
69 hz->Draw();
70 Canvas2->Update();
72 }
73
74 Int_t zf = hz->GetNbinsX()*0.05; // zoom factor
75 hz->GetXaxis()->SetRange(xhb-zf, xhb+zf);
76
77 Canvas2->Modified();
78 Canvas2->Update();
79}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
const Bool_t kFALSE
Definition RtypesCore.h:92
@ kGray
Definition Rtypes.h:65
@ kRed
Definition Rtypes.h:66
@ kCannotPick
Definition TObject.h:359
#define gROOT
Definition TROOT.h:406
R__EXTERN TStyle * gStyle
Definition TStyle.h:412
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
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:46
virtual void SetRange(Int_t first=0, Int_t last=0)
Set the viewing range for the axis using bin numbers.
Definition TAxis.cxx:920
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:213
virtual void SetParameters(const Double_t *params)
Definition TF1.h:644
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:575
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition TH1.cxx:6678
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition TH1.h:320
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition TH1.cxx:2740
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition TH1.cxx:3525
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition TH1.cxx:3892
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition TH1.cxx:8390
virtual Int_t GetNbinsX() const
Definition TH1.h:296
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3073
virtual void SetHighlight(Bool_t set=kTRUE)
Set highlight (enable/disable) mode for the histogram by default highlight mode is disable.
Definition TH1.cxx:4422
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8830
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
virtual const char * GetTitle() const
Returns title of object.
Definition TNamed.h:48
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
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:2331
void SetGridColor(Color_t color=0)
Definition TStyle.h:353
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
TH1F * h1
Definition legend1.C:5
TF1 * f1
Definition legend1.C:11