Histogram with broken axis

I have a pretty complicated piece of code that spits out a histogram.

Unfortunately, the histograms x-axis appears completely broken upon being drawn, with all the labels being shown at the very left end of the x-axis. The three bins should be labeled “CR”, “SR1” and “SR2”.


I would like to know what I can do to reset the x-axis to a proper state such that drawing of this histogram is possible.

I have attached a ROOT file with the histogram with the broken axis inside.

Can anyone help me?
test.root (5.59 KB)

It seems the X axis is in a weird state. See its name:

root [2] BDT->GetXaxis()->GetName()
(const char *) "(fmod([EventIndex],2)==0) ? [MVA:weights/run2_vbf_MVA_BDT0_bVeto_ODD.weights.xml] : [MVA:weights/run2_vbf_MVA_BDT0_bVeto_EVEN.weights.xml]"

Yes, the axis is in a weird state, that’s why I’m posting here :slight_smile:

The weird axis name is intentional. However, even setting the axis name to something else doesn’t solve the problem for me.

If you have any idea on how to reset the axis to a normal state, please let me know.

As it is a simple histogram with only 3 bins I would recreate a new one with the same bin labels and fill it with the content of this histogram.

Unfortunately, the problem occurs inside a much larger software package (~100k lines of code), and since “Clone()” will also clone the defect, the only solution is hardcoding the histogram properties into a loop that runs over thousands of histograms that do not exhibit this problem - so that is not really an option.

Can you identify the point where it becomes corrupted ?

Unfortunately not. I’ve tried to recreate the things that we do to the histogram in a small script.
However, the result looks fine, so I’m really puzzled what causes the problem. Here’s what I’ve tried so far:

{
  TH1* hist = new TH1F("hist","hist",200,-1,1);
  TString name(hist->GetName());
  hist->FillRandom("gaus");
  std::vector<double> newBins = {-1.,0.3,0.9,1.};
  TH1* newhist = hist->Rebin(newBins.size()-1,name,&(newBins[0]));
  newhist->GetXaxis()->Set(newhist->GetNbinsX(),0.,1.);
  newhist->GetXaxis()->SetBinLabel(1,"CR");
  newhist->GetXaxis()->SetBinLabel(2,"SR1");
  newhist->GetXaxis()->SetBinLabel(3,"SR2");
  newhist->Draw();
}

If you try this, you will see that the resulting histogram looks fine, which I don’t understand because this is pretty much what we are doing…

I was hoping there would be some magical ResetBits or RestoreDefaults method that would repair the axis…

You said you clone the histogram … can you try in the small example ?

Unfortunately, the example code I posted does not exhibit the problem, so also cloning it doesn’t reproduce the issue. It’s just that when I clone the histogram from the root file I posted that it will also be corrupted.

Ah ok … so the histogram is corrupted in you big program. My guess is that there is some overwriting going there.
Have you tried to compile it in debug mode ? use tool like valgrind to analyse memory corruption ?

This is well possible, I’ll have a look as soon as time permits.
However, I would hope that it would somehow be possible to fix the corruption and reset the axis properties to the defaults, no?

The dump doesn’t show anything weird except the name:

root [3] BDT->GetXaxis()->Dump()
==> Dumping object at: 0x00007fe3d0678058, name=(fmod([EventIndex],2)==0) ? [MVA:weights/run2_vbf_MVA_BDT0_bVeto_ODD.weights.xml] : [MVA:weights/run2_vbf_MVA_BDT0_bVeto_EVEN.weights.xml], class=TAxis

fNbins                        3                   Number of bins
fXmin                         0                   low edge of first bin
fXmax                         1                   upper edge of last bin
fXbins                        ->7fe3d06780d8      Bin edges array in X
fXbins.*fArray                ->0                 [fN] Array of fN doubles
fXbins.fN                     0                   Number of array elements
fFirst                        0                   first bin to display
fLast                         0                   last bin to display
fBits2                        0                   second bit status word
fTimeDisplay                  false               on/off displaying time values instead of numerics
fTimeFormat                                       Date&time format, ex: 09/12/99 12:34:00
fTimeFormat.fRep              ->7fe3d0678108      ! String data
fTimeFormat.fRep.             ->7fe3d0678108      
*fParent                      ->7fe3d0677fe0      !Object owning this axis
*fLabels                      ->7fe3d3453710      List of labels
fName                         (fmod([EventIndex],2object identifier
fName.fRep                    ->7fe3d0678070      ! String data
fName.fRep.                   ->7fe3d0678070      
fTitle                        BDT                 object title
fTitle.fRep                   ->7fe3d0678088      ! String data
fTitle.fRep.                  ->7fe3d0678088      
fUniqueID                     0                   object unique identifier
fBits                         0x03000008          bit field status word
fNdivisions                   510                 Number of divisions(10000*n3 + 100*n2 + n1)
fAxisColor                    1                   color of the line axis
fLabelColor                   1                   color of labels
fLabelFont                    42                  font for labels
fLabelOffset                  0.005               offset of labels
fLabelSize                    0.035               size of labels
fTickLength                   0.03                length of tick marks
fTitleOffset                  1                   offset of axis title
fTitleSize                    0.035               size of axis title
fTitleColor                   1                   color of axis title
fTitleFont                    42                  font for axis title


Do you see something which is not right ?

I don’t know, I’m a bit suspicious of the “fBits” and “fBits2” flags, but I wouldn’t know what you would expect to see there, so to me, everything looks fine…