TSpectrum on TGraph

What is the easiest way to utilize TSpectrum on a TGraph?
The TGraph contains several very clean peaks.
I just need a quick method to get their positions (maybe instead of a TSpectrum I should try something else).

As TSpectrum work with TH1 the simplest is may be to convert the TGraph to a TH1.
Have you tried GetHistogram on the TGraph ?

Thanks for your reply.
I did try “g->GetHistogram()”, but it returns an empty histogram (just the axes are right).
I seem to miss some magic call.

Ah yes the histogram in TGraph is used to draw the frame but is not filled.
So you should get the data form the TGraph and fill a 1d histogram with them.
(TGraph::GetX, TGraph::GetY and then TH1::Fill)

O.K. Thanks.
Is there any other simple method for quick peak finding available in ROOT?
(Just a TGraph with 12 peaks, clearly visible, almost no background.)
(I don’t plan to fit the TH1 spectrum - I will fit the TGraph itself, I just need to find these peaks.)


TSpectrum works only on an histogram. So, the simplest thing is to convert the TGraph in a TH1, using for example SetBinContent and SetBinError. If you are using a TGraphErrors, use the errors from the graph, otherwise set some reasonable values or 1. By default the histogram uses sqrt(N), which could not make any sense if the values are not counts or if they are scaled.

Otherwise, for only Graph, you have to write yourself a peak fitting function (e.g. multi-gaussians on top of a polynomial background)

Best Regards


Thanks. I already have a fitting routine for my TGraph. I just needed a quick way to find peaks. I’ll design a simple routine myself (I’ve already got some ideas how).
The question that remains is why there is no “TSpectrum::Search(TGraph *, …)” method (and possibly why TSpectrum works internally with floats only, not doubles). It could be something similar to the “TH1 *” routine (except that one would additionally need to make a copy of the TGraph somewhere inside and call “TGraph::Sort()” on it).