Fitting a function in one range, and drawing it in another


I have a TH1 histogram that I want to fit with a TF1 function. I would like to fit the function within a narrow range, and then draw it over a wider range. I tried the obvious things:

TH1F* sub1=(TH1F*)adsdt1->Clone();

TF1 finc1= new TF1(“finc1”,"[0]/(([1][1]+x)([1][1]+x))");


but I can’t find the magic combination that will fit in the narrow range from tincmin to tincmax, and then draw over the full 0 to 0.5 range. What am I missing?


Spencer Klein

Does the following help?

Replace your last three lines with:

sub1->Fit(finc1,"0","",tincmin,tincmax); //add 0 option, i.e. don't draw
// also you can use finc1 directly instead of via-name
sub1->Draw(); // reorder: first draw sub1
finc1->Draw("same"); // add "same"

In your code, a fitted function for the fit range gets added to the list of functions. Then you draw your fitted function in the whole range and immediately overwrite the plot with the histogram plus its added function.

Dear Wolf:

Your fix worked. Thanks!


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