Logo ROOT   6.18/05
Reference Guide
exec2.C
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.
5///
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.
19///
20/// \macro_code
21///
22/// \author Rene Brun
23
24void exec2()
25{
26
27 if (!gPad) {
28 Error("exec2", "gPad is null, you are not supposed to run this macro");
29 return;
30 }
31
32
33 TObject *select = gPad->GetSelected();
34 if(!select) return;
35 if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
36 gPad->GetCanvas()->FeedbackMode(kTRUE);
37
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);
51
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();
58
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();
70}
void Class()
Definition: Class.C:29
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:41
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
void Error(const char *location, const char *msgfmt,...)
#define gROOT
Definition: TROOT.h:414
#define gPad
Definition: TVirtualPad.h:286
#define gVirtualX
Definition: TVirtualX.h:345
The Canvas class.
Definition: TCanvas.h:31
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:614
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition: TH1.cxx:6309
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:3791
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8374
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:443
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:50
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
Double_t y[n]
Definition: legend1.C:17
return c2
Definition: legend2.C:14