# Is it possible to convert a function into a histogram?

**URL:** <https://root-forum.cern.ch/t/is-it-possible-to-convert-a-function-into-a-histogram/52698>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [December 8, 2022, 5:01pm UTC](https://root-forum.cern.ch/t/is-it-possible-to-convert-a-function-into-a-histogram/52698 "2022-12-08T17:01:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 8, 2022, 5:01pm UTC](https://root-forum.cern.ch/t/is-it-possible-to-convert-a-function-into-a-histogram/52698/1 "2022-12-08T17:01:17Z")

</div>

Hi,  
Using functions in root, I made a gaussian function as below:

{  
TF1 \*f12 = new TF1(“f12”,“[0]_exp(-0.5_((x-[1])/[2])\*\*2)”,45,130);  
f12-\>SetFillColor(4);

f12-\>SetParameter(0,10000);  
f12-\>SetParLimits(0,0,100000);

f12-\>SetParameter(1, 88);  
f12-\>SetParLimits(1,0,150);

f12-\>SetParameter(2, 13);  
f12-\>SetParLimits(2,0,20);

f12-\>Draw(“”);  
}

I would like to add 3.5 units resolution for each of the points in the gaussian function. For which I think I need to convert the plotted function into a histogram. Is it possible ?

---

<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:** [December 8, 2022, 5:13pm UTC](https://root-forum.cern.ch/t/is-it-possible-to-convert-a-function-into-a-histogram/52698/2 "2022-12-08T17:13:29Z")

</div>

Create a new empty histogram and then use your function to “fill” it with the help of one of the following:  
[TH1::Add](https://root.cern/doc/master/classTH1.html#a6e3008f571628f0c9d17d754c8b88730)  
[TH1::Eval](https://root.cern/doc/master/classTH1.html#aface309e19417389f8355ac269f35e3d)

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 9, 2022, 6:19am UTC](https://root-forum.cern.ch/t/is-it-possible-to-convert-a-function-into-a-histogram/52698/3 "2022-12-09T06:19:20Z")

</div>

I am not sure about how to do it. Could you please elaborate on how to convert the function f1 to a histogram ?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [December 9, 2022, 8:43am UTC](https://root-forum.cern.ch/t/is-it-possible-to-convert-a-function-into-a-histogram/52698/4 "2022-12-09T08:43:36Z")

</div>

I am not sure that’s what you are looking for, but when a function is drawn an underlying histogram is created. May be that’s enough for you ?

```auto
{
TF1 *f12 = new TF1("f12","[0]*exp(-0.5*((x-[1])/[2])**2)",45,130);
f12->SetFillColor(4);

f12->SetParameter(0,10000);
f12->SetParLimits(0,0,100000);

f12->SetParameter(1, 88);
f12->SetParLimits(1,0,150);

f12->SetParameter(2, 13);
f12->SetParLimits(2,0,20);

f12->Draw("");
auto h1 = (TH1D*)f12->GetHistogram();
h1->Draw();
}

```

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [December 9, 2022, 8:57am UTC](https://root-forum.cern.ch/t/is-it-possible-to-convert-a-function-into-a-histogram/52698/5 "2022-12-09T08:57:05Z")

</div>

Yes, thats what I was looking for. Thanks you @couet !
