Help with fitting a graph and taking residual

Hello,
I am still new to root. I have multiple graphs with zoomed peaks. I want to fit each of the peak and then take the residual by subtracting the actual data from the fit. Then, I am creating a new histogram of the residual.
Although I defined my plots as TH1F, and after fitting the data, I am using GetBinCenter() to access the values from the graph. But I am getting an error as follows-

no member named ‘GetBinContent’ in ‘TCanvas’
Double_t counts = ch->GetBinContent(i);

Here is the chunk of code that leads to error -

   for(int i = 0; i < 4096; i++){

       Double_t counts = ch->GetBinContent(i);

       Double_t fit_counts = myfunction->Eval(ch->GetBinCenter(i));

       Double_t residual = counts - fit_counts;

       fit->SetBinContent(i,residual);

    }

   fit->cd();

   peakfit[i]->Draw(""); 

   fit->Print("Peaks_disk126_fit.pdf");

ch here is the plot that has the fit. I want to plot the difference in a new plot which I am calling fit.
Can anyone please help? I am lost. I tried converting the graph to histogram by using - ch->GetPoint(i, x, y); fit->Fill(x,y);

but it did nothing. I don’t know how to get around. Thank you for your time!

ROOT Version: 6.18/04
Platform: C++
Compiler: Visual Studio Code

Only histograms have “[GS]etBinContent” so you need a pointer to some histogram.

1 Like

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