How to use deconvolution in TSpectrum when data is both negative and positve


I have spectrum with few negative values. I would like to use deconvolution in TSpectrum to extract peaks. However, it doesn’t work well. You can see it in c1.jpg.
I include here my code and my spectrum. Could you please help me to make it work well. Only positive peak is considered as a peak.

Thank you so much,
6901.txt (103 KB)
na.C (1.6 KB)


According to the documentation, … 295f01e05e

the deconvolution works well for positive data. I would suggest to do not subtract the offset that makes the data negative.
I am not expert on this function, so I am afraid I am not able to help you more on this problem

Best Regards


Either before or after you “d->GetXaxis()->SetRange(xmin-1,xmax);”, try to add: if (d->GetMinimum() < 0.0) { TF1 *f = gROOT->GetFunction("pol0"); // use ROOT's pre-defined function f->SetRange(d->GetXaxis()->GetXmin(), d->GetXaxis()->GetXmax()); f->SetParameter(0, TMath::Abs(d->GetMinimum())); d->Add(f); // shift bins } or: if (d->GetMinimum() < 0.0) { TF1 *f = new TF1("f", TString::Format("%25.17g", TMath::Abs(d->GetMinimum())), d->GetXaxis()->GetXmin(), d->GetXaxis()->GetXmax()); d->Add(f); // shift bins delete f; // no longer needed }

Thank you Moneta and Pepe,

I tried the solution of Pepe, however it still does not work well. There is no detail tutorial for respond spectrum, did I make it wrong?

Here is my current result.

Thank very much for your kind help,


You can’t use TSpectrum::Deconvolution for positive and negative data at the same time.
It is property of algorithm.
I have the same problem.

I have contacted with developer of TSpectrum class (Vladislav Matousek) and he told: “My short answer is: Gold deconvolution algorithm is constrained to process positive data only. But this needs further explanation.”.

I am not expert in this theme but, as I understand, if you use only positive data you will have more stable solution.

What should we do in case of positive and negative data at the same time?

  1. use another algorighm.
    For example, Wiener deconvolution.

  2. Transform our signal in positive only.
    pole zero compensation, for example

  3. just say, that values negative values are 0.

Your solution will depends of data meaning.
Do you have x vs y information or it’s histogram with some noise?

Best regards, Vladislav.

Thank you very much, Vladislav

I tried to offset my code in order to get all positive values, but the result does not seem good as you can see in fig that I attached in post #4.

Best regards,
Ngoc Anh,


Could you explain why do you have negative values in 6901.txt?

  501    -102.26
  502   -7.94451
  503   -12.1206
  504   -4.19399
  505   -2.41914
  506   -1.82086
  507   -1.26398
  508    3.71028
  509  -0.601139


Best regards, Vladislav.

Dear Vladislav,

I’m so sorry to answer very late. It was a long vacation in my country.

The reason that I have negative value come from my background subtraction algorithms. In my method, I cannot not subtract background directly, therefore I need to detect background in another experiment, than subtract it. The statistical fluctuation and the instability of devices caused the negative values after subtraction, but the total average of background is near zero.

Thank you Vladislav,


In the first approximation say that
if(bin_value < 0)
bin_value = 0;

And try to minimize negative values using information about this experiment.
TSpectrum works correct.
You just should input appropriate data.

I do not know better solution for this problem.

Best regards, Vladislav.