Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
15 void HighlightZoom(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
16 
17 TText *info;
18 
19 
20 void 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");
29  gStyle->SetGridColor(kGray);
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 
40  h1->SetHighlight();
41  Canvas1->HighlightConnect("HighlightZoom(TVirtualPad*,TObject*,Int_t,Int_t)");
42 }
43 
44 
45 void 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();
71  hz->SetHighlight(kFALSE);
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 }