Proper fitting of TH1 data (extract TGraph function?)

Good day,

My problem: Acquire a good fit for a TH1. Save the function in a .txt file. Repeat for ~7800 similar histograms, and append .txt file with function.

My proposition for solution: Extract function or set of functions from a TGraph object. I.e. the same function evaluated using TGraph::Eval(x,0).

Is it possible to extract the function directly from the TGraph or would I need to create my own function for it? Any other good fit you can think of?

Using TH1::SetBinContent() you can recreate the histogram yourself using the .txt file: … 4.txt?dl=0

Or with this working script (put the above histogram in a subdirectory “output”):

Thanks in advance!


you can extract all functions fitted to a certain histogram with the method TH1::GetListOfFunctions().
Of course if you want to write out some feature of these functions, such as values of the parameters or their uncertainties, you’ll have to write a simple routine for that.


Thank you dpiparo!

I have successfully saved my TGraph using TList*TGraph::GetListOfFunctions(). I am currently doing my head in to understand how I can go about extracting the functions from it. I can’t find any example regarding it.

The TList doc ( has some documentation on iterating over a TList, but how could I use those examples to extract the functions of interest?



you’ll have a list of pointers to functions. You’ll have to iterate through it or use FindObject to identify your function. Note that if you fit only one function, there will be one function in the list.


Still not solved. But I appreciate your help.

TGraph *f = new TGraph(sizeof(x)/sizeof(double),x,y);
TList *lf = f->GetListOfFunctions();
cout << lf->GetEntries();

Just returns 0 for me. So I take it as the interpolation from TGraph::Eval(x,0) isn’t just hanging around in TGraph, but only done for the Eval.

I would like the functions from TGraph::Eval(), or some other interpolation or good fit. Any ideas?

calcR.C (702 Bytes)

Thanks Pepe, that’s a much cleaner code! It does unfortunately not resolve the issue of the interpolation being receivable from GetListOfFunctions().

You have the interpolating function. It’s simply:
double runR(double evalTime);

I understand thanks.

The issue previously was that I thought I could fetch the interpolation done when you do TGraph::Eval(). Since it doesn’t require you to do an interpolation yourself.