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();
}
Date
March 2018
Author
Jan Musinsky

Definition in file hlGraph2.C.

TH1::FindObject
virtual TObject * FindObject(const char *name) const
Search object named name in the list of functions.
Definition: TH1.cxx:3780
TTree::GetV4
virtual Double_t * GetV4()
Definition: TTree.h:537
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
kCanDelete
@ kCanDelete
Definition: TObject.h:339
TNtuple
Definition: TNtuple.h:28
TTree::GetV1
virtual Double_t * GetV1()
Definition: TTree.h:531
TH2::GetBinContent
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:88
TString::Data
const char * Data() const
Definition: TString.h:369
TMath::Sqrt
Double_t Sqrt(Double_t x)
Definition: TMath.h:691
gInterpreter
#define gInterpreter
Definition: TInterpreter.h:558
Int_t
int Int_t
Definition: RtypesCore.h:45
TH1::GetBinWidth
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition: TH1.cxx:8618
TString::Format
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
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
kBlack
@ kBlack
Definition: Rtypes.h:65
TTree::GetV2
virtual Double_t * GetV2()
Definition: TTree.h:533
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
TTree::Draw
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:427
TH1::GetMinimum
virtual Double_t GetMinimum(Double_t minval=-FLT_MAX) const
Return minimum value larger than minval of bins in the range, unless the value has been overridden by...
Definition: TH1.cxx:8090
y
Double_t y[n]
Definition: legend1.C:17
kRed
@ kRed
Definition: Rtypes.h:66
TFile
Definition: TFile.h:54
TBox
Definition: TBox.h:22
TH1::GetBinLowEdge
virtual Double_t GetBinLowEdge(Int_t bin) const
Return bin lower edge for 1D histogram.
Definition: TH1.cxx:8607
TText
Definition: TText.h:22
Double_t
double Double_t
Definition: RtypesCore.h:59
TGraph
Definition: TGraph.h:41
TVirtualPad
Definition: TVirtualPad.h:50
TCanvas
Definition: TCanvas.h:23
TH1::FindBin
virtual Int_t FindBin(Double_t x, Double_t y=0, Double_t z=0)
Return Global bin number corresponding to x,y,z.
Definition: TH1.cxx:3595
file
Definition: file.py:1
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
graph
Definition: graph.py:1
TObject
Definition: TObject.h:37
kCannotPick
@ kCannotPick
Definition: TObject.h:344
gPad
#define gPad
Definition: TVirtualPad.h:287
TAxis::GetXmax
Double_t GetXmax() const
Definition: TAxis.h:134
TH1::GetXaxis
TAxis * GetXaxis()
Get the behaviour adopted by the object about the statoverflows. See EStatOverflows for more informat...
Definition: TH1.h:317
TH1::GetMaximum
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition: TH1.cxx:8005
TTree::GetV3
virtual Double_t * GetV3()
Definition: TTree.h:535
TCanvas::HighlightConnect
virtual void HighlightConnect(const char *slot)
This is "simplification" for function TCanvas::Connect with Highlighted signal for specific slot.
Definition: TCanvas.cxx:1629
gROOT
#define gROOT
Definition: TROOT.h:406