00001
00002 void DynamicSlice()
00003 {
00004
00005 c1 = new TCanvas("c1","Dynamic Slice Example",10,10,700,500);
00006 c1->SetFillColor(42);
00007 c1->SetFrameFillColor(33);
00008
00009
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
00020 c1->AddExec("dynamic","DynamicExec()");
00021 }
00022
00023 void DynamicExec()
00024 {
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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
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
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 }