Logo ROOT  
Reference Guide
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// Echo object at mouse position and show a graphics line.
4/// Example of macro called when a mouse event occurs in a pad.
6/// Example:
7/// ~~~{.cpp}
8/// Root > TFile f("hsimple.root");
9/// Root > hpxpy.Draw();
10/// Root > c1.AddExec("ex2",".x exec2.C");
11/// ~~~
12/// When moving the mouse in the canvas, a second canvas shows the
13/// projection along X of the bin corresponding to the Y position
14/// of the mouse. The resulting histogram is fitted with a gaussian.
15/// A "dynamic" line shows the current bin position in Y.
16/// This more elaborated example can be used as a starting point
17/// to develop more powerful interactive applications exploiting CINT
18/// as a development engine.
20/// \macro_code
22/// \author Rene Brun
24void exec2()
27 if (!gPad) {
28 Error("exec2", "gPad is null, you are not supposed to run this macro");
29 return;
30 }
33 TObject *select = gPad->GetSelected();
34 if(!select) return;
35 if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
36 gPad->GetCanvas()->FeedbackMode(kTRUE);
38 //erase old position and draw a line at current position
39 int pyold = gPad->GetUniqueID();
40 int px = gPad->GetEventX();
41 int py = gPad->GetEventY();
42 float uxmin = gPad->GetUxmin();
43 float uxmax = gPad->GetUxmax();
44 int pxmin = gPad->XtoAbsPixel(uxmin);
45 int pxmax = gPad->XtoAbsPixel(uxmax);
46 if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold);
47 gVirtualX->DrawLine(pxmin,py,pxmax,py);
48 gPad->SetUniqueID(py);
49 Float_t upy = gPad->AbsPixeltoY(py);
50 Float_t y = gPad->PadtoY(upy);
52 //create or set the new canvas c2
53 TVirtualPad *padsav = gPad;
54 TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
55 if(c2) delete c2->GetPrimitive("Projection");
56 else c2 = new TCanvas("c2");
57 c2->cd();
59 //draw slice corresponding to mouse position
60 TH2 *h = (TH2*)select;
61 Int_t biny = h->GetYaxis()->FindBin(y);
62 TH1D *hp = h->ProjectionX("",biny,biny);
63 char title[80];
64 sprintf(title,"Projection of biny=%d",biny);
65 hp->SetName("Projection");
66 hp->SetTitle(title);
67 hp->Fit("gaus","ql");
68 c2->Update();
69 padsav->cd();
void Class()
Definition: Class.C:29
#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
Definition: TVirtualPad.h:287
#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: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
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
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