Problem with TH1D's SetBins()

I am having difficulty with the SetBins() method of the TH1D class. I have a sample piece of code here to illustrate my problem:

    TH1D* hist = new TH1D();
    Double_t xmin, xmax;
    xmin = 0; xmax = 100;
    Int_t nbins = 10;
    hist->SetBins(nbins, xmin, xmax);
    TH1D* hist2 = new TH1D("hist2", "hist2", nbins, xmin, xmax);
    xmax = 50;
    hist2->SetBins(nbins, xmin, xmax);
    int x = 0;

Running this code, I have the following values:
hist->fXaxis::fXmin is 0
hist->fXaxis::fXmax is 1

(before calling SetBins() on hist2)
hist2->fXaxis::fXmin is 0
hist2->fXaxis::fXmax is 100

(after calling SetBins() on hist2)
hist2->fXaxis::fXmin is 0
hist2->fXaxis::fXmax is 100

Why haven’t hist’s bins been set? Why haven’t hist2’s bins been reset? Also, I see no error message when this code is run (if this helps).

Thanks for your help,
Austin

Hi Austin,

the code you send works for me in both ROOT 5.18 and 5.26 both in interpreted and compiled mode

#include <iostream>

#include <TH1D.h>

void hist()
{
  TH1D* hist = new TH1D();
  Double_t xmin, xmax;
  xmin = 0; xmax = 100;
  Int_t nbins = 10;
  hist->SetBins(nbins, xmin, xmax);
  TH1D* hist2 = new TH1D("hist2", "hist2", nbins, xmin, xmax);
  std::cout << hist2->GetXaxis()->GetXmax() << std::endl;
  xmax = 50;
  hist2->SetBins(nbins, xmin, xmax);
  std::cout << hist->GetXaxis()->GetXmax() << std::endl;
  std::cout << hist2->GetXaxis()->GetXmax() << std::endl;
  
}

as you can see in both cases the first output of hist2->GetXaxis()->GetXmax() gives 100 and after the SetBin call it is 50 while for hist it still is 100.

Can you provide any more hints on what goes wrong for you?

Cheers,

Erik

Hi Erik,

Thank you for your response. I thought of two possible problems:

(1) I am running this in Qt.
(2) I am running the ROOT 5.28 Mac OS X binaries

I tested (1) by writing a simple c program with your code:

TH1D* hist = new TH1D(); Double_t xmin, xmax; xmin = 0; xmax = 100; Int_t nbins = 10; hist->SetBins(nbins, xmin, xmax); TH1D* hist2 = new TH1D("hist2", "hist2", nbins, xmin, xmax); std::cout << hist2->GetXaxis()->GetXmax() << std::endl; xmax = 50; hist2->SetBins(nbins, xmin, xmax); std::cout << hist->GetXaxis()->GetXmax() << std::endl; std::cout << hist2->GetXaxis()->GetXmax() << std::endl;

The output, however, is different:

bash-3.2# ./a.out 100 1 100

Could it be that I am using version 5.28? I will try to run it with a different version of root.

Thanks again,
Austin

I tested the same piece of code on ROOT version 5.28 from source on Ubuntu. This time, I was given the correct output:

$: ./a.out 100 100 50

Strange… I will try out a few more tests on OS X, and maybe try to install from source on OS X.