/* * The modified version of original DynamicSlice.C macro for gamma-gamma matrices. * The X & Y projections corresponding to the cursor position are calculated * and displayed on two canvases. The peaks above "threshold" are labeled * with their energies. It is assumed that the 2D-histogram has 0.5 keV/bin * calibration. * * * Dr. Ajay Y. Deo * Dept. of Physics, * IIT Roorkee, INDIA * ajay.deo@ph.iitr.ac.in * * July 2, 2020 * * * To run this macro: * First load the root file containing 2D-Histogram * and then execute it as: * * root[].x DynamicSlice_GG_v1.C * * */ void DynamicSlice_GG_v1() { // 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(); gPad->SetCrosshair(); } 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(/Y) of the bin corresponding to the Y(/X) position // of the mouse. The crosshair cursor shows the current bin position in Y(/X). TObject *select = gPad->GetSelected(); if(!select) return; if (!select->InheritsFrom(TH2::Class())) {gPad->SetUniqueID(0); return;} TH2 *h = (TH2*)select; gPad->GetCanvas()->FeedbackMode(kTRUE); int px = gPad->GetEventX(); int py = gPad->GetEventY(); 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,"X-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); 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); c2->Update(); //padsav->cd(); //create or set the new canvas c3 //TVirtualPad *padsav2 = gPad; 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(title2,"Y-Projection of %d keV from %d to %d", (binx/2), ((binx-4)/2), ((binx+4)/2)); hpy->SetName("yProjection"); hpy->SetTitle(title2); hpy->Draw(); Int_t npeaks2 = 100; Double_t threshold2 = 0.1; TSpectrum *s2 = new TSpectrum(2*npeaks2); Int_t nfound2 = s2->Search(hpy,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); 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(); //padsav2->cd(); }