Re: Can not fit user function

Valery Fine (fine@mail.cern.ch)
Thu, 10 Jul 1997 11:08:33 +0100


On 10 Jul 97 at 10:36, Torsten Henkel wrote:

>
> I want to fit an user function to a profile histogram:
>
> [first part of the macro is sniped]
>
> h1->Draw("sm2:p>>smprof","side==1","profs");
> TF1 *fun2 = new TF1("fun2","[0]+[1]*pow(x,2)",3,10,2);
> fun2->SetParName(0,"const");
> fun2->SetParName(1,"sigma_b");
> fun2->SetParameter(0,0.03);
> fun2->SetParameter(1,0.003);
> smprof->Fit("fun2");
>
> [the rest also]
>
> I'm doing this in a macro, but all I get is a 'segmentation
> violation'. If i comment out the fit part it is working and making
> the histogram, but with it I get the error every time. I tried
> different names and functions but it's still the same. I'm running
> root version 1.01/07 on a Sparc 20. I hope anyone can help.
>

Class TF1 has sevral contructors as follows:

1. class TF1::TF1(char* name, char* formula, Float_t xmin = 0,
Float_t xmax = 1)

2. class TF1::TF1(char* name, void* fcn, Float_t xmin = 0, Float_t
xmax = 1, Int_t npar = 0)

3. class TF1::TF1(char* name, Double_t (*)(Double_t *, Double_t *)
fcn, Int_t npar = 0)

Second parameter ("[0]+[1]*pow(x,2)") of your contructor looks like
you wanted to supply it as "char* formula". This means first ctor
fits your needs. But then you added 3 parameters. From this I
concluded you did want the second one but first.
For second ctor you have to supply the pointer to "REAL" function
but pointer to the character string and it causes the problem you
faced. So try to replace

TF1 *fun2 = new TF1("fun2","[0]+[1]*pow(x,2)",3,10,2);

with

TF1 *fun2 = new TF1("fun2","[0]+[1]*pow(x,2)",3,10);

To check this I did

root [0] TF1 *fun2 = new TF1("fun2","[0]+[1]*pow(x,2)",3,10);
root [1] fun2->SetParameter(0,0.03);
root [2] fun2->SetParameter(1,0.003);
root [3] fun2.Draw()

And it works as expected.

Hope this helps.
Valery
=================================================================
Dr. Valery Fine Telex : 911621 dubna su
-----------
LCTA/Joint Inst.for NuclearRes Phone : +7 09621 6 40 80
141980 Dubna, Moscow region Fax : +7 09621 6 51 45
Russia mailto:fine@main1.jinr.dubna.su

Dr. Valeri Faine
------------ Phone: +41 22 767 6468
CERN FAX : +41 22 767 7910
CH-1211 Geneva, 23 mailto:fine@mail.cern.ch
Switzerland http://nicewww.cern.ch/~fine