Logo ROOT  
Reference Guide
hlGraph2.C File Reference

Detailed Description

This tutorial demonstrates how to use the highlight mode on graph.

TNtuple *ntuple = 0;
void HighlightBinId(TVirtualPad *pad, TObject *obj, Int_t ihp, Int_t y);
void hlGraph2()
{
auto dir = gROOT->GetTutorialDir();
dir.Append("/hsimple.C");
dir.ReplaceAll("/./","/");
if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
auto file = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
if (!file) return;
file->GetObject("ntuple", ntuple);
if (!ntuple) return;
TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 500, 500);
const char *cut = "pz > 3.0";
ntuple->Draw("px:py", cut);
TGraph *graph = (TGraph *)gPad->FindObject("Graph");
auto info = new TText(0.0, 4.5, "please move the mouse over the graph");
info->SetTextAlign(22);
info->SetTextSize(0.03);
info->SetTextColor(kRed+1);
info->SetBit(kCannotPick);
info->Draw();
graph->SetHighlight();
Canvas1->HighlightConnect("HighlightBinId(TVirtualPad*,TObject*,Int_t,Int_t)");
auto Canvas2 = new TCanvas("Canvas2", "Canvas2", 505, 0, 600, 400);
ntuple->Draw("TMath::Sqrt(px*px + py*py + pz*pz)>>histo(100, 0, 15)", cut);
// Must be last
ntuple->Draw("px:py:pz:i", cut, "goff");
}
void HighlightBinId(TVirtualPad *pad, TObject *obj, Int_t ihp, Int_t y)
{
auto Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
if (!Canvas2) return;
auto histo = (TH1F *)Canvas2->FindObject("histo");
if (!histo) return;
Double_t px = ntuple->GetV1()[ihp];
Double_t py = ntuple->GetV2()[ihp];
Double_t pz = ntuple->GetV3()[ihp];
Double_t i = ntuple->GetV4()[ihp];
Double_t p = TMath::Sqrt(px*px + py*py + pz*pz);
Int_t hbin = histo->FindBin(p);
Bool_t redraw = kFALSE;
auto bh = (TBox *)Canvas2->FindObject("TBox");
if (!bh) {
bh = new TBox();
bh->SetFillColor(kBlack);
bh->SetFillStyle(3001);
bh->SetBit(kCannotPick);
bh->SetBit(kCanDelete);
redraw = kTRUE;
}
bh->SetX1(histo->GetBinLowEdge(hbin));
bh->SetY1(histo->GetMinimum());
bh->SetX2(histo->GetBinWidth(hbin) + histo->GetBinLowEdge(hbin));
bh->SetY2(histo->GetBinContent(hbin));
auto th = (TText *)Canvas2->FindObject("TText");
if (!th) {
th = new TText();
th->SetName("TText");
th->SetTextColor(bh->GetFillColor());
th->SetBit(kCanDelete);
redraw = kTRUE;
}
th->SetText(histo->GetXaxis()->GetXmax()*0.75, histo->GetMaximum()*0.5,
TString::Format("id = %d", (Int_t)i));
if (ihp == -1) { // after highlight disabled
delete bh;
delete th;
}
Canvas2->Modified();
Canvas2->Update();
if (!redraw) return;
auto savepad = gPad;
Canvas2->cd();
bh->Draw();
th->Draw();
Canvas2->Update();
savepad->cd();
}
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
Date
March 2018
Author
Jan Musinsky

Definition in file hlGraph2.C.