Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
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
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:4094

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 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);
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
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
#define gROOT
Definition TROOT.h:406
#define gVirtualX
Definition TVirtualX.h:337
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:669
Service class for 2-D histogram classes.
Definition TH2.h:30
Mother of all ROOT objects.
Definition TObject.h:41
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:791
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:2378
Double_t y[n]
Definition legend1.C:17
return c2
Definition legend2.C:14
Authors
Rene Brun, Sergey Linev

Definition in file exec2.C.