Public Types | Public Member Functions | Private Member Functions | List of all members
reco_tool::WaveformTools Class Reference
Inheritance diagram for reco_tool::WaveformTools:
reco_tool::IWaveformTool

Public Types

using PeakTuple = std::tuple< size_t, size_t, size_t >
 
using PeakTupleVec = std::vector< PeakTuple >
 

Public Member Functions

 WaveformTools (const fhicl::ParameterSet &pset)
 
 ~WaveformTools ()
 
void configure (const fhicl::ParameterSet &pset) override
 
void triangleSmooth (const std::vector< float > &, std::vector< float > &, size_t=0) const override
 
void triangleSmooth (const std::vector< double > &, std::vector< double > &, size_t=0) const override
 
void medianSmooth (const std::vector< float > &, std::vector< float > &, size_t=3) const override
 
void medianSmooth (const std::vector< double > &, std::vector< double > &, size_t=3) const override
 
void getTruncatedMeanRMS (const std::vector< double > &, double &, double &, double &, int &) const override
 
void getTruncatedMeanRMS (const std::vector< float > &, float &, float &, float &, int &) const override
 
void firstDerivative (const std::vector< float > &, std::vector< float > &) const override
 
void firstDerivative (const std::vector< double > &, std::vector< double > &) const override
 
void findPeaks (std::vector< float >::iterator, std::vector< float >::iterator, PeakTupleVec &, float, size_t) const override
 
void findPeaks (std::vector< double >::iterator, std::vector< double >::iterator, PeakTupleVec &, double, size_t) const override
 
void getFFTPower (const std::vector< float > &inputVec, std::vector< float > &outputPowerVec) const override
 
void getFFTPower (const std::vector< double > &inputVec, std::vector< double > &outputPowerVec) const override
 
void getErosionDilationAverageDifference (const Waveform< short > &, int, HistogramMap &, Waveform< short > &, Waveform< short > &, Waveform< short > &, Waveform< short > &) const override
 
void getErosionDilationAverageDifference (const Waveform< float > &, int, HistogramMap &, Waveform< float > &, Waveform< float > &, Waveform< float > &, Waveform< float > &) const override
 
void getErosionDilationAverageDifference (const Waveform< double > &, int, HistogramMap &, Waveform< double > &, Waveform< double > &, Waveform< double > &, Waveform< double > &) const override
 
void getOpeningAndClosing (const Waveform< short > &, const Waveform< short > &, int, HistogramMap &, Waveform< short > &, Waveform< short > &) const override
 
void getOpeningAndClosing (const Waveform< float > &, const Waveform< float > &, int, HistogramMap &, Waveform< float > &, Waveform< float > &) const override
 
void getOpeningAndClosing (const Waveform< double > &, const Waveform< double > &, int, HistogramMap &, Waveform< double > &, Waveform< double > &) const override
 

Private Member Functions

template<typename T >
void triangleSmooth (const std::vector< T > &, std::vector< T > &, size_t=0) const
 
template<typename T >
void medianSmooth (const std::vector< T > &, std::vector< T > &, size_t=3) const
 
template<typename T >
void getTruncatedMeanRMS (const std::vector< T > &, T &, T &, T &, int &) const
 
template<typename T >
void firstDerivative (const std::vector< T > &, std::vector< T > &) const
 
template<typename T >
void findPeaks (typename std::vector< T >::iterator, typename std::vector< T >::iterator, PeakTupleVec &, T, size_t) const
 
template<typename T >
void getErosionDilationAverageDifference (const Waveform< T > &, int, HistogramMap &, Waveform< T > &, Waveform< T > &, Waveform< T > &, Waveform< T > &) const
 
template<typename T >
void getOpeningAndClosing (const Waveform< T > &, const Waveform< T > &, int, HistogramMap &, Waveform< T > &, Waveform< T > &) const
 
- Private Member Functions inherited from reco_tool::IWaveformTool
virtual ~IWaveformTool () noexcept=default
 

Additional Inherited Members

- Private Types inherited from reco_tool::IWaveformTool
using PeakTuple = std::tuple< size_t, size_t, size_t >
 
using PeakTupleVec = std::vector< PeakTuple >
 

Detailed Description

Definition at line 16 of file WaveformTools_tool.cc.

Member Typedef Documentation

using reco_tool::WaveformTools::PeakTuple = std::tuple<size_t,size_t,size_t>

Definition at line 25 of file WaveformTools_tool.cc.

Definition at line 26 of file WaveformTools_tool.cc.

Constructor & Destructor Documentation

reco_tool::WaveformTools::WaveformTools ( const fhicl::ParameterSet pset)
explicit

Definition at line 87 of file WaveformTools_tool.cc.

88 {
89  configure(pset);
90 }
void configure(const fhicl::ParameterSet &pset) override
reco_tool::WaveformTools::~WaveformTools ( )
inline

Definition at line 21 of file WaveformTools_tool.cc.

21 {}

Member Function Documentation

void reco_tool::WaveformTools::configure ( const fhicl::ParameterSet pset)
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 92 of file WaveformTools_tool.cc.

93 {
94  // Start by recovering the parameters
95 // fThisPlane = pset.get<size_t>("Plane");
96 
97  return;
98 }
void reco_tool::WaveformTools::findPeaks ( std::vector< float >::iterator  startItr,
std::vector< float >::iterator  stopItr,
PeakTupleVec peakTupleVec,
float  threshold,
size_t  firstTick 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 296 of file WaveformTools_tool.cc.

297 {
298  findPeaks<float>(startItr, stopItr, peakTupleVec, threshold, firstTick);
299 
300  return;
301 }
void reco_tool::WaveformTools::findPeaks ( std::vector< double >::iterator  startItr,
std::vector< double >::iterator  stopItr,
PeakTupleVec peakTupleVec,
double  threshold,
size_t  firstTick 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 289 of file WaveformTools_tool.cc.

290 {
291  findPeaks<double>(startItr, stopItr, peakTupleVec, threshold, firstTick);
292 
293  return;
294 }
template<typename T >
void reco_tool::WaveformTools::findPeaks ( typename std::vector< T >::iterator  startItr,
typename std::vector< T >::iterator  stopItr,
PeakTupleVec peakTupleVec,
threshold,
size_t  firstTick 
) const
private

Definition at line 303 of file WaveformTools_tool.cc.

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 }
intermediate_table::iterator iterator
std::tuple< size_t, size_t, size_t > PeakTuple
void findPeaks(std::vector< float >::iterator, std::vector< float >::iterator, PeakTupleVec &, float, size_t) const override
void swap(Handle< T > &a, Handle< T > &b)
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
void reco_tool::WaveformTools::firstDerivative ( const std::vector< float > &  inputVec,
std::vector< float > &  derivVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 272 of file WaveformTools_tool.cc.

273 {
274  firstDerivative<float>(inputVec, derivVec);
275 
276  return;
277 }
void reco_tool::WaveformTools::firstDerivative ( const std::vector< double > &  inputVec,
std::vector< double > &  derivVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 265 of file WaveformTools_tool.cc.

266 {
267  firstDerivative<double>(inputVec, derivVec);
268 
269  return;
270 }
template<typename T >
void reco_tool::WaveformTools::firstDerivative ( const std::vector< T > &  inputVec,
std::vector< T > &  derivVec 
) const
private

Definition at line 279 of file WaveformTools_tool.cc.

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 }
void reco_tool::WaveformTools::getErosionDilationAverageDifference ( const Waveform< short > &  waveform,
int  structuringElement,
HistogramMap histogramMap,
Waveform< short > &  erosionVec,
Waveform< short > &  dilationVec,
Waveform< short > &  averageVec,
Waveform< short > &  differenceVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 425 of file WaveformTools_tool.cc.

432 {
433  getErosionDilationAverageDifference<short>(waveform, structuringElement, histogramMap, erosionVec, dilationVec, averageVec, differenceVec);
434 
435  return;
436 }
void reco_tool::WaveformTools::getErosionDilationAverageDifference ( const Waveform< float > &  waveform,
int  structuringElement,
HistogramMap histogramMap,
Waveform< float > &  erosionVec,
Waveform< float > &  dilationVec,
Waveform< float > &  averageVec,
Waveform< float > &  differenceVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 438 of file WaveformTools_tool.cc.

445 {
446  getErosionDilationAverageDifference<float>(waveform, structuringElement, histogramMap, erosionVec, dilationVec, averageVec, differenceVec);
447 
448  return;
449 }
void reco_tool::WaveformTools::getErosionDilationAverageDifference ( const Waveform< double > &  waveform,
int  structuringElement,
HistogramMap histogramMap,
Waveform< double > &  erosionVec,
Waveform< double > &  dilationVec,
Waveform< double > &  averageVec,
Waveform< double > &  differenceVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 451 of file WaveformTools_tool.cc.

458 {
459  getErosionDilationAverageDifference<double>(waveform, structuringElement, histogramMap, erosionVec, dilationVec, averageVec, differenceVec);
460 
461  return;
462 }
template<typename T >
void reco_tool::WaveformTools::getErosionDilationAverageDifference ( const Waveform< T > &  inputWaveform,
int  structuringElement,
HistogramMap histogramMap,
Waveform< T > &  erosionVec,
Waveform< T > &  dilationVec,
Waveform< T > &  averageVec,
Waveform< T > &  differenceVec 
) const
private

Definition at line 464 of file WaveformTools_tool.cc.

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 }
intermediate_table::iterator iterator
intermediate_table::const_iterator const_iterator
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
void reco_tool::WaveformTools::getFFTPower ( const std::vector< float > &  inputVec,
std::vector< float > &  outputPowerVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 384 of file WaveformTools_tool.cc.

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 }
void getFFTPower(const std::vector< float > &inputVec, std::vector< float > &outputPowerVec) const override
T copy(T const &v)
void reco_tool::WaveformTools::getFFTPower ( const std::vector< double > &  inputVec,
std::vector< double > &  outputPowerVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 400 of file WaveformTools_tool.cc.

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 }
void reco_tool::WaveformTools::getOpeningAndClosing ( const Waveform< short > &  erosionVec,
const Waveform< short > &  dilationVec,
int  structuringElement,
HistogramMap histogramMap,
Waveform< short > &  openingVec,
Waveform< short > &  closingVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 536 of file WaveformTools_tool.cc.

542 {
543  getOpeningAndClosing<short>(erosionVec, dilationVec, structuringElement, histogramMap, openingVec, closingVec);
544 
545  return;
546 }
void reco_tool::WaveformTools::getOpeningAndClosing ( const Waveform< float > &  erosionVec,
const Waveform< float > &  dilationVec,
int  structuringElement,
HistogramMap histogramMap,
Waveform< float > &  openingVec,
Waveform< float > &  closingVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 548 of file WaveformTools_tool.cc.

554 {
555  getOpeningAndClosing<float>(erosionVec, dilationVec, structuringElement, histogramMap, openingVec, closingVec);
556 
557  return;
558 }
void reco_tool::WaveformTools::getOpeningAndClosing ( const Waveform< double > &  erosionVec,
const Waveform< double > &  dilationVec,
int  structuringElement,
HistogramMap histogramMap,
Waveform< double > &  openingVec,
Waveform< double > &  closingVec 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 560 of file WaveformTools_tool.cc.

566 {
567  getOpeningAndClosing<double>(erosionVec, dilationVec, structuringElement, histogramMap, openingVec, closingVec);
568 
569  return;
570 }
template<typename T >
void reco_tool::WaveformTools::getOpeningAndClosing ( const Waveform< T > &  erosionVec,
const Waveform< T > &  dilationVec,
int  structuringElement,
HistogramMap histogramMap,
Waveform< T > &  openingVec,
Waveform< T > &  closingVec 
) const
private

Definition at line 572 of file WaveformTools_tool.cc.

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 }
intermediate_table::iterator iterator
intermediate_table::const_iterator const_iterator
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
void reco_tool::WaveformTools::getTruncatedMeanRMS ( const std::vector< double > &  waveform,
double &  mean,
double &  rmsFull,
double &  rmsTrunc,
int &  nTrunc 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 194 of file WaveformTools_tool.cc.

195 {
196  getTruncatedMeanRMS<double>(waveform, mean, rmsFull, rmsTrunc, nTrunc);
197 }
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
void reco_tool::WaveformTools::getTruncatedMeanRMS ( const std::vector< float > &  waveform,
float &  mean,
float &  rmsFull,
float &  rmsTrunc,
int &  nTrunc 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 199 of file WaveformTools_tool.cc.

200 {
201  getTruncatedMeanRMS<float>(waveform, mean, rmsFull, rmsTrunc, nTrunc);
202 }
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
template<typename T >
void reco_tool::WaveformTools::getTruncatedMeanRMS ( const std::vector< T > &  waveform,
T &  mean,
T &  rmsFull,
T &  rmsTrunc,
int &  nTrunc 
) const
private

Definition at line 204 of file WaveformTools_tool.cc.

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 }
intermediate_table::iterator iterator
static int max(int a, int b)
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
void reco_tool::WaveformTools::medianSmooth ( const std::vector< float > &  inputVec,
std::vector< float > &  smoothVec,
size_t  nBins = 3 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 141 of file WaveformTools_tool.cc.

142 {
143  medianSmooth<float>(inputVec, smoothVec, nBins);
144 
145  return;
146 }
void reco_tool::WaveformTools::medianSmooth ( const std::vector< double > &  inputVec,
std::vector< double > &  smoothVec,
size_t  nBins = 3 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 148 of file WaveformTools_tool.cc.

149 {
150  medianSmooth<double>(inputVec, smoothVec, nBins);
151 
152  return;
153 }
template<typename T >
void reco_tool::WaveformTools::medianSmooth ( const std::vector< T > &  inputVec,
std::vector< T > &  smoothVec,
size_t  nBins = 3 
) const
private

Definition at line 155 of file WaveformTools_tool.cc.

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 }
intermediate_table::const_iterator const_iterator
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
T copy(T const &v)
void reco_tool::WaveformTools::triangleSmooth ( const std::vector< float > &  inputVec,
std::vector< float > &  smoothVec,
size_t  lowestBin = 0 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 107 of file WaveformTools_tool.cc.

108 {
109  triangleSmooth<float>(inputVec, smoothVec, lowestBin);
110 
111  return;
112 }
void reco_tool::WaveformTools::triangleSmooth ( const std::vector< double > &  inputVec,
std::vector< double > &  smoothVec,
size_t  lowestBin = 0 
) const
overridevirtual

Implements reco_tool::IWaveformTool.

Definition at line 100 of file WaveformTools_tool.cc.

101 {
102  triangleSmooth<double>(inputVec, smoothVec, lowestBin);
103 
104  return;
105 }
template<typename T >
void reco_tool::WaveformTools::triangleSmooth ( const std::vector< T > &  inputVec,
std::vector< T > &  smoothVec,
size_t  lowestBin = 0 
) const
private

Definition at line 114 of file WaveformTools_tool.cc.

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 }
intermediate_table::iterator iterator
intermediate_table::const_iterator const_iterator
T copy(T const &v)

The documentation for this class was generated from the following file: