Logo ROOT   6.10/09
Reference Guide
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 /// \author Rene Brun
10 
11 
12 void 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 
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  TObject *select = gPad->GetSelected();
43  if(!select) return;
44  if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
45  TH2 *h = (TH2*)select;
46  gPad->GetCanvas()->FeedbackMode(kTRUE);
47 
48  //erase old position and draw a line at current position
49  int pyold = gPad->GetUniqueID();
50  int px = gPad->GetEventX();
51  int py = gPad->GetEventY();
52  float uxmin = gPad->GetUxmin();
53  float uxmax = gPad->GetUxmax();
54  int pxmin = gPad->XtoAbsPixel(uxmin);
55  int pxmax = gPad->XtoAbsPixel(uxmax);
56  if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold);
57  gVirtualX->DrawLine(pxmin,py,pxmax,py);
58  gPad->SetUniqueID(py);
59  Float_t upy = gPad->AbsPixeltoY(py);
60  Float_t y = gPad->PadtoY(upy);
61 
62  //create or set the new canvas c2
63  TVirtualPad *padsav = gPad;
64  TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
65  if(c2) delete c2->GetPrimitive("Projection");
66  else c2 = new TCanvas("c2","Projection Canvas",710,10,700,500);
67  c2->SetGrid();
68  c2->cd();
69 
70  //draw slice corresponding to mouse position
71  Int_t biny = h->GetYaxis()->FindBin(y);
72  TH1D *hp = h->ProjectionX("",biny,biny);
73  hp->SetFillColor(38);
74  char title[80];
75  sprintf(title,"Projection of biny=%d",biny);
76  hp->SetName("Projection");
77  hp->SetTitle(title);
78  hp->Fit("gaus","ql");
79  hp->GetFunction("gaus")->SetLineColor(kRed);
80  hp->GetFunction("gaus")->SetLineWidth(6);
81  c2->Update();
82  padsav->cd();
83 }
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
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:460
float Float_t
Definition: RtypesCore.h:53
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8163
return c1
Definition: legend1.C:41
Definition: Rtypes.h:56
TH1 * h
Definition: legend2.C:5
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:679
TH1D * ProjectionX(const char *name="_px", Int_t firstybin=0, Int_t lastybin=-1, Option_t *option="") const
Project a 2-D histogram into a 1-D histogram along X.
Definition: TH2.cxx:2288
#define gROOT
Definition: TROOT.h:375
int Int_t
Definition: RtypesCore.h:41
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
void Class()
Definition: Class.C:29
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:323
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:49
virtual void AddExec(const char *name, const char *command)
Add a new TExec object to the list of Execs.
Definition: TPad.cxx:417
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
Service class for 2-Dim histogram classes.
Definition: TH2.h:30
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:436
virtual TObject * GetPrimitive(const char *name) const
Get primitive.
Definition: TPad.cxx:2818
TAxis * GetYaxis()
Definition: TH1.h:301
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
tomato 1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:594
#define gVirtualX
Definition: TVirtualX.h:350
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:279
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8073
The Canvas class.
Definition: TCanvas.h:31
return c2
Definition: legend2.C:14
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
Mother of all ROOT objects.
Definition: TObject.h:37
#define gPad
Definition: TVirtualPad.h:284
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6028
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2208
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:317
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:3564
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8103