#include #include #include #include #include Int_t MRF_GetColorFromPalette ( TH2 * histo, Double_t z ) { /// http://root.cern.ch/phpBB3/viewtopic.php?f=12&t=4101&p=16258 -> See THistPainter::PaintColorLevels() Int_t ncolors = gStyle->GetNumberOfColors() ; Int_t ndiv = histo->GetContour() ; if (ndiv == 0 ) { ndiv = gStyle->GetNumberContours() ; } Int_t ndivz = TMath::Abs(ndiv) ; Double_t zmin = histo->GetMinimum(), zmax = histo->GetMaximum() ; Double_t dz = zmax - zmin ; Double_t scale = ndivz/dz ; Int_t color = Int_t(0.01+(z-zmin)*scale) ; Int_t theColor = Int_t((color+0.99)*Float_t(ncolors)/Float_t(ndivz)) ; if (theColor > ncolors-1) theColor = ncolors-1 ; // printf ("\ntheColor=%d",theColor) ; return (gStyle->GetColorPalette(theColor)) ; } void ztest () { gStyle->SetPalette(1) ; gStyle->SetPadRightMargin(0.13) ; gStyle->SetLineScalePS(1) ; gStyle->SetLineStyleString( 2,"6 6") ; gStyle->SetLineStyleString( 3,"3 4") ; TH2D * histo = new TH2D ("histo","", 15,-5.,5., 15,-5.,5.) ; histo->FillRandom("gaus",1000) ; histo->SetLineColor(kBlack) ; histo->SetFillColor(kRed) ; histo->SetStats(kFALSE) ; //histo->SetMaximum(histo->GetMaximum()*1.05) ; Int_t nbins = histo->GetNbinsX() * histo->GetNbinsY() ; Int_t npts=5 ; Double_t * x3D = new Double_t [npts], * x2D = new Double_t [npts] ; for (Int_t k=0; kGetMaximum() ; TPolyLine ** line = new TPolyLine * [nbins] ; Int_t idx=-1 ; Int_t ncols = histo->GetNbinsX(), nrows = histo->GetNbinsY() ; TCanvas * canvas = new TCanvas ("canvas","canvas",0,0,1300,860) ; canvas->Divide(5,4) ; canvas->cd( 1) ; histo->Draw("lego ,fb") ; /// computing top plane parameters (no drawing) gPad->Update(); TView * view = gPad->GetView() ; /// 3D->2D, http://root.cern.ch/phpBB3/viewtopic.php?f=3&t=9616&p=41094 for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { x3D[0] = histo->GetXaxis()->GetBinLowEdge(icol) ; y3D[0] = histo->GetYaxis()->GetBinLowEdge(irow) ; z3D[0] = zmax ; xyz3D[0]=x3D[0] ; xyz3D[1]=y3D[0] ; xyz3D[2]=z3D[0] ; view->WCtoNDC(xyz3D,&xyz2D[0]) ; x2D[0]=xyz2D[0] ; y2D[0]=xyz2D[1] ; x3D[1] = histo->GetXaxis()->GetBinLowEdge(icol) ; y3D[1] = histo->GetYaxis()->GetBinUpEdge (irow) ; z3D[1] = zmax ; xyz3D[0]=x3D[1] ; xyz3D[1]=y3D[1] ; xyz3D[2]=z3D[1] ; view->WCtoNDC(xyz3D,&xyz2D[0]) ; x2D[1]=xyz2D[0] ; y2D[1]=xyz2D[1] ; x3D[2] = histo->GetXaxis()->GetBinUpEdge (icol) ; y3D[2] = histo->GetYaxis()->GetBinUpEdge (irow) ; z3D[2] = zmax ; xyz3D[0]=x3D[2] ; xyz3D[1]=y3D[2] ; xyz3D[2]=z3D[2] ; view->WCtoNDC(xyz3D,&xyz2D[0]) ; x2D[2]=xyz2D[0] ; y2D[2]=xyz2D[1] ; x3D[3] = histo->GetXaxis()->GetBinUpEdge (icol) ; y3D[3] = histo->GetYaxis()->GetBinLowEdge(irow) ; z3D[3] = zmax ; xyz3D[0]=x3D[3] ; xyz3D[1]=y3D[3] ; xyz3D[2]=z3D[3] ; view->WCtoNDC(xyz3D,&xyz2D[0]) ; x2D[3]=xyz2D[0] ; y2D[3]=xyz2D[1] ; x3D[4] = histo->GetXaxis()->GetBinLowEdge(icol) ; y3D[4] = histo->GetYaxis()->GetBinLowEdge(irow) ; z3D[4] = zmax ; xyz3D[0]=x3D[4] ; xyz3D[1]=y3D[4] ; xyz3D[2]=z3D[4] ; view->WCtoNDC(xyz3D,&xyz2D[0]) ; x2D[4]=xyz2D[0] ; y2D[4]=xyz2D[1] ; colorz = MRF_GetColorFromPalette (histo,histo->GetBinContent(icol,irow)) ; //printf ("\nibin(%03d,%03d) = %g -> icolor=%d & x,y={(%+.1f,%+.1f),(%+.1f,%+.1f),(%+.1f,%+.1f),(%+.1f,%+.1f)}", icol,irow, histo->GetBinContent(icol,irow),colorz, x[0],y[0], x[1],y[1], x[2],y[2], x[3],y[3]) ; idx = (icol-1)*nrows+(irow-1) ; line[idx] = new TPolyLine (npts,x2D,y2D) ; line[idx]->SetLineColor(colorz) ; line[idx]->SetFillColor(colorz) ; } } canvas->cd( 2) ; histo->Draw("col ,z ") ; canvas->cd( 3) ; histo->Draw("surf3,z, fb") ; canvas->cd( 6) ; histo->Draw("lego ,z, fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } histo->Draw("lego , fb,same") ; canvas->cd( 7) ; histo->Draw("lego1,z, fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } canvas->cd( 8) ; histo->Draw("lego2,z, fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } canvas->cd( 9) ; histo->Draw("lego3,z, fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } canvas->cd(10) ; histo->Draw("lego4,z, fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } canvas->cd(11) ; histo->Draw("lego ,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } histo->Draw("lego ,0,fb,same") ; canvas->cd(12) ; histo->Draw("lego1,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } canvas->cd(13) ; histo->Draw("lego2,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } canvas->cd(14) ; histo->Draw("lego3,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } canvas->cd(15) ; histo->Draw("lego4,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } canvas->cd(16) ; histo->Draw("lego ,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { if ( histo->GetBinContent(icol,irow) > 0. ) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } } histo->Draw("lego ,0,fb,same") ; canvas->cd(17) ; histo->Draw("lego1,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { if ( histo->GetBinContent(icol,irow) > 0. ) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } } canvas->cd(18) ; histo->Draw("lego2,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { if ( histo->GetBinContent(icol,irow) > 0. ) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } } canvas->cd(19) ; histo->Draw("lego3,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { if ( histo->GetBinContent(icol,irow) > 0. ) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } } canvas->cd(20) ; histo->Draw("lego4,z,0,fb") ; for (Int_t icol=1; icol<=ncols; icol++) { for (Int_t irow=1; irow<=nrows; irow++) { if ( histo->GetBinContent(icol,irow) > 0. ) { line[(icol-1)*nrows+(irow-1)]->Draw("f") ; } } } canvas->SaveAs("canvas.png") ; canvas->SaveAs("canvas.pdf") ; return ; }