ROOT
master
Reference Guide
Loading...
Searching...
No Matches
hist105_TExec_dynamic_slice.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_hist
3
/// \notebook -js
4
/// \preview Show the slice of a TH2 following the mouse position.
5
///
6
/// \macro_image
7
/// \macro_code
8
///
9
/// \date November 2022
10
/// \authors Rene Brun, Sergey Linev
11
12
void
hist105_TExec_dynamic_slice()
13
{
14
// Create a new canvas.
15
TCanvas
*
c1
=
new
TCanvas
(
"c1"
,
"Dynamic Slice Example"
, 10, 10, 700, 500);
16
17
// create a 2-d histogram, fill and draw it
18
TH2F
*
hpxpy
=
new
TH2F
(
"hpxpy"
,
"py vs px"
, 40, -4, 4, 40, -4, 4);
19
hpxpy
->SetStats(0);
20
Double_t
px, py;
21
for
(
Int_t
i = 0; i < 50000; i++) {
22
gRandom
->
Rannor
(px, py);
23
hpxpy
->Fill(px, py);
24
}
25
hpxpy
->Draw(
"col"
);
26
27
// Add a TExec object to the canvas
28
c1
->AddExec(
"dynamic"
,
"DynamicExec()"
);
29
}
30
31
void
DynamicExec
()
32
{
33
// Example of function called when a mouse event occurs in a pad.
34
// When moving the mouse in the canvas, a second canvas shows the
35
// projection along X of the bin corresponding to the Y position
36
// of the mouse. The resulting histogram is fitted with a gaussian.
37
// A "dynamic" line shows the current bin position in Y.
38
// This more elaborated example can be used as a starting point
39
// to develop more powerful interactive applications exploiting Cling
40
// as a development engine.
41
42
static
int
pyold = 0;
43
44
gPad
->FeedbackMode(
kTRUE
);
45
int
px =
gPad
->GetEventX();
46
int
py =
gPad
->GetEventY();
47
TObject
*select =
gPad->GetSelected();
48
float
uxmin
=
gPad
->GetUxmin();
49
float
uxmax
=
gPad
->GetUxmax();
50
auto
c2
=
static_cast<
TCanvas
*
>
(
gROOT
->GetListOfCanvases()->FindObject(
"c2"
));
51
52
TH2
*
h
=
dynamic_cast<
TH2
*
>
(select);
53
if
(!
h
)
54
return
;
55
56
if
(pyold) {
57
// erase line at old position
58
Float_t
upyold
=
gPad
->AbsPixeltoY(pyold);
59
gPad
->PaintLine(
uxmin
,
upyold
,
uxmax
,
upyold
);
60
pyold = 0;
61
}
62
63
Float_t
upy
=
gPad
->AbsPixeltoY(py);
64
Float_t
y
=
gPad
->PadtoY(
upy
);
65
66
// draw a line at current position
67
gPad
->GetPainter()->DrawLine(
uxmin
,
upy
,
uxmax
,
upy
);
68
69
// remember position of last painted line
70
pyold = py;
71
72
// remember active pad and restore it when leave function
73
TVirtualPad::TContext
ctxt
;
74
75
// create or set the new canvas c2
76
if
(
c2
)
77
delete
c2
->GetPrimitive(
"Projection"
);
78
else
79
c2
=
new
TCanvas
(
"c2"
,
"Projection Canvas"
, 710, 10, 700, 500);
80
c2
->SetGrid();
81
c2
->cd();
82
83
// draw slice corresponding to mouse position
84
Int_t
biny
=
h
->GetYaxis()->FindBin(
y
);
85
TH1D
*
hp
=
h
->ProjectionX(
""
,
biny
,
biny
);
86
hp
->SetFillColor(38);
87
hp
->SetName(
"Projection"
);
88
hp
->SetTitle(
TString::Format
(
"Projection of biny=%d"
,
biny
));
89
hp
->Fit(
"gaus"
,
"ql"
);
90
hp
->GetFunction(
"gaus"
)->SetLineColor(
kRed
);
91
hp
->GetFunction(
"gaus"
)->SetLineWidth(6);
92
c2
->Update();
93
}
h
#define h(i)
Definition
RSha256.hxx:106
Int_t
int Int_t
Signed integer 4 bytes (int)
Definition
RtypesCore.h:60
Float_t
float Float_t
Float 4 bytes (float)
Definition
RtypesCore.h:72
Double_t
double Double_t
Double 8 bytes.
Definition
RtypesCore.h:74
kTRUE
constexpr Bool_t kTRUE
Definition
RtypesCore.h:108
kRed
@ kRed
Definition
Rtypes.h:66
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:359
gROOT
#define gROOT
Definition
TROOT.h:417
gRandom
R__EXTERN TRandom * gRandom
Definition
TRandom.h:73
gPad
#define gPad
Definition
TVirtualPad.h:337
ROOT::Detail::TRangeCast
Definition
TCollection.h:312
TCanvas
The Canvas class.
Definition
TCanvas.h:23
TH1D
1-D histogram with a double per channel (see TH1 documentation)
Definition
TH1.h:926
TH2F
2-D histogram with a float per channel (see TH1 documentation)
Definition
TH2.h:345
TH2
Service class for 2-D histogram classes.
Definition
TH2.h:30
TObject
Mother of all ROOT objects.
Definition
TObject.h:42
TRandom::Rannor
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition
TRandom.cxx:506
TString::Format
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
TVirtualPad::TContext
small helper class to store/restore gPad context in TPad methods
Definition
TVirtualPad.h:61
y
Double_t y[n]
Definition
legend1.C:17
c1
return c1
Definition
legend1.C:41
c2
return c2
Definition
legend2.C:14
df101_h1Analysis.select
select(rdf)
Definition
df101_h1Analysis.py:37
hist105_TExec_dynamic_slice
Definition
hist105_TExec_dynamic_slice.py:1
tutorials
hist
hist105_TExec_dynamic_slice.C
ROOTmaster - Reference Guide Generated on Fri Oct 2 2026 04:42:52 (GVA Time) using Doxygen 1.10.0