Re: plotting user functions

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Jul 01 1997 - 10:33:41 MEST


Wouter Hulsbergen wrote:
> 
> Dear ROOT people,
> 
> I have written the following code
> 
>   {
>   #include <math.h>
> 
>   float distribution(float pt, float pbar)
>   {
>     float pzbar = sqrt(pbar*pbar-pt*pt) ;
>     float Norm = 1. ;
>     return (pbar*pt/2* (pbar*pzbar + pt*pt*log(pbar+pzbar))
>       - pt/3 * pzbar*pzbar*pzbar - pt*pt*pt * pzbar) /Norm ;
>   };
> 
>   int main()
>   {
>     float pbar = 4. ;
>     TF1 func("func","distribution(x,pbar)",0,pbar) ;
>     func.Draw();
>   }
>   }
> 
> which I want to use to plot the `distribution' function, using
>   root> .L distrib.C ;
>   root> main();
> 
> However, this doesn't work because
>     Unknown name : "distribution(x,pbar)"
> although the function `distribution' is known. Can I use user
> defined functions in a TF1 or TFormula definition?
> Regards, Wouter.

The class TF1 supports several constructors:
  - constructor where you pass the function as one single string.
     examples:
       "sin(x)/x"  ,   "[0]*x/sin([1]*x)"
  - constructor where you pass a pointer to a function
    of type double(double*x, double*params)

I have modified your macro to use the second and more general form:
{
// macro wouter
  double distribution(double *x, double *par)
  {
    double pbar  = par[0];
    double pt    = x[0];
    double pzbar = sqrt(pbar*pbar-pt*pt) ;
    float Norm   = 1. ;
    return (pbar*pt/2* (pbar*pzbar + pt*pt*log(pbar+pzbar))
      - pt/3 * pzbar*pzbar*pzbar - pt*pt*pt * pzbar) /Norm ;
  }
  int main()
  {
    double pbar = 4. ;
    TF1 *func = new TF1("func",distribution,0,pbar,1) ;
    func->SetParameter(0,pbar);
    func->Draw();
  }
}
To execute this macro:
 Root > .L wouter.C
 Root > main()

Note that in your original macro, you add an additional problem>
Your TF1 object func was automatically deleted when exiting
from the scope of main(). You should create the TF1 object via new.

Rene Brun



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:19 MET