Re: user defined function fails

From: daniel cussol <>
Date: Thu, 29 Sep 2005 09:46:11 +0200

Roger Mason a écrit :

>I have this function defined in tmp.C:
>#include "Riostream.h"
>#include "TH1.h"
>#include "TF1.h"
>#include "TLegend.h"
>#include "TCanvas.h"
>// Fitting. Based on FittingDemo.C
>// Quadratic background function
>Double_t background(Double_t *x, Double_t *par) {
> return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
>When I try to execute it I get:
>root [1] .x tmp.C
>.x tmp.C
>Limitation: Function can not be defined in a command line or a tempfile
>You need to write it in a source file FILE:tmp.C LINE:11
>*** Interpreter error recovered ***
>As the comment says, it is a fragment taken from FittingDemo.C (which
>runs perfectly, and which I am trying to adapt to my needs).
>Can anyone help?
>Roger Mason

Hello Roger,
 The problem is in the way you call your function. When using ".x", the function name has to match the file name. In your case, your file should be named "background.C". Secondly, no argument is given to your function. The best way to use this function is to compile it and to call it separately. I think that the best is to give you examples.

root[0] .L tmp.C+  //--> compile and load the function defined in tmp.C
root[1] Double_t param[3]={3,2,1}; //--> Declare the parameter array
root[2] Double_t x[1]={2.} //--> Declare the argument array
root[3] background(param,x) //--> return the value at x=2

There is a much more easier way to use this function by declaring a TF1 object

root[0] .L tmp.C+ //--> compile and load the function defined in tmp.C root[1] TF1 *bck=new TF1("bg",background,0,100,3) // --> Create a TF1 object with x ranging from 0 to 100. The last argument is the number of parameters of your function

root[2] bck->SetParameters(3,1,2) //--> Set the parameters values
root[3] bck->Eval(2.) // --> return the value for x=2.
root[4] bck->Draw() // --> draw the function for x ranging from 0 to 100

You can finf more details in the tutorials. You can also have a look at the in-line class description of TF1

I hope this helps.


LPC Caen IN2P3/ENSICAEN/Universite de Caen
Boulevard du Marechal Juin

e-mail :
Tel    : +33-(0)2-31-45-29-73
FAX    : +33-(0)2-31-45-25-49
Received on Thu Sep 29 2005 - 09:43:36 MEST

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:12 MET