Logo ROOT  
Reference Guide
exec2.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 exec2.C");
#define gPad
Definition: TVirtualPad.h:288
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:4053

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 CINT as a development engine.

void exec2()
{
if (!gPad) {
Error("exec2", "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
Definition: RtypesCore.h:45
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:100
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:188
#define gROOT
Definition: TROOT.h:404
#define gVirtualX
Definition: TVirtualX.h:338
The Canvas class.
Definition: TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:620
void SetTitle(const char *title) override
Change (i.e.
Definition: TH1.cxx:6700
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:3894
void SetName(const char *name) override
Change the name of this histogram.
Definition: TH1.cxx:8827
Service class for 2-D histogram classes.
Definition: TH2.h:30
Mother of all ROOT objects.
Definition: TObject.h:41
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:2345
Double_t y[n]
Definition: legend1.C:17
return c2
Definition: legend2.C:14
Authors
Rene Brun, Sergey Linev

Definition in file exec2.C.