How to convert a fix bin size to a variable bin size TH1D

Dear Rooter,

I have an 1D hsitogram for invariant spectra with fixed bin sizes. Since there are some empty bins in it, it is necessay to merge some bins to get nice invariant spectra. The problem is how to get correct bin contents and errors for each merged bin.

Thanks for your kind help.
Zhongbao

This is only possible if your new bin edges are not in the middle of the old bins. In this case, make a look on the old histogram bins and
fill the new variable bin size histogram with
for (int i=0;i<nbinsold,i++) {
hnew->Fill(hold->GetXaxis()->GetBinCenter(i),hold->GetBinContent(i));
}

Rene

[quote=“brun”]This is only possible if your new bin edges are not in the middle of the old bins. In this case, make a look on the old histogram bins and
fill the new variable bin size histogram with
for (int i=0;i<nbinsold,i++) {
hnew->Fill(hold->GetXaxis()->GetBinCenter(i),hold->GetBinContent(i));
}

Rene[/quote]

Hi Rene,

thank you for your prompt reply.

The new bin edges are not in the middle of the old bins. But I don’t think your method will work since the errors are not necessary relate to the contents of each bin. By fill the new histogram you haven’t taken care of the errors for each bin.

Zhongbao

well, this should be more or less obvious.
replace:
hnew->Fill(hold->GetXaxis()->GetBinCenter(i),hold->GetBinContent(i));
by
hnew->Fill(hold->GetXaxis()->GetBinCenter(i),
hold->GetBinContent(i)
hold->GetBinError(i));

Rene

[quote=“brun”]well, this should be more or less obvious.
replace:
hnew->Fill(hold->GetXaxis()->GetBinCenter(i),hold->GetBinContent(i));
by
hnew->Fill(hold->GetXaxis()->GetBinCenter(i),
hold->GetBinContent(i)
hold->GetBinError(i));

Rene[/quote]

I am sorry, I just can’t find the nice method you wrote above in class TH1.

Zhongbao

Apologies! you are right.
I suggest the following:
Int_t i,j;
TH1 *hold, *hnew; //the old fix bin and the new var bin hist
Int_t nold = hold->GetXaxis()->GetNbins();
Int_t nnew = hnew->GetXaxis()->GetNbins();
Double_t e = new Double_t[nnew]
for (i=0;i<nnew;i++) e[i]=0;
for (i=0;i<nold;i++) {
Double_t eold = hold->GetBinError(i);
j = hnew->Fill(hold->GetXaxis()->GetBinCenter(i), hold->GetBinContent(i));
e[j] += eold
eold;
}
for (j=0;j<nnew;j++) hnew->SetBinError(TMath::Sqrt(e[j]));
delete [] e;

Rene