# Fit function uses initial parameter values

**URL:** <https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516>\
**Category:** Newbie\
**Created:** [March 21, 2020, 3:01pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516 "2020-03-21T15:01:53Z")\
**Posts on this page:** 13\
**Page:** 2

<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 25, 2020, 3:30pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/21 "2020-03-25T15:30:05Z")

</div>

Hi, using “R” only sets the x range, right? I would like to set my y range. Is this possible?

---

<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 25, 2020, 3:54pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/22 "2020-03-25T15:54:56Z")

</div>

Hi,

The fit range works on data points. You can easily find the bins (or data points) of your histogram or graph objects that have the desired Y values.  
See for example [TH1::FindFirstBinAbove](https://root.cern.ch/doc/master/classTH1.html#a63960594f84aea92e5fa2d5129b49007) or [TH1::FindLastBinAbove](https://root.cern.ch/doc/master/classTH1.html#a10b0ae1f09335b4b6ed251d1c5c62a11)

Lorenzo

---

<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 25, 2020, 3:57pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/23 "2020-03-25T15:57:36Z")

</div>

Ah okay, great. Thanks a lot for your help!

---

<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, 11:27am UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/24 "2020-03-26T11:27:21Z")

</div>

If I only want my efficiency curves to be fitted for y values between 0.8 and 1, I suppose I have to use the FindLastBinAbove function on my efficiency plots as

```auto
eff[i]->FindLastBinAbove(threshold = 0.8, axis = 2, first bin = 0, last bin = 1)

```

And then use the bins that this function found to fit my sigmoid to it. The problem with this is that my eff[I] are TGraphAsymmErrors object, and the FindLastBinAbove is from TH1, so it doesn’t work. Is there a way around this? Thanks!

---

<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 26, 2020, 2:27pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/25 "2020-03-26T14:27:25Z")

</div>

This is correct. But in this case since the TGraphAsymmErrors is created form histogram you can easly convert it back.  
Let’s suppose `h1` is one of the histogram passed to the `Divide` function and `'graph` is the TGraphAsymmErrors` object. You can then do:

```auto
auto histEff = (TH1*) hpass->Clone();
histEff->Reset(); 
for (int i = 0; i < graph->GetN(); ++i) { 
    int bin = histEff->FindBin( graph->GetX()[i] );
    histEff->SetBinContent( bin, graph->GetY()[i] ); 
}

```

and now histEff will be an histogram filled with the efficiency values. You can find the bin values above the threshold and then find the corresponding edges that you can use to set the function range for fitting

Lorenzo

---

<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, 2:56pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/26 "2020-03-26T14:56:07Z")

</div>

Ah okay, thanks a lot! I get one error when I try running the above code, which is ‘use of undeclared identifier hpass’…

---

<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 26, 2020, 4:42pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/27 "2020-03-26T16:42:51Z")

</div>

As I said in the text `hpass` will be one of the histogram that you have at the beginning and you use to compute the efficiency .In the code you showed above is for example, `k1`:

```auto
TH1D *k1 = (TH1D*)f->Get("notrigger");

```

Lorenzo

---

<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, 4:47pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/28 "2020-03-26T16:47:59Z")

</div>

Ah yes of course, I’m sorry! Thanks, I think it works!

---

<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, 5:01pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/29 "2020-03-26T17:01:19Z")

</div>

I am sorry to bother you again, but I was wondering two things:

1. How can I now use the bins that it found with the FindBinsAbove function
2. I asked it to find the bins for which y lies between 0.8 and 1, but then I got a warning saying that y is an invalid axis and x is used instead, which is because I am using a 1D histogram I guess? But so, I am not able to select my y (which is my efficiency) to lie between 0.8 and 1?  
Many thanks!

---

<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:12am UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/30 "2020-03-27T10:12:38Z")

</div>

HI,

You can find from the bin the minimum and maximum value that you can use to set then the range in the function to fit. To do that do:

```auto
double xmin = histEff->GetXaxis()->GetBinLowEdge(histEff->FindFirstBinAbove(yMin));
double xmax = histEff->GetXaxis()->GetBinUpEdge(histEff->FindFirstBinAbove(yMax)-1);

```

Lorenzo

---

<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, 4:06pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/31 "2020-03-27T16:06:32Z")

</div>

Hi Lorenzo! I tried to do what you suggested, but the problem is that my fitted function only fits a range where my y = 0 (I have attached a picture of what it looks like when I tried fitting the red curve). I am not sure what went wrong… Thanks!  
[fit.pdf](https://root-forum.cern.ch/uploads/short-url/sx8pHKl4IVYHZDjH1XgsrbcPYdn.pdf) (52.3 KB)

---

<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 28, 2020, 10:27pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/32 "2020-03-28T22:27:40Z")

</div>

Hi,  
I think there is something wrong with the fitting range. The best is that you upload your macro and your histogram you are fitting in a ROOT file

Lorenzo

---

<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 29, 2020, 1:25pm UTC](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516/33 "2020-03-29T13:25:03Z")

</div>

Yes sure! Here is my macro and file with the histograms I used (in this macro I am only trying to fit the first curve, J20).  
[histossss.root](https://root-forum.cern.ch/uploads/short-url/zl2OPO1TcVf5KCwRfweGbWfrAiK.root) (22.8 KB)  
[fitting.C](https://root-forum.cern.ch/uploads/short-url/qIJPMNuu9sfvgoCsz1nhQCbj2Qn.C) (1.3 KB)

[Previous page](https://root-forum.cern.ch/t/fit-function-uses-initial-parameter-values/38516.md?page=1)
