Wrong Gaussian Fit (data from File)


Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.20/02
_Platform:
_Compiler: gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)


Hello Everyone!

I have a program that produces some results. These results are saved in a .txt file.(*note These results change every time I run the program, but They are almost the same).
I make a Graph with these results and then I want to have a Gaussian Fit.
However, some times I have 3 problems:

  1. Negative mean for the gaussian fit
  2. Invalid result status 4
  3. Big errors for mean and sigma.

I attached 2 files, one of them has data that produces big errors and the other one is for negative mean and invalid result.
(When I run the program my file’s name is poscount that’s why I have this name in my codeposcount(wrong errors).txt (524 Bytes) poscount(invalid|negativemean).txt (494 Bytes) )

So, what is wrong with my data (or with this part of the code?) I can not understand.

TGraph *MyGraph=new TGraph("poscount.txt");
//ROOT::Math::MinimizerOptions::SetDefaultStrategy(0);
//ROOT::Math::MinimizerOptions::SetDefaultMaxFunctionCalls(10000);
//ROOT::Math::MinimizerOptions::SetDefaultTolerance(1); 
MyGraph->Fit("gaus");
MyGraph->Draw("A*");

Thank youuu!!

Hi,

Your data do not look much to follow the shape of a Gaussian function. This is the main reason the fits failed.
However, if you still want to fit them you would need to force to pass some valid initial parameters for the Gaussian. You need to set the values in the function object and use fit option "B" for this.
Here is the example:

TGraph *MyGraph=new TGraph("poscount.txt");
TF1 *  f1 = new TF1("f1","gaus");
f1->SetParameters(10,10,3);
MyGraph->Fit(f1,"B");
MyGraph->Draw("A*");

Lorenzo

Yeah, I tried it now and everything looks fine.

However, I would like to ask you how can I know what numbers I need to define?
I mean, you set (10,10,3), why those numbers?
(I think that they are related with me data)

Thanks a lot,
Elita

Hi,
I set those values looking at the data. The Graphs showed to me that a Gaussian function with constant value=10, mean=10 and sigma=3 could be a good starting point for your data.

Lorenzo

1 Like