Crystalball fitting function

Dear Rooters

Can I use the crystalball fitting function using ROOT 5, it gives me error that this function is not defined.
if not, this means I must define the function manually and fit using user define function method.
but this not working, I have strange fitting function

/****************************************/
Double_t crystalball(Double_t *x, Double_t *par)


{

double A  =  pow((par[1]/par[0]),2)* TMath::Exp(-par[0]*par[0]/2);
double B  =  (par[1]/par[0])-par[0];
double C  =  (par[1]/par[0]) *(1/(par[1]-1))* TMath::Exp(-par[0]*par[0]/2);
double D  =  TMath::Sqrt(TMath::Pi()/2)*(1+TMath::Erf(par[0]/TMath::Sqrt(2)));
double N= 1/(par[3]*(C+D));
double fitval = 0.;

		
		
		if( (x[0]-par[2])/par[3] > -par[0])
			fitval = N*TMath::Exp   (-0.5*pow((x[0]-par[2])/par[3],2));
		else
			fitval = A*(B- pow(((x[0]-par[2])/par[3] ),(-par[1])));	
			//fitval = N*TMath::Exp   (-0.5*pow((x[0]-par[2])/par[3],2))  ;
	

		return fitval;
}
...
...
...

TF1 *func = new TF1("crystalball",crystalball,3,8,4);

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

calibrated_energy->Fit("crystalball","XR");
/****************************************************/

this is my defined function and the fitting lines, do I have wrong estimation for the initial parameters??

please any suggestion for the best fitting

I am sorry for the bad quality image

//
// The "crystalball" function for ROOT 5.x (mimics ROOT 6.x).
//
// Create the "crystalball" TF1 somewhere in your source code using:
// double xmin = 3., xmax = 8.; // whatever you need
// TF1 *crystalball = new TF1("crystalball", crystalball_function, xmin, xmax, 5);
// crystalball->SetParNames("Constant", "Mean", "Sigma", "Alpha", "N");
// crystalball->SetTitle("crystalball"); // not strictly necessary
//

// #include "TMath.h"
#include <cmath>

// see math/mathcore/src/PdfFuncMathCore.cxx in ROOT 6.x
double crystalball_function(double x, double alpha, double n, double sigma, double mean) {
  // evaluate the crystal ball function
  if (sigma < 0.)     return 0.;
  double z = (x - mean)/sigma; 
  if (alpha < 0) z = -z; 
  double abs_alpha = std::abs(alpha);
  // double C = n/abs_alpha * 1./(n-1.) * std::exp(-alpha*alpha/2.);
  // double D = std::sqrt(M_PI/2.)*(1.+ROOT::Math::erf(abs_alpha/std::sqrt(2.)));
  // double N = 1./(sigma*(C+D));
  if (z  > - abs_alpha)
    return std::exp(- 0.5 * z * z);
  else {
    //double A = std::pow(n/abs_alpha,n) * std::exp(-0.5*abs_alpha*abs_alpha);
    double nDivAlpha = n/abs_alpha;
    double AA =  std::exp(-0.5*abs_alpha*abs_alpha);
    double B = nDivAlpha -abs_alpha;
    double arg = nDivAlpha/(B-z);
    return AA * std::pow(arg,n);
  }
}

double crystalball_function(const double *x, const double *p) {
  // if ((!x) || (!p)) return 0.; // just a precaution
  // [Constant] * ROOT::Math::crystalball_function(x, [Alpha], [N], [Sigma], [Mean])
  return (p[0] * crystalball_function(x[0], p[3], p[4], p[2], p[1]));
}
1 Like

https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#file-menu
TPad::SaveAs

@Wile_E_Coyote thank you for the reply and defining the crystal-ball function, that was really helpful and the fitting is going perfectly now :slight_smile:

Noor

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.