Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
gr302_highlight2.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3///
4/// \preview This tutorial demonstrates how to use the highlight mode on graph, thanks to the TCanvas [HighlightConnect](https://root.cern/doc/master/classTCanvas.html#a462b8dc286a2d29152fefa9b31f89920) method.
5///
6/// \macro_code
7/// \date March 2018
8/// \author Jan Musinsky
9
10TNtuple *ntuple = nullptr;
11
13{
14 auto Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
15 if (!Canvas2) return;
16 auto histo = (TH1F *)Canvas2->FindObject("histo");
17 if (!histo) return;
18
19 Double_t px = ntuple->GetV1()[ihp];
20 Double_t py = ntuple->GetV2()[ihp];
21 Double_t pz = ntuple->GetV3()[ihp];
22 Double_t i = ntuple->GetV4()[ihp];
23 Double_t p = TMath::Sqrt(px*px + py*py + pz*pz);
24 Int_t hbin = histo->FindBin(p);
25
27 auto bh = (TBox *)Canvas2->FindObject("TBox");
28 if (!bh) {
29 bh = new TBox();
30 bh->SetFillColor(kBlack);
31 bh->SetFillStyle(3001);
32 bh->SetBit(kCannotPick);
33 bh->SetBit(kCanDelete);
34 redraw = kTRUE;
35 }
36 bh->SetX1(histo->GetBinLowEdge(hbin));
37 bh->SetY1(histo->GetMinimum());
38 bh->SetX2(histo->GetBinWidth(hbin) + histo->GetBinLowEdge(hbin));
39 bh->SetY2(histo->GetBinContent(hbin));
40
41 auto th = (TText *)Canvas2->FindObject("TText");
42 if (!th) {
43 th = new TText();
44 th->SetName("TText");
45 th->SetTextColor(bh->GetFillColor());
46 th->SetBit(kCanDelete);
47 redraw = kTRUE;
48 }
49 th->SetText(histo->GetXaxis()->GetXmax()*0.75, histo->GetMaximum()*0.5,
50 TString::Format("id = %d", (Int_t)i));
51
52 if (ihp == -1) { // after highlight disabled
53 delete bh;
54 delete th;
55 }
56 Canvas2->Modified();
57 Canvas2->Update();
58 if (!redraw) return;
59
60 auto savepad = gPad;
61 Canvas2->cd();
62 bh->Draw();
63 th->Draw();
64 Canvas2->Update();
65 savepad->cd();
66}
67
69{
70 auto dir = gROOT->GetTutorialDir();
71 dir.Append("/hsimple.C");
72 dir.ReplaceAll("/./","/");
73 if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
74 auto file = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
75 if (!file) return;
76
77 file->GetObject("ntuple", ntuple);
78 if (!ntuple) return;
79
80 TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 500, 500);
81 Canvas1->HighlightConnect("HighlightBinId(TVirtualPad*,TObject*,Int_t,Int_t)");
82
83 const char *cut = "pz > 3.0";
84 ntuple->Draw("px:py", cut);
85 TGraph *graph = (TGraph *)gPad->FindObject("Graph");
86
87 auto info = new TText(0.0, 4.5, "please move the mouse over the graph");
88 info->SetTextAlign(22);
89 info->SetTextSize(0.03);
90 info->SetTextColor(kRed+1);
91 info->SetBit(kCannotPick);
92 info->Draw();
93
94 graph->SetHighlight();
95
96 auto Canvas2 = new TCanvas("Canvas2", "Canvas2", 505, 0, 600, 400);
97 ntuple->Draw("TMath::Sqrt(px*px + py*py + pz*pz)>>histo(100, 0, 15)", cut);
98
99 // Must be last
100 ntuple->Draw("px:py:pz:i", cut, "goff");
101}
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
@ kRed
Definition Rtypes.h:66
@ kBlack
Definition Rtypes.h:65
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
winID h TVirtualViewer3D TVirtualGLPainter p
#define gInterpreter
@ kCanDelete
Definition TObject.h:367
@ kCannotPick
Definition TObject.h:372
#define gROOT
Definition TROOT.h:406
#define gPad
Create a Box.
Definition TBox.h:22
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:131
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetHighlight(Bool_t set=kTRUE)
Set highlight (enable/disable) mode for the graph by default highlight mode is disable.
Definition TGraph.cxx:2316
TObject * FindObject(const char *name) const override
Search object named name in the list of functions.
Definition TGraph.cxx:1113
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:645
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Mother of all ROOT objects.
Definition TObject.h:41
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:2378
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
Double_t y[n]
Definition legend1.C:17
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666