2D plotting error using default palette

Hello,
I would like to use the default palette for my 2D histogram. I am trying to plot a fraction and the final bin content in fraction_2D ranges between 0 to 0.5. But the plot doesn’t show the values.

Your help is much appreciated. Thanks!

The following is my code:

gStyle->SetOptStat("ne");
gStyle->SetPalette(55);

TH2D *num_databkg = (TH2D*)f3->Get("hist_data");
TH2D *num_bkg = (TH2D*)f3->Get("totalBkg");
TH2D *num_data = (TH2D*)num_databkg->Clone("num_data");
num_bkg->Scale(-1);
num_data->Add(num_bkg);//data-bkg

TH2D *denom_databkg = (TH2D*)f4->Get("hist_data");
TH2D *denom_bkg = (TH2D*)f4->Get("totalBkg");
TH2D *denom_data = (TH2D*)denom_databkg->Clone("denom_data");
denom_bkg->Scale(-1);
denom_data->Add(denom_bkg);

TH2D *fraction_2D = (TH2D*)num_data->Clone("fraction_2D");
fraction_2D->Divide(denom_data);
fraction_2D->GetZaxis()->SetRangeUser(0.0,0.5);

//fraction_2D->SetMaximum(0.5);
//fraction_2D->SetMinimum(0.0);

fraction_2D->Draw("COLZ0");

Can you provide an example we can run ?

You mean you want the input histograms?

Yes and what you posted seems just a part of your code.
So: something we can run reproducing the problem

f3.root (39.2 KB) f4.root (39.0 KB) histtest.cxx (981 Bytes)

Hello,
The files and the script are attached.
Thanks!

I see that when drawing with the LEGO option the histogram looks fine. But with the COL option it looks empty. I need to investigate.

Running it on ROOT 5.34.38 shows some errors and warnings. Maybe there’s something going on with the file or data?

ROOT 5.34/38 (v5-34-38@v5-34-38, Mar 12 2018, 15:49:39 on win32)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .x histtest.C
Error in <TClass::New>: cannot create object of class TH2
Error in <TClass::New>: cannot create object of class TH1
Warning in <TStreamerInfo::BuildCheck>:
   The StreamerInfo for version 4 of class TH2 read from the file f4.root
   has a different checksum than the previously loaded StreamerInfo.
   Reading objects of type TH2 from the file f4.root
   (and potentially other files) might not work correctly.
   Most likely the version number of the class was not properly
   updated [See ClassDef(TH2,4)].
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 4 of class 'TH2' is missing from
the on-file layout version 4:
   double fScalefactor; //
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 4 of class 'TH2' is missing from
the on-file layout version 4:
   double fTsumwy; //
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 4 of class 'TH2' is missing from
the on-file layout version 4:
   double fTsumwy2; //
Warning in <TStreamerInfo::CompareContent>: The following data member of
the in-memory layout version 4 of class 'TH2' is missing from
the on-file layout version 4:
   double fTsumwxy; //
Error in <TBufferFile::CheckByteCount>: object of class TH2 read too few bytes: 1251 instead of 1283
Error in <TBufferFile::CheckByteCount>: object of class TH2 read too few bytes: 864 instead of 896
Error in <TH2F::Add>: Attempt to add histograms with different number of bins
Error in <TBufferFile::CheckByteCount>: object of class TH2 read too few bytes: 1251 instead of 1283
Error in <TBufferFile::CheckByteCount>: object of class TH2 read too few bytes: 864 instead of 896
Error in <TH2F::Add>: Attempt to add histograms with different number of bins
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
For bin [i,k] = [1,1]
FF = 0
For bin [i,k] = [1,3]
FF = 0.42477
For bin [i,k] = [2,1]
FF = 0
For bin [i,k] = [2,3]
FF = 0.12186
For bin [i,k] = [3,1]
FF = 0
For bin [i,k] = [3,3]
FF = 0.12186
For bin [i,k] = [4,1]
FF = 0
For bin [i,k] = [4,3]
FF = 0.12186
root [1]
void histtest(){

   TFile *f3 = TFile::Open("f3.root");
   TFile *f4 = TFile::Open("f4.root");

   gStyle->SetOptStat("ne");
   gStyle->SetPalette(55);

   TH2D *num_databkg = (TH2D*)f3->Get("hist_data");
   TH2D *num_bkg = (TH2D*)f3->Get("totalBkg");
   TH2D *num_data = (TH2D*)num_databkg->Clone("num_data");
   num_data->ResetBit(TH1::kUserContour);

   num_bkg->Scale(-1);
   num_data->Add(num_bkg);//data-bkg

   TH2D *denom_databkg = (TH2D*)f4->Get("hist_data");
   TH2D *denom_bkg = (TH2D*)f4->Get("totalBkg");
   TH2D *denom_data = (TH2D*)denom_databkg->Clone("denom_data");
   denom_data->ResetBit(TH1::kUserContour);

   denom_bkg->Scale(-1);
   denom_data->Add(denom_bkg);


   TH2D *fraction_2D = (TH2D*)num_data->Clone("fraction_2D");
   fraction_2D->Divide(denom_data);
   fraction_2D->SetMaximum(0.5);
   fraction_2D->SetMinimum(0.0);
   fraction_2D->Draw("colz");

   for (int i=1;i<5;i++){
     for (int k=1;k<4;k+=2){
       std::cout<<"For bin [i,k] = ["<<i<<","<<k<<"]"<<std::endl;
       std::cout<<"FF = "<<fraction_2D->GetBinContent(i,k)<<std::endl;
    }
   }
}

Thank you very much!
But I do not seem to fully understand how ResetBin fixed the problem. Could you please help?

Apparently the histograms you start from have some wrongly defined (color) contours defined. That’s what I found while debugging problem. Reseting these contours fixes your problem.

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