Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DynamicSlice.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook -js
4/// Show the slice of a TH2 following the mouse position.
5///
6/// \macro_image
7/// \macro_code
8///
9/// \authors Rene Brun, Sergey Linev
10
11
12void DynamicSlice()
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
31void 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 float uxmin = gPad->GetUxmin();
45 float uxmax = gPad->GetUxmax();
46 int pxmin = gPad->XtoAbsPixel(uxmin);
47 int pxmax = gPad->XtoAbsPixel(uxmax);
48 int px = gPad->GetEventX();
49 int py = gPad->GetEventY();
50 TObject *select = gPad->GetSelected();
51
52 gPad->GetCanvas()->FeedbackMode(kTRUE);
53 if (pyold) {
54 // erase line at old position
55 gVirtualX->DrawLine(pxmin, pyold, pxmax, pyold);
56 pyold = 0;
57 }
58
59 if(!select || !select->InheritsFrom(TH2::Class()))
60 return;
61
62 TH2 *h = (TH2*)select;
63
64 // draw a line at current position
65 gVirtualX->DrawLine(pxmin, py, pxmax, py);
66 pyold = py;
67
68 Float_t upy = gPad->AbsPixeltoY(py);
69 Float_t y = gPad->PadtoY(upy);
70
71 //create or set the new canvas c2
72 TVirtualPad *padsav = gPad;
73 TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
74 if(c2) delete c2->GetPrimitive("Projection");
75 else c2 = new TCanvas("c2","Projection Canvas",710,10,700,500);
76 c2->SetGrid();
77 c2->cd();
78
79 //draw slice corresponding to mouse position
80 Int_t biny = h->GetYaxis()->FindBin(y);
81 TH1D *hp = h->ProjectionX("",biny,biny);
82 hp->SetFillColor(38);
83 hp->SetName("Projection");
84 hp->SetTitle(TString::Format("Projection of biny=%d",biny));
85 hp->Fit("gaus","ql");
86 hp->GetFunction("gaus")->SetLineColor(kRed);
87 hp->GetFunction("gaus")->SetLineWidth(6);
88 c2->Update();
89 padsav->cd();
90}
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
@ kRed
Definition Rtypes.h:66
#define gROOT
Definition TROOT.h:407
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
#define gVirtualX
Definition TVirtualX.h:338
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)}
Definition TH1.h:620
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6707
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:3901
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3067
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition TH1.cxx:8968
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:8877
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition TH1.cxx:8907
2-D histogram with a float per channel (see TH1 documentation)}
Definition TH2.h:258
Service class for 2-D histogram classes.
Definition TH2.h:30
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:347
static TClass * Class()
Mother of all ROOT objects.
Definition TObject.h:41
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:525
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:2604
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:500
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:2356
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
return c2
Definition legend2.C:14