Hi, I am trying to fit a function TF1, on a histogram TH1F which has variable binwidth. First of all, is that possible and if yes how can one perform it? Below is a test code I wrote, which doesn’t seem to be working. It fills a Gaussian histogram, then rebins it, and fits a gaussian to both. When I try to take the fit integrals, it doesn’t seem to be giving me identical results. Any help would be great, thanks.
import ROOT
from array import array
binning =array('d', [-3.0,-2.0,-1.0,-0.8,-0.6,-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4,0.6,0.8,1.0,2.0,3.0])
r = ROOT.TRandom3(0)
r.SetSeed(1234)
c=ROOT.TCanvas("c","c",1000,800)
c.Divide(2,2)
c.cd(1)
# Fill a histogram with Gaussian
h1 = ROOT.TH1F("h1", "histo from a gaussian", 60, -3.0, 3.0);
h1.SetLineColor(2)
for m in range(10000):
h1.Fill( r.Gaus(0,1))
h1.Draw()
# Fit with Gauss
h1.Fit('gaus','IL')
f1 = h1.GetFunction('gaus')
c.cd(2)
# Rebin original histogram into variable binwidt
h2 = h1.Clone("").Rebin( len(binning)-1, "hist", binning)
h2.SetLineColor(4)
h2.Draw()
# Fit with Gaus
h2.Fit('gaus','IL')
f2 = h2.GetFunction('gaus')
c.cd(3)
#Calculate the integrals below the TF1
h_integral1 = h1.Clone()
for k in range(1,h1.GetNbinsX()+1):
binw = abs(h1.GetBinLowEdge(k)-h1.GetBinLowEdge(k+1))
Nint1 =f1.Integral( h1.GetBinLowEdge(k) , h1.GetBinLowEdge(k+1) )/binw
h_integral1.SetBinContent(k, Nint1)
#And rebin to compare with other
h_integral1 = h_integral1.Rebin( len(binning)-1, "hist", binning)
h_integral1.Draw("HIST")
c.cd(4)
#Calculate the integrals below the TF1
h_integral2 = h2.Clone()
for i in range(1,h2.GetNbinsX()+1):
binw = abs(h2.GetBinLowEdge(i)-h2.GetBinLowEdge(i+1))
Nint2 =f2.Integral( h2.GetBinLowEdge(i) , h2.GetBinLowEdge(i+1) )/binw
h_integral2.SetBinContent(i, Nint2)
h_integral2.Draw("HIST")
raw_input("")
Cheers,
Christos