Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 = nullptr;
12
13void HighlightBinId(TVirtualPad *pad, TObject *obj, Int_t ihp, Int_t y)
14{
15 auto Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
16 if (!Canvas2)
17 return;
18 auto histo = (TH1F *)Canvas2->FindObject("histo");
19 if (!histo)
20 return;
21
22 Double_t px = ntuple->GetV1()[ihp];
23 Double_t py = ntuple->GetV2()[ihp];
24 Double_t pz = ntuple->GetV3()[ihp];
25 Double_t i = ntuple->GetV4()[ihp];
26 Double_t p = TMath::Sqrt(px * px + py * py + pz * pz);
27 Int_t hbin = histo->FindBin(p);
28
29 Bool_t redraw = kFALSE;
30 auto bh = (TBox *)Canvas2->FindObject("TBox");
31 if (!bh) {
32 bh = new TBox();
33 bh->SetFillColor(kBlack);
34 bh->SetFillStyle(3001);
35 bh->SetBit(kCannotPick);
36 bh->SetBit(kCanDelete);
37 redraw = kTRUE;
38 }
39 bh->SetX1(histo->GetBinLowEdge(hbin));
40 bh->SetY1(histo->GetMinimum());
41 bh->SetX2(histo->GetBinWidth(hbin) + histo->GetBinLowEdge(hbin));
42 bh->SetY2(histo->GetBinContent(hbin));
43
44 auto th = (TText *)Canvas2->FindObject("TText");
45 if (!th) {
46 th = new TText();
47 th->SetName("TText");
48 th->SetTextColor(bh->GetFillColor());
49 th->SetBit(kCanDelete);
50 redraw = kTRUE;
51 }
52 th->SetText(histo->GetXaxis()->GetXmax() * 0.75, histo->GetMaximum() * 0.5, TString::Format("id = %d", (Int_t)i));
53
54 if (ihp == -1) { // after highlight disabled
55 delete bh;
56 delete th;
57 }
58 Canvas2->Modified();
59 Canvas2->Update();
60 if (!redraw)
61 return;
62
63 auto savepad = gPad;
64 Canvas2->cd();
65 bh->Draw();
66 th->Draw();
67 Canvas2->Update();
68 savepad->cd();
69}
70
71void hlGraph2()
72{
73 auto dir = gROOT->GetTutorialDir();
74 dir.Append("/hsimple.C");
75 dir.ReplaceAll("/./", "/");
76 if (!gInterpreter->IsLoaded(dir.Data()))
77 gInterpreter->LoadMacro(dir.Data());
78 auto file = (TFile *)gROOT->ProcessLineFast("hsimple(1)");
79 if (!file)
80 return;
81
82 file->GetObject("ntuple", ntuple);
83 if (!ntuple)
84 return;
85
86 TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 500, 500);
87 Canvas1->HighlightConnect("HighlightBinId(TVirtualPad*,TObject*,Int_t,Int_t)");
88
89 const char *cut = "pz > 3.0";
90 ntuple->Draw("px:py", cut);
91 TGraph *graph = (TGraph *)gPad->FindObject("Graph");
92
93 auto info = new TText(0.0, 4.5, "please move the mouse over the graph");
94 info->SetTextAlign(22);
95 info->SetTextSize(0.03);
96 info->SetTextColor(kRed + 1);
97 info->SetBit(kCannotPick);
98 info->Draw();
99
100 graph->SetHighlight();
101
102 auto Canvas2 = new TCanvas("Canvas2", "Canvas2", 505, 0, 600, 400);
103 ntuple->Draw("TMath::Sqrt(px*px + py*py + pz*pz)>>histo(100, 0, 15)", cut);
104
105 // Must be last
106 ntuple->Draw("px:py:pz:i", cut, "goff");
107}
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
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
virtual void HighlightConnect(const char *slot)
This is "simplification" for function TCanvas::Connect with Highlighted signal for specific slot.
Definition TCanvas.cxx:1631
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
A TGraph is an 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:623
A simple TTree restricted to a list of float variables only.
Definition TNtuple.h:28
Mother of all ROOT objects.
Definition TObject.h:41
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:420
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
void Draw(Option_t *opt) override
Default Draw method for all objects.
Definition TTree.h:431
virtual Double_t * GetV3()
Definition TTree.h:580
virtual Double_t * GetV1()
Definition TTree.h:576
virtual Double_t * GetV4()
Definition TTree.h:582
virtual Double_t * GetV2()
Definition TTree.h:578
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
Definition graph.py:1