Logo ROOT  
Reference Guide
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.


Root > TFile f("hsimple.root");
Root > hpxpy.Draw();
Root > c1.AddExec("ex2",".x exec2.C");
#define f(i)
Definition RSha256.hxx:104
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
return c1
Definition legend1.C:41

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");
TObject *select = gPad->GetSelected();
if(!select) return;
if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
//erase old position and draw a line at current position
int pyold = gPad->GetUniqueID();
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);
if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold);
Float_t upy = gPad->AbsPixeltoY(py);
Float_t y = gPad->PadtoY(upy);
//create or set the new canvas c2
TVirtualPad *padsav = gPad;
TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
if(c2) delete c2->GetPrimitive("Projection");
else c2 = new TCanvas("c2");
//draw slice corresponding to mouse position
TH2 *h = (TH2*)select;
Int_t biny = h->GetYaxis()->FindBin(y);
TH1D *hp = h->ProjectionX("",biny,biny);
char title[80];
sprintf(title,"Projection of biny=%d",biny);
#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:91
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
#define gROOT
Definition TROOT.h:406
#define gPad
#define gVirtualX
Definition TVirtualX.h:338
bool Update(const std::shared_ptr< ROOT::Math::Minimizer > &min, const ROOT::Fit::FitConfig &fconfig, bool isValid, unsigned int ncalls=0)
Update the fit result with a new minimization status To be run only if same fit is performed with sam...
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition TAxis.cxx:293
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)}
Definition TH1.h:618
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition TH1.cxx:6678
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:3892
TAxis * GetYaxis()
Definition TH1.h:321
virtual void SetName(const char *name)
Change the name of this histogram.
Definition TH1.cxx:8800
Service class for 2-Dim histogram classes.
Definition TH2.h:30
Mother of all ROOT objects.
Definition TObject.h:37
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:445
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:707
TObject * FindObject(const char *name) const override
Search if object named name is inside this pad or in pads inside this pad.
Definition TPad.cxx:2625
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
Double_t y[n]
Definition legend1.C:17
return c2
Definition legend2.C:14
Rene Brun

Definition in file exec2.C.