All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WaveformTools_tool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file WaveformTools_tool.cc
3 /// \author T. Usher
4 ////////////////////////////////////////////////////////////////////////
5 
6 #include <cmath>
7 #include <numeric> // std::inner_product
9 #include "art/Utilities/ToolMacros.h"
10 
11 #include "TProfile.h"
12 #include "TVirtualFFT.h"
13 
14 namespace reco_tool
15 {
16 
18 {
19 public:
20  explicit WaveformTools(const fhicl::ParameterSet& pset);
21 
23 
24  void configure(const fhicl::ParameterSet& pset) override;
25 
26  using PeakTuple = std::tuple<size_t,size_t,size_t>; // first bin, peak bin, last bin
27  using PeakTupleVec = std::vector<PeakTuple>;
28 
29  void triangleSmooth(const std::vector<float>&, std::vector<float>&, size_t = 0) const override;
30  void triangleSmooth(const std::vector<double>&, std::vector<double>&, size_t = 0) const override;
31  void medianSmooth( const std::vector<float>&, std::vector<float>&, size_t = 3) const override;
32  void medianSmooth( const std::vector<double>&, std::vector<double>&, size_t = 3) const override;
33  void getTruncatedMeanRMS(const std::vector<double>&, double&, double&, double&, int&) const override;
34  void getTruncatedMeanRMS(const std::vector<float>&, float&, float&, float&, int&) const override;
35  void firstDerivative(const std::vector<float>&, std::vector<float>&) const override;
36  void firstDerivative(const std::vector<double>&, std::vector<double>&) const override;
37  void findPeaks(std::vector<float>::iterator, std::vector<float>::iterator, PeakTupleVec&, float, size_t) const override;
38  void findPeaks(std::vector<double>::iterator, std::vector<double>::iterator, PeakTupleVec&, double, size_t) const override;
39  void getFFTPower(const std::vector<float>& inputVec, std::vector<float>& outputPowerVec) const override;
40  void getFFTPower(const std::vector<double>& inputVec, std::vector<double>& outputPowerVec) const override;
41 
43  int,
44  HistogramMap&,
48  Waveform<short>&) const override;
50  int,
51  HistogramMap&,
55  Waveform<float>&) const override;
57  int,
58  HistogramMap&,
62  Waveform<double>&) const override;
63 
67 
68 private:
69  template <typename T> void triangleSmooth(const std::vector<T>&, std::vector<T>&, size_t = 0) const;
70  template <typename T> void medianSmooth( const std::vector<T>&, std::vector<T>&, size_t = 3) const;
71  template <typename T> void getTruncatedMeanRMS(const std::vector<T>&, T&, T&, T&, int&) const;
72  template <typename T> void firstDerivative(const std::vector<T>&, std::vector<T>&) const;
73  template <typename T> void findPeaks(typename std::vector<T>::iterator, typename std::vector<T>::iterator, PeakTupleVec&, T, size_t) const;
74 
75  template <typename T> void getErosionDilationAverageDifference(const Waveform<T>&,
76  int,
77  HistogramMap&,
78  Waveform<T>&,
79  Waveform<T>&,
80  Waveform<T>&,
81  Waveform<T>&) const;
82 
83  template <typename T> void getOpeningAndClosing(const Waveform<T>&, const Waveform<T>&, int, HistogramMap&, Waveform<T>&, Waveform<T>&) const;
84 };
85 
86 //----------------------------------------------------------------------
87 // Constructor.
88 WaveformTools::WaveformTools(const fhicl::ParameterSet& pset)
89 {
90  configure(pset);
91 }
92 
93 void WaveformTools::configure(const fhicl::ParameterSet& pset)
94 {
95  // Start by recovering the parameters
96 // fThisPlane = pset.get<size_t>("Plane");
97 
98  return;
99 }
100 
101 void WaveformTools::triangleSmooth(const std::vector<double>& inputVec, std::vector<double>& smoothVec, size_t lowestBin) const
102 {
103  triangleSmooth<double>(inputVec, smoothVec, lowestBin);
104 
105  return;
106 }
107 
108 void WaveformTools::triangleSmooth(const std::vector<float>& inputVec, std::vector<float>& smoothVec, size_t lowestBin) const
109 {
110  triangleSmooth<float>(inputVec, smoothVec, lowestBin);
111 
112  return;
113 }
114 
115 template <typename T> void WaveformTools::triangleSmooth(const std::vector<T>& inputVec, std::vector<T>& smoothVec, size_t lowestBin) const
116 {
117  if (inputVec.size() != smoothVec.size()) smoothVec.resize(inputVec.size());
118 
119  // Watch for edge condition
120  if (inputVec.size() > 4)
121  {
122  std::copy(inputVec.begin(), inputVec.begin() + 2 + lowestBin, smoothVec.begin());
123  std::copy(inputVec.end() - 2, inputVec.end(), smoothVec.end() - 2);
124 
125  typename std::vector<T>::iterator curItr = smoothVec.begin() + 2 + lowestBin;
126  typename std::vector<T>::const_iterator curInItr = inputVec.begin() + 1 + lowestBin;
127  typename std::vector<T>::const_iterator stopInItr = inputVec.end() - 3;
128 
129  while(curInItr++ != stopInItr)
130  {
131  // Take the weighted average of five consecutive points centered on current point
132  T newVal = (*(curInItr - 2) + 2. * *(curInItr - 1) + 3. * *curInItr + 2. * *(curInItr + 1) + *(curInItr + 2)) / 9.;
133 
134  *curItr++ = newVal;
135  }
136  }
137  else std::copy(inputVec.begin(), inputVec.end(), smoothVec.begin());
138 
139  return;
140 }
141 
142 void WaveformTools::medianSmooth(const std::vector<float>&inputVec, std::vector<float>& smoothVec, size_t nBins) const
143 {
144  medianSmooth<float>(inputVec, smoothVec, nBins);
145 
146  return;
147 }
148 
149 void WaveformTools::medianSmooth(const std::vector<double>& inputVec, std::vector<double>& smoothVec, size_t nBins) const
150 {
151  medianSmooth<double>(inputVec, smoothVec, nBins);
152 
153  return;
154 }
155 
156 template <typename T> void WaveformTools::medianSmooth(const std::vector<T>& inputVec, std::vector<T>& smoothVec, size_t nBins) const
157 {
158  // For our purposes, nBins must be odd
159  if (nBins % 2 == 0) nBins++;
160 
161  // Make sure the input vector is right sized
162  if (inputVec.size() != smoothVec.size()) smoothVec.resize(inputVec.size());
163 
164  // Basic set up
165  typename std::vector<T> medianVec(nBins);
166  typename std::vector<T>::const_iterator startItr = inputVec.begin();
167  typename std::vector<T>::const_iterator stopItr = startItr;
168 
169  std::advance(stopItr, inputVec.size() - nBins);
170 
171  size_t medianBin = nBins/2;
172  size_t smoothBin = medianBin;
173 
174  // First bins are not smoothed
175  std::copy(startItr, startItr + medianBin, smoothVec.begin());
176 
177  while(std::distance(startItr,stopItr) > 0)
178  {
179  std::copy(startItr,startItr+nBins,medianVec.begin());
180  std::sort(medianVec.begin(),medianVec.end());
181 
182  T medianVal = medianVec[medianBin];
183 
184  smoothVec[smoothBin++] = medianVal;
185 
186  startItr++;
187  }
188 
189  // Last bins are not smoothed
190  std::copy(startItr + medianBin, inputVec.end(), smoothVec.begin() + smoothBin);
191 
192  return;
193 }
194 
195 void WaveformTools::getTruncatedMeanRMS(const std::vector<double>& waveform, double& mean, double& rmsFull, double& rmsTrunc, int& nTrunc) const
196 {
197  getTruncatedMeanRMS<double>(waveform, mean, rmsFull, rmsTrunc, nTrunc);
198 }
199 
200 void WaveformTools::getTruncatedMeanRMS(const std::vector<float>& waveform, float& mean, float& rmsFull, float& rmsTrunc, int& nTrunc) const
201 {
202  getTruncatedMeanRMS<float>(waveform, mean, rmsFull, rmsTrunc, nTrunc);
203 }
204 
205 template <typename T> void WaveformTools::getTruncatedMeanRMS(const std::vector<T>& waveform, T& mean, T& rmsFull, T& rmsTrunc, int& nTrunc) const
206 {
207  // We need to get a reliable estimate of the mean and can't assume the input waveform will be ~zero mean...
208  // Basic idea is to find the most probable value in the ROI presented to us
209  // From that we can develop an average of the true baseline of the ROI.
210  // To do that we employ a map based scheme
211  std::map<int,int> frequencyMap;
212  int mpCount(0);
213  int mpVal(0);
214 
215  for(const auto& val : waveform)
216  {
217  int intVal = std::round(4.*val);
218 
219  frequencyMap[intVal]++;
220 
221  if (frequencyMap.at(intVal) > mpCount)
222  {
223  mpCount = frequencyMap.at(intVal);
224  mpVal = intVal;
225  }
226  }
227 
228  // take a weighted average of two neighbor bins
229  int meanCnt = 0;
230  int meanSum = 0;
231  int binRange = std::min(16, int(frequencyMap.size()/2 + 1));
232 
233  for(int idx = -binRange; idx <= binRange; idx++)
234  {
235  std::map<int,int>::iterator neighborItr = frequencyMap.find(mpVal+idx);
236 
237  if (neighborItr != frequencyMap.end() && 5 * neighborItr->second > mpCount)
238  {
239  meanSum += neighborItr->first * neighborItr->second;
240  meanCnt += neighborItr->second;
241  }
242  }
243 
244  mean = 0.25 * T(meanSum) / T(meanCnt); // Note that bins were expanded by a factor of 4 above
245 
246  // do rms calculation - the old fashioned way and over all adc values
247  typename std::vector<T> locWaveform = waveform;
248 
249  std::transform(locWaveform.begin(), locWaveform.end(), locWaveform.begin(),std::bind(std::minus<T>(),std::placeholders::_1,mean));
250 
251  // sort in ascending order so we can truncate the sume
252  std::sort(locWaveform.begin(), locWaveform.end(),[](const auto& left, const auto& right){return std::fabs(left) < std::fabs(right);});
253 
254  // recalculate the rms for truncation
255  rmsFull = std::inner_product(locWaveform.begin(), locWaveform.end(), locWaveform.begin(), 0.);
256  rmsFull = std::sqrt(std::max(T(0.),rmsFull / T(locWaveform.size())));
257 
258  // recalculate the rms for truncation
259  rmsTrunc = std::inner_product(locWaveform.begin(), locWaveform.begin() + meanCnt, locWaveform.begin(), 0.);
260  rmsTrunc = std::sqrt(std::max(T(0.),rmsTrunc / T(meanCnt)));
261  nTrunc = meanCnt;
262 
263  return;
264 }
265 
266 void WaveformTools::firstDerivative(const std::vector<double>& inputVec, std::vector<double>& derivVec) const
267 {
268  firstDerivative<double>(inputVec, derivVec);
269 
270  return;
271 }
272 
273 void WaveformTools::firstDerivative(const std::vector<float>& inputVec, std::vector<float>& derivVec) const
274 {
275  firstDerivative<float>(inputVec, derivVec);
276 
277  return;
278 }
279 
280 template <typename T> void WaveformTools::firstDerivative(const std::vector<T>& inputVec, std::vector<T>& derivVec) const
281 {
282  derivVec.resize(inputVec.size(), 0.);
283 
284  for(size_t idx = 1; idx < derivVec.size() - 1; idx++)
285  derivVec.at(idx) = 0.5 * (inputVec.at(idx + 1) - inputVec.at(idx - 1));
286 
287  return;
288 }
289 
290 void WaveformTools::findPeaks(std::vector<double>::iterator startItr, std::vector<double>::iterator stopItr, PeakTupleVec& peakTupleVec, double threshold, size_t firstTick) const
291 {
292  findPeaks<double>(startItr, stopItr, peakTupleVec, threshold, firstTick);
293 
294  return;
295 }
296 
297 void WaveformTools::findPeaks(std::vector<float>::iterator startItr, std::vector<float>::iterator stopItr, PeakTupleVec& peakTupleVec, float threshold, size_t firstTick) const
298 {
299  findPeaks<float>(startItr, stopItr, peakTupleVec, threshold, firstTick);
300 
301  return;
302 }
303 
304 template <typename T> void WaveformTools::findPeaks(typename std::vector<T>::iterator startItr,
305  typename std::vector<T>::iterator stopItr,
306  PeakTupleVec& peakTupleVec,
307  T threshold,
308  size_t firstTick) const
309 {
310  // Need a minimum distance or else nothing to do
311  if (std::distance(startItr,stopItr) > 4)
312  {
313  // This is a divide and conquer algorithm, start by finding the maximum element.
314  typename std::vector<T>::iterator firstItr = std::max_element(startItr,stopItr,[](float left, float right){return std::fabs(left) < std::fabs(right);});
315 
316  // Are we over threshold?
317  if (std::fabs(*firstItr) > threshold)
318  {
319  // What am I thinking?
320  // First task is to find the "other" lobe max point
321  // Set one to the "first", the other to the "second"
322  // Search backward from first to find start point, forward from second to find end point
323  // Set mid point between first and second as "peak"?
324  typename std::vector<T>::iterator secondItr = firstItr;
325 
326  // Assume if max bin is positive then second lobe is later
327  if (*firstItr > 0)
328  {
329  typename std::vector<T>::iterator tempItr = secondItr;
330 
331  while(tempItr != stopItr)
332  {
333  if (*++tempItr < -threshold)
334  {
335  if (*tempItr < *secondItr) secondItr = tempItr;
336  }
337  else if (secondItr != firstItr) break;
338  }
339  }
340  // Otherwise it goes the other way
341  else
342  {
343  typename std::vector<T>::iterator tempItr = secondItr;
344 
345  while(tempItr != startItr)
346  {
347  if (*--tempItr > threshold)
348  {
349  if (*tempItr > *secondItr) secondItr = tempItr;
350  }
351  else if (secondItr != firstItr) break;
352  }
353 
354  std::swap(firstItr,secondItr);
355  }
356 
357  // It might that no real pulse was found
358  if (firstItr != secondItr)
359  {
360  // Get the "peak" position
361  size_t peakBin = std::distance(startItr,firstItr) + std::distance(firstItr,secondItr) / 2;
362 
363  // Advance (forward or backward) the first and second iterators to get back to zero crossing
364  while(firstItr != startItr) if (*--firstItr < 0.) break;
365  while(secondItr != stopItr) if (*++secondItr > 0.) break;
366 
367  size_t firstBin = std::distance(startItr,firstItr);
368  size_t lastBin = std::distance(startItr,secondItr);
369 
370  // Find leading peaks
371  findPeaks(startItr, firstItr, peakTupleVec, threshold, firstTick);
372 
373  // Save this peak
374  peakTupleVec.push_back(PeakTuple(firstBin+firstTick,peakBin+firstTick,lastBin+firstTick));
375 
376  // Find downstream peaks
377  findPeaks(secondItr, stopItr, peakTupleVec, threshold, firstTick + std::distance(startItr,secondItr));
378  }
379  }
380  }
381 
382  return;
383 }
384 
385 void WaveformTools::getFFTPower(const std::vector<float>& inputVec, std::vector<float>& outputPowerVec) const
386 {
387  std::vector<double> inputDoubleVec(inputVec.size());
388  std::vector<double> outputDoubleVec(inputVec.size()/2);
389 
390  std::copy(inputVec.begin(),inputVec.end(),inputDoubleVec.begin());
391 
392  getFFTPower(inputDoubleVec, outputDoubleVec);
393 
394  if (outputDoubleVec.size() != outputPowerVec.size()) outputPowerVec.resize(outputDoubleVec.size());
395 
396  std::copy(outputDoubleVec.begin(),outputDoubleVec.end(),outputPowerVec.begin());
397 
398  return;
399 }
400 
401 void WaveformTools::getFFTPower(const std::vector<double>& inputVec, std::vector<double>& outputPowerVec) const
402 {
403  // Get the FFT of the response
404  int fftDataSize = inputVec.size();
405 
406  TVirtualFFT* fftr2c = TVirtualFFT::FFT(1, &fftDataSize, "R2C");
407 
408  fftr2c->SetPoints(inputVec.data());
409  fftr2c->Transform();
410 
411  // Recover the results so we can compute the power spectrum
412  size_t halfFFTDataSize(fftDataSize/2 + 1);
413 
414  std::vector<double> realVals(halfFFTDataSize);
415  std::vector<double> imaginaryVals(halfFFTDataSize);
416 
417  fftr2c->GetPointsComplex(realVals.data(), imaginaryVals.data());
418 
419  if (outputPowerVec.size() != halfFFTDataSize) outputPowerVec.resize(halfFFTDataSize,0.);
420 
421  std::transform(realVals.begin(), realVals.begin() + halfFFTDataSize, imaginaryVals.begin(), outputPowerVec.begin(), [](const double& real, const double& imaginary){return std::sqrt(real*real + imaginary*imaginary);});
422 
423  return;
424 }
425 
427  int structuringElement,
428  HistogramMap& histogramMap,
429  Waveform<short>& erosionVec,
430  Waveform<short>& dilationVec,
431  Waveform<short>& averageVec,
432  Waveform<short>& differenceVec) const
433 {
434  getErosionDilationAverageDifference<short>(waveform, structuringElement, histogramMap, erosionVec, dilationVec, averageVec, differenceVec);
435 
436  return;
437 }
438 
440  int structuringElement,
441  HistogramMap& histogramMap,
442  Waveform<float>& erosionVec,
443  Waveform<float>& dilationVec,
444  Waveform<float>& averageVec,
445  Waveform<float>& differenceVec) const
446 {
447  getErosionDilationAverageDifference<float>(waveform, structuringElement, histogramMap, erosionVec, dilationVec, averageVec, differenceVec);
448 
449  return;
450 }
451 
453  int structuringElement,
454  HistogramMap& histogramMap,
455  Waveform<double>& erosionVec,
456  Waveform<double>& dilationVec,
457  Waveform<double>& averageVec,
458  Waveform<double>& differenceVec) const
459 {
460  getErosionDilationAverageDifference<double>(waveform, structuringElement, histogramMap, erosionVec, dilationVec, averageVec, differenceVec);
461 
462  return;
463 }
464 
465 template <typename T> void WaveformTools::getErosionDilationAverageDifference(const Waveform<T>& inputWaveform,
466  int structuringElement,
467  HistogramMap& histogramMap,
468  Waveform<T>& erosionVec,
469  Waveform<T>& dilationVec,
470  Waveform<T>& averageVec,
471  Waveform<T>& differenceVec) const
472 {
473  // Set the window size
474  int halfWindowSize(structuringElement/2);
475 
476  // Initialize min and max elements
477  std::pair<typename Waveform<T>::const_iterator,typename Waveform<T>::const_iterator> minMaxItr =
478  std::minmax_element(inputWaveform.begin(),inputWaveform.begin()+halfWindowSize);
479 
480  typename Waveform<T>::const_iterator minElementItr = minMaxItr.first;
481  typename Waveform<T>::const_iterator maxElementItr = minMaxItr.second;
482 
483  // Initialize the erosion and dilation vectors
484  erosionVec.resize(inputWaveform.size());
485  dilationVec.resize(inputWaveform.size());
486  averageVec.resize(inputWaveform.size());
487  differenceVec.resize(inputWaveform.size());
488 
489  // Now loop through remaining elements and complete the vectors
490  typename Waveform<T>::iterator minItr = erosionVec.begin();
491  typename Waveform<T>::iterator maxItr = dilationVec.begin();
492  typename Waveform<T>::iterator aveItr = averageVec.begin();
493  typename Waveform<T>::iterator difItr = differenceVec.begin();
494 
495  for (typename Waveform<T>::const_iterator inputItr = inputWaveform.begin(); inputItr != inputWaveform.end(); inputItr++)
496  {
497  // There are two conditions to check:
498  // 1) is the current min/max element outside the current window?
499  // 2) is the new element smaller/larger than the current min/max?
500 
501  // Make sure we are not running off the end of the vector
502  if (std::distance(inputItr,inputWaveform.end()) > halfWindowSize)
503  {
504  if (std::distance(minElementItr,inputItr) >= halfWindowSize)
505  minElementItr = std::min_element(inputItr - halfWindowSize + 1, inputItr + halfWindowSize + 1);
506  else if (*(inputItr + halfWindowSize) < *minElementItr)
507  minElementItr = inputItr + halfWindowSize;
508 
509  if (std::distance(maxElementItr,inputItr) >= halfWindowSize)
510  maxElementItr = std::max_element(inputItr - halfWindowSize + 1, inputItr + halfWindowSize + 1);
511  else if (*(inputItr + halfWindowSize) > *maxElementItr)
512  maxElementItr = inputItr + halfWindowSize;
513  }
514 
515  // Update the vectors
516  *minItr++ = *minElementItr;
517  *maxItr++ = *maxElementItr;
518  *aveItr++ = 0.5 * (*maxElementItr + *minElementItr);
519  *difItr++ = *maxElementItr - *minElementItr;
520 
521  if (!histogramMap.empty())
522  {
523  int curBin = std::distance(inputWaveform.begin(),inputItr);
524 
525  histogramMap.at(WAVEFORM)->Fill( curBin, *inputItr);
526  histogramMap.at(EROSION)->Fill( curBin, *minElementItr);
527  histogramMap.at(DILATION)->Fill( curBin, *maxElementItr);
528  histogramMap.at(AVERAGE)->Fill( curBin, 0.5*(*maxElementItr + *minElementItr));
529  histogramMap.at(DIFFERENCE)->Fill( curBin, *maxElementItr - *minElementItr);
530  }
531 
532  }
533 
534  return;
535 }
536 
538  const Waveform<short>& dilationVec,
539  int structuringElement,
540  HistogramMap& histogramMap,
541  Waveform<short>& openingVec,
542  Waveform<short>& closingVec) const
543 {
544  getOpeningAndClosing<short>(erosionVec, dilationVec, structuringElement, histogramMap, openingVec, closingVec);
545 
546  return;
547 }
548 
550  const Waveform<float>& dilationVec,
551  int structuringElement,
552  HistogramMap& histogramMap,
553  Waveform<float>& openingVec,
554  Waveform<float>& closingVec) const
555 {
556  getOpeningAndClosing<float>(erosionVec, dilationVec, structuringElement, histogramMap, openingVec, closingVec);
557 
558  return;
559 }
560 
562  const Waveform<double>& dilationVec,
563  int structuringElement,
564  HistogramMap& histogramMap,
565  Waveform<double>& openingVec,
566  Waveform<double>& closingVec) const
567 {
568  getOpeningAndClosing<double>(erosionVec, dilationVec, structuringElement, histogramMap, openingVec, closingVec);
569 
570  return;
571 }
572 
573 template <typename T> void WaveformTools::getOpeningAndClosing(const Waveform<T>& erosionVec,
574  const Waveform<T>& dilationVec,
575  int structuringElement,
576  HistogramMap& histogramMap,
577  Waveform<T>& openingVec,
578  Waveform<T>& closingVec) const
579 {
580  // Set the window size
581  int halfWindowSize(structuringElement/2);
582 
583  // Start with the opening, here we get the max element in the input erosion vector
584  typename Waveform<T>::const_iterator maxElementItr = std::max_element(erosionVec.begin(),erosionVec.begin()+halfWindowSize);
585 
586  // Initialize the opening vector
587  openingVec.resize(erosionVec.size());
588 
589  // Now loop through remaining elements and complete the vectors
590  typename Waveform<T>::iterator maxItr = openingVec.begin();
591 
592  for (typename Waveform<T>::const_iterator inputItr = erosionVec.begin(); inputItr != erosionVec.end(); inputItr++)
593  {
594  // There are two conditions to check:
595  // 1) is the current min/max element outside the current window?
596  // 2) is the new element smaller/larger than the current min/max?
597 
598  // Make sure we are not running off the end of the vector
599  if (std::distance(inputItr,erosionVec.end()) > halfWindowSize)
600  {
601  if (std::distance(maxElementItr,inputItr) >= halfWindowSize)
602  maxElementItr = std::max_element(inputItr - halfWindowSize + 1, inputItr + halfWindowSize + 1);
603  else if (*(inputItr + halfWindowSize) > *maxElementItr)
604  maxElementItr = inputItr + halfWindowSize;
605  }
606 
607  // Update the vectors
608  *maxItr++ = *maxElementItr;
609 
610  if (!histogramMap.empty())
611  {
612  int curBin = std::distance(erosionVec.begin(),inputItr);
613 
614  histogramMap.at(OPENING)->Fill(curBin, *maxElementItr);
615  }
616  }
617 
618  // Now go with the closling, here we get the min element in the input dilation vector
619  typename Waveform<T>::const_iterator minElementItr = std::min_element(dilationVec.begin(),dilationVec.begin()+halfWindowSize);
620 
621  // Initialize the opening and closing vectors
622  closingVec.resize(dilationVec.size());
623 
624  // Now loop through remaining elements and complete the vectors
625  typename Waveform<T>::iterator minItr = closingVec.begin();
626 
627  for (typename Waveform<T>::const_iterator inputItr = dilationVec.begin(); inputItr != dilationVec.end(); inputItr++)
628  {
629  // There are two conditions to check:
630  // 1) is the current min/max element outside the current window?
631  // 2) is the new element smaller/larger than the current min/max?
632 
633  // Make sure we are not running off the end of the vector
634  if (std::distance(inputItr,dilationVec.end()) > halfWindowSize)
635  {
636  if (std::distance(minElementItr,inputItr) >= halfWindowSize)
637  minElementItr = std::min_element(inputItr - halfWindowSize + 1, inputItr + halfWindowSize + 1);
638  else if (*(inputItr + halfWindowSize) < *minElementItr)
639  minElementItr = inputItr + halfWindowSize;
640  }
641 
642  // Update the vectors
643  *minItr++ = *minElementItr;
644 
645  if (!histogramMap.empty())
646  {
647  int curBin = std::distance(dilationVec.begin(),inputItr);
648 
649  histogramMap.at(CLOSING)->Fill(curBin, *minElementItr);
650  histogramMap.at(DOPENCLOSING)->Fill(curBin, *minElementItr - openingVec.at(curBin));
651  }
652  }
653 
654  return;
655 }
656 
657 DEFINE_ART_CLASS_TOOL(WaveformTools)
658 }
WaveformTools(const fhicl::ParameterSet &pset)
std::tuple< size_t, size_t, size_t > PeakTuple
Definition: IWaveformTool.h:50
std::tuple< size_t, size_t, size_t > PeakTuple
static constexpr Sample_t transform(Sample_t sample)
void configure(const fhicl::ParameterSet &pset) override
std::vector< T > Waveform
Definition: IWaveformTool.h:25
This is the interface class for tools/algorithms that perform various operations on waveforms...
walls no right
Definition: selectors.fcl:105
std::vector< PeakTuple > PeakTupleVec
Definition: IWaveformTool.h:51
void triangleSmooth(const std::vector< float > &, std::vector< float > &, size_t=0) const override
void getTruncatedMeanRMS(const std::vector< double > &, double &, double &, double &, int &) const override
void medianSmooth(const std::vector< float > &, std::vector< float > &, size_t=3) const override
void firstDerivative(const std::vector< float > &, std::vector< float > &) const override
double distance(geo::Point_t const &point, CathodeDesc_t const &cathode)
Returns the distance of a point from the cathode.
void getOpeningAndClosing(const Waveform< short > &, const Waveform< short > &, int, HistogramMap &, Waveform< short > &, Waveform< short > &) const override
walls no left
Definition: selectors.fcl:105
std::map< int, TProfile * > HistogramMap
Definition: IWaveformTool.h:41
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
void findPeaks(std::vector< float >::iterator, std::vector< float >::iterator, PeakTupleVec &, float, size_t) const override
void getFFTPower(const std::vector< float > &inputVec, std::vector< float > &outputPowerVec) const override
T copy(T const &v)
void getErosionDilationAverageDifference(const Waveform< short > &, int, HistogramMap &, Waveform< short > &, Waveform< short > &, Waveform< short > &, Waveform< short > &) const override