Home | News | Documentation | Download

User-Defined Function Histogram Fitting


#1

Dear ROOT experts

I am trying to fit the a/x function in Python to the following histogram:

import ROOT
%jsroot on

h_sharers = ROOT.TH1F("Sharers","Sharers; Shares; Amount",102, 1, 103)
    
for i in sorted_shared_dict:
    h_sharers.Fill(i[1])
    
c = ROOT.TCanvas()
h_sharers.SetFillColor(ROOT.kBlue-10)
c.SetGrid()
h_sharers.Draw()

c.Draw()

Tried to do the e^-x, as this is the predefined function in the ROOT library, but found it hard to define my own fit for a/x. Do you have an idea how to do it (in Python, as I lack C/C++ skills)?

Many thanks in advance :smiley:!


#2

Hi Jakub,

your code would look like this:

import ROOT
%jsroot on

h_sharers = ROOT.TH1F("Sharers","Sharers; Shares; Amount",102, 1, 103)
    
for i in sorted_shared_dict:
    h_sharers.Fill(i[1])
    
f = ROOT.TF1("myFitFunction", "[a]/x")
f.SetParameter(0, 1) # to help the fit converge
h_sharers.Fit(f)

c = ROOT.TCanvas()
h_sharers.SetFillColor(ROOT.kBlue-10)
c.SetGrid()
h_sharers.Draw()
c.Draw()

Looking forward to see the plot!

Cheers,
Danilo