# Alternative for TSpectrum?

**URL:** <https://root-forum.cern.ch/t/alternative-for-tspectrum/49984>\
**Category:** Newbie\
**Created:** [May 16, 2022, 8:52am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984 "2022-05-16T08:52:52Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![afyqazraei](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afyqazraei/32/29688_2.png) [@afyqazraei](https://root-forum.cern.ch/u/afyqazraei)\
**Post date:** [May 16, 2022, 8:52am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/1 "2022-05-16T08:52:52Z")

</div>

Hi!

We are currently doing some spectrum analysis on a dataset obtained from a fiber scintillator detector setup. Our old code uses TSpectrum in it. Since it’s mentioned that TSpectrum is now legacy, are there any general alternatives to use it? Is it possible to use RooFit or RooStats?

\_ROOT Version: 6.26.02

Best regards,  
Joey

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [May 16, 2022, 10:47am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/2 "2022-05-16T10:47:08Z")

</div>

Hi @afyqazraei; I am sure @moneta or @couet know.

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 16, 2022, 10:50am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/3 "2022-05-16T10:50:45Z")

</div>

I do not think there a one to one replacement. Depending on how you are using TSpectrum there might be several other possibilities. @moneta can tell you I am sure.

@moneta: may be il will be good to add your reply on the [TSpectrum page](https://root.cern/doc/master/classTSpectrum.html).

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 16, 2022, 11:22am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/4 "2022-05-16T11:22:05Z")

</div>

Hi,

Yes in principle it should be possible to use RooFit for such analysis.However TSpectrum will not be removed, one can continue to use it as it is, there will be only no new developments or fixes in the code.  
Which kind of functionality of TSpectrum are you using it ?

Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [May 16, 2022, 12:56pm UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/5 "2022-05-16T12:56:01Z")

</div>

hmm, This is now the x-th time that I have seen this kind of answer. Wording like “in principle” and “possible” are not very encouraging. Could anyone then fill in the blanks with RooFit.

Why not take one of the TSpectrum tutorials and do it in RooFit 😉

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 16, 2022, 1:21pm UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/6 "2022-05-16T13:21:51Z")

</div>

TSpectrum has also some other functionalities like the “peak finder” that’s why Lorenzo asked which functionality is requested here. That said, as I suggested, I might be good to update the TSpectrum page with a list of “replacements” and some examples showing how to do it.

---

<div class="post-metadata">

**Author:** ![Eddy\_Offermann](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eddy_offermann/32/25079_2.png) [@Eddy\_Offermann](https://root-forum.cern.ch/u/Eddy_Offermann)\
**Post date:** [May 16, 2022, 2:32pm UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/7 "2022-05-16T14:32:21Z")

</div>

That is why I asked. In the end they are all wrappers around Minuit

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 16, 2022, 4:06pm UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/8 "2022-05-16T16:06:17Z")

</div>

TSpectrum provides also some specific algorithm developed by the original author, which we are not sure if these are actually used and they are the standard in the spectrum community. Examples include deconvolution, unfolding, background estimation, etc,…  
Some of this will be possible with existing ROOT classes, e.g. RooFit, TUnfold, etc…, others  
maybe not.  
I think first is important to understand first which functionality in TSpectrum is really used and in this case we can provide some replacement examples.

Lorenzo

---

<div class="post-metadata">

**Author:** ![afyqazraei](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afyqazraei/32/29688_2.png) [@afyqazraei](https://root-forum.cern.ch/u/afyqazraei)\
**Post date:** [May 17, 2022, 1:50am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/9 "2022-05-17T01:50:00Z")

</div>

Thanks for the quick response.

To answer @moneta 's question, we use TSpectrum only to find the position of the first two peaks in the photon-electron spectrum of our dataset. Those x-axis position values are then later used for fitting Gaussian functions within a range of the peaks. The fits are then later used for normalization procedures and so on.

Below is an example of the output after fitting.

![Screenshot 2022-05-17 at 10.40.13](https://root-forum.cern.ch/uploads/default/original/3X/5/8/585a1ea65e48a5f756b45c3d0fc54e3ca92479ac.png)

The current code we’re running:

```auto
  // DECLARE HISTO
  h_X = new TH1F();
  h_Y = new TH1F();
  
  // DECLARE TSPECTRUM
  TSpectrum *s_X = new TSpectrum(2);
  TSpectrum *s_Y = new TSpectrum(2);

  // DECLARE GAUSSIAN TF1
  for(int i_count=0; i_count < maxpeaks; i_count++){
    f_X[i_count] = new TF1(Form("f_X%d",i_count), "gaus");
    f_Y[i_count] = new TF1(Form("f_Y%d",i_count), "gaus");
  }

  c1 = new TCanvas("c1","c1");
  
  // BEGIN--Channel loop
  for(int i_Ch=0; i_Ch < max_Ch; i_Ch++){
    
    // RETRIEVE HISTOS
    h_X = (TH1F*)folder_X->Get(Form("ch%i", i_Ch));
    h_Y = (TH1F*)folder_Y->Get(Form("ch%i", i_Ch));

    h_X->SetTitle(Form("ch%i X;ADC;Counts", i_Ch));
    h_Y->SetTitle(Form("ch%i Y;ADC;Counts", i_Ch));
    
    // TSPECTRUM::SEARCH
    s_X->Search(h_X, 2, "", 0.0001);
    s_Y->Search(h_Y, 2, "", 0.0001);
    
    nMaxPeak_X = s_X->GetNPeaks();
    nMaxPeak_Y = s_Y->GetNPeaks();

    x_MaxPeak_X = s_X->GetPositionX()[0];
    x_MaxPeak_Y = s_Y->GetPositionX()[0];
        
    // X-AXIS FIBERS
    for(int i_peak=0; i_peak < nMaxPeak_X; i_peak++){//for X
      
      x_peak_X = s_X->GetPositionX()[i_peak];
      
      if(x_peak_X < x_MaxPeak_X) continue;
      
      // FIT TF1 TO HISTOS
      f_X[i_peak]->SetParameter(1, x_peak_X);
      h_X->Fit(Form("f_X%i", i_peak),"+","", x_peak_X-10, x_peak_X+10);
      
      x_mean_X = f_X[i_peak]->GetParameter(1);
      
      std::cout << "X: "<< i_Ch << "\t" << i_peak << "\t" << x_mean_X << std::endl;
    }
    

```

Best,  
Joey

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 17, 2022, 7:24am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/10 "2022-05-17T07:24:07Z")

</div>

Hi

Thank you for the answer. The peak finding position is one of the unique feature of TSpectrum. In case we deprecate the class we will need to add an alternative implementation in the ROOT Histogram library

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [May 19, 2022, 9:48pm UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/11 "2022-05-19T21:48:11Z")

</div>

Related: I frequently use the SNIP algorithm implemented in the TSpectrum::Background function, [ROOT: TSpectrum Class Reference](https://root.cern.ch/doc/master/classTSpectrum.html#a7830480612d1ee301964e6c79319f444)

Is there a suggested alternative to it? Thanks!

---

<div class="post-metadata">

**Author:** ![afyqazraei](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/afyqazraei/32/29688_2.png) [@afyqazraei](https://root-forum.cern.ch/u/afyqazraei)\
**Post date:** [May 20, 2022, 12:25am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/12 "2022-05-20T00:25:13Z")

</div>

I assume that means there’s no present alternative for use. Hope that a new implementation will be made available soon, possibly integrated into RooStats or RooFit (not that I would not use the legacy class at all).

Best,  
Joey

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [May 20, 2022, 7:29am UTC](https://root-forum.cern.ch/t/alternative-for-tspectrum/49984/13 "2022-05-20T07:29:03Z")

</div>

Hi,

There is no direct alternative for such specific algorithm of gamma or X ray spectra, as the one mentioned. We don’t have also the needed expertise in ROOT to provide and maintain such algorithms.  
What you can do, is estimate the background using a more general approach based on modelling the likelihood function using RooFit.

Best regards

Lorenzo
