DynamicSlice.C

Go to the documentation of this file.
00001 // Show the slice of a TH2 following the mouse position
00002 void DynamicSlice()
00003 {
00004    // Create a new canvas.
00005    c1 = new TCanvas("c1","Dynamic Slice Example",10,10,700,500);
00006    c1->SetFillColor(42);
00007    c1->SetFrameFillColor(33);
00008   
00009    //create a 2-d histogram, fill and draw it
00010    TH2F *hpxpy  = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
00011    hpxpy->SetStats(0);
00012    Double_t px,py;
00013    for (Int_t i = 0; i < 50000; i++) {
00014       gRandom->Rannor(px,py);
00015       hpxpy->Fill(px,py);
00016    }
00017    hpxpy->Draw("col");
00018    
00019    //Add a TExec object to the canvas
00020    c1->AddExec("dynamic","DynamicExec()");
00021 }
00022 
00023 void DynamicExec()
00024 {
00025    // Example of function called when a mouse event occurs in a pad.
00026    // When moving the mouse in the canvas, a second canvas shows the
00027    // projection along X of the bin corresponding to the Y position
00028    // of the mouse. The resulting histogram is fitted with a gaussian.
00029    // A "dynamic" line shows the current bin position in Y.
00030    // This more elaborated example can be used as a starting point
00031    // to develop more powerful interactive applications exploiting CINT
00032    // as a development engine.
00033    //
00034    // Author:  Rene Brun
00035    
00036    TObject *select = gPad->GetSelected();
00037    if(!select) return;
00038    if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;}
00039    TH2 *h = (TH2*)select;
00040    gPad->GetCanvas()->FeedbackMode(kTRUE);
00041 
00042    //erase old position and draw a line at current position
00043    int pyold = gPad->GetUniqueID();
00044    int px = gPad->GetEventX();
00045    int py = gPad->GetEventY();
00046    float uxmin = gPad->GetUxmin();
00047    float uxmax = gPad->GetUxmax();
00048    int pxmin = gPad->XtoAbsPixel(uxmin);
00049    int pxmax = gPad->XtoAbsPixel(uxmax);
00050    if(pyold) gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold);
00051    gVirtualX->DrawLine(pxmin,py,pxmax,py);
00052    gPad->SetUniqueID(py);
00053    Float_t upy = gPad->AbsPixeltoY(py);
00054    Float_t y = gPad->PadtoY(upy);
00055 
00056    //create or set the new canvas c2
00057    TVirtualPad *padsav = gPad;
00058    TCanvas *c2 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c2");
00059    if(c2) delete c2->GetPrimitive("Projection");
00060    else   c2 = new TCanvas("c2","Projection Canvas",710,10,700,500);
00061    c2->SetGrid();
00062    c2->cd();
00063 
00064    //draw slice corresponding to mouse position
00065    Int_t biny = h->GetYaxis()->FindBin(y);
00066    TH1D *hp = h->ProjectionX("",biny,biny);
00067    hp->SetFillColor(38);
00068    char title[80];
00069    sprintf(title,"Projection of biny=%d",biny);
00070    hp->SetName("Projection");
00071    hp->SetTitle(title);
00072    hp->Fit("gaus","ql");
00073    hp->GetFunction("gaus")->SetLineColor(kRed);
00074    hp->GetFunction("gaus")->SetLineWidth(6);
00075    c2->Update();
00076    padsav->cd();
00077 }

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