Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
hlHisto2.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 and
6 /// drawn with the "col" option.
7 /// Then an highlight method is connected to the histogram. Moving the mouse
8 /// on the histogram open a new canvas displaying the two X and Y projections
9 /// at the highlighted bin.
10 ///
11 /// \macro_code
12 ///
13 /// \date March 2018
14 /// \author Jan Musinsky
15 
16 void Highlight2(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
17 
18 TText *info;
19 
20 
21 void hlHisto2()
22 {
23  auto Canvas = new TCanvas("Canvas", "Canvas", 0, 0, 500, 500);
24  auto h2 = new TH2F("h2", "", 50, -5.0, 5.0, 50, -5.0, 5.0);
25  for (Int_t i = 0; i < 10000; i++) h2->Fill(gRandom->Gaus(), gRandom->Gaus());
26  h2->Draw("col");
27 
28  info = new TText(0.0, -4.0, "please move the mouse over the frame");
29  info->SetTextAlign(22);
30  info->SetTextSize(0.04);
31  info->SetTextColor(kRed+1);
32  info->SetBit(kCannotPick);
33  info->Draw();
34  Canvas->Update();
35 
36  h2->SetHighlight();
37  Canvas->HighlightConnect("Highlight2(TVirtualPad*,TObject*,Int_t,Int_t)");
38 }
39 
40 
41 void Highlight2(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
42 {
43  auto h2 = (TH2F *)obj;
44  if(!h2) return;
45  auto CanvasProj = (TCanvas *) gROOT->GetListOfCanvases()->FindObject("CanvasProj");
46  if (!h2->IsHighlight()) { // after highlight disabled
47  if (CanvasProj) delete CanvasProj;
48  return;
49  }
50 
51  info->SetTitle("");
52 
53  auto px = h2->ProjectionX("_px", yhb, yhb);
54  auto py = h2->ProjectionY("_py", xhb, xhb);
55  px->SetTitle(TString::Format("ProjectionX of biny[%02d]", yhb));
56  py->SetTitle(TString::Format("ProjectionY of binx[%02d]", xhb));
57 
58  if (!CanvasProj) {
59  CanvasProj = new TCanvas("CanvasProj", "CanvasProj", 505, 0, 600, 600);
60  CanvasProj->Divide(1, 2);
61  CanvasProj->cd(1);
62  px->Draw();
63  CanvasProj->cd(2);
64  py->Draw();
65  }
66 
67  CanvasProj->GetPad(1)->Modified();
68  CanvasProj->GetPad(2)->Modified();
69  CanvasProj->Update();
70 }