Draw a line in histogram with option CONT4

Hi,

I encounter a problem when I try to draw a line on a TH2D histogram which is drawn with option CONT4. The coordinate system of 2D histogram is weird if the CONT4 option is chosen. The following code is an example of my problem.

{
//This is a working code, draw with option CONT 
  TH2D *h2 = new TH2D("h2","h2",40,-4,4,40,-4,4);
  h2->FillRandom("gaus",40000);
  h2->Draw("CONT");
  TLine *l = new TLine(-3,-3,3,3);
  l->Draw();
}

If I choose CONT, the coordinate system works as I expected. However,

{
//This is what I want to do but doesn't work. Draw with option CONT4 
  TH2D *h2 = new TH2D("h2","h2",40,-4,4,40,-4,4);
  h2->FillRandom("gaus",40000);
  h2->Draw("CONT4");
  TLine *l = new TLine(-3,-3,3,3);
  l->Draw();
}

if I choose CONT4, I can’t get the line I want.
My root version is 5.34/05.

What is the reason for this? Could anyone help me to solve the problem?
Thanks a lot.

[root.cern.ch/phpBB3/viewtopic.p … 875#p57875](Vertical shaded area

[root.cern.ch/phpBB3/viewtopic.p … 131#p58131](gPad->GetUxmin(),GetUxmax() for 2D histogram

{
   TCanvas *c1 = new TCanvas("c1","c1",200,10,700,500);

   // Make a test histogram
   TH2F *h1 = new TH2F("h1","h1",40,-4,4,40,-4,4);
   Double_t a,b;
   for (Int_t i=0;i<306500;i++) {gRandom->Rannor(a,b);h1->Fill(a-1.5,b-1.5);}

   for (Int_t i=0;i<550;i++) {h1->Fill(3.5,3);}
   for (Int_t i=0;i<450;i++) {h1->Fill(3.5,2);}
   for (Int_t i=0;i<350;i++) {h1->Fill(3.5,1);}
   for (Int_t i=0;i<250;i++) {h1->Fill(3.5,0);}
   for (Int_t i=0;i<150;i++) {h1->Fill(3.5,-1);}
   for (Int_t i=0;i< 50;i++) {h1->Fill(3.5,-2);}
   for (Int_t i=0;i<  5;i++) {h1->Fill(3.5,-3);}


   // The histogram is drawn in pad1, any option can be used.
   TPad *pad1 = new TPad("pad1","",0,0,1,1);
   pad1->Draw();
   pad1->cd();
   h1->Draw("cont4");


   // Create a transparent "null" pad on top of pad1 with exactly the same
   // geometry but with the histogram coordinates. In case CONT4 is used to
   // draw the histogram any additionnal graaphics drawn in "null" will
   // appeared as drawn in the histograms coordinates.
   TPad *null = new TPad("null","",0,0,1,1);
   null->SetFillStyle(0);
   null->SetFrameFillStyle(0);
   null->Draw();
   null->cd();

   Double_t bm = pad1->GetBottomMargin();
   Double_t lm = pad1->GetLeftMargin();
   Double_t rm = pad1->GetRightMargin();
   Double_t to = pad1->GetTopMargin();
   Double_t x1 = h1->GetXaxis()->GetXmin();
   Double_t yf = h1->GetYaxis()->GetXmin();
   Double_t x2 = h1->GetXaxis()->GetXmax();
   Double_t y2 = h1->GetYaxis()->GetXmax();

   Double_t Xa = (x2-x1)/(1-lm-rm)-(x2-x1);
   Double_t Ya = (y2-yf)/(1-bm-to)-(y2-yf);
   Double_t LM = Xa*(lm/(lm+rm));
   Double_t RM = Xa*(rm/(lm+rm));
   Double_t BM = Ya*(bm/(bm+to));
   Double_t TM = Ya*(to/(bm+to));
   null->Range(x1-LM,yf-BM,x2+RM,y2+TM);


   // Some graphics in histogram's coordinates.
   TLine l;
   l.DrawLine(-3,-3,3,-3);
   l.DrawLine(-3,-3,-3,3);
   l.DrawLine(-3,3,3,3);
   l.DrawLine(3,3,3,-3);
   l.DrawLine(-2,-2,2,-2);
   l.DrawLine(-2,-2,-2,2);
   l.DrawLine(-2,2,2,2);
   l.DrawLine(2,2,2,-2);
   l.DrawLine(-1,-1,1,-1);
   l.DrawLine(-1,-1,-1,1);
   l.DrawLine(-1,1,1,1);
   l.DrawLine(1,1,1,-1);
}


Thanks a lot! It’s very helpful.

But I got new problem. When I try to save the plot into pdf/eps/ps, some white grid line appears. A pdf plot from your code is attached. Is there a way to get rid of it?
c1.pdf (48.6 KB)

This is a bug in your previewer. Turn off the smoothing in the preferences and it will show correctly.
We have tried to improve that for the COL option in some recent ROOT version…
But at the very end that’s really a bug in some PDF previewer… Adobe acrobat works fine.