Problem fitting with a Voigt function

Hi,

I am trying to make a fit with the Voigt function, but it always gives some errors.

I have tried these alternatives:

TF1 *vfit;

(1) vfit = new TF1(“vfit”, “voigt”, 0., 180.);
(2) vfit = new TF1(“vfit”, “Voigt”, 0., 180.);
(3) vfit = new TF1(“vfit”, “TMath::Voigt”, 0., 180.);
(4) vfit = new TF1(“vfit”, “TMath::voigt”, 0., 180.);
(5) vfit = new TF1(“vfit”, TMath::Voigt, 0., 180.);
(6) vfit = new TF1(“vfit”, TMath::voigt, 0., 180.);

h1LatScat->Fit(vfit, “RS”);
vfit->SetLineColor(3);
vfit->Draw(“same”);

The alternatives (1), (2), (3), (4) compile (make), but when I run the file it gives:
Error in TFormula::Compile: Bad numerical expression : "voigt"
Error in TF1::TF1: function: vfit/voigt has 0 parameters instead of 1
Error in : function is zombie

The alternatives (5), (6) don’t compile and give the error that the expressions are ambiguous:
call of overloaded ‘TF1(const char [5], Double_t (&)(Double_t, Double_t, Double_t, Int_t), double, double)’ is ambiguous
/home/micaela/root/include/TF1.h:98: note: candidates are: TF1::TF1(const char*, ROOT::Math::ParamFunctor, Double_t, Double_t, Int_t)
/home/micaela/root/include/TF1.h:94: note: TF1::TF1(const char*, Double_t ()(const Double_t, const Double_t*), Double_t, Double_t, Int_t)
/home/micaela/root/include/TF1.h:93: note: TF1::TF1(const char*, Double_t ()(Double_t, Double_t*), Double_t, Double_t, Int_t)

The most curious fact about all of this is if I use:
vfit = new TF1(“vfit”, “gaus”, 0., 180.);
It runs wonderfully…

Thanks in advance,
Micaela

Could you provide the shortest possible running script and data file reproducing your problem? (as attachments)

Rene

Hi and thanks for the reply.

Well, as an attachment is my code in a “small” version… I can’t send any data because the smallest file has 120 MB. The data is produced in Geant4 and corresponds to a 18-MeV proton beam scattered by a water target and I want to fit a Voigt profile to the angular distribution of this scattering.

Thanks in advance, once again,
Micaela
p_for_sending.cpp (11.4 KB)

Since I have similar problem, I will post here.

Questio nis, how to use Voigt function and/or how to correctly define predefined function (e.g. gaus, pol1, exp, etc.).

Here is my code

[code]
#include <TMath.h>
#include <TF1.h>

Double_t funVoigt(Double_t *x, Double_t *par) {
return par[0] * TMath::Voigt(x[0] - par[1], par[2], par[3], 4);
}

int main()
{
TROOT AnalysisDST_Cal1(“Analysis”,“compiled analysis macros”);
TApplication app(“test”, NULL, NULL, NULL, 0);

TF1 * bkg = new TF1("bkg", "pol1", 1095, 1150);
TF1 * fVoigt = new TF1("voigt", funVoigt, 1095, 1150, 4);

TF1 * ftot1 = new TF1("f_total1", "gaus+bkg", 1095, 1150);
TF1 * ftot2 = new TF1("f_total2", "voigt+bkg", 1095, 1150);

printf("\n\ngaus + pol1");
ftot1->Print();
printf("npar1 = %d\n", ftot1->GetNpar());

printf("\n\nvoigt + pol1");
ftot2->Print();
printf("npar2 = %d\n", ftot2->GetNpar());

} [/code]

and here is the output:

[code]Error in TFormula::Compile: Empty String
Error in TF1::TF1: function: f_total2/voigt+bkg has 0 parameters instead of 1

gaus + pol1 f_total1 : gaus+bkg Ndim= 1, Npar= 5, Noper= 3
fExpr[0] = gaus action = 110 action param = 0
fExpr[1] = pol1 action = 130 action param = 101
fExpr[2] = + action = 1 action param = 0
Optimized expression
fExpr[0] = gaus action = 110 action param = 0
fExpr[1] = pol1 action = 130 action param = 101
fExpr[2] = + action = 1 action param = 0
Par 0 p0 = 0
Par 1 p1 = 0
Par 2 p2 = 0
Par 3 p3 = 0
Par 4 p4 = 0
npar1 = 5

voigt + pol1 f_total2 : voigt+bkg Ndim= 0, Npar= 0, Noper= 0
npar2 = 0[/code]

First we see that there is some error when defining function, so second problem is related to the first problem, that there is no correcte “voigt + pol1” function. How to do it correctly?

Search for the string “TF1 objects can reference” in the TF1 class description (see also the TFormula::Analyze method description).

Oh, I missed that I am of type C… Thanks!

Hi,
I know it is a very old topic, but I have exactly the same problem…I used your (1) and got the same errors as you did:

Error in TFormula::Compile: Bad numerical expression : “voigt”
Error in TF1::TF1: function: vfit/voigt has 0 parameters instead of 1
Error in : function is zombie

how did you solve it?