Problem making zoom with TBox

I have noticed that if a SetRange, SetRangeUser or just a zoom with the mouse over a TH2 which has a TBox on it, the TBox range is not properly displayed. For instance if we make a SetRangeUser as in the example:


{
   TCanvas *c1 = new TCanvas("c1", "Test",4,18,500,500);         
   TBox *box1 = new TBox(3.7,3.7,15.9,15.9);   
   
   TH2F *h2=new TH2F("h2","myhist",40,0,40,40,0,40);
  
   h2->Draw("COLZ");   
   gStyle->SetPalette(1);
   gPad->Update();
         
   box1->SetLineColor(2);
   box1->SetLineWidth(2);
   box1->SetFillColor(2);
   box1->Draw();     

  h2->GetXaxis()->SetRangeUser(10,20);
}

Some part of the box outside the range is displayed. Is it possible to overcome this feature?
Thank you.

Only high level objects like TH1, TGraph, TF1 are clipped to the frame boundary, not low level classes like TLine, TBox, TText.
I suggest to use a TGraph instead of TBox if you need clipping.

Rene

OK, I guess that there is not an easy way to clip general kind of objects to the frame.
Thank you

No,we can easily clip any kind of polygon, but by principle we do not clip the low level objects to the frame boundary because you may want to draw these objects anywhere in the canvas.
As I said, if you want to clip a box, replace the TBox by a TGraph and it will be clipped at the frame boundary.

Rene

Ok, thank you for your advise.