Drawing multiple TH2 as COL+CONT with the same z-scale

Hello

I have two TH2 histograms that I want to draw on the same pad, superimposed.
To be able to see both, one is to be drawn as COL, while the other as one as CONT1 (which shows the absolute height as line color).

However, I cannot figure out how to match the scales in z. I can do it when both histograms are drawn as COL, but not if the second one is drawn as CONT1.

Here is a macro showing what I’m trying to do (CONT0 helps visualizing, but CONT1 is the same)

void colcontsame() {
   TH2F *h1 = new TH2F("h1","h1",10,-2,2,10,-2,2);
   TH2F *h2 = new TH2F("h2","h2",10,-2,2,10,-2,2);
   TH2F *h3 = new TH2F("h3","h3",10,-2,2,10,-2,2);
   for (Int_t i=0;i<1000000;i++) {
      double x,y;
      gRandom->Rannor(x,y);
      if(x<0 && y<0) h1->Fill(x,y, 5);
      if(x>0.4 && y>0.4) h2->Fill(x,y);
      if(x>0.4 && y<-0.4) h3->Fill(x,y);
   }
   h1->Draw("COLZ TEXT");
   h2->Draw("CONT0 SAME");
   h3->Draw("COL TEXT SAME"); // works if I use COL again
}

While h3 is correctly drawn as all dark blue (since it falls in the lowest z-bins of h1), h2 is drawn as if it had bins of the same order as h1 and seemingly has its own z-scale.

Am I missing some option? Is this supported at all?

Thank you in advance.

your macro:

void colcontsame() {
   auto h1 = new TH2F("h1","h1",10,-2,2,10,-2,2);
   auto h2 = new TH2F("h2","h2",10,-2,2,10,-2,2);
   auto h3 = new TH2F("h3","h3",10,-2,2,10,-2,2);

   for (Int_t i=0;i<1000000;i++) {
      double x,y;
      gRandom->Rannor(x,y);
      if(x<0 && y<0)      h1->Fill(x,y, 5);
      if(x>0.4 && y>0.4)  h2->Fill(x,y);
      if(x>0.4 && y<-0.4) h3->Fill(x,y);
   }

   h1->Draw("COLZ TEXT");
   h2->Draw("CONT1 SAME");
   h3->Draw("TEXT SAME");
}

Gives me:

What is wrong?

h2, the histogram drawn as CONT1, should have nothing yellow, since its maximum is more or less 18k, which is light blue in h1 (bottom left histogram).

You removed the COL option from h3 (bottom right), but the colors of h2 and h3 should be more or less the same

void colcontsame() {
   auto h1 = new TH2F("h1","h1",10,-2,2,10,-2,2);
   auto h2 = new TH2F("h2","h2",10,-2,2,10,-2,2);
   auto h3 = new TH2F("h3","h3",10,-2,2,10,-2,2);

   for (Int_t i=0;i<1000000;i++) {
      double x,y;
      gRandom->Rannor(x,y);
      if(x<0 && y<0)      h1->Fill(x,y, 5);
      if(x>0.4 && y>0.4)  h2->Fill(x,y);
      if(x>0.4 && y<-0.4) h3->Fill(x,y);
   }

   double max  = h1->GetMaximum();
   double max2 = h2->GetMaximum();
   double max3 = h3->GetMaximum();
   double min  = h1->GetMinimum();
   double min2 = h2->GetMinimum();
   double min3 = h3->GetMinimum();
   if (max2>max) max = max2;
   if (max3>max) max = max3;
   if (min2<min) min = min2;
   if (min3<min) min = min3;

   h1->SetMaximum(max);
   h2->SetMaximum(max);
   h3->SetMaximum(max);
   h1->SetMinimum(min);
   h2->SetMinimum(min);
   h3->SetMinimum(min);
   h2->SetContour(50);

   h1->Draw("COLZ TEXT");
   h2->Draw("CONT1 SAME");
   h3->Draw("TEXT SAME");
}

Thank you, that works, if the two z-ranges are mostly comparable (they are in my case).

Just a note, if the bin count in the CONT histogram is too low compared to the COL histogram, the CONT histogram just disappears without warning

void colcontsame() {
   auto h1 = new TH2F("h1","h1",10,-2,2,10,-2,2);
   auto h2 = new TH2F("h2","h2",10,-2,2,10,-2,2);
   auto h3 = new TH2F("h3","h3",10,-2,2,10,-2,2);

   for (Int_t i=0;i<1000000;i++) {
      double x,y;
      gRandom->Rannor(x,y);
      if(x<0 && y<0)      h1->Fill(x,y, 50);
      if(x>0.4 && y>0.4)  h2->Fill(x,y);
      if(x>0.4 && y<-0.4) h3->Fill(x,y);
   }

   double max  = h1->GetMaximum();
   double max2 = h2->GetMaximum();
   double max3 = h3->GetMaximum();
   double min  = h1->GetMinimum();
   double min2 = h2->GetMinimum();
   double min3 = h3->GetMinimum();
   if (max2>max) max = max2;
   if (max3>max) max = max3;
   if (min2<min) min = min2;
   if (min3<min) min = min3;

   h1->SetMaximum(max);
   h2->SetMaximum(max);
   h3->SetMaximum(max);
   h1->SetMinimum(min);
   h2->SetMinimum(min);
   h3->SetMinimum(min);
   h2->SetContour(50);

   h1->Draw("COLZ TEXT");
   h2->Draw("CONT1 SAME");
   h3->Draw("TEXT SAME");
}

Is that expected?

It is because the number of contours is too low for the small Z dynamic.
Increase the number of contours.

Ah, you are correct, thank you.

I also see that there is a maximum number of contours (it says 104 in the warning), so there is a hard limit on the difference in bin count you can represent.

Yes, that’s a limitation…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.