# Integral from fit and from terminal-differences

**URL:** <https://root-forum.cern.ch/t/integral-from-fit-and-from-terminal-differences/33522>\
**Category:** Newbie\
**Created:** [April 10, 2019, 7:45am UTC](https://root-forum.cern.ch/t/integral-from-fit-and-from-terminal-differences/33522 "2019-04-10T07:45:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Svante](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@Svante](https://root-forum.cern.ch/u/Svante)\
**Post date:** [April 10, 2019, 7:45am UTC](https://root-forum.cern.ch/t/integral-from-fit-and-from-terminal-differences/33522/1 "2019-04-10T07:45:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [April 10, 2019, 11:31am UTC](https://root-forum.cern.ch/t/integral-from-fit-and-from-terminal-differences/33522/2 "2019-04-10T11:31:50Z")

</div>

Maybe @moneta can help here.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [April 10, 2019, 3:00pm UTC](https://root-forum.cern.ch/t/integral-from-fit-and-from-terminal-differences/33522/3 "2019-04-10T15:00:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Svante](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@Svante](https://root-forum.cern.ch/u/Svante)\
**Post date:** [April 10, 2019, 3:20pm UTC](https://root-forum.cern.ch/t/integral-from-fit-and-from-terminal-differences/33522/4 "2019-04-10T15:20:11Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 10, 2019, 3:46pm UTC](https://root-forum.cern.ch/t/integral-from-fit-and-from-terminal-differences/33522/5 "2019-04-10T15:46:56Z")

</div>

Try:

```auto
	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).

---

<div class="post-metadata">

**Author:** ![Svante](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/d26b3c/32.png) [@Svante](https://root-forum.cern.ch/u/Svante)\
**Post date:** [April 10, 2019, 3:54pm UTC](https://root-forum.cern.ch/t/integral-from-fit-and-from-terminal-differences/33522/6 "2019-04-10T15:54:37Z")

</div>

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.
