Fitting example with combined functions

I am attempting to fit an existing histogram with a gaussian and a quadratic background for a small region around the peak.

As i am new to root, I have been attempting this following the example on pg 71 of the July 2004 (v 4.08) users guide. The code is also available in $ROOTSYS/tutorials/FitDemo.C

When executing, I get an error:

Limitation: Function can not be defined in a command line or a tempfile
You need to write it in a source file

So after some investigation i decided to proceed with two separate macro’s… for example

[code]
{
gROOT->LoadMacro("/ignatz/s2/clas/g11/functions.C");

//Create function of type fitfunc
TF1 *tofit = new TF1(“tofit” , fitfunc , 0.47 , 0.54 , 6 );

//Fit peak with tofit
clone->Fit(“tofit”);

}

and the file “functions.C” (defining the functions) :

{

//Quadratic Background
Double_t background(Double_t *x, Double_t *par) {
return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
}

//Gaussian Peak
Double_t gausspeak(Double_t *x, Double_t par) {
return par[0]TMath::Exp(-0.5((x[0]-par[1])/par[2])
((x[0]-par[1])/par[2]));
}

//background + gausspeak -> fit function
Double_t fitfunc(Double_t *x, Double_t *par) {
return background(x , par[0] + gausspeak(x , &par[3]);
}

}[/code]

in this scheme, i recieve the error :

Error: Bad source file(unnamed macro) /ignatz/s2/clas/g11/functions.C FILE:/ignatz/s2/clas/g11/functions.C LINE:1
unnamed macro has to be executed by ‘x’ command
*** Interpreter error recovered ***
Error: Unexpected EOF G__exec_statement() FILE:/ignatz/s2/clas/g11/functions.C LINE:1
Advice: You may need to use +P or -p option
*** Interpreter error recovered ***

So instead i tried changing the line gROOT->LoadMacro("/ignatz/s2/clas/g11/functions.C"); to:
gROOT->ProcessLine(".x /ignatz/s2/clas/g11/functions.C");

… in which case it’s back to the same story:

Limitation: Function can not be defined in a command line or a tempfile
You need to write it in a source file FILE:/ignatz/s2/clas/g11/functions.C LINE:4

So… If i try making a void function, say defs() inside of functions.C which contains the function definitions, then replace the original gROOT->LoadMacro("/ignatz/s2/clas/g11/functions.C"); and then run defs() as the next command in test.C, I get:

Error: Bad source file(unnamed macro) /ignatz/s2/clas/g11/functions.C FILE:/ignatz/s2/clas/g11/functions.C LINE:1
unnamed macro has to be executed by ‘x’ command

So what have i missed?
Thanks very much for any help you might offer.

Johnny

You have two problems in your example
-remove the extra “{” at the beginning and “}” at the end of functions.C
-you have a missing parenthesis in function fitfunc (at the end of the return statement

Rene

Thanks for that.
Now i get something like this:

Error: Function background(x,par[0]) is not defined in current scope FILE:/ignatz/s2/clas/g11/functions.C LINE:15
Possible candidates are…
filename line:size busy function type and name
/ignatz/s2/clas/g11/functions.C 4:3 0 public: Double_t background(Double_t* x,Double_t* par);

Hi,

Another C++ mistake, change fitfunction to

return background(x , &par[0]) + gausspeak(x , &par[3]);

Eddy