Re: [ROOT] user fit-functions in compiled code

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu May 31 2001 - 09:10:27 MEST


Hi Christian,

You will find a short example below that you can run via the interpreter or
compiler.
root > .x myfit.C  
root > .x myfit.C++  (to run with the native compiler)


//--------------file myfit.C
#include "TH1.h"
#include "TF1.h"
   
Double_t fitf(Double_t *x, Double_t *par)
{
   Double_t arg = 0;
   if (par[2]) arg = (x[0] - par[1])/par[2];

   Double_t fitval = par[0]*TMath::Exp(-0.5*arg*arg);
   return fitval;
}
void myfit()
{

   TH1F *hpx = new TH1F("hpx","px distribution",100,-4,4);
   hpx->FillRandom("gaus",25000);
   
// Creates a Root function based on function fitf above
   TF1 *func = new TF1("fitf",fitf,-2,2,3);

// Sets initial values and parameter names
   func->SetParameters(100,0,1);
   func->SetParNames("Constant","Mean_value","Sigma");

// Fit histogram in range defined by function
   hpx->Fit("fitf","r");
}


Rene Brun

Christian Flacco wrote:
> 
> Hello, Rooters,
> 
> I am trying to use the user defined fit-function option to fit a TH1F in
> stand-alone compiled code.  We are running on Linux, using ROOT version
> 2.25/03.
> 
> I use several ROOT classes in this code, and include the following ROOT
> headers:
> 
>     #include "TRandom.h"
>     #include "TF1.h"
>     #include "TH1.h"
>     #include "TMath.h"
> 
> I declare the TF1 pointer:
> 
>     TF1 * scurvef = new TF1("scurve", fitf, -200, 200, 3);
> 
> fitf is declared and defined as follows:
> 
>     Double_t fitf(Double_t*, Double_t*);
> 
>     Double_t fitf(Double_t *x, Double_t *par){
>         double arg = 0;
>         if (par[2]) arg = (x[0]-par[1])/(1.414*par[2]);
>         double fitval = par[0]*0.5*TMath::Erfc(arg);
>         return fitval;
>     }
> 
> I have also tried making the Double_ts into doubles.
> 
> The program crashes at the instantiation of TF1 * scurvef, with a
> segmentation fault.
> 
> Is there something else I need to do to use this method in compiled
> code?  (It runs as a ROOT macro.)  An additional include file needed?
> Any help would be appreciated.
> 
> Thanks!
> --
> Christian J.P. Flacco ~ CJFlacco@LBL.gov ~ 510.486.5451
> 
> http://www-physics.lbl.gov/~cflacco



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:47 MET