Get function from a histogram

Hi!
So I plotted a histogram, which is filled with data from a text file (column 1: energy, column 2: probability). Now my question is, if it is possible to somehow get a function that would fit this histogram.

Hi,

I guess that the choice of the function to fit your trend is really up to you: you can start from a priori considerations for example. For what concerns the technical aspect, you can always define your function as a TF1 and use the Fit method to fit your histogram.

Cheers,
D

I tried with the Fit method, but I have a TH1 histogram and I think I cannot fit it with a TF1 function. I get the error message “Error in : function may not be null pointer”.

For what it’s worth, I know the approximate function that should be fitted, but the value of the parameters is not known.

Hi,

did you try this very method?
https://root.cern.ch/doc/master/classTH1.html#a7e7d34c91d5ebab4fc9bba3ca47dabdd
in case you want to input a formula in the form of a string, you can use:
https://root.cern.ch/doc/master/classTH1.html#a63eb028df86bc86c8e20c989eb23fb2a

Cheers,
D

Thank you, I tried again, now it works to some degree :slight_smile: The histogram has an x-range [0-2.5], and if I want to fit a function between this limits, the fit is just y=0, so a straight line, and if I plot the function in the x-range [0-1.8], it’s a perfect fit. So the deviation from the histogram starts when fitting in the range [0-1.9]. But that is probably because I have to modify the function a bit.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.