# Extract data points from a Gaussian fit

**URL:** <https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202>\
**Category:** Newbie\
**Created:** [November 2, 2022, 1:02pm UTC](https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202 "2022-11-02T13:02:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [November 2, 2022, 1:02pm UTC](https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202/1 "2022-11-02T13:02:46Z")

</div>

Hii experts,

I have three Gaussian fit in one canvas and want to extract data points from one Gaussian fit (the red one) as attached image.

 ![Screenshot from 2022-11-02 18-25-24](https://root-forum.cern.ch/uploads/default/original/3X/4/3/43c22dcb0536c09adb496ec7117604ba9c24f2e9.png)  
Can I extract the data point from the red one fit? If so then how can I proceed? Waiting for your kind help.

Thanks,  
Anil

\_ROOT Version:\_6.26/02  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<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:** [November 2, 2022, 1:06pm UTC](https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202/2 "2022-11-02T13:06:41Z")

</div>

[`pointer_to_the_red_TF1->Eval(some_x_value)`](https://root.cern/doc/master/classTF1.html#ada14729afe8e4d63d07c28a71e0585d4)

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [November 2, 2022, 1:50pm UTC](https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202/3 "2022-11-02T13:50:11Z")

</div>

Thank you @Wile_E_Coyote for the reply.  
The limit of x value for red line is -5. to 5. and TF1 defined as g1. Suppose the Eval will be print x=0.12. However I messed up. The data will save in a txt file. Here is the macro:

> `ofstream myfile;`  
> `myfile.open("data.txt");`  
> `TFile *f = new TFile("fit_gaus.root");`  
> `TH1F *h1 = (TH1F*)f->Get("h1");`  
> ` TF1 *g1 = new TF1("fit1", "gaus", -5.,5.);`  
> ` g1->Eval(0.12);`  
> `g1->SetLineColor(kRed);`  
> ` TF1 *g2 = new TF1("fit2", "gaus", -5., 5.);`  
> `g2->SetLineColor(kGreen);`  
> `TF1 *f1 = new TF1("double_gaus", "gaus(0) + gaus(3)", -2.4, 3.);`  
> ` f1->SetParNames("Constant 1", "Mean 1", "Sigma 1", "Constant 2", "Mean 2", "Sigma 2");`  
> `f1->SetLineColor(kBlue);`  
> ` gStyle->SetOptFit(1);`  
> ` h1->Fit(g1, "", "", -2.4, 0.1);`  
> `h1->Fit(g2, "+", "", 0.6, 2.9);`  
> `Double_t par[6];`  
> ` g1->GetParameters(&par[0]);`  
> `g2->GetParameters(&par[3]);`  
> `f1->SetParameters(par);`  
> ` h1->Fit(f1, "R+");`  
> `g1->Draw("SAME");`  
> `// g1->Eval(x);`  
> `g2->Draw("SAME");`  
> ` f1->Draw("SAME");`  
> `myfile.close();`

---

<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:** [November 2, 2022, 2:22pm UTC](https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202/4 "2022-11-02T14:22:39Z")

</div>

`std::cout << g1->Eval(0.12) << "\n";`

BTW. You can safely evaluate your function also outside of its “x limits”.

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [November 3, 2022, 8:11am UTC](https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202/5 "2022-11-03T08:11:47Z")

</div>

`std::cout << g1->Eval(0.12) << "\n";`  
Output is zero for the above line.

Here my g1 limit is -5.0 to 5.0. So to get the data points within this limit,

> ` std::cout << g1->Eval(-5.0, 5.0) << "\n";`

Even the output is also zero. How to get the points within this limit?

---

<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:** [November 3, 2022, 8:14am UTC](https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202/6 "2022-11-03T08:14:09Z")

</div>

You need to create a loop over “x” values.

BTW. I assume the “`Output is zero`” because you executed it before fitting.

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [November 3, 2022, 8:44am UTC](https://root-forum.cern.ch/t/extract-data-points-from-a-gaussian-fit/52202/7 "2022-11-03T08:44:42Z")

</div>

Now it works. Thank you so much @Wile_E_Coyote
