Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist105_TExec_dynamic_slice.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Show the slice of a TH2 following the mouse position.

void hist105_TExec_dynamic_slice()
{
// 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.
static int pyold = 0;
gPad->FeedbackMode(kTRUE);
int px = gPad->GetEventX();
int py = gPad->GetEventY();
float uxmin = gPad->GetUxmin();
float uxmax = gPad->GetUxmax();
auto c2 = static_cast<TCanvas *>(gROOT->GetListOfCanvases()->FindObject("c2"));
TH2 *h = dynamic_cast<TH2 *>(select);
if (!h)
return;
if (pyold) {
// erase line at old position
Float_t upyold = gPad->AbsPixeltoY(pyold);
gPad->PaintLine(uxmin, upyold, uxmax, upyold);
pyold = 0;
}
Float_t upy = gPad->AbsPixeltoY(py);
Float_t y = gPad->PadtoY(upy);
// draw a line at current position
gPad->GetPainter()->DrawLine(uxmin, upy, uxmax, upy);
// remember position of last painted line
pyold = py;
// remember active pad and restore it when leave function
// create or set the new canvas 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);
hp->SetName("Projection");
hp->SetTitle(TString::Format("Projection of biny=%d", biny));
hp->Fit("gaus", "ql");
hp->GetFunction("gaus")->SetLineColor(kRed);
hp->GetFunction("gaus")->SetLineWidth(6);
c2->Update();
}
#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
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
@ kRed
Definition Rtypes.h:66
#define gROOT
Definition TROOT.h:417
R__EXTERN TRandom * gRandom
Definition TRandom.h:73
#define gPad
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
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 Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3193
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition TH1.cxx:9281
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:9193
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:9223
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
Service class for 2-D histogram classes.
Definition TH2.h:30
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:368
Mother of all ROOT objects.
Definition TObject.h:42
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
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:63
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
return c2
Definition legend2.C:14
Date
November 2022
Authors
Rene Brun, Sergey Linev

Definition in file hist105_TExec_dynamic_slice.C.