Funky fit "error"

Hello everyone,

So I am fitting a histogram with a Poisson distribution. I do not use directly ROOT, I use g++ to compile an executable which links to ROOT.
Here is the problem which unfortunately I cannot show but just explain:

I run my code -> it works :slight_smile:
The histogram shows up, but the fit is flat when I declared it as Poissonian-> problem :frowning:
I open fit panel. :bulb:
I fit manually a gaussian to the histogram -> it works (that means I get a gaussian fit but it is obviously not what I want) #-o
I fit manually the exact same Poisson distribution that I have on my code -> it works!! :question: :question: :question: :question:

Note that I do not get any error messages during the whole process and that I NEED to first fit a gaussian before fitting Poisson for it to work. (and yes I did try to do exactly that in the code but without success) #-o #-o
Problem is, I have over 1000 histograms to fit and I just can’t do this manually for each of them.

Anyone ever encountered this problem? Can you help?

Here are some portion of my codes which I think could be relevant to the problem:

TH1F *h1[i][j];
  char *hist2name = new char[10];
  for (int v=0; v<i; v++){
    for (int p=0; p<j; p++){
      sprintf(hist2name, "h2v%dp%d",i,j);
      h1[v][p]=new TH1F(hist2name,"",500,0,0.05);
    }
  }

TF1 *f2 = new TF1("f2","[0]*TMath::Power(([1]/[2]),(x/[2]))*(TMath::Exp(-([1]/[2])))/TMath::Gamma((x/[2])+1)",lowerbound,upperbound);

  f2->SetParameters(0.1, 0.1, 0.1);
  h1[1][24]->Fit("f2","R"); // I have many histogram but for now I am just drawing/fitting this one to figure out the problem

Thanks for your help!

This is possibly a problem with initial parameters (some of them are too far away from what they should be). After you “manually” fit your displayed histogram (with Poisson), notice the fit parameters. Put them back into your source code (“f2->SetParameters(…);”) and then run your code again. If it works now, you just need to develop a better initial parameters’ estimation.
And maybe you could also try to play with: http://root.cern.ch/root/html/TF1.html#TF1:SetParLimits
See “TGraph::InitGaus” http://root.cern.ch/root/html/src/TGraph.cxx.html#U0.VsC and/or “TGraph::InitExpo” http://root.cern.ch/root/html/src/TGraph.cxx.html#peTa3C and/or “TGraph::InitPolynom” http://root.cern.ch/root/html/src/TGraph.cxx.html#MkwxrC for examples.

I see… indeed the problem comes from the parameter initialization. Thank you :slight_smile:

However this poses another problem. Like I said I have many histograms to fit and unfortunately many require different parameters from one another. If I use SetParLimits, it doesn’t help because the range gets too big and I receive a warning and the fit is not optimal. So isn’t there an automated way to check for the ideal parameters for example?

Thanks

For every “histo” that you need to “fit”, try to estimate initial parameters playing with:

// 1 = "X axis"
histo->GetMean(1)
histo->GetRMS(1)
// the "X" values of the first and last bins
histo->GetBinCenter(1)
histo->GetBinCenter(histo->GetNbinsX())

// 2 = "Y axis"
histo->GetMean(2)
histo->GetRMS(2)
// the "Y" values of the first and last bins
histo->GetBinContent(1)
histo->GetBinContent(histo->GetNbinsX())
// the "Y" minimum value and its "X" position
histo->GetMinimum()
histo->GetBinCenter(histo->GetMinimumBin())
// the "Y" maximum value and its "X" position
histo->GetMaximum()
histo->GetBinCenter(histo->GetMaximumBin())

See http://root.cern.ch/root/html/TH1.html for details.

Ok that is what I was trying to do… I guess I’ll have to keep playing with the parameters until it works for each of the histograms!

Thanks a lot for your time :slight_smile: