# Integral from fit and from terminal-differences

Dear all,
I have an histogram and I’m doing a fit of part of it. I need the integral under the curve and the error, to do this I have done:

``````Double_t integralefit2 =fit2-> Integral(111.,114.);
Double_t integralerrfit2= fit2->IntegralError(111.,114.);
``````

where fit2 is my fitting function.
The result for the integral is a value while the result for the error is always 0.

I tried to calculate the integral and the error by root on termina:
Parameters->Integral(val, val)
Parameters->ErrorIntegral(val, val)
In this way the results are different and in particular the ErrorIntegral is non0 value.

My questions are: why are the results so different? There is something wrong?If I need the number of counts under the fit function what I have to do?

Maybe @moneta can help here.

Hi,
Can you please post a running macro reproducing this problem. Without knowing what you are doing before, it is difficult to get an answer. Normally TF1::IntegralError needs to be applied after having performed the fit, otherwise you can pass the needed parameters and covariance matrix to the function

Lorenzo

the macro take an external file and put it in a histogram. After that it fit part of the histogram with a function.
This is part of the code:

``````Double_t PeakFit(Double_t *x, Double_t *p)
{
return (p[0]* exp( -0.5 * ((x[0]-p[1])/p[2]) * ((x[0]-p[1])/p[2])))+ p[3]*TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
}

void plot()

{

TH1D *h1= new TH1D("histogram","SIChip",16384,0,16383);
ifstream in ("file.txt");
int i=0;
double TotSign =0;
double num1=0;
while (1)
{
i=i+1;
in >> num1;
h1-> Fill(i,num1);
TotSign = TotSign + num1;
if (in. eof()) break;
}
Double_t p=0.3025;
Double_t q=-0.09129;

TH1D *h2= new TH1D("histogram","SIChips",16384,0,(16383*p)+q);
for(int i=0; i<h1->GetNbinsX(); i++)
{
h2->SetBinContent(i, h1->GetBinContent(i));
}

TCanvas *c1 = new TCanvas("c1","GEM-SiChips",1000,700);
c1->SetGrid();
c1->SetLogy();

h2-> SetTitle("GEM-SiChips; Energy [KeV]; counts [a.u.]");
h2-> SetLineColor(4);
h2-> SetFillColor(4);
h2-> SetFillStyle(4);
h2-> Draw("histosame");
h2-> SetMinimum(1);

TF1 *fit1= new TF1 ("fit1", PeakFit ,132.,136.,5);
fit1-> SetParName(0,"normgaus");
fit1-> SetParameter(0,3800);
fit1-> SetParName(1,"mean");
fit1-> SetParameter(1,133);
fit1-> SetParName(2,"sigma");
fit1-> SetParameter(2, 3);
fit1-> SetParName(3,"offsetshelf");
fit1-> SetParameter(3, 80);
fit1-> SetParName(4,"shiftshelf");
fit1-> SetParameter(4, 388);
fit1-> SetLineWidth(1);
gStyle-> SetOptFit(11111111);
fit1-> SetLineColor(kRed);
Double_t integralefit1 =fit1-> Integral(115.,125.);
Double_t integralerrfit1= fit1->IntegralError(2747.,2757.);
h2-> Fit(fit1, "R", "L");
fit1-> Draw("same");
//Double_t fit1chi2= fit1->GetChisquare();
cout<< "integrale="<<integralefit1 << endl;
cout<<"errore integrale="<<integralerrfit1<<endl;

return 0;

}
``````

The part I said before directly with terminal is up.
thank you

Try:

``````	h2-> Fit(fit1, "WR", "L");
Double_t integralefit1 =fit1-> Integral(115.,125.);
Double_t integralerrfit1= fit1->IntegralError(2747.,2757.);
``````

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

I tried adding the WR but the error is always 0. In the post above the range is wrong, I want the error in the same range of the integral and of the fit. Even if it’s correct the range the result is wrong.