LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
AlgoCFD.cxx
Go to the documentation of this file.
1 //
3 // AlgoCFD source
4 //
6 
7 #include "AlgoCFD.h"
8 #include "UtilFunc.h"
9 
10 #include "fhiclcpp/ParameterSet.h"
11 
12 #include <unordered_map>
13 
14 namespace pmtana {
15 
16  //*********************************************************************
17  AlgoCFD::AlgoCFD(const std::string name) : PMTPulseRecoBase(name)
18  //*********************************************************************
19  {}
20 
21  //*********************************************************************
23  std::unique_ptr<pmtana::RiseTimeCalculatorBase> risetimecalculator,
24  //AlgoCFD::AlgoCFD(const ::fcllite::PSet &pset,
25  const std::string name)
26  : PMTPulseRecoBase(name)
27  //*********************************************************************
28  {
29 
30  _F = pset.get<float>("Fraction");
31  _D = pset.get<int>("Delay");
32 
33  //_number_presample = pset.get<int> ("BaselinePreSample");
34  _peak_thresh = pset.get<double>("PeakThresh");
35  _start_thresh = pset.get<double>("StartThresh");
36  _end_thresh = pset.get<double>("EndThresh");
37 
38  _risetime_calc_ptr = std::move(risetimecalculator);
39 
40  Reset();
41  }
42 
43  //***************************************************************
45  //***************************************************************
46  {
48  }
49 
50  //***************************************************************
52  const pmtana::PedestalMean_t& mean_v,
53  const pmtana::PedestalSigma_t& sigma_v)
54  //***************************************************************
55  {
56 
57  Reset();
58 
59  std::vector<double> cfd;
60  cfd.reserve(wf.size());
61 
62  // follow cfd procedure: invert waveform, multiply by constant fraction
63  // add to delayed waveform.
64  for (unsigned int k = 0; k < wf.size(); ++k) {
65 
66  auto delayed = -1.0 * _F * ((float)wf.at(k) - mean_v.at(k));
67 
68  if ((int)k < _D)
69 
70  cfd.push_back(delayed);
71 
72  else
73 
74  cfd.push_back(delayed + ((float)wf.at(k - _D) - mean_v.at(k)));
75  }
76 
77  // Get the zero point crossings, how can I tell which are meaningful?
78  // go to each crossing, see if waveform is above pedestal (high above pedestal)
79 
80  auto crossings = LinearZeroPointX(cfd);
81 
82  // lambda criteria to determine if inside pulse
83 
84  auto in_peak = [&wf, &sigma_v, &mean_v](int i, float thresh) -> bool {
85  return wf.at(i) > sigma_v.at(i) * thresh + mean_v.at(i);
86  };
87 
88  // loop over CFD crossings
89  for (const auto& cross : crossings) {
90 
91  if (in_peak(cross.first, _peak_thresh)) {
93 
94  int i = cross.first;
95 
96  //backwards
97  while (in_peak(i, _start_thresh)) {
98  i--;
99  if (i < 0) {
100  i = 0;
101  break;
102  }
103  }
104  _pulse.t_start = i;
105 
106  //walk a little further backwards to see if we can get 5 low RMS
107  // while ( !in_peak(i,_start_thresh) ) {
108  // if (i == ( _pulse.t_start - _number_presample ) ) break;
109  // i--;
110  // if ( i < 0 ) { i = 0; break; }
111  // }
112 
113  // auto before_mean = double{0.0};
114 
115  // if ( _pulse.t_start - i > 0 )
116  // before_mean = std::accumulate(std::begin(mean_v) + i,
117  // std::begin(mean_v) + _pulse.t_start, 0.0) / ((double) (_pulse.t_start - i));
118 
119  i = _pulse.t_start + 1;
120 
121  //forwards
122  while (in_peak(i, _end_thresh)) {
123  i++;
124  if (i > (int)(wf.size()) - 1) {
125  i = (int)(wf.size()) - 1;
126  break;
127  }
128  }
129 
130  _pulse.t_end = i;
131 
132  // //walk a little further forwards to see if we can get 5 low RMS
133  // while ( !in_peak(i,_end_thresh) ) {
134  // if (i == ( _pulse.t_end + _number_presample ) ) break;
135  // i++;
136  // if ( i > wf.size() - 1 ) { i = wf.size() - 1; break; }
137  // }
138 
139  // auto after_mean = double{0.0};
140 
141  // if( i - _pulse.t_end > 0)
142  // after_mean = std::accumulate(std::begin(mean_v) + _pulse.t_end + 1,
143  // std::begin(mean_v) + i + 1, 0.0) / ((double) (i - _pulse.t_end));
144 
145  //how to decide before or after? set before for now
146  //if ( wf.size() < 1500 ) //it's cosmic discriminator
147  //before_mean = mean_v.front();
148 
149  // if( after_mean <= 0 and before_mean <= 0 ) {
150  // std::cerr << "\033[93m<<" << __FUNCTION__ << ">>\033[00m Could not find good pedestal for CDF"
151  // << " both before_mean and after_mean are zero or less? Ignoring this crossing." << std::endl;
152  // continue;
153  // }
154 
155  //x
156 
157  auto start_ped = mean_v.at(_pulse.t_start);
158  auto end_ped = mean_v.at(_pulse.t_end);
159 
160  //just take the "smaller one"
161  _pulse.ped_mean = start_ped <= end_ped ? start_ped : end_ped;
162 
163  if (wf.size() < 50) _pulse.ped_mean = mean_v.front(); //is COSMIC DISCRIMINATOR
164 
165  auto it = std::max_element(std::begin(wf) + _pulse.t_start, std::begin(wf) + _pulse.t_end);
166 
167  _pulse.t_max = it - std::begin(wf);
168  _pulse.peak = *it - _pulse.ped_mean;
169  _pulse.t_cfdcross = cross.second;
170 
171  for (auto k = _pulse.t_start; k <= _pulse.t_end; ++k) {
172  auto a = wf.at(k) - _pulse.ped_mean;
173  if (a > 0) _pulse.area += a;
174  }
175 
176  if (_risetime_calc_ptr)
177  _pulse.t_rise = _risetime_calc_ptr->RiseTime(
178  {wf.begin() + _pulse.t_start, wf.begin() + _pulse.t_end},
179  {mean_v.begin() + _pulse.t_start, mean_v.begin() + _pulse.t_end},
180  true);
181 
182  _pulse_v.push_back(_pulse);
183  }
184  }
185 
186  // Vic:
187  // Very close in time pulses have multiple CFD
188  // crossing points. Should we check that pulses now have
189  // some multiplicity? No lets just delete them.
190 
191  auto pulses_copy = _pulse_v;
192  _pulse_v.clear();
193 
194  std::unordered_map<unsigned, pulse_param> delta;
195 
196  //unsigned width = 0;
197  for (const auto& p : pulses_copy) {
198 
199  if (delta.count(p.t_start)) {
200  if ((p.t_end - p.t_start) > (delta[p.t_start].t_end - delta[p.t_start].t_start))
201  delta[p.t_start] = p;
202  else
203  continue;
204  }
205  else {
206  delta[p.t_start] = p;
207  }
208  }
209 
210  for (const auto& p : delta)
211  _pulse_v.push_back(p.second);
212 
213  //do the same now ensure t_final's are all unique
214  //width = 0;
215 
216  pulses_copy.clear();
217  pulses_copy = _pulse_v;
218 
219  _pulse_v.clear();
220  delta.clear();
221 
222  for (const auto& p : pulses_copy) {
223 
224  if (delta.count(p.t_end)) {
225  if ((p.t_end - p.t_start) > (delta[p.t_end].t_end - delta[p.t_end].t_start))
226  delta[p.t_end] = p;
227  else
228  continue;
229  }
230  else {
231  delta[p.t_end] = p;
232  }
233  }
234 
235  for (const auto& p : delta)
236  _pulse_v.push_back(p.second);
237 
238  //there should be no overlapping pulses now...
239 
240  return true;
241  }
242 
243  // currently returns ALL zero point crossings, we really just want ones associated with peak...
244  const std::map<unsigned, double> AlgoCFD::LinearZeroPointX(const std::vector<double>& trace)
245  {
246 
247  std::map<unsigned, double> crossing;
248 
249  //step through the trace and find where slope is POSITIVE across zero
250  for (unsigned i = 0; i < trace.size() - 1; ++i) {
251 
252  auto si = ::pmtana::sign(trace.at(i));
253  auto sf = ::pmtana::sign(trace.at(i + 1));
254 
255  if (si == sf) //no sign flip, no zero cross
256  continue;
257 
258  if (sf < si) //this is a negative slope, continue
259  continue;
260 
261  //calculate the crossing X based on linear interpolation bt two pts
262 
263  crossing[i] = (double)i - trace.at(i) * (1.0 / (trace.at(i + 1) - trace.at(i)));
264  }
265 
266  return crossing;
267  }
268 
269 }
const std::map< unsigned, double > LinearZeroPointX(const std::vector< double > &trace)
Definition: AlgoCFD.cxx:244
std::vector< double > PedestalSigma_t
virtual void Reset()
A method to be called event-wise to reset parameters.
void Reset()
Implementation of AlgoCFD::reset() method.
Definition: AlgoCFD.cxx:44
double _end_thresh
Definition: AlgoCFD.h:66
pulse_param _pulse
A subject pulse_param object to be filled with the last reconstructed pulse parameters.
M::value_type trace(const M &m)
double _start_thresh
Definition: AlgoCFD.h:65
AlgoCFD(const std::string name="CFD")
Default constructor.
Definition: AlgoCFD.cxx:17
Class definition file of AlgoCFD.
T get(std::string const &key) const
Definition: ParameterSet.h:314
std::vector< short > Waveform_t
int sign(double val)
Definition: UtilFunc.cxx:104
std::unique_ptr< pmtana::RiseTimeCalculatorBase > _risetime_calc_ptr
Tool for rise time calculation.
double _peak_thresh
Definition: AlgoCFD.h:64
bool RecoPulse(const pmtana::Waveform_t &, const pmtana::PedestalMean_t &, const pmtana::PedestalSigma_t &)
Implementation of AlgoCFD::reco() method.
Definition: AlgoCFD.cxx:51
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
Vector cross(Vector const &a, Vector const &b)
Return cross product of two vectors.
std::vector< double > PedestalMean_t
pulse_param_array _pulse_v
A container array of pulse_param struct objects to store (possibly multiple) reconstructed pulse(s)...