SAME option use on TH2F::Draw

Hi Everyone,

When I have two TH2F Histograms plotted on the same pad :

  auto *c1 = new TCanvas("c1", "c1",900,900);
  gStyle->SetOptStat(0);
  gStyle->SetPalette(1);

  h2 = new TH2D("h2","",80,-4,12,80,-20,60);
  Float_t px, py;
  for (Int_t i = 0; i < 25000; i++) {
    gRandom->Rannor(px,py);
    h2->Fill(px,5*py);
  }
  h2->Draw("COLZ");

  h2a = new TH2D("h2a","",80,-4,12,80,-20,60);
  for (Int_t i = 0; i < 25000; i++) {
    gRandom->Rannor(px,py);
    px += 6.;
    py += 8.;
    h2a->Fill(px,5*py);
  }
  h2a->Draw("COL SAME");

It works fine:

Even when the binning is not the same for each TH2F :


  auto *c1 = new TCanvas("c1", "c1",900,900);
  gStyle->SetOptStat(0);
  gStyle->SetPalette(1);

  h2 = new TH2D("h2","",80,-4,12,80,-20,60);
  Float_t px, py;
  for (Int_t i = 0; i < 25000; i++) {
    gRandom->Rannor(px,py);
    h2->Fill(px,5*py);
  }
  h2->Draw("COLZ");

  h2a = new TH2D("h2a","",40,0,12,40,0,60);
  for (Int_t i = 0; i < 25000; i++) {
    gRandom->Rannor(px,py);
    px += 6.;
    py += 8.;
    h2a->Fill(px,5*py);
  }
  h2a->Draw("COL SAME");
  c1->Update();

It looks fine for me, but I’m not sure if they are sharing the same Z Scale (color)

However, if I choose not to fill the first histogram:


  auto *c1 = new TCanvas("c1", "c1",900,900);
  gStyle->SetOptStat(0);
  gStyle->SetPalette(1);

  h2 = new TH2D("h2","",80,-4,12,80,-20,60);
  Float_t px, py;
  h2->Draw();

  h2a = new TH2D("h2a","",40,0,12,40,0,60);
  for (Int_t i = 0; i < 25000; i++) {
    gRandom->Rannor(px,py);
    px += 6.;
    py += 8.;
    h2a->Fill(px,5*py);
  }
  h2a->Draw("COLZ SAME");
  c1->Update();

The output is :

I came across this problem in a totally different context, so I wonder if this is the expected behavior when using the option SAME

Thanks a lot,
A.

ROOT Version: 6.13/01
Platform: linuxx8664gcc
Compiler: gcc


I’m sure @couet knows the answer

In principal since ROOT 6.09 the same option is handled correctly with option col.
See:
https://root.cern/doc/master/classTHistPainter.html#HP14

@couet Thanks. I’m actually using 6.13/01 but as the documentation said the final plot will relay on the very first histo being drawn.

However if I use the “COL2Z” option:

  auto *c1 = new TCanvas("c1", "c1",900,900);
  gStyle->SetOptStat(0);
  gStyle->SetPalette(1);

  h2 = new TH2D("h2","",80,-4,12,80,-20,60);
  Float_t px, py;
  h2->Draw();

  h2a = new TH2D("h2a","",40,0,12,40,0,60);
  for (Int_t i = 0; i < 25000; i++) {
    gRandom->Rannor(px,py);
    px += 6.;
    py += 8.;
    h2a->Fill(px,5*py);
  }
  h2a->Draw("COL2Z SAME");
  c1->Update();

I actually get something that is more similar to what I was expecting:

I came across this problem when calling TH2F from a root file and plotting them by using a Macro. I didn’t understand what we were seeing because the data just didn’t make sense. Do you think a warning message can be included when using “COLZ” when the first histogram is empty or if the first histogram is empty using the following histogram as a reference for the z-scale so we get the same behaviour for “COLZ” and “COL2Z” in this scenario?

Thank you

when you use “SAME” the first plot impose its scale along Z. To have a Z scale automatically computed containing all the histograms a THStack should be used. But option COL is not available for THSTACK

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