xboa
_uphill_downhill_peak_finder.py
Go to the documentation of this file.
1 # This file is a part of xboa
2 #
3 # xboa is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # xboa is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with xboa in the doc folder. If not, see
15 # <http://www.gnu.org/licenses/>.
16 
17 import xboa.Common as common
18 
19 try:
20  import ROOT
21 except ImportError:
22  pass
23 
24 class UphillDownhillPeakFinder(object):
25  """
26  Find peaks in a list of data by looking at first derivative
27  """
28  def __init__(self):
29  """
30  Initialise the peak finder
31  """
32  self.fit = ROOT.TF1("fit","[0]+[1]*x+[2]*x*x+[3]*x*x*x+[4]*x*x*x", 0., 1.)
33 
34  def _get_derivative(self, data):
35  return [data[i+1]-data[i] for i, x in enumerate(data[1:])]
36 
37  def find_peak_errors_derivative(self, data, peak_list, delta_index, draw=False):
38  """
39  Find the error on the peak estimation based on a linear fit to the
40  derivative
41  """
42  derivative = self._get_derivative(data)
43  # we want to fit for x = my + c
44  # c is the peak
45  # chi2 gives estimate of fit errors
46  peak_with_errors = []
47  for peak in peak_list:
48  x_min = max(peak-delta_index, 0)
49  x_max = min(peak+delta_index+1, len(derivative))
50  x_list = range(x_min, x_max)
51  y_list = derivative[x_min:x_max]
52  graph = ROOT.TGraph(len(x_list))
53  for i in range(5):
54  self.fit.SetParameter(i, 0.)
55  for i in range(2, 5):
56  self.fit.FixParameter(i, 0.)
57  self.fit.SetRange(min(y_list), max(y_list))
58  for i, x in enumerate(x_list):
59  y = y_list[i]
60  graph.SetPoint(i, y, x) # note this is intentionally backwards
61  graph.Fit(self.fit, "NO")
62  if draw:
63  #print x_list, y_list
64  canvas = common.make_root_canvas("derivatives")
65  canvas.Draw()
66  canvas.cd()
67  hist, graph = common.make_root_graph("name", x_list, "", y_list, "")
68  hist.Draw()
69  graph.SetMarkerStyle(6)
70  graph.Draw('p')
71  canvas.Update()
72 
73  canvas = common.make_root_canvas("values")
74  canvas.Draw()
75  canvas.cd()
76  hist, graph = common.make_root_graph("name", x_list, "", data[x_min:x_max], "")
77  hist.Draw()
78  graph.SetMarkerStyle(6)
79  graph.Draw('p')
80  canvas.Update()
81  peak_with_errors.append([self.fit.GetParameter(0),
82  self.fit.GetParError(0)])
83  return peak_with_errors
84 
85 
86  def find_peaks(self, data):
87  """
88  Find peaks in the data
89  """
90  derivative = self._get_derivative(data)
91  peak_list = []
92  for i, x in enumerate(derivative[1:]):
93  if derivative[i] > 0. and derivative[i+1] < 0.:
94  peak_list.append(i+1)
95  return peak_list
96 
97  fit_list = []
98 
def find_peak_errors_derivative
Find the error on the peak estimation based on a linear fit to the derivative.
Deprecated accessor to xboa.common module.
Definition: Common.py:1