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) return;
17 auto histo = (TH1F *)Canvas2->FindObject("histo");
18 if (!histo) return;
19
20 Double_t px = ntuple->GetV1()[ihp];
21 Double_t py = ntuple->GetV2()[ihp];
22 Double_t pz = ntuple->GetV3()[ihp];
23 Double_t i = ntuple->GetV4()[ihp];
24 Double_t p = TMath::Sqrt(px*px + py*py + pz*pz);
25 Int_t hbin = histo->FindBin(p);
26
27 Bool_t redraw = kFALSE;
28 auto bh = (TBox *)Canvas2->FindObject("TBox");
29 if (!bh) {
30 bh = new TBox();
31 bh->SetFillColor(kBlack);
32 bh->SetFillStyle(3001);
33 bh->SetBit(kCannotPick);
34 bh->SetBit(kCanDelete);
35 redraw = kTRUE;
36 }
37 bh->SetX1(histo->GetBinLowEdge(hbin));
38 bh->SetY1(histo->GetMinimum());
39 bh->SetX2(histo->GetBinWidth(hbin) + histo->GetBinLowEdge(hbin));
40 bh->SetY2(histo->GetBinContent(hbin));
41
42 auto th = (TText *)Canvas2->FindObject("TText");
43 if (!th) {
44 th = new TText();
45 th->SetName("TText");
46 th->SetTextColor(bh->GetFillColor());
47 th->SetBit(kCanDelete);
48 redraw = kTRUE;
49 }
50 th->SetText(histo->GetXaxis()->GetXmax()*0.75, histo->GetMaximum()*0.5,
51 TString::Format("id = %d", (Int_t)i));
52
53 if (ihp == -1) { // after highlight disabled
54 delete bh;
55 delete th;
56 }
57 Canvas2->Modified();
58 Canvas2->Update();
59 if (!redraw) return;
60
61 auto savepad = gPad;
62 Canvas2->cd();
63 bh->Draw();
64 th->Draw();
65 Canvas2->Update();
66 savepad->cd();
67}
68
69void hlGraph2()
70{
71 auto dir = gROOT->GetTutorialDir();
72 dir.Append("/hsimple.C");
73 dir.ReplaceAll("/./","/");
74 if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
75 auto file = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
76 if (!file) return;
77
78 file->GetObject("ntuple", ntuple);
79 if (!ntuple) return;
80
81 TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 500, 500);
82 Canvas1->HighlightConnect("HighlightBinId(TVirtualPad*,TObject*,Int_t,Int_t)");
83
84 const char *cut = "pz > 3.0";
85 ntuple->Draw("px:py", cut);
86 TGraph *graph = (TGraph *)gPad->FindObject("Graph");
87
88 auto info = new TText(0.0, 4.5, "please move the mouse over the graph");
89 info->SetTextAlign(22);
90 info->SetTextSize(0.03);
91 info->SetTextColor(kRed+1);
92 info->SetBit(kCannotPick);
93 info->Draw();
94
95 graph->SetHighlight();
96
97 auto Canvas2 = new TCanvas("Canvas2", "Canvas2", 505, 0, 600, 400);
98 ntuple->Draw("TMath::Sqrt(px*px + py*py + pz*pz)>>histo(100, 0, 15)", cut);
99
100 // Must be last
101 ntuple->Draw("px:py:pz:i", cut, "goff");
102}
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
@ kRed
Definition Rtypes.h:66
@ kBlack
Definition Rtypes.h:65
winID h TVirtualViewer3D TVirtualGLPainter p
#define gInterpreter
@ kCanDelete
Definition TObject.h:369
@ kCannotPick
Definition TObject.h:374
#define gROOT
Definition TROOT.h:405
#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:1619
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:51
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:577
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:403
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:2356
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:428
virtual Double_t * GetV3()
Definition TTree.h:537
virtual Double_t * GetV1()
Definition TTree.h:533
virtual Double_t * GetV4()
Definition TTree.h:539
virtual Double_t * GetV2()
Definition TTree.h:535
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:660
Definition file.py:1
Definition graph.py:1