Logo ROOT  
Reference Guide
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
11TNtuple *ntuple = 0;
12
13void HighlightBinId(TVirtualPad *pad, TObject *obj, Int_t ihp, Int_t y);
14
15void 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
50void 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}
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:87
@ kRed
Definition: Rtypes.h:64
@ kBlack
Definition: Rtypes.h:63
#define gInterpreter
Definition: TInterpreter.h:555
@ kCanDelete
Definition: TObject.h:339
@ kCannotPick
Definition: TObject.h:344
#define gROOT
Definition: TROOT.h:415
#define gPad
Definition: TVirtualPad.h:286
Create a Box.
Definition: TBox.h:24
The Canvas class.
Definition: TCanvas.h:31
virtual void HighlightConnect(const char *slot)
This is "simplification" for function TCanvas::Connect with Highlighted signal for specific slot.
Definition: TCanvas.cxx:1596
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:48
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
A simple TTree restricted to a list of float variables only.
Definition: TNtuple.h:28
Mother of all ROOT objects.
Definition: TObject.h:37
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:2311
Base class for several text objects.
Definition: TText.h:23
virtual Double_t * GetV3()
Definition: TTree.h:527
virtual Double_t * GetV1()
Definition: TTree.h:523
virtual Double_t * GetV4()
Definition: TTree.h:529
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:419
virtual Double_t * GetV2()
Definition: TTree.h:525
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:50
Double_t y[n]
Definition: legend1.C:17
Double_t Sqrt(Double_t x)
Definition: TMath.h:681
Definition: file.py:1
Definition: graph.py:1