Fitting with TF1 between ROOT 5 and ROOT 6

I am trying to fit a histogram with an exponential function and constant background.
I have attached the ROOT file that contains the histogram I am trying to fit.
My simple script to fit the histogram is:


#include "TFile.h"
#include "TH1F.h"
#include "TF1.h"
#include "TCanvas.h"

#include <iostream>

void fit(){

  TFile* fIn = new TFile("output.root","READ");
  TH1F* hist = (TH1F*) fIn->Get("hist");

  TCanvas* c1 = new TCanvas("c1","c1");

  TF1* fitting_function = new TF1("fitting_function","expo(0)+pol0(2)");
  hist->Fit("fitting_function","","",0,6);

}

With ROOT 5 (/mnt/misc/sw/x86_64/Debian/8/root/gnu/5.34.32/bin/root), this script produces a reasonable fit (attached as ROOT_5.PNG), and the output is


root [0] .X fit.C
 FCN=104.494 FROM MIGRAD    STATUS=CONVERGED     193 CALLS         194 TOTAL
                     EDM=3.06699e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
  EXT PARAMETER                                   STEP         FIRST
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
   1  p0           5.56220e+00   2.40323e-02   8.08691e-05   3.16005e-02
   2  p1          -6.58121e-01   3.44066e-02   6.18487e-05   2.30452e-04
   3  p2           1.10443e+02   2.83615e+00   5.73207e-03   5.52584e-05

With ROOT 6 (/mnt/misc/sw/x86_64/Debian/8/root/gnu/6.04.02/bin/root), this no longer produces a reasonable fit (attached as ROOT_6.PNG), and the output is


root [0] .X fit.C

****************************************
Minimizer is Linear
Chi2                      = 1.58239e+282
NDf                       =          117
p0                        =      330.075   +/-   4.44962
p1                        =     -87.9062   +/-   3.01891
p2                        =      9.02567   +/-   0.454093

Can someone please tell me what has happened between ROOT 5 and ROOT 6 such that I no longer obtain the same result?





output.root (3.8 KB)

Right before doing the fit, try to add:
fitting_function->SetParameters(1, 1, 1);
Try also:
fitting_function->Print();
and see if in ROOT 6 you get the formula expression in form:
exp([p0]+[p1]*x)+[p2]

[quote=“Pepe Le Pew”]Right before doing the fit, try to add:
fitting_function->SetParameters(1, 1, 1);
Try also:
fitting_function->Print();
and see if in ROOT 6 you get the formula expression in form:
exp([p0]+[p1]*x)+[p2][/quote]

I added fitting_function->SetParameters(1,1,1), and the results are the same as before for both ROOT 5 and ROOT 6.

I added fitting_function->Print() in ROOT 6, and I obtain the formula expression that you mentioned:


.root [0] .X fit.C

****************************************
Minimizer is Linear
Chi2                      = 1.58239e+282
NDf                       =          117
p0                        =      330.075   +/-   4.44962
p1                        =     -87.9062   +/-   3.01891
p2                        =      9.02567   +/-   0.454093
Formula based function:     fitting_function
     fitting_function : expo(0)+pol0(2) Ndim= 1, Npar= 3, Number= 300
 Formula expression:
        exp([p0]+[p1]*x)+[p2]

Any help would be appreciated.

Try:
hist->Fit(fitting_function, “F”, “”, 0, 6);

[quote=“Pepe Le Pew”]Try:
hist->Fit(fitting_function, “F”, “”, 0, 6);[/quote]

That works. Thank you.
Looking at the documentation, the “F” option means “If fitting a polN, switch to minuit fitter”.
Does this mean that ROOT 5 uses MINUIT as the default minimizer, but ROOT 6 does not (and I therefore have to tell it to do so)?
And should I assume that I need to put the “F” flag in most of my fits from now on?

You’re using a quite old ROOT 6 version (maybe you’re facing a bug in it). Try a recent version.