ROOT logo

From $ROOTSYS/tutorials/hist/exec2.C

// echo object at mouse position and show a graphics line
void exec2()
{
   //example of macro called when a mouse event occurs in a pad.
   // Example:
   // Root > TFile f("hsimple.root");
   // Root > hpxpy.Draw();
   // Root > c1.AddExec("ex2",".x exec2.C");
   // 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 CINT
   // as a development engine.
   //Author: Rene Brun

   TObject *select = gPad->GetSelected();
   if(!select) return;
   if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
   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");
   c2->cd();

   //draw slice corresponding to mouse position
   TH2 *h = (TH2*)select;
   Int_t biny = h->GetYaxis()->FindBin(y);
   TH1D *hp = h->ProjectionX("",biny,biny);
   char title[80];
   sprintf(title,"Projection of biny=%d",biny);
   hp->SetName("Projection");
   hp->SetTitle(title);
   hp->Fit("gaus","ql");
   c2->Update();
   padsav->cd();
}
 exec2.C:1
 exec2.C:2
 exec2.C:3
 exec2.C:4
 exec2.C:5
 exec2.C:6
 exec2.C:7
 exec2.C:8
 exec2.C:9
 exec2.C:10
 exec2.C:11
 exec2.C:12
 exec2.C:13
 exec2.C:14
 exec2.C:15
 exec2.C:16
 exec2.C:17
 exec2.C:18
 exec2.C:19
 exec2.C:20
 exec2.C:21
 exec2.C:22
 exec2.C:23
 exec2.C:24
 exec2.C:25
 exec2.C:26
 exec2.C:27
 exec2.C:28
 exec2.C:29
 exec2.C:30
 exec2.C:31
 exec2.C:32
 exec2.C:33
 exec2.C:34
 exec2.C:35
 exec2.C:36
 exec2.C:37
 exec2.C:38
 exec2.C:39
 exec2.C:40
 exec2.C:41
 exec2.C:42
 exec2.C:43
 exec2.C:44
 exec2.C:45
 exec2.C:46
 exec2.C:47
 exec2.C:48
 exec2.C:49
 exec2.C:50
 exec2.C:51
 exec2.C:52
 exec2.C:53
 exec2.C:54
 exec2.C:55
 exec2.C:56