Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist058_TExec_th2.C File Reference

Detailed Description

Echo object at mouse position and show a graphics line.

Example of macro called when a mouse event occurs in a pad.

Example:

TFile::Open("hsimple.root");
hpxpy->Draw("colz");
gPad->AddExec("ex2", ".x hist058_TExec_th2.C");
#define gPad
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:3787

When moving the mouse in the canvas, a second canvas shows the projection along X of the bin corresponding to the Y position of the mouse. The resulting histogram is fitted with a gaussian. A "dynamic" line shows the current bin position in Y. This more elaborated example can be used as a starting point to develop more powerful interactive applications exploiting CLING as a development engine.

void hist058_TExec_th2()
{
if (!gPad) {
Error("hist058_TExec_th2", "gPad is null, you are not supposed to run this macro");
return;
}
int px = gPad->GetEventX();
int py = gPad->GetEventY();
float uxmin = gPad->GetUxmin();
float uxmax = gPad->GetUxmax();
int pxmin = gPad->XtoAbsPixel(uxmin);
int pxmax = gPad->XtoAbsPixel(uxmax);
TObject *select = gPad->GetSelected();
TCanvas *c2 = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("c2");
gPad->GetCanvas()->FeedbackMode(kTRUE);
int pyold = gPad->GetUniqueID(); // misuse of pad unique for last draw position
if (pyold && c2) {
// erase line at old position
gVirtualX->DrawLine(pxmin, pyold, pxmax, pyold);
gPad->SetUniqueID(0);
}
TH2 *h = dynamic_cast<TH2 *>(select);
if (!h)
return;
// erase old position and draw a line at current position
gVirtualX->DrawLine(pxmin, py, pxmax, py);
gPad->SetUniqueID(py);
Float_t upy = gPad->AbsPixeltoY(py);
Float_t y = gPad->PadtoY(upy);
// create or set the new canvas c2
auto padsav = gPad;
if (c2)
delete c2->GetPrimitive("Projection");
else
c2 = new TCanvas("c2", "Projection Canvas", 710, 10, 700, 500);
c2->SetGrid();
c2->cd();
// draw slice corresponding to mouse position
Int_t biny = h->GetYaxis()->FindBin(y);
TH1D *hp = h->ProjectionX("", biny, biny);
hp->SetName("Projection");
hp->SetTitle(TString::Format("Projection of biny=%d", biny));
hp->Fit("gaus", "ql");
c2->Update();
padsav->cd();
}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
#define gROOT
Definition TROOT.h:417
#define gVirtualX
Definition TVirtualX.h:375
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6836
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition TH1.cxx:3954
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:9097
Service class for 2-D histogram classes.
Definition TH2.h:30
Mother of all ROOT objects.
Definition TObject.h:42
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:2385
Double_t y[n]
Definition legend1.C:17
return c2
Definition legend2.C:14
Date
February 2023
Authors
Rene Brun, Sergey Linev

Definition in file hist058_TExec_th2.C.