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 gPad->FeedbackMode(kTRUE);
33
34 int px = gPad->GetEventX();
35 int py = gPad->GetEventY();
37 float uxmin = gPad->GetUxmin();
38 float uxmax = gPad->GetUxmax();
39 auto c2 = static_cast<TCanvas *>(gROOT->GetListOfCanvases()->FindObject("c2"));
40
41 TH2 *h = dynamic_cast<TH2 *>(select);
42 if (!h)
43 return;
44
45 // misuse of pad uniqueid for last paint position
46 int pyold = gPad->GetUniqueID();
47 gPad->SetUniqueID(0);
48
49 if (pyold && c2) {
50 // erase line at old position
51 Float_t upyold = gPad->AbsPixeltoY(pyold);
52 gPad->PaintLine(uxmin, upyold, uxmax, upyold);
53 }
54
55 Float_t upy = gPad->AbsPixeltoY(py);
56 Float_t y = gPad->PadtoY(upy);
57
58 // paint a line at current position
59 gPad->PaintLine(uxmin, upy, uxmax, upy);
60
61 // remember last paint position, misuse of pad uniqueid
62 gPad->SetUniqueID(py);
63
64 // remember active pad and restore it when leave function
66
67 // create or set the new canvas c2
68 if (c2)
69 delete c2->GetPrimitive("Projection");
70 else
71 c2 = new TCanvas("c2", "Projection Canvas", 710, 10, 700, 500);
72 c2->SetGrid();
73 c2->cd();
74
75 // draw slice corresponding to mouse position
76 Int_t biny = h->GetYaxis()->FindBin(y);
77 TH1D *hp = h->ProjectionX("", biny, biny);
78 hp->SetName("Projection");
79 hp->SetTitle(TString::Format("Projection of biny=%d", biny));
80 hp->Fit("gaus", "ql");
81 c2->Update();
82}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
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:208
#define gROOT
Definition TROOT.h:417
#define gPad
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
Service class for 2-D histogram classes.
Definition TH2.h:30
Mother of all ROOT objects.
Definition TObject.h:42
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:2460
small helper class to store/restore gPad context in TPad methods
Definition TVirtualPad.h:61
Double_t y[n]
Definition legend1.C:17
return c2
Definition legend2.C:14