Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 CLING
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
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
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
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:671
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6747
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:3906
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:8993
Service class for 2-D histogram classes.
Definition TH2.h:30
Mother of all ROOT objects.
Definition TObject.h:41
virtual UInt_t GetUniqueID() const
Return the unique object id.
Definition TObject.cxx:474
virtual void SetUniqueID(UInt_t uid)
Set the unique object id.
Definition TObject.cxx:809
TObject * FindObject(const char *name) const override
Search if object named name is inside this pad or in pads inside this pad.
Definition TPad.cxx:2700
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