Problems to fit spectrum after energy calibration using TSpectrum and TVirtualFitter


I have been using TSpectrum to perform a peak identification and the corresponding fit. For this, I followed the peaks.C example, which I have modified a little to use it as an external macro, in which I insert the histogram to be fitted and retrieve the fit parameters, you can see the modified macro here
gaussian_fit.C (3.8 KB).

The fit looks pretty well on the pulse high spectrum.

But, after I do the energy calibration, only the peak identification works, and the fit crashes.

For both cases, I have used the same sigma and threshold in “Search”. Both histograms have the same binning. But I have also unsuccessfully tried a large number of binning/sigma/threshold combinations to make it work after calibration.

I perform/store the energy calibration in a new branch in this way

  Float_t alphas_slope, alphas_inter;


  TGraph *Fit_alphas = new TGraph(2,alphas_pointsX,alphas_pointsY);
  TF1 *alphas_calibration = new TF1("alphas_calibration","pol1", 0, 630);
  Fit_alphas->Fit(alphas_calibration,"Q","",0, 630);

  alphas_inter = alphas_calibration->GetParameter(0);
  alphas_slope = alphas_calibration->GetParameter(1);

  for(unsigned int i = lowers[run]; i < uppers[run]; i++)
    if(PH_crrc4_B_2 > && PH_crrc4_B_2 <
      Energy_calibration =  (PH_crrc4_B_2*alphas_slope) + alphas_inter;

I store the calibrated branch in a new root file Energy_calibration_file.root (392.3 KB)
and I do the fit it in a separate macro Gaussian_fit_calibrated_RF.C (4.3 KB).

Even though I fill the histogram using the same binning in each case, I notice there is some difference in the binning between the two histograms before and after calibration, could it be the source of the problem?

I would highly appreciate it if you can help me or give me information to solve this issue.
Or, is there any other (more recommended) way of doing an automatic gaussian fit? I have around 150 spectra to analyze, all of them with different number peaks, and covering different ranges.
I would like to know why is TSpectrum deprecated?

In case you wanna run the macro yourself, the calibrated branch is “Energy_calibration” and the uncalibrated is “PH”. In case you need to do the energy calibration yourself, these are the slope and the intercept

alphas_slope 0.16649 alphas_inter -0.030635

Let me know if you need further information from my side.
Thanks in advance.

ROOT Version: 6.22/02
Platform: Ubuntu 20.04.4 LTS
Compiler: g+±9/gcc-9

For the “calibrated” spectrum, try with: par[3*npeaks+4] = .5; // "sigma" (i.e., 0.16649 * 3)

Thank you! It worked!
Which criteria did you use to select this number?

Looking at your “calibrated” spectrum plot, “sigma = alphas_slope * 3 = 0.5” seemed reasonable.

Actually, maybe a better “universal” initial value could be:
par[3*npeaks+4] = 3. * h->GetXaxis()->GetBinWidth(bin); // "sigma" (i.e., 3 bins wide)

In ROOT, before you try to fit your graph or histogram, you MUST set “reasonable” initial values for ALL parameters of your function (except for some “built-in” formulas, like “gaus”, for which the standard fit procedure can automatically “guess” them), otherwise the fitting procedure may easily misbehave.

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