ROOT
v6-22
Reference Guide
DynamicSlice.C File Reference
Tutorials
»
Histograms tutorials
Detailed Description
Show the slice of a
TH2
following the mouse position.
void
DynamicSlice
()
{
// Create a new canvas.
TCanvas
*
c1
=
new
TCanvas
(
"c1"
,
"Dynamic Slice Example"
,10,10,700,500);
//create a 2-d histogram, fill and draw it
TH2F
*hpxpy =
new
TH2F
(
"hpxpy"
,
"py vs px"
,40,-4,4,40,-4,4);
hpxpy->
SetStats
(0);
Double_t
px,py;
for
(
Int_t
i = 0; i < 50000; i++) {
gRandom
->
Rannor
(px,py);
hpxpy->
Fill
(px,py);
}
hpxpy->
Draw
(
"col"
);
//Add a TExec object to the canvas
c1
->AddExec(
"dynamic"
,
"DynamicExec()"
);
}
void
DynamicExec()
{
// Example of function called when a mouse event occurs in a pad.
// When moving the mouse in the canvas, a second canvas shows the
// projection along X of the bin corresponding to the Y position
// of the mouse. The resulting histogram is fitted with a gaussian.
// A "dynamic" line shows the current bin position in Y.
// This more elaborated example can be used as a starting point
// to develop more powerful interactive applications exploiting Cling
// as a development engine.
TObject
*select =
gPad
->GetSelected();
if
(!select)
return
;
if
(!select->
InheritsFrom
(
TH2::Class
())) {
gPad
->SetUniqueID(0);
return
;}
TH2
*
h
= (
TH2
*)select;
gPad
->GetCanvas()->FeedbackMode(
kTRUE
);
//erase old position and draw a line at current position
int
pyold =
gPad
->GetUniqueID();
int
px =
gPad
->GetEventX();
int
py =
gPad
->GetEventY();
float
uxmin =
gPad
->GetUxmin();
float
uxmax =
gPad
->GetUxmax();
int
pxmin =
gPad
->XtoAbsPixel(uxmin);
int
pxmax =
gPad
->XtoAbsPixel(uxmax);
if
(pyold)
gVirtualX
->DrawLine(pxmin,pyold,pxmax,pyold);
gVirtualX
->DrawLine(pxmin,py,pxmax,py);
gPad
->SetUniqueID(py);
Float_t
upy =
gPad
->AbsPixeltoY(py);
Float_t
y
=
gPad
->PadtoY(upy);
//create or set the new canvas c2
TVirtualPad
*padsav =
gPad
;
TCanvas
*
c2
= (
TCanvas
*)
gROOT
->GetListOfCanvases()->FindObject(
"c2"
);
if
(
c2
)
delete
c2
->GetPrimitive(
"Projection"
);
else
c2
=
new
TCanvas
(
"c2"
,
"Projection Canvas"
,710,10,700,500);
c2
->SetGrid();
c2
->cd();
//draw slice corresponding to mouse position
Int_t
biny =
h
->GetYaxis()->FindBin(
y
);
TH1D
*hp =
h
->ProjectionX(
""
,biny,biny);
hp->
SetFillColor
(38);
char
title[80];
sprintf(title,
"Projection of biny=%d"
,biny);
hp->
SetName
(
"Projection"
);
hp->
SetTitle
(title);
hp->
Fit
(
"gaus"
,
"ql"
);
hp->
GetFunction
(
"gaus"
)->
SetLineColor
(
kRed
);
hp->
GetFunction
(
"gaus"
)->
SetLineWidth
(6);
c2
->Update();
padsav->
cd
();
}
Class
void Class()
Definition:
Class.C:29
h
#define h(i)
Definition:
RSha256.hxx:106
Int_t
int Int_t
Definition:
RtypesCore.h:43
Double_t
double Double_t
Definition:
RtypesCore.h:57
Float_t
float Float_t
Definition:
RtypesCore.h:55
kTRUE
const Bool_t kTRUE
Definition:
RtypesCore.h:89
kRed
@ kRed
Definition:
Rtypes.h:64
gROOT
#define gROOT
Definition:
TROOT.h:406
gRandom
R__EXTERN TRandom * gRandom
Definition:
TRandom.h:62
gPad
#define gPad
Definition:
TVirtualPad.h:287
gVirtualX
#define gVirtualX
Definition:
TVirtualX.h:338
TAttFill::SetFillColor
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition:
TAttFill.h:37
TAttLine::SetLineWidth
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition:
TAttLine.h:43
TAttLine::SetLineColor
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition:
TAttLine.h:40
TCanvas
The Canvas class.
Definition:
TCanvas.h:27
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition:
TH1.h:614
TH1::SetTitle
virtual void SetTitle(const char *title)
See GetStatOverflows for more information.
Definition:
TH1.cxx:6345
TH1::Fit
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:3808
TH1::GetFunction
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition:
TH1.cxx:8507
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition:
TH1.cxx:2998
TH1::SetName
virtual void SetName(const char *name)
Change the name of this histogram.
Definition:
TH1.cxx:8416
TH1::SetStats
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition:
TH1.cxx:8446
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition:
TH2.h:251
TH2
Service class for 2-Dim histogram classes.
Definition:
TH2.h:30
TH2::Fill
Int_t Fill(Double_t)
Invalid Fill method.
Definition:
TH2.cxx:294
TObject
Mother of all ROOT objects.
Definition:
TObject.h:37
TObject::InheritsFrom
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition:
TObject.cxx:443
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:489
TVirtualPad
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition:
TVirtualPad.h:51
TVirtualPad::cd
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
c1
return c1
Definition:
legend1.C:41
y
Double_t y[n]
Definition:
legend1.C:17
c2
return c2
Definition:
legend2.C:14
DynamicSlice
Definition:
DynamicSlice.py:1
Author
Rene Brun
Definition in file
DynamicSlice.C
.
tutorials
hist
DynamicSlice.C
ROOT v6-22 - Reference Guide Generated on Fri Apr 1 2022 11:54:18 (GVA Time) using Doxygen 1.9.4