Fit a log-log histogram

I’d like to fit a histogram with a log scale on both axes, but I get a weird result. Here’s the relevant code (PyROOT, Python 2.7.2, ROOT 5.30.01). I’m posting it here as I suppose it’s not a python related issue.

canvas = TCanvas("frequency_can", "{0}".format(file_name))
histogram = TH1I("frequency_hist", "{0};frequency;entries / 1".format(file_name), 10000, 0, 10000)
histogram.GetXaxis().SetRangeUser(1, 10000)

I attached the plot. Can you help me?


You cannot fit a polynomial to an histogram that is linear in the logx logy scale.
Your fitted function must be linear in the log scale:
log y = A * logx + B

Therefore in the non-log scale the equivalent function is

y = P0 * X ^ (P1)

which in ROOT you can write as

TF1 * f1 = new TF1("f1","[0]*TMath::Power(x,[1])");

and the use for fitting. Remember You need to set also some reasonable value of the initial parameters before fitting
Best Regards,

I had wrongly assumed ROOT would think about fitting a log-linear function on its own.

Thanks for your help, Lorenzo!