# User-Defined Function Histogram Fitting

**URL:** <https://root-forum.cern.ch/t/user-defined-function-histogram-fitting/31567>\
**Category:** Newbie\
**Created:** [November 13, 2018, 12:51pm UTC](https://root-forum.cern.ch/t/user-defined-function-histogram-fitting/31567 "2018-11-13T12:51:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ljakubow](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ljakubow/32/17465_2.png) [@ljakubow](https://root-forum.cern.ch/u/ljakubow)\
**Post date:** [November 13, 2018, 12:51pm UTC](https://root-forum.cern.ch/t/user-defined-function-histogram-fitting/31567/1 "2018-11-13T12:51:19Z")

</div>

Dear ROOT experts

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

```auto
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 😃!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [November 13, 2018, 3:38pm UTC](https://root-forum.cern.ch/t/user-defined-function-histogram-fitting/31567/2 "2018-11-13T15:38:38Z")

</div>

Hi Jakub,

your code would look like this:

```auto
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
