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