LEGO plot with gridx and gridy

Hi there,

I’d like to draw a custom xyGrid for a TH2 plotted in lego mode with the “0” option.

TH2D * histo = new TH2D ("histo","",20,-10.,10.,20,-10.,10.) ; histo->FillRandom("gaus",1000) ; histo->SetFillColor(kRed) ; histo->Draw("lego1,fb,0") ; It seems to me this could be achieved hacking the method TPainter3dAlgorithms::LegoCartesian() in $ROOTSYS/hist/histpainter/src/TPainter3dAlgorithms.cxx (see Hoption.Zero)
but there might exist easier ways e.g. using TPad grids or just drawing plain lines.
What do you reckon?

Cheers,
Z

may be:

{
   TH2D * histo = new TH2D ("histo","",20,-10.,10.,20,-10.,10.) ;
   histo->FillRandom("gaus",1000) ;
   histo->SetFillColor(kRed) ;
   histo->Draw("lego1,fb,0") ;

   TPolyLine3D *l3 = new TPolyLine3D(2);   
   l3->SetPoint(0,-10, 10,  0);   
   l3->SetPoint(1, 10,-10, 20);

   l3->Draw();   
}

{ Double_t xmin=-10., xmax=10., ymin=-20., ymax=20., TH2D * histo = new TH2D ("histo","",20,xmin,xmax,20,ymin,ymax) ; histo->FillRandom("gaus",1000) ; histo->SetFillColor(kRed) ; histo->Draw("lego1,fb,0") ; Int_t nx= 5 ; Double_t x=-1., dx=(xmax-xmin)/(nx*1.) ; TPolyLine3D ** l3x = new TPolyLine3D * [nx-1] ; for (Int_t i=0; i<nx-1; i++) { l3x[i] = new TPolyLine3D (2) ; x = xmin+(i+1)*dx ; l3x[i]->SetPoint(0, x ,ymin,0.) ; l3x[i]->SetPoint(1, x ,ymax,0.) ; l3x[i]->Draw() ; } Int_t ny= 10 ; Double_t y=-1., dy=(ymax-ymin)/(ny*1.) ; TPolyLine3D ** l3y = new TPolyLine3D * [ny-1] ; for (Int_t i=0; i<ny-1; i++) { l3y[i] = new TPolyLine3D (2) ; y = ymin+(i+1)*dy ; l3y[i]->SetPoint(0, xmin,y ,0.) ; l3y[i]->SetPoint(1, xmax,y ,0.) ; l3y[i]->Draw() ; } histo->Draw("lego1,fb,0,same") ; }Thx Olivier :wink:
Z

Thanks :slight_smile:
A nice example, I will use it.

Hum… Unfortunately it does not work for THStack since the option “same” does not apply in this latter case :frowning: (see code below).
One solution could be to proceed as this is automatically done for all lines drawn in a lego plot, namely draw only the corresponding visible segments via the method TPainter3dAlgorithms::FindVisibleLine(…).
Here’s an attempt

[code]{
Double_t xmin=-10., xmax=10., ymin=-20., ymax=20., zmin=0., zmax=70. ;
TH2D * h1 = new TH2D (“h1”,"",20,xmin,xmax,20,ymin,ymax) ; h1->FillRandom(“gaus”,2000) ; h1->SetFillColor(kRed ) ; h1->SetMinimum(zmin) ; h1->SetMaximum(zmax) ; //h1->Draw(“lego1,fb,0”) ;
TH2D * h2 = new TH2D (“h2”,"",20,xmin,xmax,20,ymin,ymax) ; h2->FillRandom(“gaus”,1000) ; h2->SetFillColor(kBlue) ; h2->SetMinimum(zmin) ; h2->SetMaximum(zmax) ; //h2->Draw(“lego1,fb,0,same”) ;
THStack * hs = new THStack (“hs”,“hs”) ; hs->Add(h1) ; hs->Add(h2) ; hs->SetMinimum(zmin) ; hs->SetMaximum(zmax) ;
hs->Draw(“lego1,fb,0”) ;
Int_t nx= 5 ; Double_t x=-1., dx=(xmax-xmin)/(nx*1.) ; TPolyLine3D ** l3x = new TPolyLine3D * [nx-1] ; for (Int_t i=0; i<nx-1; i++) { l3x[i] = new TPolyLine3D (2) ; x = xmin+(i+1)dx ; l3x[i]->SetPoint(0, x ,ymin,0.) ; l3x[i]->SetPoint(1, x ,ymax,0.) ; l3x[i]->Draw() ; }
Int_t ny= 10 ; Double_t y=-1., dy=(ymax-ymin)/(ny
1.) ; TPolyLine3D ** l3y = new TPolyLine3D * [ny-1] ; for (Int_t i=0; i<ny-1; i++) { l3y[i] = new TPolyLine3D (2) ; y = ymin+(i+1)*dy ; l3y[i]->SetPoint(0, xmin,y ,0.) ; l3y[i]->SetPoint(1, xmax,y ,0.) ; l3y[i]->Draw() ; }
//hs->Draw(“lego1,fb,0,same”) ; /// DOES NOT WORK

TPolyLine3D * l3 = new TPolyLine3D (2) ; l3->SetPoint(0,-10.,0.,0.) ; l3->SetPoint(1,+10.,0.,0.) ; l3->SetLineColor(kGreen) ; l3->Draw() ;
Double_t p1[3] ; p1[0]=-10. ; p1[1]=1. ; p1[2]=0. ;
Double_t p2[3] ; p2[0]= 10. ; p2[1]=1. ; p2[2]=0. ;
Int_t ntmax=100 ; Int_t nt=-1 ; Double_t t[200] ; for (Int_t k=0; k<200; k++) { t[k]=0. ; } /// See TPainter3dAlgorithms.h
TPainter3dAlgorithms * myLego= new TPainter3dAlgorithms () ;
myLego->FindVisibleLine(p1,p2, ntmax, nt,t) ;
printf ("\nnt=%d\n",nt) ; /// number of visible segments of the line
}[/code]Unfortunately this does not work: the number of visible segments returned (nt) is 0.
It seems to me the TPainter3dAlgorithms instance above (myLego) is not “linked” to the current pad.
Using the other TPainter3DAlgorythms constructor does not help and it does not seem possible to get back that used at drawing time since it gets deleted just afterwards (see fLego in THistPainter::PaintLego()).
[url=https://root-forum.cern.ch/t/when-is-a-tview-created/2979/10 said[quote]“TPainter3DAlgorythms is not meant to be used by end users”[/quote] :wink:
Any clue though?

Cheers,
Z

FindVisibleLine(…). will work only with hidden line removal techniques anyway.
Here you are using hidden surfaces.

I do not have (yet) an solution to your question.

[quote]FindVisibleLine(…). will work only with hidden line removal techniques anyway[/quote]Ok, too bad.

[quote]I do not have (yet) a solution to your question.[/quote] [-o<
Thx Olivier,
Z

Any news?
Cheers,
Z

No …