Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
hlGraph2.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphs
3 ///
4 /// This tutorial demonstrates how to use the highlight mode on graph.
5 ///
6 /// \macro_code
7 ///
8 /// \date March 2018
9 /// \author Jan Musinsky
10 
11 TNtuple *ntuple = 0;
12 
13 void HighlightBinId(TVirtualPad *pad, TObject *obj, Int_t ihp, Int_t y);
14 
15 void hlGraph2()
16 {
17  auto dir = gROOT->GetTutorialDir();
18  dir.Append("/hsimple.C");
19  dir.ReplaceAll("/./","/");
20  if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
21  auto file = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
22  if (!file) return;
23 
24  file->GetObject("ntuple", ntuple);
25  if (!ntuple) return;
26 
27  TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 500, 500);
28  const char *cut = "pz > 3.0";
29  ntuple->Draw("px:py", cut);
30  TGraph *graph = (TGraph *)gPad->FindObject("Graph");
31 
32  auto info = new TText(0.0, 4.5, "please move the mouse over the graph");
33  info->SetTextAlign(22);
34  info->SetTextSize(0.03);
35  info->SetTextColor(kRed+1);
36  info->SetBit(kCannotPick);
37  info->Draw();
38 
39  graph->SetHighlight();
40  Canvas1->HighlightConnect("HighlightBinId(TVirtualPad*,TObject*,Int_t,Int_t)");
41 
42  auto Canvas2 = new TCanvas("Canvas2", "Canvas2", 505, 0, 600, 400);
43  ntuple->Draw("TMath::Sqrt(px*px + py*py + pz*pz)>>histo(100, 0, 15)", cut);
44 
45  // Must be last
46  ntuple->Draw("px:py:pz:i", cut, "goff");
47 }
48 
49 
50 void HighlightBinId(TVirtualPad *pad, TObject *obj, Int_t ihp, Int_t y)
51 {
52  auto Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
53  if (!Canvas2) return;
54  auto histo = (TH1F *)Canvas2->FindObject("histo");
55  if (!histo) return;
56 
57  Double_t px = ntuple->GetV1()[ihp];
58  Double_t py = ntuple->GetV2()[ihp];
59  Double_t pz = ntuple->GetV3()[ihp];
60  Double_t i = ntuple->GetV4()[ihp];
61  Double_t p = TMath::Sqrt(px*px + py*py + pz*pz);
62  Int_t hbin = histo->FindBin(p);
63 
64  Bool_t redraw = kFALSE;
65  auto bh = (TBox *)Canvas2->FindObject("TBox");
66  if (!bh) {
67  bh = new TBox();
68  bh->SetFillColor(kBlack);
69  bh->SetFillStyle(3001);
70  bh->SetBit(kCannotPick);
71  bh->SetBit(kCanDelete);
72  redraw = kTRUE;
73  }
74  bh->SetX1(histo->GetBinLowEdge(hbin));
75  bh->SetY1(histo->GetMinimum());
76  bh->SetX2(histo->GetBinWidth(hbin) + histo->GetBinLowEdge(hbin));
77  bh->SetY2(histo->GetBinContent(hbin));
78 
79  auto th = (TText *)Canvas2->FindObject("TText");
80  if (!th) {
81  th = new TText();
82  th->SetName("TText");
83  th->SetTextColor(bh->GetFillColor());
84  th->SetBit(kCanDelete);
85  redraw = kTRUE;
86  }
87  th->SetText(histo->GetXaxis()->GetXmax()*0.75, histo->GetMaximum()*0.5,
88  TString::Format("id = %d", (Int_t)i));
89 
90  if (ihp == -1) { // after highlight disabled
91  delete bh;
92  delete th;
93  }
94  Canvas2->Modified();
95  Canvas2->Update();
96  if (!redraw) return;
97 
98  auto savepad = gPad;
99  Canvas2->cd();
100  bh->Draw();
101  th->Draw();
102  Canvas2->Update();
103  savepad->cd();
104 }