diff --git a/tutorials/hist/hlHisto1.C b/tutorials/hist/hlHisto1.C index 874499de85d7387c79b192fcc4fad1f0dbc346aa..fbcff8b24f5b36a469db6e976472898e3d23a9aa 100644 --- a/tutorials/hist/hlHisto1.C +++ b/tutorials/hist/hlHisto1.C @@ -1,46 +1,50 @@ /// \file /// \ingroup tutorial_hist -/// This tutorial shows highlight mode for histogram 1 +/// +/// This tutorial demonstrates how the highlight mechanism can be used on an histogram. +/// A 2D histogram is booked an filled with a random gaussian distribution. +/// Then an highlight method is connected to the histogram. Moving the mouse +/// on the histogram will update the histogram title in real time according to +/// the highlighted bin. /// /// \macro_code /// /// \date March 2018 /// \author Jan Musinsky -#include <TCanvas.h> -#include <TH2.h> -#include <TRandom.h> -#include <TText.h> - void HighlightTitle(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb); +TText *info; + + void hlHisto1() { - TCanvas *c1 = new TCanvas(); - TH2F *h2 = new TH2F("h2", "", 50, -5.0, 5.0, 50, -5.0, 5.0); + auto Canvas = new TCanvas(); + auto h2 = new TH2F("h2", "", 50, -5.0, 5.0, 50, -5.0, 5.0); for (Int_t i = 0; i < 10000; i++) h2->Fill(gRandom->Gaus(), gRandom->Gaus()); h2->Draw(); - TText *info = new TText(0.0, -4.0, "please move the mouse over the frame"); + info = new TText(0.0, -4.0, "please move the mouse over the frame"); info->SetTextAlign(22); info->SetTextColor(kRed+1); info->SetBit(kCannotPick); info->Draw(); - c1->Update(); + Canvas->Update(); h2->SetHighlight(); - c1->HighlightConnect("HighlightTitle(TVirtualPad*,TObject*,Int_t,Int_t)"); + Canvas->HighlightConnect("HighlightTitle(TVirtualPad*,TObject*,Int_t,Int_t)"); } + void HighlightTitle(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb) { - TH2F *h2 = (TH2F *)obj; + auto h2 = (TH2F *)obj; if (!h2) return; if (!h2->IsHighlight()) { // after highlight disabled h2->SetTitle(""); return; } - + info->SetTitle(""); TString t; t.Form("bin[%02d, %02d] (%5.2f, %5.2f) content %g", xhb, yhb, h2->GetXaxis()->GetBinCenter(xhb), h2->GetYaxis()->GetBinCenter(yhb), diff --git a/tutorials/hist/hlHisto2.C b/tutorials/hist/hlHisto2.C index 6be6193699900faf6f0d19af1aeeb16400bf0dad..839131ef50a14619710295c69ff04226cc5ab315 100644 --- a/tutorials/hist/hlHisto2.C +++ b/tutorials/hist/hlHisto2.C @@ -1,64 +1,70 @@ /// \file /// \ingroup tutorial_hist -/// This tutorial shows highlight mode for histogram 2 +/// +/// This tutorial demonstrates how the highlight mechanism can be used on an histogram. +/// A 2D histogram is booked an filled with a random gaussian distribution and +/// drawn with the "col" option. +/// Then an highlight method is connected to the histogram. Moving the mouse +/// on the histogram open a new canvas displaying the two X and Y projections +/// at the highlighted bin. /// /// \macro_code /// /// \date March 2018 /// \author Jan Musinsky -#include <TCanvas.h> -#include <TH2.h> -#include <TRandom.h> -#include <TROOT.h> -#include <TText.h> - void Highlight2(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb); +TText *info; + + void hlHisto2() { - TCanvas *c1 = new TCanvas("c1", "c1", 0, 0, 500, 500); - TH2F *h2 = new TH2F("h2", "", 50, -5.0, 5.0, 50, -5.0, 5.0); + auto Canvas = new TCanvas("Canvas", "Canvas", 0, 0, 500, 500); + auto h2 = new TH2F("h2", "", 50, -5.0, 5.0, 50, -5.0, 5.0); for (Int_t i = 0; i < 10000; i++) h2->Fill(gRandom->Gaus(), gRandom->Gaus()); h2->Draw("col"); - TText *info = new TText(0.0, -4.0, "please move the mouse over the frame"); + info = new TText(0.0, -4.0, "please move the mouse over the frame"); info->SetTextAlign(22); info->SetTextSize(0.04); info->SetTextColor(kRed+1); info->SetBit(kCannotPick); info->Draw(); - c1->Update(); + Canvas->Update(); h2->SetHighlight(); - c1->HighlightConnect("Highlight2(TVirtualPad*,TObject*,Int_t,Int_t)"); + Canvas->HighlightConnect("Highlight2(TVirtualPad*,TObject*,Int_t,Int_t)"); } + void Highlight2(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb) { - TH2F *h2 = (TH2F *)obj; + auto h2 = (TH2F *)obj; if(!h2) return; - TCanvas *c2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("c2"); + auto CanvasProj = (TCanvas *) gROOT->GetListOfCanvases()->FindObject("CanvasProj"); if (!h2->IsHighlight()) { // after highlight disabled - if (c2) delete c2; + if (CanvasProj) delete CanvasProj; return; } - TH1 *px = h2->ProjectionX("_px", yhb, yhb); - TH1 *py = h2->ProjectionY("_py", xhb, xhb); + info->SetTitle(""); + + auto px = h2->ProjectionX("_px", yhb, yhb); + auto py = h2->ProjectionY("_py", xhb, xhb); px->SetTitle(TString::Format("ProjectionX of biny[%02d]", yhb)); py->SetTitle(TString::Format("ProjectionY of binx[%02d]", xhb)); - if (!c2) { - c2 = new TCanvas("c2", "c2", 505, 0, 600, 600); - c2->Divide(1, 2); - c2->cd(1); + if (!CanvasProj) { + CanvasProj = new TCanvas("CanvasProj", "CanvasProj", 505, 0, 600, 600); + CanvasProj->Divide(1, 2); + CanvasProj->cd(1); px->Draw(); - c2->cd(2); + CanvasProj->cd(2); py->Draw(); } - c2->GetPad(1)->Modified(); - c2->GetPad(2)->Modified(); - c2->Update(); + CanvasProj->GetPad(1)->Modified(); + CanvasProj->GetPad(2)->Modified(); + CanvasProj->Update(); } diff --git a/tutorials/hist/hlHisto3.C b/tutorials/hist/hlHisto3.C index 0f46c9d3a0df981be4d1fb550de266f55b1b4e70..4c6d663d881804c3480427ab6b8df5505c6a148a 100644 --- a/tutorials/hist/hlHisto3.C +++ b/tutorials/hist/hlHisto3.C @@ -1,26 +1,23 @@ /// \file /// \ingroup tutorial_hist -/// This tutorial shows highlight mode for histogram 3 +/// +/// 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. /// /// \macro_code /// /// \date March 2018 /// \author Jan Musinsky -#include <TROOT.h> -#include <TFile.h> -#include <TNtuple.h> -#include <TH2.h> -#include <TGraph.h> -#include <TCanvas.h> -#include <TText.h> - 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() { TFile *file = TFile::Open("$ROOTSYS/tutorials/hsimple.root"); @@ -34,47 +31,47 @@ void hlHisto3() if (!ntuple) return; const char *cut = "pz > 3.0"; - TCanvas *c1 = new TCanvas("c1", "c1", 0, 0, 700, 500); - c1->Divide(1, 2); - TCanvas *c2 = new TCanvas("c2", "c2", 705, 0, 500, 500); + 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 - c1->cd(1); + // Case1, histo1, pz distribution + Canvas1->cd(1); ntuple->Draw("pz>>histo1(100, 2.0, 12.0)", cut); - TH1F *histo1 = (TH1F *)gPad->FindObject("histo1"); - TText *info1 = new TText(7.0, histo1->GetMaximum()*0.6, + 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 - c1->cd(2); + // Case2, histo2, px*py*pz distribution + Canvas1->cd(2); ntuple->Draw("(px*py*pz)>>histo2(100, -50.0, 50.0)", cut); - TH1F *histo2 = (TH1F *)gPad->FindObject("histo2"); + auto histo2 = (TH1F *)gPad->FindObject("histo2"); histo2->SetLineColor(kGreen+2); - TText *info2 = new TText(10.0, histo2->GetMaximum()*0.6, info1->GetTitle()); + auto info2 = new TText(10.0, histo2->GetMaximum()*0.6, info1->GetTitle()); info2->SetTextColor(histo2->GetLineColor()); info2->SetBit(kCannotPick); info2->Draw(); - c1->Update(); + Canvas1->Update(); histo1->SetHighlight(); histo2->SetHighlight(); - c1->HighlightConnect("Highlight3(TVirtualPad*,TObject*,Int_t,Int_t)"); + Canvas1->HighlightConnect("Highlight3(TVirtualPad*,TObject*,Int_t,Int_t)"); - // common graph (all entries, all histo bins) - c2->cd(); + // Common graph (all entries, all histo bins) + Canvas2->cd(); ntuple->Draw("px:py", cut); - TGraph *gcommon = (TGraph *)gPad->FindObject("Graph"); + auto gcommon = (TGraph *)gPad->FindObject("Graph"); gcommon->SetBit(kCanDelete, kFALSE); // will be redraw - TH2F *htemp = (TH2F *)gPad->FindObject("htemp"); + 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 as last + // Must be last ntuple->Draw("px:py:pz", cut, "goff"); histo1->SetUniqueID(1); // mark as case1 histo2->SetUniqueID(2); // mark as case2 @@ -82,6 +79,7 @@ void hlHisto3() InitGraphs(ntuple, histo2); } + void InitGraphs(TNtuple *nt, TH1F *histo) { Long64_t nev = nt->GetSelectedRows(); @@ -89,7 +87,7 @@ void InitGraphs(TNtuple *nt, TH1F *histo) Double_t *py = nt->GetV2(); Double_t *pz = nt->GetV3(); - TList *list = new TList(); + auto list = new TList(); if (histo->GetUniqueID() == 1) list1 = list; else if (histo->GetUniqueID() == 2) list2 = list; else return; @@ -117,14 +115,15 @@ void InitGraphs(TNtuple *nt, TH1F *histo) } } + void Highlight3(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb) { - TH1F *histo = (TH1F *)obj; + auto histo = (TH1F *)obj; if(!histo) return; - TCanvas *c2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("c2"); - if (!c2) return; - TGraph *gcommon = (TGraph *)c2->FindObject("Graph"); + TCanvas *Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2"); + if (!Canvas2) return; + TGraph *gcommon = (TGraph *)Canvas2->FindObject("Graph"); if (!gcommon) return; TList *list = 0; @@ -135,11 +134,11 @@ void Highlight3(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb) if (!g) return; TVirtualPad *savepad = gPad; - c2->cd(); + 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"); - c2->Update(); + Canvas2->Update(); savepad->cd(); } diff --git a/tutorials/hist/hlHisto4.C b/tutorials/hist/hlHisto4.C index 4d1c4c023568c56f1db9409f0bbf31bd2455cd28..5435b1ee7dcf9addcd0cb20d0bf9ae8de08e785e 100644 --- a/tutorials/hist/hlHisto4.C +++ b/tutorials/hist/hlHisto4.C @@ -1,61 +1,65 @@ /// \file /// \ingroup tutorial_hist -/// This tutorial shows highlight mode for histogram 4 +/// +/// This tutorial demonstrates how the highlight mechanism can be used on an histogram. +/// A 1D histogram is created. +/// Then an highlight method is connected to the histogram. Moving the mouse +/// on the histogram will open a new canvas showing in real time a zoom around +/// the highlighted bin. /// /// \macro_code /// /// \date March 2018 /// \author Jan Musinsky -#include <TCanvas.h> -#include <TF1.h> -#include <TH1.h> -#include <TText.h> -#include <TROOT.h> -#include <TStyle.h> - void HighlightZoom(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb); +TText *info; + + void hlHisto4() { - TCanvas *c1 = new TCanvas("c1", "", 0, 0, 600, 400); - TF1 *f1 = new TF1("f1", "x*gaus(0) + [3]*abs(sin(x)/x)", -50.0, 50.0); + auto Canvas1 = new TCanvas("Canvas1", "", 0, 0, 600, 400); + auto f1 = new TF1("f1", "x*gaus(0) + [3]*abs(sin(x)/x)", -50.0, 50.0); f1->SetParameters(20.0, 4.0, 1.0, 20.0); - TH1F *h1 = new TH1F("h1", "Test random numbers", 200, -50.0, 50.0); + auto h1 = new TH1F("h1", "Test random numbers", 200, -50.0, 50.0); h1->FillRandom("f1", 100000); h1->Draw(); h1->Fit(f1, "Q"); gStyle->SetGridColor(kGray); - c1->SetGrid(); + Canvas1->SetGrid(); - TText *info = new TText(0.0, h1->GetMaximum()*0.7, "please move the mouse over the frame"); + info = new TText(0.0, h1->GetMaximum()*0.7, "please move the mouse over the frame"); info->SetTextSize(0.04); info->SetTextAlign(22); info->SetTextColor(kRed-1); info->SetBit(kCannotPick); info->Draw(); - c1->Update(); + Canvas1->Update(); h1->SetHighlight(); - c1->HighlightConnect("HighlightZoom(TVirtualPad*,TObject*,Int_t,Int_t)"); + Canvas1->HighlightConnect("HighlightZoom(TVirtualPad*,TObject*,Int_t,Int_t)"); } + void HighlightZoom(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb) { - TH1F *h = (TH1F *)obj; + auto h = (TH1F *)obj; if(!h) return; - TCanvas *c2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("c2"); + auto Canvas2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("Canvas2"); static TH1 *hz = 0; if (!h->IsHighlight()) { // after highlight disabled - if (c2) delete c2; + if (Canvas2) delete Canvas2; if (hz) { delete hz; hz = 0; } return; } - if (!c2) { - c2 = new TCanvas("c2", "c2", 605, 0, 400, 400); - c2->SetGrid(); + info->SetTitle(""); + + if (!Canvas2) { + Canvas2 = new TCanvas("Canvas2", "Canvas2", 605, 0, 400, 400); + Canvas2->SetGrid(); if (hz) hz->Draw(); // after reopen this canvas } if (!hz) { @@ -63,13 +67,13 @@ void HighlightZoom(TVirtualPad *pad, TObject *obj, Int_t xhb, Int_t yhb) hz->SetTitle(TString::Format("%s (zoomed)", hz->GetTitle())); hz->SetStats(kFALSE); hz->Draw(); - c2->Update(); + Canvas2->Update(); hz->SetHighlight(kFALSE); } Int_t zf = hz->GetNbinsX()*0.05; // zoom factor hz->GetXaxis()->SetRange(xhb-zf, xhb+zf); - c2->Modified(); - c2->Update(); + Canvas2->Modified(); + Canvas2->Update(); }