Logo ROOT  
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/// TFile::Open("hsimple.root");
9/// hpxpy->Draw("colz");
10/// gPad->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/// \authors Rene Brun, Sergey Linev
23
24void exec2()
25{
26 if (!gPad) {
27 Error("exec2", "gPad is null, you are not supposed to run this macro");
28 return;
29 }
30
31 int px = gPad->GetEventX();
32 int py = gPad->GetEventY();
33 float uxmin = gPad->GetUxmin();
34 float uxmax = gPad->GetUxmax();
35 int pxmin = gPad->XtoAbsPixel(uxmin);
36 int pxmax = gPad->XtoAbsPixel(uxmax);
37 TObject *select = gPad->GetSelected();
38 TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
39
40 gPad->GetCanvas()->FeedbackMode(kTRUE);
41
42 int pyold = gPad->GetUniqueID(); // misuse of pad unique for last draw position
43
44 if (pyold && c2) {
45 // erase line at old position
46 gVirtualX->DrawLine(pxmin, pyold, pxmax, pyold);
47 gPad->SetUniqueID(0);
48 }
49
50 TH2 *h = dynamic_cast<TH2 *>(select);
51 if(!h) return;
52
53 //erase old position and draw a line at current position
54 gVirtualX->DrawLine(pxmin, py, pxmax, py);
55 gPad->SetUniqueID(py);
56
57 Float_t upy = gPad->AbsPixeltoY(py);
58 Float_t y = gPad->PadtoY(upy);
59
60 //create or set the new canvas c2
61 auto padsav = gPad;
62 if(c2) delete c2->GetPrimitive("Projection");
63 else c2 = new TCanvas("c2","Projection Canvas",710,10,700,500);
64 c2->SetGrid();
65 c2->cd();
66
67 //draw slice corresponding to mouse position
68 Int_t biny = h->GetYaxis()->FindBin(y);
69 TH1D *hp = h->ProjectionX("",biny,biny);
70 hp->SetName("Projection");
71 hp->SetTitle(TString::Format("Projection of biny=%d",biny));
72 hp->Fit("gaus","ql");
73 c2->Update();
74
75 padsav->cd();
76}
#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 gPad
Definition: TVirtualPad.h:288
#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