# How can one fit a histogram in a range

**URL:** <https://root-forum.cern.ch/t/how-can-one-fit-a-histogram-in-a-range/38847>\
**Category:** ROOT\
**Created:** [April 13, 2020, 11:02am UTC](https://root-forum.cern.ch/t/how-can-one-fit-a-histogram-in-a-range/38847 "2020-04-13T11:02:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![enagy\_rootforum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/ac8455/32.png) [@enagy\_rootforum](https://root-forum.cern.ch/u/enagy_rootforum)\
**Post date:** [April 13, 2020, 11:02am UTC](https://root-forum.cern.ch/t/how-can-one-fit-a-histogram-in-a-range/38847/1 "2020-04-13T11:02:06Z")

</div>

\_ROOT Version: 6.20/02  
\_Platform: MAC OS Catalina on MacBook Air  
\_Compiler: version 10.15.4

I have in my code:

#include “TH1.h”  
#include “TF1.h”  
…  
TF1 \*f1 = new TF1(“f1”,"[0]_exp(-0.5_((x-[1])/[2])\*\*2)",12., 14.5);  
…  
NewInf-\>Fit(“f1”,r);

and get an error:  
error: use of undeclared identifier ‘r’  
although in the documentation it is written on [https://root.cern.ch/how-use-histograms-classes](https://root.cern.ch/how-use-histograms-classes)  
By default,[TH1::Fit](https://root.cern.ch/doc/master/classTH1.html#TH1:Fit) will fit the function on the defined histogram range. You can specify the option " **r**" in the second parameter to restrict the fit to the range specified in the [TF1](https://root.cern.ch/doc/master/classTF1.html)object.  
BTW f1-\>Draw(""); works fine  
BTW in the above documentation the link to [A simple fitting example](http://root.cern.ch/doc/master/classtutorials/fit/fit1.C.html) and [Fitting histogram sub-ranges](http://root.cern.ch/doc/master/classtutorials/fit/multifit.C.html) doesn’t exist.  
Thanks for telling me how can one fit a function on a histogram in a range specified in the function, and if possible, give me a working example.  
Elemer

---

<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 13, 2020, 11:04am UTC](https://root-forum.cern.ch/t/how-can-one-fit-a-histogram-in-a-range/38847/2 "2020-04-13T11:04:28Z")

</div>

`NewInf->Fit("f1", "R");`

---

<div class="post-metadata">

**Author:** ![enagy\_rootforum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/ac8455/32.png) [@enagy\_rootforum](https://root-forum.cern.ch/u/enagy_rootforum)\
**Post date:** [April 13, 2020, 11:11am UTC](https://root-forum.cern.ch/t/how-can-one-fit-a-histogram-in-a-range/38847/3 "2020-04-13T11:11:54Z")

</div>

Many thanks, it works fine! Hopefully the documentation will be updated soon.  
Elemer

---

<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:** [April 14, 2020, 8:50am UTC](https://root-forum.cern.ch/t/how-can-one-fit-a-histogram-in-a-range/38847/4 "2020-04-14T08:50:20Z")

</div>

> [@enagy\_rootforum](#):
>
> Hopefully the documentation will be updated soon

In the documentation you pointed the option `"R"` is between quotes.  
You missed them. I do not think [this documentation](https://root-forum.cern.ch/t/how-to-fit-histograms-or-data-points/38870) should be updated.

---

<div class="post-metadata">

**Author:** ![enagy\_rootforum](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/ac8455/32.png) [@enagy\_rootforum](https://root-forum.cern.ch/u/enagy_rootforum)\
**Post date:** [April 14, 2020, 9:25am UTC](https://root-forum.cern.ch/t/how-can-one-fit-a-histogram-in-a-range/38847/5 "2020-04-14T09:25:09Z")

</div>

I have meant that in the documentation: " For more complete examples, see [A simple fitting example](http://root.cern.ch/doc/master/classtutorials/fit/fit1.C.html) and [Fitting histogram sub-ranges](http://root.cern.ch/doc/master/classtutorials/fit/multifit.C.html)." [A simple fitting example](http://root.cern.ch/doc/master/classtutorials/fit/fit1.C.html) and [Fitting histogram sub-ranges](http://root.cern.ch/doc/master/classtutorials/fit/multifit.C.html) are missing, although they would be very useful.  
Elemer

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [April 28, 2020, 9:25am UTC](https://root-forum.cern.ch/t/how-can-one-fit-a-histogram-in-a-range/38847/6 "2020-04-28T09:25:11Z")

</div>

Closed on April 28, 2020, 9:25am UTC

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