Subtracting a fit from a histogram

Hello all,

I have a histogram that I’ve booked and a custom function to which I’ve fitted it to. Now what I’m trying to do is subtract the fit off of the histogram to produce a plot of the residuals. I’ve built a bit of code, which I’ve attached below. It effectively fails here:

// Build the difference histogram TH1F DiffHist(*(residual)); DiffHist.SetNameTitle("DiffHist", "Difference in error functions (all-fit)"); DiffHist->GetXaxis()->SetTitle("'Data'"); for ( int i = 1; i <= DiffHist->GetNbinsX(); i++ ) { DiffHist.SetBinContent(i,residual->GetBinContent(i) - f1->GetBinContent(i)); }

I believe that the problem is that f1->GetBinContent(i) doesn’t exist as the proper type of object, though I’m not sure how I can modify this code to fix my problem.


Thank you for your time,
residual.C (2.64 KB)



TF1::GetBinContent(int) does not exist, since the bins belongs to the histogram and not the function. You need first to get the bin center value form the histogram and then evaluate the function: :

DiffHist.SetBinContent(i,residual->GetBinContent(i) - 
f1->Eval(residual->GetBinCenter(i)) );

Best Regards