Re: user defined function fails

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


Roger Mason a écrit :

>Hello,
>
>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?
>
>Thanks,
>
>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. http://root.cern.ch/root/html/examples/formula1.C.html You can also have a look at the in-line class description of TF1 http://root.cern.ch/root/html/TF1.html

I hope this helps.

-- 
Daniel CUSSOL

LPC Caen IN2P3/ENSICAEN/Universite de Caen
Boulevard du Marechal Juin
14050 CAEN CEDEX

e-mail : cussol_at_in2p3.fr
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