Problem in fitting pt histogram

Dear Experts,
I have been trying to fit a histogram, but the status of the fit always shows FAILED. Moreover, the final values of the fitted parameters are strongly dependent on the initial values that I provide. I have attached the data file(pt.root) and the code (fit_pt.C) for reference.

PS: I am relatively new to this, so it is highly likely that I am missing something obvious.

Thanks a lot in advance!!
pt.root (4.04 KB)
fit_pt.C (4.72 KB)


I took a look to the program and simplified it here* a bit to better isolate the problem.
My fear is that this fit is too complex: how was the function distilled? Is there a way to get rid of 2-3 parameters? And to fix them to a sensible value after some heuristics?


TH1D* histogram();//function to read the data from a root file
double thefunction(double* px,double* p); //nction which defines the mathematical function used in fitting

void fit_pt() {
   TH1D* hsum = histogram();
   c = new TCanvas;

   TF1* func = new TF1("fun",thefunction,3.,25.,8);

   func->SetParameter(0, 2.60070e+03);
   func->SetParameter(1, 0.1495044  );
   func->SetParameter(4, 2.84451    );
   func->SetParameter(5, 92134.6    );
   func->SetParameter(6, -2121.56   );
   func->SetParameter(7, 153.92     );


   for (int i=0;i<8;++i)
      cout << "  p" << i << " = " << func->GetParameter(i)<<";"  <<endl;

TH1D* histogram(){
   TFile* file = TFile::Open("pt.root","read");
   hsum = (TH1D*)file->Get("h1");
   return hsum;

double thefunction(double* px,double* p){

   // muon pT

   Double_t x=*px;
   if( x < 1. ) return 0.;
   Float_t p0,p1,p2,p3, p4, p5, p6, p7;
   p0 = p[0];// 1.17314e+03; //
   p1 = p[1];// 9.64518e-02; //
   p2 = p[2];//-1.78926e-02; //
   p3 = p[3];//-1.73028e-01; //
   p4 = p[4];// 4.78214e+00; //
   p5 = p[5];// 8.89662e+04; //
   p6 = p[6];// 1.96674e+03; //
   p7 = p[7];// 1.31215e-03; //
   //TF1 *f = new TF1("fa","1418.27*(TMath::Exp(0.15*(1-TMath::Exp(-0.012*x)))- 0.242861)*(1/TMath::Power(x,2.96929))*(92177 - 2249*(x)  + 163*(x)*(x))",4,20);

   // norm
   Float_t norm = p0;
   Float_t slope = p1 * ( 1. - TMath::Exp( p2*x)) + p3;
   Float_t den = 1.*TMath::Power(x,-p4);
   // pol
   Float_t pol = p5 + p6*x+p7*x*x;

   return norm * TMath::Exp( slope*x ) * den * pol;



Unfortunately, I do not know how or why this particular function was chosen. I got this from a PhD student, who also does not seem to know why we use it.
I did try to fit the histogram with a function of the form

p_0/(p1 + x^p2)^p3

but this, too failed. I am trying to understand does the criteria, whether the fit is successful or not depend on the initial parameter value? In an ideal case, I suppose the final value of the fit parameter should be independent of the initial value that we give?


yes, a good fit should be independent from the initial values.
My suggestion would be to try and fit a simpler curve as the present one is not a viable solution.