/// \file /// \ingroup tutorial_hist /// \notebook -js /// Show the slice of a TH2 following the mouse position. /// /// \macro_image /// \macro_code /// /// \author Rene Brun void DynamicSlice_GG() { // Create a new canvas. TCanvas* c1 = new TCanvas("c1","Dynamic Slicer",10,10,700,500); //TFile *f = new TFile("gg_DySl.root"); TFile *f = new TFile("test54.root"); //TH2F *his2D_wb = (TH2F*)gROOT->FindObject("his2D_wb"); TH2F *his2D_wb = (TH2F*)gROOT->FindObject("his2D_clean"); his2D_wb->GetXaxis()->SetRangeUser(100., 2000.); his2D_wb->GetYaxis()->SetRangeUser(100., 2000.); Double_t xmin = his2D_wb->GetXaxis()->GetXmin(); Double_t xmax = his2D_wb->GetXaxis()->GetXmax(); Double_t new_xmin = xmin/2; Double_t new_xmax = xmax/2; his2D_wb->GetXaxis()->SetLimits(new_xmin, new_xmax); his2D_wb->GetYaxis()->SetLimits(new_xmin, new_xmax); his2D_wb->Draw("colz"); //Add a TExec object to the canvas c1->AddExec("dynamic","DynamicExec()"); c1->SetLogz(); c1->Update(); } 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 Cling // as a development engine. 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(); float uymin = gPad->GetUymin(); float uymax = gPad->GetUymax(); int pxmin = gPad->XtoAbsPixel(uxmin); int pxmax = gPad->XtoAbsPixel(uxmax); int pymin = gPad->YtoAbsPixel(uymin); int pymax = gPad->YtoAbsPixel(uymax); if(pyold){ gVirtualX->DrawLine(pxmin,pyold,pxmax,pyold); gVirtualX->DrawLine(pxmin,py,pxmax,py); } gVirtualX->DrawLine(px,pymin,px,pymax); gVirtualX->DrawLine(pxmin,py,pxmax,py); px = gPad->GetEventX(); py = gPad->GetEventY(); gPad->SetUniqueID(py); Float_t upy = gPad->AbsPixeltoY(py); Float_t y = gPad->PadtoY(upy); Float_t upx = gPad->AbsPixeltoX(px); Float_t x = gPad->PadtoX(upx); //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","X-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-4,biny+4); hp->SetFillColor(38); char title[80]; //sprintf(title,"Projection of biny=%d",biny); sprintf(title,"Projection of %d keV from %d to %d", (biny/2), ((biny-4)/2), ((biny+4)/2)); hp->SetName("Projection"); hp->SetTitle(title); hp->Draw(); Int_t npeaks = 100; Double_t threshold = 0.1; TSpectrum *s = new TSpectrum(2*npeaks); Int_t nfound = s->Search(hp,3,"",threshold); Double_t *xfound; xfound = s->GetPositionX(); Double_t pPosition[nfound]; Double_t pHeight[nfound]; for(Int_t p=0; pGetXaxis()->FindBin(pPosition[p]); pHeight[p] = hp->GetBinContent(xbin); //cout << pPosition[p] << pHeight[p] << endl; TText *t1 = new TText(pPosition[p]-1,pHeight[p]*0.90,Form("%g",pPosition[p])); t1->SetTextAngle(90); t1->SetTextColor(kBlue); t1->SetTextSize(0.03); t1->Draw(); } TList *functions = hp->GetListOfFunctions(); TPolyMarker *pm = (TPolyMarker*)functions->FindObject("TPolyMarker"); functions->Remove(pm); //gPad->Modified(); //gPad->Update(); c2->Update(); padsav->cd(); //create or set the new canvas c3 TCanvas *c3 = (TCanvas*)gROOT->GetListOfCanvases()->FindObject("c3"); if(c3) delete c3->GetPrimitive("yProjection"); else c3 = new TCanvas("c3","Y-Projection Canvas",710,10,700,500); c3->SetGrid(); c3->cd(); //draw slice corresponding to mouse position Int_t binx = h->GetXaxis()->FindBin(x); TH1D *hpy = h->ProjectionY("",binx-4,binx+4); hpy->SetFillColor(38); char title2[80]; //sprintf(title,"Projection of biny=%d",biny); sprintf(title2,"Y Projection of %d keV from %d to %d", (binx/2), ((binx-4)/2), ((binx+4)/2)); hpy->SetName("yProjection"); hpy->SetTitle(title); hpy->Draw(); Int_t npeaks2 = 100; Double_t threshold2 = 0.1; TSpectrum *s2 = new TSpectrum(2*npeaks2); Int_t nfound2 = s2->Search(hp,3,"",threshold2); Double_t *xfound2; xfound2 = s2->GetPositionX(); Double_t pPosition2[nfound2]; Double_t pHeight2[nfound2]; for(Int_t p=0; pGetXaxis()->FindBin(pPosition2[p]); pHeight2[p] = hpy->GetBinContent(xbin2); //cout << pPosition[p] << pHeight[p] << endl; TText *t2 = new TText(pPosition2[p]-1,pHeight2[p]*0.90,Form("%g",pPosition2[p])); t2->SetTextAngle(90); t2->SetTextColor(kBlue); t2->SetTextSize(0.03); t2->Draw(); } TList *functions2 = hpy->GetListOfFunctions(); TPolyMarker *pm2 = (TPolyMarker*)functions2->FindObject("TPolyMarker"); functions2->Remove(pm2); c3->Update(); }