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
25void hist058_TExec_th2()
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 TH2 *h = dynamic_cast<TH2 *>(gPad->GetSelected());
33 if (!h)
34 return;
35
36 Double_t py = gPad->AbsPixeltoY(gPad->GetEventY());
37 Double_t xmin = gPad->GetUxmin();
38 Double_t xmax = gPad->GetUxmax();
39 Double_t ymin = gPad->GetUymin();
40 Double_t ymax = gPad->GetUymax();
41 Int_t biny = h->GetYaxis()->FindBin(py);
42
43 if ((py >= ymin) && (py <= ymax)) {
44 ymin = h->GetYaxis()->GetBinLowEdge(biny);
45 ymax = h->GetYaxis()->GetBinUpEdge(biny);
46 } else {
47 xmax = xmin; // paint dummy rect to remove previous one
48 ymax = ymin;
49 biny = -1;
50 }
51
52 gPad->PaintBox(xmin, ymin, xmax, ymax, "ilhist058");
53
54 gPad->Update();
55
56 if (biny < 0)
57 return;
58
59 auto c2 = static_cast<TCanvas *>(gROOT->GetListOfCanvases()->FindObject("c2"));
60
61 // remember active pad and restore it when leave function
63
64 // create or set the new canvas c2
65 if (c2)
66 delete c2->GetPrimitive("Projection");
67 else
68 c2 = new TCanvas("c2", "Projection Canvas", 710, 10, 700, 500);
69 c2->SetGrid();
70 c2->cd();
71
72 // draw slice corresponding to mouse position
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}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
float xmin
float ymin
float xmax
float ymax
#define gROOT
Definition TROOT.h:417
#define gPad
bool Update(const std::shared_ptr< ROOT::Math::Minimizer > &min, const ROOT::Fit::FitConfig &fconfig, bool isValid, unsigned int ncalls=0)
Update the fit result with a new minimization status To be run only if same fit is performed with sam...
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:956
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6932
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:4050
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:9193
Service class for 2-D histogram classes.
Definition TH2.h:30
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:58
return c2
Definition legend2.C:14