ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DynamicSlice.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Show the slice of a TH2 following the mouse position.
4 ///
5 /// \macro_image
6 /// \macro_code
7 ///
8 /// \author Rene Brun
9 
10 
11 void DynamicSlice()
12 {
13  // Create a new canvas.
14  c1 = new TCanvas("c1","Dynamic Slice Example",10,10,700,500);
15  c1->SetFillColor(42);
16  c1->SetFrameFillColor(33);
17 
18  //create a 2-d histogram, fill and draw it
19  TH2F *hpxpy = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
20  hpxpy->SetStats(0);
21  Double_t px,py;
22  for (Int_t i = 0; i < 50000; i++) {
23  gRandom->Rannor(px,py);
24  hpxpy->Fill(px,py);
25  }
26  hpxpy->Draw("col");
27 
28  //Add a TExec object to the canvas
29  c1->AddExec("dynamic","DynamicExec()");
30 }
31 
32 void DynamicExec()
33 {
34  // Example of function called when a mouse event occurs in a pad.
35  // When moving the mouse in the canvas, a second canvas shows the
36  // projection along X of the bin corresponding to the Y position
37  // of the mouse. The resulting histogram is fitted with a gaussian.
38  // A "dynamic" line shows the current bin position in Y.
39  // This more elaborated example can be used as a starting point
40  // to develop more powerful interactive applications exploiting Cling
41  // as a development engine.
42 
43  TObject *select = gPad->GetSelected();
44  if(!select) return;
45  if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
46  TH2 *h = (TH2*)select;
47  gPad->GetCanvas()->FeedbackMode(kTRUE);
48 
49  //erase old position and draw a line at current position
50  int pyold = gPad->GetUniqueID();
51  int px = gPad->GetEventX();
52  int py = gPad->GetEventY();
53  float uxmin = gPad->GetUxmin();
54  float uxmax = gPad->GetUxmax();
55  int pxmin = gPad->XtoAbsPixel(uxmin);
56  int pxmax = gPad->XtoAbsPixel(uxmax);
57  if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold);
58  gVirtualX->DrawLine(pxmin,py,pxmax,py);
59  gPad->SetUniqueID(py);
60  Float_t upy = gPad->AbsPixeltoY(py);
61  Float_t y = gPad->PadtoY(upy);
62 
63  //create or set the new canvas c2
64  TVirtualPad *padsav = gPad;
65  TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
66  if(c2) delete c2->GetPrimitive("Projection");
67  else c2 = new TCanvas("c2","Projection Canvas",710,10,700,500);
68  c2->SetGrid();
69  c2->cd();
70 
71  //draw slice corresponding to mouse position
72  Int_t biny = h->GetYaxis()->FindBin(y);
73  TH1D *hp = h->ProjectionX("",biny,biny);
74  hp->SetFillColor(38);
75  char title[80];
76  sprintf(title,"Projection of biny=%d",biny);
77  hp->SetName("Projection");
78  hp->SetTitle(title);
79  hp->Fit("gaus","ql");
80  hp->GetFunction("gaus")->SetLineColor(kRed);
81  hp->GetFunction("gaus")->SetLineWidth(6);
82  c2->Update();
83  padsav->cd();
84 }
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
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
virtual TObject * GetPrimitive(const char *name) const
Get primitive.
Definition: TPad.cxx:2753
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:487
float Float_t
Definition: RtypesCore.h:53
TCanvas * c1
Definition: legend1.C:2
Definition: Rtypes.h:61
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
#define gROOT
Definition: TROOT.h:344
int Int_t
Definition: RtypesCore.h:41
Float_t py
Definition: hprod.C:33
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:326
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:2523
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:59
virtual void AddExec(const char *name, const char *command)
Add a new TExec object to the list of Execs.
Definition: TPad.cxx:378
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
Service class for 2-Dim histogram classes.
Definition: TH2.h:36
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
TAxis * GetYaxis()
Definition: TH1.h:320
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
#define gVirtualX
Definition: TVirtualX.h:362
virtual Int_t FindBin(Double_t x)
Find bin number corresponding to abscissa x.
Definition: TAxis.cxx:264
virtual void SetName(const char *name)
Change the name of this histogram.
Definition: TH1.cxx:8288
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
Mother of all ROOT objects.
Definition: TObject.h:58
Float_t px
Definition: hprod.C:33
#define gPad
Definition: TVirtualPad.h:288
void SetFrameFillColor(Color_t color=1)
Definition: TAttPad.h:83
virtual void SetTitle(const char *title)
Change (i.e.
Definition: TH1.cxx:6268
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
const Bool_t kTRUE
Definition: Rtypes.h:91
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287
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:3607
virtual TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TH1.cxx:8382
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8320