Fit with "gaus" converges but doesn't work

Hi, I’m trying to fit an histogram using Fit(“gaus”, “”, “”, xmin, xmax), but even if it converges, it doesn’t work properly, as you ca see in the images (up-left histogram in the image).
The code is the following:

``````TH1F* hpKDiff = new TH1F("hpKDiff", "hpKDiff", 500, 0, 4);
hpKDiff->Fit("gaus", "", "", 0.65, 1.1);
``````

I also tried to change the restiction and the number of bins, but it still doesn’t work.
Another thing I tried is to fit with “gaus” (from the canvas) the down-left histogram, end it does the same thing.
What can be the problem?

Maybe @moneta can give more details, and maybe I’m wrong, but if I understand correctly the TH1::Fit() documentation:

In order to use the Range option, one must first create a function with the expression to be fitted. For example, if your histogram has a defined range between -4 and 4 and you want to fit a gaussian only in the interval 1 to 3, you can do:

`````` TF1 *f1 = new TF1("f1", "gaus", 1, 3);
histo->Fit("f1", "R");
``````

So in your case, can you try something like:

``````TF1 *f1 = new TF1("f1", "gaus", 0.65, 1.1);
hpKDiff->Fit("f1", "R");
``````

I think that is just a different way to do the same think, in fact I tried it but the result is the same.

Hi @Fatess, welcome in the ROOT forum.
It is important to give reasonable starting parameters for the fit.
Try to define the function and set the parameters and then try again the fit.

``````TF1  * f=new TF1("f","gaus",0.65,1.1);
f->SetParameters(7000,0.9,0.04);
hpKDiff->Fit("f1", "R");
``````

Stefano

1 Like

Unfortunately, I’ve just tried it but it did’t work, nothing changed.

I just noticed that I wrote f1 here
` hpKDiff->Fit("f1", "R");`
` hpKDiff->Fit("f", "R");`

Can I ask you how the histogram hpKDiff has been computed, since there are bins with negative value.
Otherwise I think the best way to fit histogram is to use the Likelihood method with the option “L”, but I am not sure if is correct in your case.

1 Like

I noticed it before testing, so it doesn’t work anyway >.<
The histogram has been computed as the difference of this other 2:

with the Add method I wrote in the code above in the post.
Moreover, as I said I tried also with the lower histogram in the post image, right clicking it from the canvas and trying a “gaus” fit, but it did the same thing, and that one is just filled with the Fill method.

You are right, I missed it.

Before do the Add operation call hpKDiff->Sumw2(), otherwise the errors of the bin are not computed correctly.

On the last Histogram the L option should work for sure, set Likelihood as fit method and maybe try to use a narrower range.

1 Like

I already used the Sumw2 method for the two operands, and now I added it for hpKDiff too, but again nothing changed

But even the fit with the Likelihood option on the lower histogram fails, also with the fit range between 0.82 and 0.96?

If also the likelihood fit of the bottom histogram fails, I have really no idea of what is going on

Yes, here both are with the Likelihood

Can you share for the last histogram share a file txt with bin center and the bin content together and the histogram binning ?
I will try to do some test on my own.

1 Like

I was trying to share the .C file with the canvas but it says new users can’t share links (I used the upload button on this forum). How can I send that?

1 Like

It doesn’t work, but I think I can copy and paste it here.

``````void Test()
{
//=========Macro generated from canvas: test/test
//=========  (Fri Oct 29 20:31:58 2021) by ROOT version 6.22/08
TCanvas *test = new TCanvas("test", "test",112,135,750,750);
test->Range(0,0,1,1);
test->SetFillColor(0);
test->SetBorderMode(0);
test->SetBorderSize(2);
test->SetFrameBorderMode(0);

TH1F *hInvMassDecay__5 = new TH1F("hInvMassDecay__5","Inv. Mass, Pion and Kaon, decay products",500,0.5,1.5);
hInvMassDecay__5->SetBinContent(78,1);
hInvMassDecay__5->SetBinContent(90,1);
hInvMassDecay__5->SetBinContent(91,1);
hInvMassDecay__5->SetBinContent(93,1);
hInvMassDecay__5->SetBinContent(96,1);
hInvMassDecay__5->SetBinContent(100,1);
hInvMassDecay__5->SetBinContent(101,1);
hInvMassDecay__5->SetBinContent(102,2);
hInvMassDecay__5->SetBinContent(103,3);
hInvMassDecay__5->SetBinContent(104,2);
hInvMassDecay__5->SetBinContent(105,1);
hInvMassDecay__5->SetBinContent(106,1);
hInvMassDecay__5->SetBinContent(107,4);
hInvMassDecay__5->SetBinContent(108,5);
hInvMassDecay__5->SetBinContent(109,4);
hInvMassDecay__5->SetBinContent(110,1);
hInvMassDecay__5->SetBinContent(111,3);
hInvMassDecay__5->SetBinContent(112,4);
hInvMassDecay__5->SetBinContent(113,9);
hInvMassDecay__5->SetBinContent(114,6);
hInvMassDecay__5->SetBinContent(115,7);
hInvMassDecay__5->SetBinContent(116,9);
hInvMassDecay__5->SetBinContent(117,13);
hInvMassDecay__5->SetBinContent(118,12);
hInvMassDecay__5->SetBinContent(119,17);
hInvMassDecay__5->SetBinContent(120,16);
hInvMassDecay__5->SetBinContent(121,13);
hInvMassDecay__5->SetBinContent(122,23);
hInvMassDecay__5->SetBinContent(123,14);
hInvMassDecay__5->SetBinContent(124,19);
hInvMassDecay__5->SetBinContent(125,26);
hInvMassDecay__5->SetBinContent(126,24);
hInvMassDecay__5->SetBinContent(127,43);
hInvMassDecay__5->SetBinContent(128,36);
hInvMassDecay__5->SetBinContent(129,43);
hInvMassDecay__5->SetBinContent(130,35);
hInvMassDecay__5->SetBinContent(131,56);
hInvMassDecay__5->SetBinContent(132,55);
hInvMassDecay__5->SetBinContent(133,67);
hInvMassDecay__5->SetBinContent(134,90);
hInvMassDecay__5->SetBinContent(135,63);
hInvMassDecay__5->SetBinContent(136,86);
hInvMassDecay__5->SetBinContent(137,96);
hInvMassDecay__5->SetBinContent(138,107);
hInvMassDecay__5->SetBinContent(139,131);
hInvMassDecay__5->SetBinContent(140,135);
hInvMassDecay__5->SetBinContent(141,144);
hInvMassDecay__5->SetBinContent(142,180);
hInvMassDecay__5->SetBinContent(143,150);
hInvMassDecay__5->SetBinContent(144,156);
hInvMassDecay__5->SetBinContent(145,188);
hInvMassDecay__5->SetBinContent(146,257);
hInvMassDecay__5->SetBinContent(147,232);
hInvMassDecay__5->SetBinContent(148,254);
hInvMassDecay__5->SetBinContent(149,259);
hInvMassDecay__5->SetBinContent(150,300);
hInvMassDecay__5->SetBinContent(151,314);
hInvMassDecay__5->SetBinContent(152,329);
hInvMassDecay__5->SetBinContent(153,369);
hInvMassDecay__5->SetBinContent(154,346);
hInvMassDecay__5->SetBinContent(155,449);
hInvMassDecay__5->SetBinContent(156,415);
hInvMassDecay__5->SetBinContent(157,420);
hInvMassDecay__5->SetBinContent(158,459);
hInvMassDecay__5->SetBinContent(159,517);
hInvMassDecay__5->SetBinContent(160,564);
hInvMassDecay__5->SetBinContent(161,622);
hInvMassDecay__5->SetBinContent(162,635);
hInvMassDecay__5->SetBinContent(163,691);
hInvMassDecay__5->SetBinContent(164,713);
hInvMassDecay__5->SetBinContent(165,759);
hInvMassDecay__5->SetBinContent(166,798);
hInvMassDecay__5->SetBinContent(167,805);
hInvMassDecay__5->SetBinContent(168,821);
hInvMassDecay__5->SetBinContent(169,787);
hInvMassDecay__5->SetBinContent(170,951);
hInvMassDecay__5->SetBinContent(171,976);
hInvMassDecay__5->SetBinContent(172,1058);
hInvMassDecay__5->SetBinContent(173,1058);
hInvMassDecay__5->SetBinContent(174,1113);
hInvMassDecay__5->SetBinContent(175,1137);
hInvMassDecay__5->SetBinContent(176,1117);
hInvMassDecay__5->SetBinContent(177,1220);
hInvMassDecay__5->SetBinContent(178,1220);
hInvMassDecay__5->SetBinContent(179,1263);
hInvMassDecay__5->SetBinContent(180,1346);
hInvMassDecay__5->SetBinContent(181,1283);
hInvMassDecay__5->SetBinContent(182,1343);
hInvMassDecay__5->SetBinContent(183,1341);
hInvMassDecay__5->SetBinContent(184,1404);
hInvMassDecay__5->SetBinContent(185,1352);
hInvMassDecay__5->SetBinContent(186,1440);
hInvMassDecay__5->SetBinContent(187,1570);
hInvMassDecay__5->SetBinContent(188,1577);
hInvMassDecay__5->SetBinContent(189,1541);
hInvMassDecay__5->SetBinContent(190,1576);
hInvMassDecay__5->SetBinContent(191,1555);
hInvMassDecay__5->SetBinContent(192,1524);
hInvMassDecay__5->SetBinContent(193,1518);
hInvMassDecay__5->SetBinContent(194,1537);
hInvMassDecay__5->SetBinContent(195,1569);
hInvMassDecay__5->SetBinContent(196,1620);
hInvMassDecay__5->SetBinContent(197,1554);
hInvMassDecay__5->SetBinContent(198,1658);
hInvMassDecay__5->SetBinContent(199,1611);
hInvMassDecay__5->SetBinContent(200,1561);
hInvMassDecay__5->SetBinContent(201,1564);
hInvMassDecay__5->SetBinContent(202,1583);
hInvMassDecay__5->SetBinContent(203,1554);
hInvMassDecay__5->SetBinContent(204,1523);
hInvMassDecay__5->SetBinContent(205,1579);
hInvMassDecay__5->SetBinContent(206,1457);
hInvMassDecay__5->SetBinContent(207,1449);
hInvMassDecay__5->SetBinContent(208,1437);
hInvMassDecay__5->SetBinContent(209,1505);
hInvMassDecay__5->SetBinContent(210,1364);
hInvMassDecay__5->SetBinContent(211,1295);
hInvMassDecay__5->SetBinContent(212,1324);
hInvMassDecay__5->SetBinContent(213,1293);
hInvMassDecay__5->SetBinContent(214,1231);
hInvMassDecay__5->SetBinContent(215,1204);
hInvMassDecay__5->SetBinContent(216,1126);
hInvMassDecay__5->SetBinContent(217,1170);
hInvMassDecay__5->SetBinContent(218,1031);
hInvMassDecay__5->SetBinContent(219,1065);
hInvMassDecay__5->SetBinContent(220,1035);
hInvMassDecay__5->SetBinContent(221,1040);
hInvMassDecay__5->SetBinContent(222,926);
hInvMassDecay__5->SetBinContent(223,887);
hInvMassDecay__5->SetBinContent(224,848);
hInvMassDecay__5->SetBinContent(225,817);
hInvMassDecay__5->SetBinContent(226,833);
hInvMassDecay__5->SetBinContent(227,749);
hInvMassDecay__5->SetBinContent(228,706);
hInvMassDecay__5->SetBinContent(229,641);
hInvMassDecay__5->SetBinContent(230,633);
hInvMassDecay__5->SetBinContent(231,625);
hInvMassDecay__5->SetBinContent(232,596);
hInvMassDecay__5->SetBinContent(233,552);
hInvMassDecay__5->SetBinContent(234,540);
hInvMassDecay__5->SetBinContent(235,510);
hInvMassDecay__5->SetBinContent(236,453);
hInvMassDecay__5->SetBinContent(237,447);
hInvMassDecay__5->SetBinContent(238,397);
hInvMassDecay__5->SetBinContent(239,386);
hInvMassDecay__5->SetBinContent(240,358);
hInvMassDecay__5->SetBinContent(241,328);
hInvMassDecay__5->SetBinContent(242,303);
hInvMassDecay__5->SetBinContent(243,278);
hInvMassDecay__5->SetBinContent(244,267);
hInvMassDecay__5->SetBinContent(245,224);
hInvMassDecay__5->SetBinContent(246,219);
hInvMassDecay__5->SetBinContent(247,197);
hInvMassDecay__5->SetBinContent(248,190);
hInvMassDecay__5->SetBinContent(249,174);
hInvMassDecay__5->SetBinContent(250,159);
hInvMassDecay__5->SetBinContent(251,161);
hInvMassDecay__5->SetBinContent(252,155);
hInvMassDecay__5->SetBinContent(253,107);
hInvMassDecay__5->SetBinContent(254,120);
hInvMassDecay__5->SetBinContent(255,116);
hInvMassDecay__5->SetBinContent(256,91);
hInvMassDecay__5->SetBinContent(257,89);
hInvMassDecay__5->SetBinContent(258,58);
hInvMassDecay__5->SetBinContent(259,73);
hInvMassDecay__5->SetBinContent(260,71);
hInvMassDecay__5->SetBinContent(261,59);
hInvMassDecay__5->SetBinContent(262,39);
hInvMassDecay__5->SetBinContent(263,53);
hInvMassDecay__5->SetBinContent(264,45);
hInvMassDecay__5->SetBinContent(265,43);
hInvMassDecay__5->SetBinContent(266,26);
hInvMassDecay__5->SetBinContent(267,43);
hInvMassDecay__5->SetBinContent(268,21);
hInvMassDecay__5->SetBinContent(269,28);
hInvMassDecay__5->SetBinContent(270,26);
hInvMassDecay__5->SetBinContent(271,20);
hInvMassDecay__5->SetBinContent(272,17);
hInvMassDecay__5->SetBinContent(273,13);
hInvMassDecay__5->SetBinContent(274,16);
hInvMassDecay__5->SetBinContent(275,12);
hInvMassDecay__5->SetBinContent(276,15);
hInvMassDecay__5->SetBinContent(277,8);
hInvMassDecay__5->SetBinContent(278,9);
hInvMassDecay__5->SetBinContent(279,7);
hInvMassDecay__5->SetBinContent(280,5);
hInvMassDecay__5->SetBinContent(281,6);
hInvMassDecay__5->SetBinContent(282,3);
hInvMassDecay__5->SetBinContent(283,6);
hInvMassDecay__5->SetBinContent(284,3);
hInvMassDecay__5->SetBinContent(285,3);
hInvMassDecay__5->SetBinContent(286,2);
hInvMassDecay__5->SetBinContent(288,1);
hInvMassDecay__5->SetBinContent(289,1);
hInvMassDecay__5->SetBinContent(290,1);
hInvMassDecay__5->SetBinContent(291,4);
hInvMassDecay__5->SetBinContent(293,1);
hInvMassDecay__5->SetBinContent(294,2);
hInvMassDecay__5->SetBinContent(295,1);
hInvMassDecay__5->SetEntries(100466);

TPaveStats *ptstats = new TPaveStats(0.78,0.775,0.98,0.935,"brNDC");
ptstats->SetName("stats");
ptstats->SetBorderSize(1);
ptstats->SetFillColor(0);
ptstats->SetTextAlign(12);
ptstats->SetTextFont(42);
ptstats_LaTex->SetTextSize(0.0368);
ptstats_LaTex = ptstats->AddText("Entries = 100466 ");
ptstats_LaTex = ptstats->AddText("Std Dev   = 0.05016");
ptstats->SetOptStat(1111);
ptstats->SetOptFit(0);
ptstats->Draw();
ptstats->SetParent(hInvMassDecay__5);

Int_t ci;      // for color index setting
TColor *color; // for color definition with alpha
ci = TColor::GetColor("#000099");
hInvMassDecay__5->SetLineColor(ci);
hInvMassDecay__5->GetXaxis()->SetLabelFont(42);
hInvMassDecay__5->GetXaxis()->SetTitleOffset(1);
hInvMassDecay__5->GetXaxis()->SetTitleFont(42);
hInvMassDecay__5->GetYaxis()->SetLabelFont(42);
hInvMassDecay__5->GetYaxis()->SetTitleFont(42);
hInvMassDecay__5->GetZaxis()->SetLabelFont(42);
hInvMassDecay__5->GetZaxis()->SetTitleOffset(1);
hInvMassDecay__5->GetZaxis()->SetTitleFont(42);
hInvMassDecay__5->Draw("");
test->Modified();
test->cd();
test->SetSelected(test);
}
``````

No problem here:

``````root [0] .x Test.C
root [1] hInvMassDecay__5->Print("base");
TH1.Print Name  = hInvMassDecay__5, Entries= 100466, Total sum= 100466
Title = Inv. Mass, Pion and Kaon, decay products
NbinsX= 500, xmin= 0.5, xmax=1.5
root [2] ROOT::Math::MinimizerOptions::SetDefaultMinimizer("Minuit2");
root [3] gStyle->SetOptFit(1)
root [4] hInvMassDecay__5->Fit("gaus", "L", "", 0.65, 1.1);

****************************************
MinFCN                    =      119.353
Chi2                      =      232.755
NDf                       =          222
Edm                       =  6.67897e-08
NCalls                    =           56
Constant                  =      1597.99   +/-   6.177
Mean                      =     0.891675   +/-   0.00015829
Sigma                     =    0.0501642   +/-   0.000112075  	 (limited)
``````
1 Like

I’ve just found out that it works if I use the .C file, but it doesn’t work if I do the exact same things on the macro that generates it.
About the up-left one, in the macro the fit is just wrong, in the .C file it actually shows the right values, but graphically it’s diplayed as a stright line