exec2.C

Go to the documentation of this file.
00001 // echo object at mouse position and show a graphics line
00002 void exec2()
00003 {
00004    //example of macro called when a mouse event occurs in a pad.
00005    // Example:
00006    // Root > TFile f("hsimple.root");
00007    // Root > hpxpy.Draw();
00008    // Root > c1.AddExec("ex2",".x exec2.C");
00009    // When moving the mouse in the canvas, a second canvas shows the
00010    // projection along X of the bin corresponding to the Y position
00011    // of the mouse. The resulting histogram is fitted with a gaussian.
00012    // A "dynamic" line shows the current bin position in Y.
00013    // This more elaborated example can be used as a starting point
00014    // to develop more powerful interactive applications exploiting CINT
00015    // as a development engine.
00016    //Author: Rene Brun
00017 
00018    TObject *select = gPad->GetSelected();
00019    if(!select) return;
00020    if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
00021    gPad->GetCanvas()->FeedbackMode(kTRUE);
00022 
00023    //erase old position and draw a line at current position
00024    int pyold = gPad->GetUniqueID();
00025    int px = gPad->GetEventX();
00026    int py = gPad->GetEventY();
00027    float uxmin = gPad->GetUxmin();
00028    float uxmax = gPad->GetUxmax();
00029    int pxmin = gPad->XtoAbsPixel(uxmin);
00030    int pxmax = gPad->XtoAbsPixel(uxmax);
00031    if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold);
00032    gVirtualX->DrawLine(pxmin,py,pxmax,py);
00033    gPad->SetUniqueID(py);
00034    Float_t upy = gPad->AbsPixeltoY(py);
00035    Float_t y = gPad->PadtoY(upy);
00036 
00037    //create or set the new canvas c2
00038    TVirtualPad *padsav = gPad;
00039    TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
00040    if(c2) delete c2->GetPrimitive("Projection");
00041    else   c2 = new TCanvas("c2");
00042    c2->cd();
00043 
00044    //draw slice corresponding to mouse position
00045    TH2 *h = (TH2*)select;
00046    Int_t biny = h->GetYaxis()->FindBin(y);
00047    TH1D *hp = h->ProjectionX("",biny,biny);
00048    char title[80];
00049    sprintf(title,"Projection of biny=%d",biny);
00050    hp->SetName("Projection");
00051    hp->SetTitle(title);
00052    hp->Fit("gaus","ql");
00053    c2->Update();
00054    padsav->cd();
00055 }

Generated on Tue Jul 5 15:44:26 2011 for ROOT_528-00b_version by  doxygen 1.5.1