Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
reco_tool::CandHitMorphological Class Reference
Inheritance diagram for reco_tool::CandHitMorphological:
reco_tool::ICandidateHitFinder

Public Member Functions

 CandHitMorphological (const fhicl::ParameterSet &pset)
 
void findHitCandidates (const recob::Wire::RegionsOfInterest_t::datarange_t &, const size_t, const size_t, const size_t, HitCandidateVec &) const override
 
void MergeHitCandidates (const recob::Wire::RegionsOfInterest_t::datarange_t &, const HitCandidateVec &, MergeHitCandidateVec &) const override
 

Private Types

using MaxMinPair = std::pair< Waveform::const_iterator, Waveform::const_iterator >
 
using CandHitParams = std::tuple< Waveform::const_iterator, Waveform::const_iterator, Waveform::const_iterator, Waveform::const_iterator >
 
using CandHitParamsVec = std::vector< CandHitParams >
 
- Private Types inherited from reco_tool::ICandidateHitFinder
using HitCandidateVec = std::vector< HitCandidate >
 
using MergeHitCandidateVec = std::vector< HitCandidateVec >
 
using Waveform = std::vector< float >
 

Private Member Functions

void findHitCandidates (Waveform::const_iterator, Waveform::const_iterator, Waveform::const_iterator, Waveform::const_iterator, Waveform::const_iterator, Waveform::const_iterator, const size_t, float, HitCandidateVec &) const
 
void findHitCandidates (Waveform::const_iterator, Waveform::const_iterator, const size_t, int, float, HitCandidateVec &) const
 
bool getListOfHitCandidates (Waveform::const_iterator, Waveform::const_iterator, int, float, CandHitParamsVec &) const
 
Waveform::const_iterator findNearestMax (Waveform::const_iterator, Waveform::const_iterator) const
 
Waveform::const_iterator findNearestMin (Waveform::const_iterator, Waveform::const_iterator) const
 
Waveform::const_iterator findStartTick (Waveform::const_iterator, Waveform::const_iterator) const
 
Waveform::const_iterator findStopTick (Waveform::const_iterator, Waveform::const_iterator) const
 
- Private Member Functions inherited from reco_tool::ICandidateHitFinder
virtual ~ICandidateHitFinder () noexcept=default
 

Private Attributes

const size_t fPlane
 
const float fDilationThreshold
 
const float fDilationFraction
 
const float fErosionFraction
 
const int fMinDeltaTicks
 
const float fMinDeltaPeaks
 
const float fMinHitHeight
 
const size_t fNumInterveningTicks
 
const int fStructuringElement
 
const bool fOutputHistograms
 
const bool fOutputWaveforms
 
const float fFitNSigmaFromCenter
 
art::TFileDirectory * fHistDirectory
 
TH1F * fDStopStartHist
 
TH1F * fDMaxTickMinTickHist
 
TH1F * fDMaxDerivMinDerivHist
 
TH1F * fMaxErosionHist
 
TH1F * fMaxDilationHist
 
TH1F * fMaxDilEroRatHist
 
size_t fLastChannel
 
size_t fChannelCnt
 
std::unique_ptr< reco_tool::IWaveformToolfWaveformTool
 
const geo::GeometryCorefGeometry = lar::providerFrom<geo::Geometry>()
 

Detailed Description

Definition at line 23 of file CandHitMorphological_tool.cc.

Member Typedef Documentation

Definition at line 63 of file CandHitMorphological_tool.cc.

Definition at line 64 of file CandHitMorphological_tool.cc.

Definition at line 59 of file CandHitMorphological_tool.cc.

Constructor & Destructor Documentation

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

Definition at line 124 of file CandHitMorphological_tool.cc.

125  : fPlane(pset.get<size_t>("Plane", 0))
126  , fDilationThreshold(pset.get<float>("DilationThreshold", 4.))
127  , fDilationFraction(pset.get<float>("DilationFraction", 0.75))
128  , fErosionFraction(pset.get<float>("ErosionFraction", 0.2))
129  , fMinDeltaTicks(pset.get<int>("MinDeltaTicks", 0))
130  , fMinDeltaPeaks(pset.get<float>("MinDeltaPeaks", 0.025))
131  , fMinHitHeight(pset.get<float>("MinHitHeight", 1.0))
132  , fNumInterveningTicks(pset.get<size_t>("NumInterveningTicks", 6))
133  , fStructuringElement(pset.get<int>("StructuringElement", 20))
134  , fOutputHistograms(pset.get<bool>("OutputHistograms", false))
135  , fOutputWaveforms(pset.get<bool>("OutputWaveforms", false))
136  , fFitNSigmaFromCenter(pset.get<float>("FitNSigmaFromCenter", 5.))
137  {
138 
139  if (art::Globals::instance()->nthreads() > 1u) {
140  if (fOutputHistograms) {
142  << "Cannot fill histograms when multiple threads configured, please set "
143  "fOutputHistograms to false or change number of threads to 1\n";
144  }
145 
146  if (fOutputWaveforms) {
148  << "Cannot write output waveforms when multiple threads configured, please set "
149  "fOutputHistograms to false or change number of threads to 1\n";
150  }
151  }
152  // Recover the baseline tool
153  fWaveformTool =
154  art::make_tool<reco_tool::IWaveformTool>(pset.get<fhicl::ParameterSet>("WaveformAlgs"));
155 
156  // Set the last channel to some nonsensical value
158  fChannelCnt = 0;
159 
160  // If asked, define the global histograms
161  if (fOutputHistograms) {
162  // Access ART's TFileService, which will handle creating and writing
163  // histograms and n-tuples for us.
165 
166  fHistDirectory = tfs.get();
167 
168  // Make a directory for these histograms
169  art::TFileDirectory dir = fHistDirectory->mkdir(Form("HitPlane_%1zu", fPlane));
170 
172  dir.make<TH1F>(Form("DStopStart_%1zu", fPlane), ";Delta Stop/Start;", 100, 0., 100.);
174  dir.make<TH1F>(Form("DMaxTMinT_%1zu", fPlane), ";Delta Max/Min Tick;", 100, 0., 100.);
176  dir.make<TH1F>(Form("DMaxDMinD_%1zu", fPlane), ";Delta Max/Min Deriv;", 200, 0., 100.);
178  dir.make<TH1F>(Form("MaxErosion_%1zu", fPlane), ";Max Erosion;", 200, -50., 150.);
180  dir.make<TH1F>(Form("MaxDilation_%1zu", fPlane), ";Max Dilation;", 200, -50., 150.);
182  dir.make<TH1F>(Form("MaxDilEroRat_%1zu", fPlane), ";Max Dil/Ero;", 200, -1., 1.);
183  }
184 
185  return;
186  }
T * get() const
Definition: ServiceHandle.h:63
string dir
std::unique_ptr< reco_tool::IWaveformTool > fWaveformTool
T get(std::string const &key) const
Definition: ParameterSet.h:271
static int max(int a, int b)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
static Globals * instance()
Definition: Globals.cc:17

Member Function Documentation

void reco_tool::CandHitMorphological::findHitCandidates ( const recob::Wire::RegionsOfInterest_t::datarange_t &  dataRange,
const size_t  roiStartTick,
const size_t  channel,
const size_t  eventCount,
HitCandidateVec hitCandidateVec 
) const
overridevirtual

Implements reco_tool::ICandidateHitFinder.

Definition at line 189 of file CandHitMorphological_tool.cc.

195  {
196  // In this case we want to find hit candidates based on the derivative of of the input waveform
197  // We get this from our waveform algs too...
198  Waveform rawDerivativeVec;
199  Waveform derivativeVec;
200 
201  // Recover the actual waveform
202  const Waveform& waveform = dataRange.data();
203 
204  fWaveformTool->firstDerivative(waveform, rawDerivativeVec);
205  fWaveformTool->triangleSmooth(rawDerivativeVec, derivativeVec);
206 
207  // Now we get the erosion/dilation vectors
208  Waveform erosionVec;
209  Waveform dilationVec;
210  Waveform averageVec;
211  Waveform differenceVec;
212 
213  reco_tool::HistogramMap histogramMap;
214 
215  // Compute the morphological filter vectors
216  fWaveformTool->getErosionDilationAverageDifference(waveform,
218  histogramMap,
219  erosionVec,
220  dilationVec,
221  averageVec,
222  differenceVec);
223 
224  // Now find the hits
225  findHitCandidates(derivativeVec.begin(),
226  derivativeVec.end(),
227  erosionVec.begin(),
228  erosionVec.end(),
229  dilationVec.begin(),
230  dilationVec.end(),
231  roiStartTick,
233  hitCandidateVec);
234 
235  // Limit start and stop tick to the neighborhood of the peak
236  if (fFitNSigmaFromCenter > 0) {
237  for (auto& hc : hitCandidateVec) {
238  auto startCand = hc.hitCenter - fFitNSigmaFromCenter * hc.hitSigma;
239  if (startCand >= 0) hc.startTick = std::max(hc.startTick, size_t(startCand));
240  hc.stopTick =
241  std::min(hc.stopTick, size_t(hc.hitCenter + fFitNSigmaFromCenter * hc.hitSigma));
242  }
243  }
244 
245  // Reset the hit height from the input waveform
246  for (auto& hitCandidate : hitCandidateVec) {
247  size_t centerIdx = hitCandidate.hitCenter;
248 
249  hitCandidate.hitHeight = waveform.at(centerIdx);
250  }
251 
252  // Keep track of histograms if requested
253  if (fOutputWaveforms) {
254  // Recover the details...
255  std::vector<geo::WireID> wids = fGeometry->ChannelToWire(channel);
256  size_t plane = wids[0].Plane;
257  size_t cryo = wids[0].Cryostat;
258  size_t tpc = wids[0].TPC;
259  size_t wire = wids[0].Wire;
260 
261  if (channel != fLastChannel) fChannelCnt = 0;
262 
263  // Make a directory for these histograms
264  art::TFileDirectory dir = fHistDirectory->mkdir(
265  Form("Event%04zu/c%1zuT%1zuP%1zu/Wire_%05zu", eventCount, cryo, tpc, plane, wire));
266 
267  size_t waveformSize = waveform.size();
268  size_t waveStart = dataRange.begin_index();
269 
270  TProfile* waveHist =
271  dir.make<TProfile>(Form("HWfm_%03zu_roiStart-%05zu", fChannelCnt, waveStart),
272  "Waveform",
273  waveformSize,
274  0,
275  waveformSize,
276  -500.,
277  500.);
278  TProfile* derivHist =
279  dir.make<TProfile>(Form("HDer_%03zu_roiStart-%05zu", fChannelCnt, waveStart),
280  "Derivative",
281  waveformSize,
282  0,
283  waveformSize,
284  -500.,
285  500.);
286  TProfile* erosionHist =
287  dir.make<TProfile>(Form("HEro_%03zu_roiStart-%05zu", fChannelCnt, waveStart),
288  "Erosion",
289  waveformSize,
290  0,
291  waveformSize,
292  -500.,
293  500.);
294  TProfile* dilationHist =
295  dir.make<TProfile>(Form("HDil_%03zu_roiStart-%05zu", fChannelCnt, waveStart),
296  "Dilation",
297  waveformSize,
298  0,
299  waveformSize,
300  -500.,
301  500.);
302  TProfile* candHitHist =
303  dir.make<TProfile>(Form("HCan_%03zu_roiStart-%05zu", fChannelCnt, waveStart),
304  "Cand Hits",
305  waveformSize,
306  0,
307  waveformSize,
308  -500.,
309  500.);
310  TProfile* maxDerivHist =
311  dir.make<TProfile>(Form("HMax_%03zu_roiStart-%05zu", fChannelCnt, waveStart),
312  "Maxima",
313  waveformSize,
314  0,
315  waveformSize,
316  -500.,
317  500.);
318  TProfile* strtStopHist =
319  dir.make<TProfile>(Form("HSSS_%03zu_roiStart-%05zu", fChannelCnt, waveStart),
320  "Start/Stop",
321  waveformSize,
322  0,
323  waveformSize,
324  -500.,
325  500.);
326 
327  // Fill wave/derivative
328  for (size_t idx = 0; idx < waveform.size(); idx++) {
329  waveHist->Fill(roiStartTick + idx, waveform.at(idx));
330  derivHist->Fill(roiStartTick + idx, derivativeVec.at(idx));
331  erosionHist->Fill(roiStartTick + idx, erosionVec.at(idx));
332  dilationHist->Fill(roiStartTick + idx, dilationVec.at(idx));
333  }
334 
335  // Fill hits
336  for (const auto& hitCandidate : hitCandidateVec) {
337  candHitHist->Fill(hitCandidate.hitCenter, hitCandidate.hitHeight);
338  maxDerivHist->Fill(hitCandidate.maxTick, hitCandidate.maxDerivative);
339  maxDerivHist->Fill(hitCandidate.minTick, hitCandidate.minDerivative);
340  strtStopHist->Fill(hitCandidate.startTick, waveform.at(hitCandidate.startTick));
341  strtStopHist->Fill(hitCandidate.stopTick, waveform.at(hitCandidate.stopTick));
342  }
343 
345  fChannelCnt++;
346  }
347 
348  if (fOutputHistograms) {
349  // Fill hits
350  for (const auto& hitCandidate : hitCandidateVec) {
351  fDStopStartHist->Fill(hitCandidate.stopTick - hitCandidate.startTick, 1.);
352  fDMaxTickMinTickHist->Fill(hitCandidate.minTick - hitCandidate.maxTick, 1.);
353  fDMaxDerivMinDerivHist->Fill(hitCandidate.maxDerivative - hitCandidate.minDerivative, 1.);
354  }
355 
356  // Get the max dilation/erosion
357  Waveform::const_iterator maxDilationItr =
358  std::max_element(dilationVec.begin(), dilationVec.end());
359  Waveform::const_iterator maxErosionItr =
360  std::max_element(erosionVec.begin(), erosionVec.end());
361 
362  float dilEroRat(1.);
363 
364  if (std::abs(*maxDilationItr) > 0.) dilEroRat = *maxErosionItr / *maxDilationItr;
365 
366  fMaxErosionHist->Fill(*maxErosionItr, 1.);
367  fMaxDilationHist->Fill(*maxDilationItr, 1.);
368  fMaxDilEroRatHist->Fill(dilEroRat, 1.);
369  }
370 
371  return;
372  }
std::vector< geo::WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
intermediate_table::const_iterator const_iterator
uint8_t channel
Definition: CRTFragment.hh:201
string dir
void findHitCandidates(const recob::Wire::RegionsOfInterest_t::datarange_t &, const size_t, const size_t, const size_t, HitCandidateVec &) const override
T abs(T value)
std::unique_ptr< reco_tool::IWaveformTool > fWaveformTool
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
void reco_tool::CandHitMorphological::findHitCandidates ( Waveform::const_iterator  derivStartItr,
Waveform::const_iterator  derivStopItr,
Waveform::const_iterator  erosionStartItr,
Waveform::const_iterator  erosionStopItr,
Waveform::const_iterator  dilationStartItr,
Waveform::const_iterator  dilationStopItr,
const size_t  roiStartTick,
float  dilationThreshold,
HitCandidateVec hitCandidateVec 
) const
private

Definition at line 375 of file CandHitMorphological_tool.cc.

384  {
385  // This function aims to use the erosion/dilation vectors to find candidate hit regions
386  // Once armed with a region then the "standard" differential approach is used to return the candidate peaks
387 
388  // Don't do anything if not enough ticks
389  int ticksInInputWaveform = std::distance(derivStartItr, derivStopItr);
390 
391  if (ticksInInputWaveform < fMinDeltaTicks) return;
392 
393  // This function is recursive, we start by finding the largest element of the dilation vector
394  Waveform::const_iterator maxItr = std::max_element(dilationStartItr, dilationStopItr);
395  float maxVal = *maxItr;
396 
397  // Check that the peak is of reasonable height...
398  if (maxVal < dilationThreshold) return;
399 
400  int maxBin = std::distance(dilationStartItr, maxItr);
401 
402  // The candidate hit region we want lies between the two nearest minima to the maximum we just found
403  // subject to the condition that the erosion vector has return to less than zero
404  Waveform::const_iterator firstDerItr = derivStartItr + maxBin;
405  Waveform::const_iterator erosionItr = erosionStartItr + maxBin;
406 
407  float firstDerivValue = -1.;
408  float erosionCutValue = fErosionFraction * maxVal;
409 
410  // Search for starting point
411  while (firstDerItr != derivStartItr) {
412  // Look for the turnover point
413  if (*erosionItr-- < erosionCutValue) {
414  // We are looking for the zero crossing signifying a minimum value in the waveform
415  // (so the previous derivative < 0 while current is > 0)
416  // We are moving "backwards" so the current value <= 0, the previous value > 0
417  if (*firstDerItr * firstDerivValue <= 0. && firstDerivValue > 0.) break;
418  }
419 
420  firstDerivValue = *firstDerItr--;
421  }
422 
423  // Set the start bin
424  int hitRegionStart = std::distance(derivStartItr, firstDerItr);
425 
426  // Now go the other way
427  Waveform::const_iterator lastDerItr = derivStartItr + maxBin;
428 
429  // Reset the local variables
430  float lastDerivValue = 1.;
431 
432  erosionItr = erosionStartItr + maxBin;
433 
434  // Search for starting point
435  while (lastDerItr != derivStopItr) {
436  if (*erosionItr++ <= erosionCutValue) {
437  // We are again looking for the zero crossing signifying a minimum value in the waveform
438  // This time we are moving forward, so test is that previous value < 0, new value >= 0
439  if (*lastDerItr * lastDerivValue <= 0. && lastDerivValue < 0.) break;
440  }
441 
442  lastDerivValue = *lastDerItr++;
443  }
444 
445  // Set the stop bin
446  int hitRegionStop = std::distance(derivStartItr, lastDerItr);
447 
448  // Recursive call to find any hits in front of where we are now
449  if (hitRegionStart > fMinDeltaTicks)
450  findHitCandidates(derivStartItr,
451  derivStartItr + hitRegionStart,
452  erosionStartItr,
453  erosionStartItr + hitRegionStart,
454  dilationStartItr,
455  dilationStartItr + hitRegionStart,
456  roiStartTick,
458  hitCandidateVec);
459 
460  // Call the differential hit finding to get the actual hits within the region
461  findHitCandidates(derivStartItr + hitRegionStart,
462  derivStartItr + hitRegionStop,
463  roiStartTick + hitRegionStart,
466  hitCandidateVec);
467 
468  // Now call ourselves again to find any hits trailing the region we just identified
469  if (std::distance(lastDerItr, derivStopItr) > fMinDeltaTicks)
470  findHitCandidates(derivStartItr + hitRegionStop,
471  derivStopItr,
472  erosionStartItr + hitRegionStop,
473  erosionStopItr,
474  dilationStartItr + hitRegionStop,
475  dilationStopItr,
476  roiStartTick + hitRegionStop,
478  hitCandidateVec);
479 
480  return;
481  }
intermediate_table::const_iterator const_iterator
void findHitCandidates(const recob::Wire::RegionsOfInterest_t::datarange_t &, const size_t, const size_t, const size_t, HitCandidateVec &) const override
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
void reco_tool::CandHitMorphological::findHitCandidates ( Waveform::const_iterator  startItr,
Waveform::const_iterator  stopItr,
const size_t  roiStartTick,
int  dTicksThreshold,
float  dPeakThreshold,
HitCandidateVec hitCandidateVec 
) const
private

Definition at line 484 of file CandHitMorphological_tool.cc.

490  {
491  // Search for candidate hits...
492  // Strategy is to get the list of all possible max/min pairs of the input derivative vector and then
493  // look for candidate hits in that list
494  CandHitParamsVec candHitParamsVec;
495 
497  startItr, stopItr, dTicksThreshold, dPeakThreshold, candHitParamsVec)) {
498  // We've been given a list of candidate hits so now convert to hits
499  // Version one... simply convert all the candidates
500  for (const auto& tuple : candHitParamsVec) {
501  // Create a new hit candidate and store away
502  HitCandidate hitCandidate;
503 
504  Waveform::const_iterator candStartItr = std::get<0>(tuple);
505  Waveform::const_iterator maxItr = std::get<1>(tuple);
506  Waveform::const_iterator minItr = std::get<2>(tuple);
507  Waveform::const_iterator candStopItr = std::get<3>(tuple);
508 
509  Waveform::const_iterator peakItr =
510  std::min_element(maxItr, minItr, [](const auto& left, const auto& right) {
511  return std::fabs(left) < std::fabs(right);
512  });
513 
514  // Check balance
515  if (2 * std::distance(peakItr, minItr) < std::distance(maxItr, peakItr))
516  peakItr--;
517  else if (2 * std::distance(maxItr, peakItr) < std::distance(peakItr, minItr))
518  peakItr++;
519 
520  // Special handling of the start tick for multiple hits
521  size_t hitCandidateStartTick = roiStartTick + std::distance(startItr, candStartItr);
522 
523  if (!hitCandidateVec.empty()) {
524  int deltaTicks = hitCandidateStartTick - hitCandidateVec.back().stopTick;
525 
526  if (deltaTicks > 0) {
527  hitCandidateStartTick -= deltaTicks / 2;
528  hitCandidateVec.back().stopTick += deltaTicks / 2;
529  }
530  }
531 
532  hitCandidate.startTick = hitCandidateStartTick;
533  hitCandidate.stopTick = roiStartTick + std::distance(startItr, candStopItr);
534  hitCandidate.maxTick = roiStartTick + std::distance(startItr, maxItr);
535  hitCandidate.minTick = roiStartTick + std::distance(startItr, minItr);
536  hitCandidate.maxDerivative = maxItr != stopItr ? *maxItr : 0.;
537  hitCandidate.minDerivative = minItr != stopItr ? *minItr : 0.;
538  hitCandidate.hitCenter = roiStartTick + std::distance(startItr, peakItr) + 0.5;
539  hitCandidate.hitSigma = 0.5 * float(hitCandidate.minTick - hitCandidate.maxTick);
540  hitCandidate.hitHeight = hitCandidate.hitSigma *
541  (hitCandidate.maxDerivative - hitCandidate.minDerivative) / 1.2130;
542 
543  hitCandidateVec.push_back(hitCandidate);
544  }
545  }
546 
547  // // The idea will be to find the largest deviation in the input derivative waveform as the starting point. Depending
548  // // on if a maximum or minimum, we search forward or backward to find the minimum or maximum that our extremum
549  // // corresponds to.
550  // std::pair<Waveform::const_iterator, Waveform::const_iterator> minMaxPair = std::minmax_element(startItr, stopItr);
551  //
552  // Waveform::const_iterator maxItr = minMaxPair.second;
553  // Waveform::const_iterator minItr = minMaxPair.first;
554  //
555  // // Use the larger of the two as the starting point and recover the nearest max or min
556  // if (std::fabs(*maxItr) > std::fabs(*minItr)) minItr = findNearestMin(maxItr, stopItr);
557  // else maxItr = findNearestMax(minItr,startItr);
558  //
559  // int deltaTicks = std::distance(maxItr,minItr);
560  // float range = *maxItr - *minItr;
561  //
562  // // At some point small rolling oscillations on the waveform need to be ignored...
563  // if (deltaTicks >= dTicksThreshold && range > dPeakThreshold)
564  // {
565  // // Need to back up to find zero crossing, this will be the starting point of our
566  // // candidate hit but also the endpoint of the pre sub-waveform we'll search next
567  // Waveform::const_iterator newEndItr = findStartTick(maxItr, startItr);
568  //
569  // int startTick = std::distance(startItr,newEndItr);
570  //
571  // // Now need to go forward to again get close to zero, this will then be the end point
572  // // of our candidate hit and the starting point for the post sub-waveform to search
573  // Waveform::const_iterator newStartItr = findStopTick(minItr, stopItr);
574  //
575  // int stopTick = std::distance(startItr,newStartItr);
576  //
577  // // Find hits in the section of the waveform leading up to this candidate hit
578  // if (startTick > 2)
579  // {
580  // // Special handling for merged hits
581  // if (*(newEndItr-1) > 0.) {dTicksThreshold = 2; dPeakThreshold = 0.; }
582  // else {dTicksThreshold = fMinDeltaTicks; dPeakThreshold = fMinDeltaPeaks;}
583  //
584  // findHitCandidates(startItr,newEndItr+1,roiStartTick,dTicksThreshold,dPeakThreshold,hitCandidateVec);
585  // }
586  //
587  // // Create a new hit candidate and store away
588  // HitCandidate_t hitCandidate;
589  //
590  // Waveform::const_iterator peakItr = std::min_element(maxItr,minItr,[](const auto& left, const auto& right){return std::fabs(left) < std::fabs(right);});
591  //
592  // // Check balance
593  // if (2 * std::distance(peakItr,minItr) < std::distance(maxItr,peakItr)) peakItr--;
594  // else if (2 * std::distance(maxItr,peakItr) < std::distance(peakItr,minItr)) peakItr++;
595  //
596  // // Special handling of the start tick for multiple hits
597  // size_t hitCandidateStartTick = roiStartTick + startTick;
598  //
599  // if (!hitCandidateVec.empty())
600  // {
601  // int deltaTicks = hitCandidateStartTick - hitCandidateVec.back().stopTick;
602  //
603  // if (deltaTicks > 0)
604  // {
605  // hitCandidateStartTick -= deltaTicks / 2;
606  // hitCandidateVec.back().stopTick += deltaTicks / 2;
607  // }
608  // }
609  //
610  // hitCandidate.startTick = hitCandidateStartTick;
611  // hitCandidate.stopTick = roiStartTick + stopTick;
612  // hitCandidate.maxTick = roiStartTick + std::distance(startItr,maxItr);
613  // hitCandidate.minTick = roiStartTick + std::distance(startItr,minItr);
614  // hitCandidate.maxDerivative = maxItr != stopItr ? *maxItr : 0.;
615  // hitCandidate.minDerivative = minItr != stopItr ? *minItr : 0.;
616  // hitCandidate.hitCenter = roiStartTick + std::distance(startItr,peakItr) + 0.5;
617  // hitCandidate.hitSigma = 0.5 * float(hitCandidate.minTick - hitCandidate.maxTick);
618  // hitCandidate.hitHeight = hitCandidate.hitSigma * (hitCandidate.maxDerivative - hitCandidate.minDerivative) / 1.2130;
619  //
620  // hitCandidateVec.push_back(hitCandidate);
621  //
622  // // Finally, search the section of the waveform following this candidate for more hits
623  // if (std::distance(newStartItr,stopItr) > 2)
624  // {
625  // // Special handling for merged hits
626  // if (*(newStartItr+1) < 0.) {dTicksThreshold = 2; dPeakThreshold = 0.; }
627  // else {dTicksThreshold = fMinDeltaTicks; dPeakThreshold = fMinDeltaPeaks;}
628  //
629  // findHitCandidates(newStartItr,stopItr,roiStartTick + stopTick,dTicksThreshold,dPeakThreshold,hitCandidateVec);
630  // }
631  // }
632 
633  return;
634  }
std::vector< CandHitParams > CandHitParamsVec
intermediate_table::const_iterator const_iterator
bool getListOfHitCandidates(Waveform::const_iterator, Waveform::const_iterator, int, float, CandHitParamsVec &) const
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
ICandidateHitFinder::Waveform::const_iterator reco_tool::CandHitMorphological::findNearestMax ( Waveform::const_iterator  minItr,
Waveform::const_iterator  startItr 
) const
private

Definition at line 758 of file CandHitMorphological_tool.cc.

760  {
761  // Set the internal loop variable...
762  Waveform::const_iterator lastItr = minItr;
763 
764  // One extra condition to watch for here, make sure we can actually back up!
765  if (std::distance(startItr, minItr) > 0) {
766  // Similar to searching for a maximum, we loop backward over ticks looking for the waveform to start decreasing
767  while ((lastItr - 1) != startItr) {
768  if (*(lastItr - 1) < *lastItr) break;
769 
770  lastItr--;
771  }
772  }
773 
774  return lastItr;
775  }
intermediate_table::const_iterator const_iterator
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
ICandidateHitFinder::Waveform::const_iterator reco_tool::CandHitMorphological::findNearestMin ( Waveform::const_iterator  maxItr,
Waveform::const_iterator  stopItr 
) const
private

Definition at line 739 of file CandHitMorphological_tool.cc.

741  {
742  // reset the min iterator and search forward to find the nearest minimum
743  Waveform::const_iterator lastItr = maxItr;
744 
745  // The strategy is simple...
746  // We are at a maximum so we search forward until we find the lowest negative point
747  while ((lastItr + 1) != stopItr) {
748  if (*(lastItr + 1) > *lastItr) break;
749 
750  lastItr++;
751  }
752 
753  // The minimum will be the last iterator value...
754  return lastItr;
755  }
intermediate_table::const_iterator const_iterator
ICandidateHitFinder::Waveform::const_iterator reco_tool::CandHitMorphological::findStartTick ( Waveform::const_iterator  maxItr,
Waveform::const_iterator  startItr 
) const
private

Definition at line 778 of file CandHitMorphological_tool.cc.

780  {
781  Waveform::const_iterator lastItr = maxItr;
782 
783  // If we can't back up then there is nothing to do
784  if (std::distance(startItr, lastItr) > 0) {
785  // In theory, we are starting at a maximum and want to find the "start" of the candidate peak
786  // Ideally we would look to search backward to the point where the (derivative) waveform crosses zero again.
787  // However, the complication is that we need to watch for the case where two peaks are merged together and
788  // we might run through another peak before crossing zero...
789  // So... loop until we hit the startItr...
790  Waveform::const_iterator loopItr = lastItr - 1;
791 
792  while (loopItr != startItr) {
793  // Ideal world case, we cross zero... but we might encounter a minimum... or an inflection point
794  if (*loopItr < 0. || !(*loopItr < *lastItr)) break;
795 
796  lastItr = loopItr--;
797  }
798  }
799  else
800  lastItr = startItr;
801 
802  return lastItr;
803  }
intermediate_table::const_iterator const_iterator
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
ICandidateHitFinder::Waveform::const_iterator reco_tool::CandHitMorphological::findStopTick ( Waveform::const_iterator  minItr,
Waveform::const_iterator  stopItr 
) const
private

Definition at line 806 of file CandHitMorphological_tool.cc.

808  {
809  Waveform::const_iterator lastItr = minItr;
810 
811  // If we can't go forward then there is really nothing to do
812  if (std::distance(minItr, stopItr) > 1) {
813  // Pretty much the same strategy as for finding the start tick...
814  Waveform::const_iterator loopItr = lastItr + 1;
815 
816  while (loopItr != stopItr) {
817  // Ideal case that we have crossed zero coming from a minimum... but watch for a maximum as well
818  if (*loopItr > 0. || !(*loopItr > *lastItr)) break;
819 
820  lastItr = loopItr++;
821  }
822  }
823 
824  return lastItr;
825  }
intermediate_table::const_iterator const_iterator
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
bool reco_tool::CandHitMorphological::getListOfHitCandidates ( Waveform::const_iterator  startItr,
Waveform::const_iterator  stopItr,
int  dTicksThreshold,
float  dPeakThreshold,
CandHitParamsVec candHitParamsVec 
) const
private

Definition at line 637 of file CandHitMorphological_tool.cc.

642  {
643  // We'll check if any of our candidates meet the requirements so declare the result here
644  bool foundCandidate(false);
645 
646  int dTicks = std::distance(startItr, stopItr);
647 
648  // Search for candidate hits...
649  // But only if enough ticks
650  if (dTicks < fMinDeltaTicks) return foundCandidate;
651 
652  // Generally, the mission is simple... the goal is to find all possible combinations of maximum/minimum pairs in
653  // the input (presumed) derivative waveform. We can do this with a divice and conquer approach where we start by
654  // finding the largerst max or min and start from there
655  MaxMinPair minMaxPair = std::minmax_element(startItr, stopItr);
656 
657  Waveform::const_iterator maxItr = minMaxPair.second;
658  Waveform::const_iterator minItr = minMaxPair.first;
659 
660  // Use the larger of the two as the starting point and recover the nearest max or min
661  if (std::fabs(*maxItr) > std::fabs(*minItr))
662  minItr = findNearestMin(maxItr, stopItr);
663  else
664  maxItr = findNearestMax(minItr, startItr);
665 
666  int deltaTicks = std::distance(maxItr, minItr);
667  float range = *maxItr - *minItr;
668 
669  if (deltaTicks < 2) return foundCandidate;
670 
671  // Check if this particular max/min pair would meet the requirements...
672  if (deltaTicks >= dTicksThreshold && range > dPeakThreshold) foundCandidate = true;
673 
674  // Need to back up to find zero crossing, this will be the starting point of our
675  // candidate hit but also the endpoint of the pre sub-waveform we'll search next
676  Waveform::const_iterator candStartItr = findStartTick(maxItr, startItr);
677 
678  // Now need to go forward to again get close to zero, this will then be the end point
679  // of our candidate hit and the starting point for the post sub-waveform to search
680  Waveform::const_iterator candStopItr = findStopTick(minItr, stopItr);
681 
682  // Call ourself to find hit candidates preceding this one
683  bool prevTicks = getListOfHitCandidates(
684  startItr, candStartItr, dTicksThreshold, dPeakThreshold, candHitParamsVec);
685 
686  // The above call will have populated the list of candidate max/min pairs preceding this one, so now add our contribution
687  candHitParamsVec.emplace_back(candStartItr, maxItr, minItr, candStopItr);
688 
689  // Now catch any that might follow this one
690  bool postTicks = getListOfHitCandidates(
691  candStopItr, stopItr, dTicksThreshold, dPeakThreshold, candHitParamsVec);
692 
693  return foundCandidate || prevTicks || postTicks;
694  }
Waveform::const_iterator findNearestMin(Waveform::const_iterator, Waveform::const_iterator) const
Waveform::const_iterator findStopTick(Waveform::const_iterator, Waveform::const_iterator) const
intermediate_table::const_iterator const_iterator
Waveform::const_iterator findStartTick(Waveform::const_iterator, Waveform::const_iterator) const
bool getListOfHitCandidates(Waveform::const_iterator, Waveform::const_iterator, int, float, CandHitParamsVec &) const
double distance(double x1, double y1, double z1, double x2, double y2, double z2)
std::pair< Waveform::const_iterator, Waveform::const_iterator > MaxMinPair
Waveform::const_iterator findNearestMax(Waveform::const_iterator, Waveform::const_iterator) const
void reco_tool::CandHitMorphological::MergeHitCandidates ( const recob::Wire::RegionsOfInterest_t::datarange_t &  rangeData,
const HitCandidateVec hitCandidateVec,
MergeHitCandidateVec mergedHitsVec 
) const
overridevirtual

Implements reco_tool::ICandidateHitFinder.

Definition at line 697 of file CandHitMorphological_tool.cc.

701  {
702  // If nothing on the input end then nothing to do
703  if (hitCandidateVec.empty()) return;
704 
705  // The idea is to group hits that "touch" so they can be part of common fit, those that
706  // don't "touch" are fit independently. So here we build the output vector to achieve that
707  // Get a container for the hits...
708  HitCandidateVec groupedHitVec;
709 
710  // Initialize the end of the last hit which we'll set to the first input hit's stop
711  size_t lastStopTick = hitCandidateVec.front().stopTick;
712 
713  // Step through the input hit candidates and group them by proximity
714  for (const auto& hitCandidate : hitCandidateVec) {
715  // Small pulse height hits should not be considered?
716  if (hitCandidate.hitHeight > fMinHitHeight) {
717  // Check condition that we have a new grouping
718  if (hitCandidate.startTick > lastStopTick + fNumInterveningTicks &&
719  !groupedHitVec.empty()) {
720  mergedHitsVec.emplace_back(groupedHitVec);
721 
722  groupedHitVec.clear();
723  }
724 
725  // Add the current hit to the current group
726  groupedHitVec.emplace_back(hitCandidate);
727 
728  lastStopTick = hitCandidate.stopTick;
729  }
730  }
731 
732  // Check end condition
733  if (!groupedHitVec.empty()) mergedHitsVec.emplace_back(groupedHitVec);
734 
735  return;
736  }
std::vector< HitCandidate > HitCandidateVec

Member Data Documentation

size_t reco_tool::CandHitMorphological::fChannelCnt
mutableprivate

Definition at line 114 of file CandHitMorphological_tool.cc.

const float reco_tool::CandHitMorphological::fDilationFraction
private

Definition at line 86 of file CandHitMorphological_tool.cc.

const float reco_tool::CandHitMorphological::fDilationThreshold
private

Definition at line 85 of file CandHitMorphological_tool.cc.

TH1F* reco_tool::CandHitMorphological::fDMaxDerivMinDerivHist
private

Definition at line 104 of file CandHitMorphological_tool.cc.

TH1F* reco_tool::CandHitMorphological::fDMaxTickMinTickHist
private

Definition at line 103 of file CandHitMorphological_tool.cc.

TH1F* reco_tool::CandHitMorphological::fDStopStartHist
private

Definition at line 102 of file CandHitMorphological_tool.cc.

const float reco_tool::CandHitMorphological::fErosionFraction
private

Definition at line 87 of file CandHitMorphological_tool.cc.

const float reco_tool::CandHitMorphological::fFitNSigmaFromCenter
private

Definition at line 97 of file CandHitMorphological_tool.cc.

const geo::GeometryCore* reco_tool::CandHitMorphological::fGeometry = lar::providerFrom<geo::Geometry>()
private

Definition at line 119 of file CandHitMorphological_tool.cc.

art::TFileDirectory* reco_tool::CandHitMorphological::fHistDirectory
private

Definition at line 99 of file CandHitMorphological_tool.cc.

size_t reco_tool::CandHitMorphological::fLastChannel
mutableprivate

Definition at line 113 of file CandHitMorphological_tool.cc.

TH1F* reco_tool::CandHitMorphological::fMaxDilationHist
private

Definition at line 106 of file CandHitMorphological_tool.cc.

TH1F* reco_tool::CandHitMorphological::fMaxDilEroRatHist
private

Definition at line 107 of file CandHitMorphological_tool.cc.

TH1F* reco_tool::CandHitMorphological::fMaxErosionHist
private

Definition at line 105 of file CandHitMorphological_tool.cc.

const float reco_tool::CandHitMorphological::fMinDeltaPeaks
private

Definition at line 89 of file CandHitMorphological_tool.cc.

const int reco_tool::CandHitMorphological::fMinDeltaTicks
private

Definition at line 88 of file CandHitMorphological_tool.cc.

const float reco_tool::CandHitMorphological::fMinHitHeight
private

Definition at line 90 of file CandHitMorphological_tool.cc.

const size_t reco_tool::CandHitMorphological::fNumInterveningTicks
private

Definition at line 91 of file CandHitMorphological_tool.cc.

const bool reco_tool::CandHitMorphological::fOutputHistograms
private

Definition at line 93 of file CandHitMorphological_tool.cc.

const bool reco_tool::CandHitMorphological::fOutputWaveforms
private

Definition at line 95 of file CandHitMorphological_tool.cc.

const size_t reco_tool::CandHitMorphological::fPlane
private

Definition at line 84 of file CandHitMorphological_tool.cc.

const int reco_tool::CandHitMorphological::fStructuringElement
private

Definition at line 92 of file CandHitMorphological_tool.cc.

std::unique_ptr<reco_tool::IWaveformTool> reco_tool::CandHitMorphological::fWaveformTool
private

Definition at line 117 of file CandHitMorphological_tool.cc.


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