Fit function uses initial parameter values

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


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 or TH1::FindLastBinAbove


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

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

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!

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 histEff = (TH1*) hpass->Clone();
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


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

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:

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


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

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!


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:

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


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 (52.3 KB)

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


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 (22.8 KB)
fitting.C (1.3 KB)