Logo ROOT  
Reference Guide
hlHisto3.C File Reference

Detailed Description

This tutorial demonstrates how the highlight mechanism can be used on a ntuple. The ntuple in hsimple.root is drawn with three differents selection. Moving the mouse ove the two 1D representation display the on 2D plot the events contributing to the highlighted bin.

TList *list1 = 0;
TList *list2 = 0;
void InitGraphs(TNtuple *nt, TH1F *histo);
void Highlight3(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb);
void hlHisto3()
{
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;
TNtuple *ntuple;
file->GetObject("ntuple", ntuple);
if (!ntuple) return;
const char *cut = "pz > 3.0";
TCanvas *Canvas1 = new TCanvas("Canvas1", "Canvas1", 0, 0, 700, 500);
Canvas1->Divide(1, 2);
TCanvas *Canvas2 = new TCanvas("Canvas2", "Canvas2", 705, 0, 500, 500);
// Case1, histo1, pz distribution
Canvas1->cd(1);
ntuple->Draw("pz>>histo1(100, 2.0, 12.0)", cut);
auto histo1 = (TH1F *)gPad->FindObject("histo1");
auto info1 = new TText(7.0, histo1->GetMaximum()*0.6,
"please move the mouse over the frame");
info1->SetTextColor(histo1->GetLineColor());
info1->SetBit(kCannotPick);
info1->Draw();
// Case2, histo2, px*py*pz distribution
Canvas1->cd(2);
ntuple->Draw("(px*py*pz)>>histo2(100, -50.0, 50.0)", cut);
auto histo2 = (TH1F *)gPad->FindObject("histo2");
histo2->SetLineColor(kGreen+2);
auto info2 = new TText(10.0, histo2->GetMaximum()*0.6, info1->GetTitle());
info2->SetTextColor(histo2->GetLineColor());
info2->SetBit(kCannotPick);
info2->Draw();
Canvas1->Update();
histo1->SetHighlight();
histo2->SetHighlight();
Canvas1->HighlightConnect("Highlight3(TVirtualPad*,TObject*,Int_t,Int_t)");
// Common graph (all entries, all histo bins)
Canvas2->cd();
ntuple->Draw("px:py", cut);
auto gcommon = (TGraph *)gPad->FindObject("Graph");
gcommon->SetBit(kCanDelete, kFALSE); // will be redraw
auto htemp = (TH2F *)gPad->FindObject("htemp");
gcommon->SetTitle(htemp->GetTitle());
gcommon->GetXaxis()->SetTitle(htemp->GetXaxis()->GetTitle());
gcommon->GetYaxis()->SetTitle(htemp->GetYaxis()->GetTitle());
gcommon->Draw("AP");
// Must be last
ntuple->Draw("px:py:pz", cut, "goff");
histo1->SetUniqueID(1); // mark as case1
histo2->SetUniqueID(2); // mark as case2
InitGraphs(ntuple, histo1);
InitGraphs(ntuple, histo2);
}
void InitGraphs(TNtuple *nt, TH1F *histo)
{
Double_t *px = nt->GetV1();
Double_t *py = nt->GetV2();
Double_t *pz = nt->GetV3();
auto list = new TList();
if (histo->GetUniqueID() == 1) list1 = list;
else if (histo->GetUniqueID() == 2) list2 = list;
else return;
Int_t nbins = histo->GetNbinsX();
Int_t bin;
for (bin = 0; bin < nbins; bin++) {
g = new TGraph();
g->SetName(TString::Format("g%sbin_%d", histo->GetName(), bin+1));
g->SetBit(kCannotPick);
g->SetMarkerStyle(25);
g->SetMarkerColor(histo->GetLineColor());
list->Add(g);
}
Double_t value = 0.0;
for (Long64_t ie = 0; ie < nev; ie++) {
if (histo->GetUniqueID() == 1) value = pz[ie];
if (histo->GetUniqueID() == 2) value = px[ie]*py[ie]*pz[ie];
bin = histo->FindBin(value) - 1;
g = (TGraph *)list->At(bin);
if (!g) continue; // under/overflow
g->SetPoint(g->GetN(), py[ie], px[ie]); // reverse as px:py
}
}
void Highlight3(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb)
{
auto histo = (TH1F *)obj;
if(!histo) return;
TCanvas *Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2");
if (!Canvas2) return;
TGraph *gcommon = (TGraph *)Canvas2->FindObject("Graph");
if (!gcommon) return;
TList *list = 0;
if (histo->GetUniqueID() == 1) list = list1; // case1
else if (histo->GetUniqueID() == 2) list = list2; // case2
if (!list) return;
TGraph *g = (TGraph *)list->At(xhb);
if (!g) return;
TVirtualPad *savepad = gPad;
Canvas2->cd();
gcommon->Draw("AP");
//gcommon->SetTitle(TString::Format("%d / %d", g->GetN(), gcommon->GetN()));
if (histo->IsHighlight()) // don't draw g after highlight disabled
if (g->GetN() > 0) g->Draw("P");
Canvas2->Update();
savepad->cd();
}
Date
March 2018
Author
Jan Musinsky

Definition in file hlHisto3.C.

TGraph::Draw
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:760
kCanDelete
@ kCanDelete
Definition: TObject.h:339
TNtuple
Definition: TNtuple.h:28
TTree::GetV1
virtual Double_t * GetV1()
Definition: TTree.h:531
kGreen
@ kGreen
Definition: Rtypes.h:66
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
TString::Data
const char * Data() const
Definition: TString.h:369
Long64_t
long long Long64_t
Definition: RtypesCore.h:73
gInterpreter
#define gInterpreter
Definition: TInterpreter.h:558
TVirtualPad::cd
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
Int_t
int Int_t
Definition: RtypesCore.h:45
TObject::GetUniqueID
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition: TObject.cxx:377
TPad::Divide
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1166
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
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
TTree::GetSelectedRows
virtual Long64_t GetSelectedRows()
Definition: TTree.h:508
TList::At
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:356
TTree::GetV2
virtual Double_t * GetV2()
Definition: TTree.h:533
TCanvas::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:704
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
TPad::FindObject
virtual TObject * FindObject(const char *name) const
Search if object named name is inside this pad or in pads inside this pad.
Definition: TPad.cxx:2609
TFile
Definition: TFile.h:54
TH1::IsHighlight
virtual Bool_t IsHighlight() const
Definition: TH1.h:331
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
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TObject
Definition: TObject.h:37
TCanvas::Update
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2500
kCannotPick
@ kCannotPick
Definition: TObject.h:344
gPad
#define gPad
Definition: TVirtualPad.h:287
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
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
TList
Definition: TList.h:44
TH1::GetNbinsX
virtual Int_t GetNbinsX() const
Definition: TH1.h:293
gROOT
#define gROOT
Definition: TROOT.h:406
g
#define g(i)
Definition: RSha256.hxx:123