Displaying Z axis graduation in Colz option

Hi everyone,
I have some issues while making some plots using “colz” option. For instance in this plot I attached, the graduation in Z axis (I mean the color bands) are


hided, so I have to use my hand once I do the plot to display it well before saving it, here is the code:

#include "TH1F.h"
#include "TPad.h"
#include "TRandom.h"
#include "Rtypes.h"

#include "AtlasLabels.h"
#include "AtlasStyle.h"
#include "AtlasUtils.h"
#include "AtlasUtils.C"
#include "AtlasLabels.C"
#include "AtlasStyle.C"

#include "TROOT.h"    
#include "TObject.h" 
#include "TFile.h"
#include "TTree.h"
#include "TH2.h"
#include "TMath.h"
#include "TGraph.h"
#include "TColor.h"
#include "TCanvas.h"
#include "TLegend.h"
#include <TStyle.h>
#include <iostream>
#include "TLatex.h"
#include "TProfile.h"

#include "AtlasStyle.h"

#include "TROOT.h"


void E12cmfoMtotVsM12()
{
  SetAtlasStyle();


  Int_t icol1=5;
  Int_t icol2=5;

   TCanvas* c1 = new TCanvas("c1","single inclusive jets",0,0,600,600);
  // TCanvas* c1 = new TCanvas("c1","single inclusive jets");
   c1->Divide(3,1);
   // TPad* thePad = (TPad*)c1->cd();
   // c1->SetLogz();
    
    TFile* f0   = new TFile("/afs/cern.ch/user/d/diboye/diboye_work/ZdZdRecoArea/run/E12cmfoMtotVsDeltaM_20_GeV.root");
   TH2D* Hist_E12VsM12_20_GeV   = (TH2D*)f0->Get("myHist_E12cmfoMtotvsDeltaM_all");
    Double_t scale1 = 1/Hist_E12VsM12_20_GeV->Integral();
    Hist_E12VsM12_20_GeV->Scale(scale1);
  
   TFile* f1  = new TFile("/afs/cern.ch/user/d/diboye/diboye_work/ZdZdRecoArea/run/E12cmfoMtotVsDeltaM_35_GeV.root");
   TH2D * Hist_E12VsM12_35_GeV   = (TH2D*)f1->Get("myHist_E12cmfoMtotvsDeltaM_all");
   Double_t scale2 = 1/Hist_E12VsM12_35_GeV->Integral();
    Hist_E12VsM12_35_GeV->Scale(scale2);
 TFile* f2  = new TFile("/afs/cern.ch/user/d/diboye/diboye_work/ZdZdRecoArea/run/E12cmfoMtotVsDeltaM_50_GeV.root");
  TH2D * Hist_E12VsM12_50_GeV   = (TH2D*)f2->Get("myHist_E12cmfoMtotvsDeltaM_all");
 Double_t scale3 = 1/Hist_E12VsM12_50_GeV->Integral();
    Hist_E12VsM12_50_GeV->Scale(scale3);
     gStyle->SetPalette(55);
    Hist_E12VsM12_35_GeV->SetTitle("");
    Hist_E12VsM12_35_GeV->GetXaxis()->SetTitle("#Deltam");
    Hist_E12VsM12_35_GeV->GetYaxis()->SetTitle("E12/M_{tot}");
    Hist_E12VsM12_20_GeV->SetTitle("");
    Hist_E12VsM12_20_GeV->GetXaxis()->SetTitle("#Deltam");
    Hist_E12VsM12_20_GeV->GetYaxis()->SetTitle("E12/M_{tot}");
    Hist_E12VsM12_50_GeV->SetTitle("");
    Hist_E12VsM12_50_GeV->GetXaxis()->SetTitle("#Deltam");
    Hist_E12VsM12_50_GeV->GetYaxis()->SetTitle("E12/M_{tot}");
    Hist_E12VsM12_20_GeV->SetLineColor(1);
    Hist_E12VsM12_35_GeV->SetLineColor(3);
    //  Hist_E12VsM12_35_GeV->SetMarkerStyle(20);
    Hist_E12VsM12_50_GeV->SetLineColor(2);
#ifdef __CINT__
  gROOT->LoadMacro("AtlasLabels.C");
#endif

   c1->cd(1);
  gPad->SetLogz();
 Hist_E12VsM12_20_GeV->GetYaxis()->SetRangeUser(0.49,0.55);
    Hist_E12VsM12_20_GeV->Draw("colz1");
myText(       0.60,  0.75, 1, "#sqrt{s}= 13 TeV");
 myText(       0.20,  0.75, 1, "H->ZdZd 20 GeV");
ATLASLabel(0.3,0.85,"Work In Progress");
c1->cd(2);
gPad->SetLogz();
Hist_E12VsM12_35_GeV->GetYaxis()->SetRangeUser(0.49,0.55);
      Hist_E12VsM12_35_GeV->Draw("colz");
myText(       0.60,  0.75, 1, "#sqrt{s}= 13 TeV");
myText(       0.20,  0.75, 1, "H->ZdZd 35 GeV");
ATLASLabel(0.3,0.85,"Work In Progress");
c1->cd(3);
gPad->SetLogz();
Hist_E12VsM12_50_GeV->GetYaxis()->SetRangeUser(0.49,0.55);
     Hist_E12VsM12_50_GeV->Draw("colz");
     myText(       0.20,  0.75, 1, "H->ZdZd 50 GeV");
    

     // TLegend* legend = new TLegend(0.2,0.7,0.4,0.75);
     //legend->SetBorderSize(0);
     //legend->AddEntry( Hist_E12VsM12_20_GeV,"H->ZdZd 20 GeV","f");
     //legend->AddEntry( Hist_E12VsM12_35_GeV,"H->ZdZd 35 GeV","f");
     //legend->AddEntry( Hist_E12VsM12_50_GeV,"H->ZdZd 50 GeV","f");
     //legend->Draw();

   myText(       0.60,  0.75, 1, "#sqrt{s}= 13 TeV");
  // myText(       0.57, 0.85, 1, "|#eta_{jet}|<0.5");
  // myMarkerText( 0.55, 0.75, 1, 3, "Data 2009",1.3);
  /*  myBoxText(    0.55, 0.67, 0.05, 1, "H->ZdZd 20 GeV");
 myBoxText(    0.55, 0.57, 0.05, 2, "H->ZdZd 50 GeV");
 myBoxText(    0.53, 0.47, 0.05, 3, "ggH bkg GeV");
 myBoxText(    0.51, 0.37, 0.05, 4, "SMZZ bkg GeV");*/
  // ATLASVersion("1.000000000");
 ATLASLabel(0.3,0.85,"Work In Progress");
  return;
}

#ifndef __CINT__
int main() { 
  E12cmfoMtotVsM12();
  gPad->Print("basic.png");
  return 0;
}
#endif

The other issue is I used almost the same code with different files but it doesn’t draw if I use a log scale on Z axis by doing this “gPad->SetLogz()” which was working well in the former code, here is the code:

#include "TH1F.h"
#include "TPad.h"
#include "TRandom.h"
#include "Rtypes.h"

#include "AtlasLabels.h"
#include "AtlasStyle.h"
#include "AtlasUtils.h"
#include "AtlasUtils.C"
#include "AtlasLabels.C"
#include "AtlasStyle.C"

#include "TROOT.h"    
#include "TObject.h" 
#include "TFile.h"
#include "TTree.h"
#include "TH2.h"
#include "TMath.h"
#include "TGraph.h"
#include "TColor.h"
#include "TCanvas.h"
#include "TLegend.h"
#include <TStyle.h>
#include <iostream>
#include "TLatex.h"
#include "TProfile.h"

#include "AtlasStyle.h"

#include "TROOT.h"


void E12cmfoMtotVsDeltaM200_400_600_800()
{
  SetAtlasStyle();


  Int_t icol1=5;
  Int_t icol2=5;

   TCanvas* c1 = new TCanvas("c1","single inclusive jets",0,0,600,600);
  // TCanvas* c1 = new TCanvas("c1","single inclusive jets");
  // TPad* thePad = (TPad*)c1->cd();
  // thePad->SetLogz();
   c1->Divide(2,2);

 
    TFile* f0   = new TFile("/afs/cern.ch/user/d/diboye/diboye_work/ZdZdTruthArea/run/Plots/E12cmfoMtotVsDeltaM_200_GeV.root");
   TH2D* Hist_E12VsM12_200_GeV   = (TH2D*)f0->Get("myHist_E12cmfoMtotvsDeltaM_all");
 Double_t scale1 = 1/Hist_E12VsM12_200_GeV->Integral();
    Hist_E12VsM12_200_GeV->Scale(scale1);
    TFile* f1  = new TFile("/afs/cern.ch/user/d/diboye/diboye_work/ZdZdTruthArea/run/Plots/E12cmfoMtotVsDeltaM_400_GeV.root");
   TH2D * Hist_E12VsM12_400_GeV   = (TH2D*)f1->Get("myHist_E12cmfoMtotvsDeltaM_all");
Double_t scale2 = 1/Hist_E12VsM12_400_GeV->Integral();
    Hist_E12VsM12_400_GeV->Scale(scale2);
 TFile* f2  = new TFile("/afs/cern.ch/user/d/diboye/diboye_work/ZdZdTruthArea/run/Plots/E12cmfoMtotVsDeltaM_600_GeV.root");
   TH2D * Hist_E12VsM12_600_GeV   = (TH2D*)f2->Get("myHist_E12cmfoMtotvsDeltaM_all");
Double_t scale3 = 1/Hist_E12VsM12_600_GeV->Integral();
Hist_E12VsM12_600_GeV->Scale(scale3);
 TFile* f3  = new TFile("/afs/cern.ch/user/d/diboye/diboye_work/ZdZdTruthArea/run/Plots/E12cmfoMtotVsDeltaM_800_GeV.root");
 TH2D * Hist_E12VsM12_800_GeV   = (TH2D*)f3->Get("myHist_E12cmfoMtotvsDeltaM_all");
Double_t scale4 = 1/Hist_E12VsM12_800_GeV->Integral();
Hist_E12VsM12_800_GeV->Scale(scale4);
 


 gStyle->SetPalette(55);
    Hist_E12VsM12_800_GeV->SetTitle("");
    Hist_E12VsM12_800_GeV->GetXaxis()->SetTitle("#Deltam");
    Hist_E12VsM12_800_GeV->GetYaxis()->SetTitle("E12/M_{tot}");

    Hist_E12VsM12_600_GeV->SetTitle("");
    Hist_E12VsM12_600_GeV->GetXaxis()->SetTitle("#Deltam");
    Hist_E12VsM12_600_GeV->GetYaxis()->SetTitle("E12/M_{tot}");
   
    Hist_E12VsM12_400_GeV->SetTitle("");
    Hist_E12VsM12_400_GeV->GetXaxis()->SetTitle("#Deltam");
    Hist_E12VsM12_400_GeV->GetYaxis()->SetTitle("E12/M_{tot}");

    Hist_E12VsM12_200_GeV->SetTitle("");
    Hist_E12VsM12_200_GeV->GetXaxis()->SetTitle("#Deltam");
    Hist_E12VsM12_200_GeV->GetYaxis()->SetTitle("E12/M_{tot}");


     Hist_E12VsM12_200_GeV->SetLineColor(1);
     Hist_E12VsM12_400_GeV->SetLineColor(2);
     Hist_E12VsM12_600_GeV->SetLineColor(3);
     Hist_E12VsM12_800_GeV->SetLineColor(4);
#ifdef __CINT__
  gROOT->LoadMacro("AtlasLabels.C");
#endif

    c1->cd(1);
   
    gPad->SetLogz();
   Hist_E12VsM12_200_GeV->Draw("colz");
 
      myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
     myText(       0.20,  0.75, 1, "Higgs set to 200 GeV");
     ATLASLabel(0.3,0.85,"Work In Progress"); 
      c1->cd(2);
       gPad->SetLogz();
 Hist_E12VsM12_400_GeV->Draw("colz");
        myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
     myText(       0.20,  0.75, 1, "Higgs set to 400 GeV");
     ATLASLabel(0.3,0.85,"Work In Progress"); 

    

 c1->cd(3);
  gPad->SetLogz();
  Hist_E12VsM12_600_GeV->Draw("colz");
        myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
     myText(       0.20,  0.75, 1, "Higgs set to 600 GeV");
     ATLASLabel(0.3,0.85,"Work In Progress"); 
   

 c1->cd(4);
  gPad->SetLogz();
 Hist_E12VsM12_800_GeV->Draw("colz");
     myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
     myText(       0.20,  0.75, 1, "Higgs set to 800 GeV ");
     ATLASLabel(0.3,0.85,"Work In Progress");
    
    

  //TLegend* legend = new TLegend(0.2,0.7,0.4,0.75);
  // legend->SetBorderSize(0);
  // legend->AddEntry( Hist_E12VsM12_200_GeV,"Higgs set 200 GeV","f");
  //legend->AddEntry( Hist_E12VsM12_400_GeV,"Higgs set 400 GeV","f");
  //legend->AddEntry( Hist_E12VsM12_600_GeV,"Higgs set 600 GeV","f");
  //legend->AddEntry( Hist_E12VsM12_800_GeV,"Higgs set 800 GeV","f");
  // legend->Draw();

  // myText(       0.57,  0.75, 1, "#sqrt{s}= 13 TeV");
  // myText(       0.57, 0.85, 1, "|#eta_{jet}|<0.5");
  // myMarkerText( 0.55, 0.75, 1, 3, "Data 2009",1.3);
  /*  myBoxText(    0.55, 0.67, 0.05, 1, "H->ZdZd 20 GeV");
 myBoxText(    0.55, 0.57, 0.05, 2, "H->ZdZd 50 GeV");
 myBoxText(    0.53, 0.47, 0.05, 3, "ggH bkg GeV");
 myBoxText(    0.51, 0.37, 0.05, 4, "SMZZ bkg GeV");*/
  // ATLASVersion("1.000000000");
     // ATLASLabel(0.3,0.85,"Work In Progress");
  return;
}

#ifndef __CINT__
int main() { 
  E12cmfoMtotVsDeltaM200_400_600_800();
  gPad->Print("basic.png");
  return 0;
}
#endif

I would appreciate if someone could help.
Cheers

1 Like

May be the fowling small example can help you:

{
   TCanvas c1;
   c1.SetRightMargin(0.18);
   TH2D ih;
   ih.SetStats(0);
   ih.SetBins(10,0,10,10,0,10);
   ih.Fill(5,5,50000);
   ih.Draw("colz");
}
1 Like

This has been an issue for as long as I’ve used ROOT. It is even worse when you add a label to the color bar. It almost is never visible without making modifications to the margins. The middle pad has a label that is nearly hidden, and the right pad has the margin set as @couet suggested. In addition, the stats box is always in the way on TH2 histograms.

#include <vector>

#include "TCanvas.h"
#include "TF2.h"
#include "TH2D.h"

{
   std::vector< TH2D* > hists;

   TF2 *f = new TF2("xygaus","xygaus", -2, 2, -2, 2);
   f->SetParameters(1,0,1,0,1);

   for (int i=0;i<3;i++) {
      hists.push_back(new TH2D(Form("h%d",i),"H", 100, -2, 2 ,100, -2 ,2));
      hists.back()->FillRandom("xygaus",5000);
   }

   TCanvas *c = new TCanvas("c","C");
   c->Divide(3,1);

   //Set the labels on the Z-axis
   hists.at(1)->GetZaxis()->SetTitle("A Z label");
   hists.at(2)->GetZaxis()->SetTitle("A Z label with Margin");

   //Turn of the stats so we can see the label.
   hists.at(2)->SetStats(false);
   //Change the right margin of the last pad tso the label is visible
   c->cd(3)->SetRightMargin(0.18);

   //Draw the hists
   for (int i=0; i<=2;i++) {
      c->cd(i+1);
      hists.at(i)->Draw("COLZ");
   }
}

Hi All,
Thanks for replying this one “c->cd(3)->SetRightMargin(0.18);” works thanks so much.
So what about the other issue? because I can’t see any change that could be the cause of the issue in the second code.
Thanks in advance.
Cheers

What do you mean by “it does not draw” ? you see an empty pad ? do you get some error message ?

In that particular example (as often) it is very hard to define a place where the stat box will not hide something: the palette, the X axis, The Y axis of the histogram itself which occupied the whole plotting space in case of option COL. We can consider using the tool recently developed for TLegend to find automatically an empty space where to put the stats. But I fear tha,t in that case, there is none and it is up to the user to choose what he wants to hide.

I understand that is true. At the very least it would be nice if it didn’t cover the color bar and was set slightly further left for TH2. (The TH1 placement seems not to get in the way often.) My default these day is to place the stats box slightly left and then reduce the alpha so that some of the data can be seen.

I’ve been using this little routine if this is useful for anyone:

/**Updates the statistics box for a given histogram. User may provide the
 * statics options, fit options, a vector of positions for the box, the fill
 * color, and alpha.
 * \note The statistics box may not be created until the histogram is drawn and
 * the canvas containing it is updated. It is suggested to mark the pad as
 * modified after updating the statistics box. For example:
 *
 * \code
 * gPad->Update();
 * UpdateStats(hist);
 * gPad->Modified();
 * \endcode
 * 
 * @author Karl Smith
 * \param[in] hist Pointer to the histogram to work on.
 * \param[in] optStat The option code for the statistics information.
 * \param[in] optFit The option code for the fit information.
 * \param[in] posNdc A vector of coordinates defining the box position. They
 *    should be in the following order: X1, Y1, X2, Y2.
 * \param[in] fillColor The fill color for the statistics box.
 * \param[in] fillAlpha The alpha value for the fill rangin from 0
 *    (transparent) to 1 (opaque).
 * \return The pointer to the stats box, null if an error occurred.
 */
const TPaveStats *UpdateStats(const TH1 *hist,
      const int &optStat = 1111, const int &optFit = 111,
      const std::vector< double > posNdc = std::vector< double >(),
      const Color_t &fillColor = kWhite, const float &fillAlpha = 1 )
{
   if (! hist) return nullptr;

   TPaveStats *stats = (TPaveStats*) hist->GetListOfFunctions()->FindObject("stats");
   if (stats) {
      stats->SetOptStat(optStat);
      stats->SetOptFit(optFit);
      if ( posNdc.size() == 4 ) {
         stats->SetX1NDC(posNdc.at(0));
         stats->SetY1NDC(posNdc.at(1));
         stats->SetX2NDC(posNdc.at(2));
         stats->SetY2NDC(posNdc.at(3));
      }
      else if ( ! posNdc.empty() ) {
         std::cerr << "ERROR: Invalid number of positions, " << posNdc.size() << ", provided!\n";
      }
      else if (dynamic_cast< const TH2* >(hist)) {
         stats->SetX1NDC(stats->GetX1NDC() - 0.1);
         stats->SetX2NDC(stats->GetX2NDC() - 0.1);
      }
      stats->SetFillColorAlpha(fillColor, fillAlpha);
   }

   return stats;
}

Yes this is a possibility… Note the Alpha is not available with X11…

Interesting, could you place a note to that affect in TAttFill or TAttFill::SetFillColorAlpha?

The opposite is already said (i.e. where it is available):
https://root.cern/doc/master/classTAttFill.html#F1

1 Like

Hi All,
sorry for my late reply, I was in a meeting.
So here is what I got with my last code


Cheers

Is there content in your histograms?
TH2::GetNumEntries

Although I believe the color bar does not show up if the histogram is empty.

More likely that your scaling is wrong.

printf("Scaling: %f\n", scale1);

Hi,
The histograms are not empty for instance if I get rid of gPad->SelLogz the plots are drawn.
if I do “printf(“Scaling: %f\n”, scale1);” I got this value 0.000026
cheers

And the histogram limits?

printf("Min: %f Max: %f\n", hist->GetMinimum(), hist->GetMaximum())

It may be faster if you could post the files.

Hi,
I put the files on my public area: /afs/cern.ch/user/d/diboye/public
here what I got Min: 0.000000 Max: 0.603430 if I do "printf(“Min: %f Max: %f\n”, hist->GetMinimum(), hist->GetMaximum())"
Cheers

Some of us do not have access to afs (including me).

Hi,
Ok I sent them here then.
E12cmfoMtotVsDeltaM_200_GeV.root (112.0 KB)
E12cmfoMtotVsDeltaM_400_GeV.root (114.1 KB)
E12cmfoMtotVsDeltaM_600_GeV.root (110.9 KB)
E12cmfoMtotVsDeltaM_800_GeV.root (110.9 KB)
Cheers

I’ll need the headers as well:

root [0]
Processing main.C...
In file included from input_line_8:1:
main.C:6:10: fatal error: 'AtlasLabels.h' file not found
#include "AtlasLabels.h"
         ^
main.C:7:10: fatal error: 'AtlasStyle.h' file not found
#include "AtlasStyle.h"
         ^
main.C:8:10: fatal error: 'AtlasUtils.h' file not found
#include "AtlasUtils.h"
         ^
main.C:9:10: fatal error: 'AtlasUtils.C' file not found
#include "AtlasUtils.C"
         ^
main.C:28:10: fatal error: 'AtlasStyle.h' file not found
#include "AtlasStyle.h"
         ^
main.C:35:3: error: use of undeclared identifier 'SetAtlasStyle'
  SetAtlasStyle();
  ^
main.C:98:7: error: use of undeclared identifier 'myText'
      myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
      ^
main.C:99:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.75, 1, "Higgs set to 200 GeV");
     ^
main.C:100:6: error: use of undeclared identifier 'ATLASLabel'
     ATLASLabel(0.3,0.85,"Work In Progress");
     ^
main.C:104:9: error: use of undeclared identifier 'myText'
        myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
        ^
main.C:105:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.75, 1, "Higgs set to 400 GeV");
     ^
main.C:106:6: error: use of undeclared identifier 'ATLASLabel'
     ATLASLabel(0.3,0.85,"Work In Progress");
     ^
main.C:113:9: error: use of undeclared identifier 'myText'
        myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
        ^
main.C:114:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.75, 1, "Higgs set to 600 GeV");
     ^
main.C:115:6: error: use of undeclared identifier 'ATLASLabel'
     ATLASLabel(0.3,0.85,"Work In Progress");
     ^
main.C:121:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
     ^
main.C:122:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.75, 1, "Higgs set to 800 GeV ");
     ^
main.C:123:6: error: use of undeclared identifier 'ATLASLabel'
     ATLASLabel(0.3,0.85,"Work In Progress");
     ^

You need to adjust the minimum:

Hist_E12VsM12_200_GeV->SetMinimum(scale1);
Hist_E12VsM12_400_GeV->SetMinimum(scale2);
Hist_E12VsM12_600_GeV->SetMinimum(scale3);
Hist_E12VsM12_800_GeV->SetMinimum(scale4);