30 Find peaks in a list of data points when you have an estimate of the peaks
31 position already, using a quadratic fit (in ROOT) to the peak. This can help
32 to refine a peak estimate e.g. in the presence of noise.
34 The quality of the fit is estimated using the RMS of the residuals compared
35 to the RMS of the actual data within a certain range of the estimated peak.
37 def __init__(self, peak_list, delta_seed, max_delta, draw):
39 Initialise the peak finder
40 - peak_list: [list of ints] list of integer indices of estimated peak
42 - delta_seed: [int] xboa initially considers points within a range given
43 by delta_seed from the estimated peak position to refine
44 the peak; xboa subsequently then varies delta to improve
46 - max_delta: [int] delta cannot increment beyond max_delta.
47 - draw: [bool] set to True to draw the fits. In fit plots, "0" is the
48 estimated peak position
57 def sigma(self, data):
59 Find the standard deviation from the mean of data
61 - data: list of floats containing data
63 Retuns float S(x**2)/N - {S(x)/N}**2 where N is the length of data
65 mean_square_sum = sum([y**2
for y
in data])/len(data)
66 mean_sum_square = sum([y
for y
in data])/len(data)
67 return (mean_square_sum-mean_sum_square**2)**0.5
69 def _peak_fit(self, data, peak_index, delta_fit, delta_bite):
71 Fit the data in the vicinity of peak_index to a quadratic
73 - data: list of floats containing data
74 - peak_index: the index at which the peak is found
75 - delta_fit: the maximum distance from the peak from which data will be
77 - delta_bite: the maximum distance from the peak from which data will be
78 drawn for assessing fit quality
80 Goodness-of-fit is an estimation of how well the data is fitted, based
81 on comparing the standard deviation of the fit residuals to the sigma of
82 data within a small "bite". fit_quality is given by comparing sigma
83 (standard deviation) of data in a small bite with sigma of fit
85 Q = (sigma(res)/sigma(bite)-1.)*N(bite)**0.5
86 sigma(bite) in this sense is taken to be the natural spread of the raw
87 data, and we seek to ensure sigma(res) does not go much above this
90 Return value is a tuple of (fit_quality, histogram, graph, fit)
91 - fit_quality: goodness-of-fit estimation.
93 x_min = max(peak_index-delta_fit, 0)
94 x_max = min(peak_index+delta_fit+1, len(data))
95 x_list = range(x_min-peak_index, x_max-peak_index)
96 y_list = data[x_min:x_max]
97 hist, graph = common.make_root_graph(
"name", x_list,
"", data[x_min:x_max],
"")
99 quadratic =
"[0]+[1]*x+[2]*x*x"
100 self.
fit = ROOT.TF1(
"fit", quadratic, min(y_list), max(y_list))
102 fit.SetRange(min(y_list), max(y_list))
104 fit.ReleaseParameter(i)
105 fit.SetParameter(i, 0.)
107 graph.Fit(fit,
'QNO')
108 delta_list = [y_list[i]-fit.Eval(x)
for i, x
in enumerate(x_list)]
109 sigma_delta = self.
sigma(delta_list)
110 sigma_bite = self.
sigma(y_list[delta_fit-delta_bite:delta_fit+delta_bite])
113 fit_quality = (sigma_delta/sigma_bite-1.)*delta_bite**0.5
114 return fit_quality, hist, graph, fit
118 Find the error on the peak estimation based on a linear fit to the
124 this_max_delta = min(len(data), self.
max_delta)
125 peak_with_errors = []
130 while fit_quality < 1.
and delta_fit < this_max_delta:
139 canvas = common.make_root_canvas(
"values")
143 graph.SetMarkerStyle(6)
145 fit_result = graph.Fit(fit,
"S")
146 self.fit_list.append(fit)
151 fit_result = graph.Fit(fit,
'QNOS')
154 p0 = fit.GetParameter(0)
155 p1 = fit.GetParameter(1)
156 p2 = fit.GetParameter(2)
159 peak_out = [-p1/2./p2+peak, -p1*p1/4./p2+p0]
160 if abs(peak_out[0]-peak) > delta_fit
or \
162 peak_out[0] > len(data):
164 J = numpy.matrix([[0., -1./2./p2, p1/2./p2/p2],
165 [1., -p1/2./p2, p1*p1/4./p2/p2]])
167 CovP = numpy.matrix([
168 [fit_result.GetCovarianceMatrix()(i, j)
for i
in range(3)]
173 print "Fit parameters"
174 print numpy.array([p0, p1, p2])
180 print numpy.array(peak_out)
181 print "Covariances errors"
184 CovPeak = [[CovPeak[i, j]
for i
in range(2)]
for j
in range(2)]
185 peak_with_errors.append({
"x":peak_out[0],
189 return peak_with_errors
193 Find peaks in the data
195 - data list of floats that contains ordinates (y-axis values) of data
196 abscissa are assumed to be data index.
198 Returns a list of indices, each index corresponding to the location of a
202 peak_list = [int(x[
"x"])
for x
in peak_error_list]
def __init__
Initialise the peak finder.
def sigma
Find the standard deviation from the mean of data.
Find peaks in a list of data points when you have an estimate of the peaks position already...
def find_peaks
Find peaks in the data.
def _peak_fit
Fit the data in the vicinity of peak_index to a quadratic.
Deprecated accessor to xboa.common module.
def find_peak_errors
Find the error on the peak estimation based on a linear fit to the derivative.