# Fit function between specific y values

**URL:** <https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582>\
**Category:** ROOT\
**Created:** [March 26, 2020, 1:52pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582 "2020-03-26T13:52:54Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 26, 2020, 1:52pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/1 "2020-03-26T13:52:54Z")

</div>

Hi, I have a TGraphAsymmErrors object, to which I added a sigmoid fit. Since I am only interested in a specific y range of that function, I would like to add my fit only to that y range (so that that range is being fitted more precisely), but I am not sure how to do that… Thanks!

---

<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:** [March 26, 2020, 2:06pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/2 "2020-03-26T14:06:43Z")

</div>

[${ROOTSYS}/tutorials/fit/fitExclude.C](https://root.cern.ch/doc/master/fitExclude_8C.html)

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 27, 2020, 9:06am UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/3 "2020-03-27T09:06:07Z")

</div>

Thanks! That tutorial is able to set the x range of the fit, but how would I make it work for the y range?

---

<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:** [March 27, 2020, 9:38am UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/4 "2020-03-27T09:38:16Z")

</div>

@moneta @couet I’m afraid I have forgotten how to easily extract the current “y” value (for the current “x”) from inside of the fit function.

---

<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:** [March 27, 2020, 10:07am UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/5 "2020-03-27T10:07:09Z")

</div>

I have already answered to this question in [Fit function uses initial parameter values](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/)

@evdv it is not efficient to open different posts for the same problem. Please refrain to do it, It just creates confusion

---

<div class="post-metadata">

**Author:** ![evdv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/2acd7d/32.png) [@evdv](https://root-forum.cern.ch/u/evdv)\
**Post date:** [March 27, 2020, 10:08am UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/6 "2020-03-27T10:08:02Z")

</div>

yes, you’re right, I’m sorry

---

<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:** [March 27, 2020, 11:18am UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/7 "2020-03-27T11:18:42Z")

</div>

@moneta I think it would still be nice to learn how one can get the actual “y”, for the actual “x”, inside of the fit function (i.e. each time it is called and gets its new “Double\_t \*x” set).

---

<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:** [March 27, 2020, 11:33am UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/8 "2020-03-27T11:33:31Z")

</div>

I am sorry I don’t understand what do you mean exactly. To get y value given x, just call TF1::Eval(x), but I think you refer to something else

---

<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:** [March 27, 2020, 11:38am UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/9 "2020-03-27T11:38:22Z")

</div>

The question is:

```cpp
Double_t fline(Double_t *x, Double_t *par)
{
  // how to get the current (to be fitted) data point's "y" for the current "x[0]"
}

```

---

<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:** [March 27, 2020, 1:44pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/10 "2020-03-27T13:44:16Z")

</div>

This is not a question for the function, but for the data used for fitting. And from each data object (TH1, TGraph, etc…) you can get this information given the x value, or vice versa, find x given y

Lorenzo

---

<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:** [March 27, 2020, 1:56pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/11 "2020-03-27T13:56:57Z")

</div>

This is a question for the ROOT’s fitter / minimizer. It calls the user provided “FCN” fit function when calculating the ch^2 or the likelihood (for each data point separately). This function gets the “Double\_t \*x” but inside I also want to know what is the (experimental) data point’s “value” (i.e. “y” for 1D data objects and “z” for 2D objects) for this particular point. This should work regardless of the data object type (the “FCN” fit function should not depend on the actual type, i.e. regardless if one does “`graph->Fit(FCN)`” or “`histo->Fit(FCN)`” it should look the same).

If I remember well, I once used [`TVirtualFitter::GetFitter()`](https://root.cern.ch/doc/master/classTVirtualFitter.html#acf7dd7f57ad2b01c76e648900247084b) to gain access to it’s settings inside of the “FCN” fit function (but there were some problems and I don’t remember now how I got around them).

---

<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:** [March 27, 2020, 2:57pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/12 "2020-03-27T14:57:39Z")

</div>

Hi,

It is up to you as you implement the function. In the ps you could use the static Fitter instance to get access to the data, it is true. Now there is no existing global fitter instance, it is up to the user to get this information from the data object itself. One for example can use a lambda function to build a TF1 and have access directly to the fitted histogram when implementing the fit user function. This is similar with what you get in the past with `TVirtulFitter::GetFitter()->GetObjectFit()`

If you want to get more lower level information there is always the way of using instead of TH1::Fit the Fitter class.

Lorenzo

Lorenzo

---

<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:** [March 27, 2020, 3:11pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/13 "2020-03-27T15:11:27Z")

</div>

Well, this reminds me the first problem. In the beginning, during fitting, inside of the “FCN” fit function, [`TVirtualFitter::GetFitter()`](https://root.cern.ch/doc/master/classTVirtualFitter.html#acf7dd7f57ad2b01c76e648900247084b) returns 0 (and first AFTER the fit completes, it returns some valid pointer).

---

<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:** [March 27, 2020, 3:53pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/14 "2020-03-27T15:53:20Z")

</div>

Hi,

This is correct ! There is a global TVirtualFitter instance created only after fitting. This was introduced mainly for backward compatibility butit should not be used anymore now. Also, when running with ROOT IMT support (multi-threading mode) this instance is not created even after fitting !  
We should probably add a deprecated warning message when calling TVirtualFitter::GetFitter()

Lorenzo

---

<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:** [March 27, 2020, 3:58pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/15 "2020-03-27T15:58:14Z")

</div>

So the question is again … from inside of the “FCN”, how can I get the actual (experimental) data point’s “value” (i.e. “y” for 1D data objects and “z” for 2D objects) for this particular point (for which this function has been called)?  
Or, how can I get access to the object being fitted (without writing object specific lambda functions).

---

<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:** [March 27, 2020, 5:26pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/16 "2020-03-27T17:26:49Z")

</div>

One thing to clarify, `FCN` is a jargon used (from Minuit) to call the objective function (chi2, likelihood, etc…) and not your fitting model function that you pass in `TH1::Fit`

Concerning your question, no there is no static function to access the data object that you can use within your TF1 implementation.  
But it is not needed, you can create the TF1 from a C++ object (a lambda, a functor, etc…) and pass the data object pointer or reference or you can always make your data object a global one and be visible inside the TF1 user implementation.  
I can provide a simple example for this if you need  
I hope this answers your question

Lorenzo

---

<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 10, 2020, 5:26pm UTC](https://root-forum.cern.ch/t/fit-function-between-specific-y-values/38582/17 "2020-04-10T17:26:51Z")

</div>

Closed on April 10, 2020, 5:26pm UTC

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