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