here is the example code on how to make a fit function (TF1) from existing histograms and use it to fit the ‘data’ in another histogram. Same approach can be used to create the fit function from arrays and other enumerable objects.
done using PyROOT
import ROOT
histograms={}
histograms[1] = ROOT.TH1F("h1","",100,-3,3)
histograms[2] = ROOT.TH1F("h2","",100,-3,3)
histograms[3] = ROOT.TH1F("h3","",100,-3,3)
histograms[1].FillRandom("gaus",10000)
histograms[2].FillRandom("gaus",2000)
histograms[3].FillRandom("gaus",3000)
def Onetracklengthfunc(x,par):
bin=histograms[2].GetXaxis().FindBin(x[0])
func=par[0]*histograms[2].GetBinContent(bin)+par[1]*histograms[3].GetBinContent(bin)
return func
fitfunc=ROOT.TF1("fitfunc",Onetracklengthfunc,-3,3,2)
histograms[1].Fit("fitfunc")
ROOT.gStyle.SetOptFit()
histograms[1].Draw("P E4")
a=raw_input("hit enter to exit")