From $ROOTSYS/tutorials/hist/DynamicSlice.C

// Show the slice of a TH2 following the mouse position
void DynamicSlice()
{
   // Create a new canvas.
   c1 = new TCanvas("c1","Dynamic Slice Example",10,10,700,500);
   c1->SetFillColor(42);
   c1->SetFrameFillColor(33);

   //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 CINT
   // as a development engine.
   //
   // Author:  Rene Brun

   TObject *select = gPad->GetSelected();
   if(!select) return;
   if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
   TH2 *h = (TH2*)select;
   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","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);
   char title[80];
   sprintf(title,"Projection of biny=%d",biny);
   hp->SetName("Projection");
   hp->SetTitle(title);
   hp->Fit("gaus","ql");
   hp->GetFunction("gaus")->SetLineColor(kRed);
   hp->GetFunction("gaus")->SetLineWidth(6);
   c2->Update();
   padsav->cd();
}
 DynamicSlice.C:1
 DynamicSlice.C:2
 DynamicSlice.C:3
 DynamicSlice.C:4
 DynamicSlice.C:5
 DynamicSlice.C:6
 DynamicSlice.C:7
 DynamicSlice.C:8
 DynamicSlice.C:9
 DynamicSlice.C:10
 DynamicSlice.C:11
 DynamicSlice.C:12
 DynamicSlice.C:13
 DynamicSlice.C:14
 DynamicSlice.C:15
 DynamicSlice.C:16
 DynamicSlice.C:17
 DynamicSlice.C:18
 DynamicSlice.C:19
 DynamicSlice.C:20
 DynamicSlice.C:21
 DynamicSlice.C:22
 DynamicSlice.C:23
 DynamicSlice.C:24
 DynamicSlice.C:25
 DynamicSlice.C:26
 DynamicSlice.C:27
 DynamicSlice.C:28
 DynamicSlice.C:29
 DynamicSlice.C:30
 DynamicSlice.C:31
 DynamicSlice.C:32
 DynamicSlice.C:33
 DynamicSlice.C:34
 DynamicSlice.C:35
 DynamicSlice.C:36
 DynamicSlice.C:37
 DynamicSlice.C:38
 DynamicSlice.C:39
 DynamicSlice.C:40
 DynamicSlice.C:41
 DynamicSlice.C:42
 DynamicSlice.C:43
 DynamicSlice.C:44
 DynamicSlice.C:45
 DynamicSlice.C:46
 DynamicSlice.C:47
 DynamicSlice.C:48
 DynamicSlice.C:49
 DynamicSlice.C:50
 DynamicSlice.C:51
 DynamicSlice.C:52
 DynamicSlice.C:53
 DynamicSlice.C:54
 DynamicSlice.C:55
 DynamicSlice.C:56
 DynamicSlice.C:57
 DynamicSlice.C:58
 DynamicSlice.C:59
 DynamicSlice.C:60
 DynamicSlice.C:61
 DynamicSlice.C:62
 DynamicSlice.C:63
 DynamicSlice.C:64
 DynamicSlice.C:65
 DynamicSlice.C:66
 DynamicSlice.C:67
 DynamicSlice.C:68
 DynamicSlice.C:69
 DynamicSlice.C:70
 DynamicSlice.C:71
 DynamicSlice.C:72
 DynamicSlice.C:73
 DynamicSlice.C:74
 DynamicSlice.C:75
 DynamicSlice.C:76
 DynamicSlice.C:77
 DynamicSlice.C:78