Error: operator '/' divided by zero

Hi guys,
I’ve been working on a fitting routine that’s just not going the way i would like it to! Below is a macro ( 1 file for script, 1 file for function definitions) that worked just fine, until i added the function ngaussian which defines ‘ng’ gaussian distributions, where ‘ng’ is set by the user running the script.

Here is fit.C

#include

//Johnny Goett
//1.28.05

int nb;
int ng;

void fitrout(TH1F *h , Double_t blo , Double_t bhi , Double_t glo , Double_t ghi, int ngauss = 1, int nback = 2)
{

nb = nback;
ng = ngauss;

//Define parameter array
Double_t par[50];

char bkgd[10];
sprintf(bkgd,"pol%d",nb);
cout << bkgd << endl;

//Define background function
TF1 *bkgrd = new TF1("bkgrd" , bkgd ,  blo , bhi );

//Define gaussian peak
TF1 *peak = new TF1("peak" , ngaussian ,  glo , ghi , 3*ng);

//Define gaussian + peak (functions.C must be loaded)
TF1 *sum = new TF1("sum" , fitfunc ,  blo , bhi , 3*ng+1+nb );


h->Fit(bkgrd,"R");
    
    //initialize widths
    for(int i=0; i < ng; i++)
    {
      peak->SetParameter(2+3*i , 0.05);
    }

h->Fit(peak,"R");

//Get bkgrd and peak parameters
peak->GetParameters(&par[0]);
bkgrd->GetParameters(&par[3*ng]);

// for(int i = 0; i<50; i++)
//	cout << par[i] << " ";
// cout << endl;

//Use parameters as inital values for "sum"
sum->SetParameters(par);

//Fit with the sum function
h->Fit(sum,"R");

//Integrate peak....
//sum->Integrate( glo , ghi );

return();

}

Here is functions.C

#include

extern int nb;
extern int ng;

//Quadratic Background
Double_t background(Double_t *x, Double_t *par) {
Double_t ret = 0.0;
for(int i=0; i <= nb; i++) {
ret+=par[i]*pow(x[0],i);
}
return ret;
// return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
}

//Gaussian Peak
Double_t ngaussian(Double_t x, Double_t par) {
Double_t ret = 0.0;
for(int i=0; i < ng; i++) {
if(par[2+3
i]=0.0) par[2+3
i]=1.0e-10;
ret+=par[0+3i]TMath::Exp(-0.5((x[0]-par[1+3i])/par[2+3i])((x[0]-par[1+3i])/par[2+3i]));
}
return ret;
// 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[3
ng]) + ngaussian(x , &par[0]);
}

When i run the script I recieve the error:
Error: operator ‘/’ divided by zero FILE: LINE:17

This repeats indefinitely until i kill ROOT…Not exactly sure where this could be coming from though, as i’ve initialized the widths to .05. My guess that this error messages is not telling me what is really going on.

Thanks for any advice you might have.

Johnny

Hi,

I see at least one C++ issue:

if(par[2+3i]=0.0) par[2+3i]=1.0e-10;

which should read

if(par[2+3i]==0.0) par[2+3i]=1.0e-10;

Since a negative width is not allowed change it to

if(par[2+3i]<=0.0) par[2+3i]=1.0e-10;

Eddy

ps. Ever considered the possiblity of overflow using exp(1.0e+10…)