Multiple fit in multiple sub-range

Dear experts,

I did several pre-fits of sub-ranges, using different functions in the range [0, 2].
Then I used this pre-fits with there parameters in a global fit to fit all the range [0, 2].

What I do not understand is that when my pre-fit is define with the sum of 3 gaussians plus an exponential as explained in … fit.C.html, despite the fact that it fit properly my histogram (in green), when I parse it to the global fit, the global fit (in blue) does not fit properly my histogram.

To do that I used this piece of code [1]. You can see all the code in cc “”, and some plots “plot?.png”.

Do you know if I’m doing something wrong for the right side, or do you have other suggestion for the fit procedure?
I want to mention that I tried to set limit or fix some parameters, but then the global fit would work for some hist, and not the other (almost 1000 hist).
Also in some cases on the right side I can have 1 up to 3 bumps then a tail. That’s the reason why I used the 3 gauss+expo.

void fit(){

//left side ok!

// right side

	  string gaus {"[0]*TMath::Exp(-0.5*TMath::Power((x-[1])/[2],2))"}; 
	  string expo {"TMath::Exp([0]+[1]*x)"};

	  Double_t par[11]; 
	  TF1 *g1    = new TF1("g1",, xbinmax, 1.02);
	  g1->SetParameter(0, hrsp->GetBinContent(hrsp->FindBin(xbinmax))); // norm
	  g1->FixParameter(0, hrsp->GetBinContent(hrsp->FindBin(xbinmax))); 
	  g1->SetParameter(1, xbinmax);  // mean
	  g1->FixParameter(1, xbinmax);
	  g1->SetParameter(2, 0.02);  // sigma
	  g1->SetParLimits(2, 0.001, 3.);

	  TF1 *g2    = new TF1("g2",, 1.02, 1.14);
	  g2->SetParameter(0, 0.1*hrsp->GetBinContent(hrsp->FindBin(xbinmax))); // norm
	  g2->SetParameter(1, xbinmax); // mean
	  g2->SetParameter(2, 0.1); //sigma

	  TF1 *g3    = new TF1("g3",, 1.14, 1.4);
	  g3->SetParameter(0, 0.1*hrsp->GetBinContent(hrsp->FindBin(xbinmax))); // norm
	  g3->SetParameter(1, xbinmax); // mean
	  g3->SetParameter(2, 0.1); //sigma

	  TF1 *exp   = new TF1("exp",, 1.4,2);
	  exp->SetParLimits(1, -10, 0); // slope

         // fit hist with the 3 function defined above
	  hrsp->Fit(g1, "R");
	  hrsp->Fit(g2, "R+");
	  hrsp->Fit(g3, "R+");

          // get their parameters for the total sum fit
	  g1 ->GetParameters(&par[0]);
	  g2 ->GetParameters(&par[3]);
	  g3 ->GetParameters(&par[6]);

          // total sum fit = gaus+gaus+exp
	  TF1 *total{0};
	  total = new TF1("total","[0]*TMath::Exp(-0.5*TMath::Power((x-[1])/[2],2))+[3]*TMath::Exp(-0.5*TMath::Power((x-[4])/[5],2))+[6]*TMath::Exp(-0.5*TMath::Power((x-[7])/[8],2))+TMath::Exp([9]+[10]*x)", xbinmax,2);

	  total->FixParameter(0, par[0]); 

	  // fit histo
	  if(total)     hrsp->Fit(total,   "R+"); 

          // define the global fit
	  TF1* modified_fdscb{0};
	  modified_fdscb = new TF1("modified_fdscb", modified_fnc_dscb, 0, 2, 26);
	    modified_fdscb->SetParameter(15, par[0]); // gauss 1
	    modified_fdscb->FixParameter(15, par[0]); 
	    modified_fdscb->SetParameter(16, par[1]);
	    modified_fdscb->SetParameter(17, par[2]);
	    modified_fdscb->SetParameter(18, par[3]); // gauss 2
	    modified_fdscb->SetParameter(19, par[4]);
	    modified_fdscb->SetParameter(20, par[5]);
	    modified_fdscb->SetParameter(21, par[6]); // gauss 3
	    modified_fdscb->SetParameter(22, par[7]);
	    modified_fdscb->SetParameter(23, par[8]);
	    modified_fdscb->SetParameter(24, par[9]); // exp
	    modified_fdscb->FixParameter(24, par[9]);
	    modified_fdscb->SetParameter(25, par[10]);
	    modified_fdscb->FixParameter(25, par[10]);
	  hrsp->Fit("modified_fdscb", "R+");


// global fit definition
    double modified_fnc_dscb(double*xx,double*pp)
      double x   = xx[0];
      // left side  

      // right side
      double normGaus1   = pp[15]; //gauss1
      double meanGaus1  = pp[16];
      double sigGaus1      = pp[17];
      double normGaus2  = pp[18]; //gauss2
      double meanGaus2 = pp[19];
      double sigGaus2     = pp[20];
      double normGaus3  = pp[21]; //gauss3
      double meanGaus3 = pp[22];
      double sigGaus3     = pp[23];
      double normExp     = pp[24]; // expo
      double slopeExp     = pp[25];

      double result(1);

      // left side fit

      // right side fit
    else if ( x>=mu )    result *= normGaus1 * exp(-0.5*pow(((x-meanGaus1)/sigGaus1),2) ) +
				   normGaus2 * exp(-0.5*pow(((x-meanGaus2)/sigGaus2),2) ) +
 				   normGaus3 * exp(-0.5*pow(((x-meanGaus3)/sigGaus3),2) ) +
			 	   exp( normExp + x*slopeExp );

      return result;


Can you post your code as a full running macro, it is then easy to reproduce the results.
However, th problem is likely in the initial definition of the function parameters or in the definition of the global function. You should check that mathematically the function makes sense for some sensible values of th parameter, for example by plotting it, before fitting

Best Regards


Dear Lorenzo,

thank you for your answer. I used another function to perform the fit. I guess that I was not parsing properly the function to the global fit.