LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
nnet::PointIdAlg Class Reference

#include "PointIdAlg.h"

Inheritance diagram for nnet::PointIdAlg:
img::DataProviderAlg

Classes

struct  Config
 

Public Types

enum  EDownscaleMode { kMax = 1, kMaxMean = 2, kMean = 3 }
 

Public Member Functions

 PointIdAlg (const fhicl::ParameterSet &pset)
 
 PointIdAlg (const Config &config)
 
 ~PointIdAlg () override
 
std::vector< std::string > const & outputLabels () const
 network output labels More...
 
float predictIdValue (unsigned int wire, float drift, size_t outIdx=0) const
 calculate single-value prediction (2-class probability) for [wire, drift] point More...
 
std::vector< float > predictIdVector (unsigned int wire, float drift) const
 calculate multi-class probabilities for [wire, drift] point More...
 
std::vector< std::vector< float > > predictIdVectors (std::vector< std::pair< unsigned int, float >> points) const
 
std::vector< std::vector< float > > const & patchData2D () const
 
std::vector< float > patchData1D () const
 
bool isInsideFiducialRegion (unsigned int wire, float drift) const
 
bool isCurrentPatch (unsigned int wire, float drift) const
 
bool isSamePatch (unsigned int wire1, float drift1, unsigned int wire2, float drift2) const
 test if two wire/drift coordinates point to the same patch More...
 
bool setWireDriftData (const detinfo::DetectorClocksData &clock_data, const detinfo::DetectorPropertiesData &det_prop, const std::vector< recob::Wire > &wires, unsigned int plane, unsigned int tpc, unsigned int cryo)
 
std::vector< float > const & wireData (size_t widx) const
 
std::vector< std::vector< float > > getPatch (size_t wire, float drift, size_t patchSizeW, size_t patchSizeD) const
 
float getPixelOrZero (int wire, int drift) const
 
double getAdcSum () const
 
size_t getAdcArea () const
 
float poolMax (int wire, int drift, size_t r=0) const
 Pool max value in a patch around the wire/drift pixel. More...
 
unsigned int Cryo () const
 Pool sum of pixels in a patch around the wire/drift pixel. More...
 
unsigned int TPC () const
 
unsigned int Plane () const
 
unsigned int NWires () const
 
unsigned int NScaledDrifts () const
 
unsigned int NCachedDrifts () const
 
unsigned int DriftWindow () const
 
float ZeroLevel () const
 Level of zero ADC after scaling. More...
 
double LifetimeCorrection (detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, double tick) const
 

Static Public Member Functions

static std::vector< float > flattenData2D (std::vector< std::vector< float >> const &patch)
 

Protected Member Functions

std::vector< float > downscaleMax (std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
 
std::vector< float > downscaleMaxMean (std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
 
std::vector< float > downscaleMean (std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
 
std::vector< float > downscale (std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
 
size_t getDriftIndex (float drift) const
 
std::optional< std::vector< float > > setWireData (std::vector< float > const &adc, size_t wireIdx) const
 
bool patchFromDownsampledView (size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float >> &patch) const
 
bool patchFromOriginalView (size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float >> &patch) const
 
virtual DataProviderAlgView resizeView (detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, size_t wires, size_t drifts)
 

Protected Attributes

DataProviderAlgView fAlgView
 
EDownscaleMode fDownscaleMode
 
size_t fDriftWindow
 
bool fDownscaleFullView
 
float fDriftWindowInv
 
calo::CalorimetryAlg fCalorimetryAlg
 
geo::Geometry const * fGeometry
 
geo::WireReadoutGeom const * fWireReadoutGeom
 

Private Member Functions

bool bufferPatch (size_t wire, float drift, std::vector< std::vector< float >> &patch) const
 
bool bufferPatch (size_t wire, float drift) const
 
void resizePatch ()
 
void deleteNNet ()
 

Private Attributes

std::string fNNetModelFilePath
 
std::vector< std::string > fNNetOutputs
 
nnet::ModelInterfacefNNet
 
std::vector< std::vector< float > > fWireDriftPatch
 
size_t fPatchSizeW
 
size_t fPatchSizeD
 
size_t fCurrentWireIdx
 
size_t fCurrentScaledDrift
 

Detailed Description

Definition at line 94 of file PointIdAlg.h.

Member Enumeration Documentation

Enumerator
kMax 
kMaxMean 
kMean 

Definition at line 62 of file DataProviderAlg.h.

Constructor & Destructor Documentation

nnet::PointIdAlg::PointIdAlg ( const fhicl::ParameterSet pset)
inline

Definition at line 110 of file PointIdAlg.h.

110 : PointIdAlg(fhicl::Table<Config>(pset, {})()) {}
PointIdAlg(const fhicl::ParameterSet &pset)
Definition: PointIdAlg.h:110
nnet::PointIdAlg::PointIdAlg ( const Config config)

Definition at line 173 of file PointIdAlg.cxx.

References deleteNNet(), fNNet, fNNetModelFilePath, fNNetOutputs, nnet::PointIdAlg::Config::NNetModelFile, nnet::PointIdAlg::Config::NNetOutputs, and resizePatch().

174  : img::DataProviderAlg(config)
175  , fNNet(0)
176  , fPatchSizeW(config.PatchSizeW())
177  , fPatchSizeD(config.PatchSizeD())
178  , fCurrentWireIdx(99999)
179  , fCurrentScaledDrift(99999)
180 {
181  fNNetModelFilePath = config.NNetModelFile();
182  fNNetOutputs = config.NNetOutputs();
183 
184  deleteNNet();
185 
186  if ((fNNetModelFilePath.length() > 5) &&
187  (fNNetModelFilePath.compare(fNNetModelFilePath.length() - 5, 5, ".nnet") == 0)) {
189  }
190  else if ((fNNetModelFilePath.length() > 3) &&
191  (fNNetModelFilePath.compare(fNNetModelFilePath.length() - 3, 3, ".pb") == 0)) {
193  }
194  else {
195  mf::LogError("PointIdAlg") << "File name extension not supported.";
196  }
197 
198  if (!fNNet) { throw cet::exception("nnet::PointIdAlg") << "Loading model from file failed."; }
199 
200  resizePatch();
201 }
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
std::vector< std::string > fNNetOutputs
Definition: PointIdAlg.h:147
size_t fPatchSizeD
Definition: PointIdAlg.h:151
size_t fCurrentScaledDrift
Definition: PointIdAlg.h:153
nnet::ModelInterface * fNNet
Definition: PointIdAlg.h:148
size_t fPatchSizeW
Definition: PointIdAlg.h:151
std::string fNNetModelFilePath
Definition: PointIdAlg.h:146
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
size_t fCurrentWireIdx
Definition: PointIdAlg.h:153
nnet::PointIdAlg::~PointIdAlg ( )
override

Definition at line 204 of file PointIdAlg.cxx.

References deleteNNet().

205 {
206  deleteNNet();
207 }

Member Function Documentation

bool nnet::PointIdAlg::bufferPatch ( size_t  wire,
float  drift,
std::vector< std::vector< float >> &  patch 
) const
inlineprivate

Definition at line 154 of file PointIdAlg.h.

Referenced by predictIdValue(), predictIdVector(), and predictIdVectors().

155  {
156  if (fDownscaleFullView) {
157  size_t sd = (size_t)(drift / fDriftWindow);
158  if ((fCurrentWireIdx == wire) && (fCurrentScaledDrift == sd))
159  return true; // still within the current position
160 
161  fCurrentWireIdx = wire;
162  fCurrentScaledDrift = sd;
163 
164  return patchFromDownsampledView(wire, drift, fPatchSizeW, fPatchSizeD, patch);
165  }
166  else {
167  if ((fCurrentWireIdx == wire) && (fCurrentScaledDrift == drift))
168  return true; // still within the current position
169 
170  fCurrentWireIdx = wire;
171  fCurrentScaledDrift = drift;
172 
173  return patchFromOriginalView(wire, drift, fPatchSizeW, fPatchSizeD, patch);
174  }
175  }
size_t fPatchSizeD
Definition: PointIdAlg.h:151
bool patchFromOriginalView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float >> &patch) const
size_t fCurrentScaledDrift
Definition: PointIdAlg.h:153
bool patchFromDownsampledView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float >> &patch) const
size_t fPatchSizeW
Definition: PointIdAlg.h:151
size_t fCurrentWireIdx
Definition: PointIdAlg.h:153
bool nnet::PointIdAlg::bufferPatch ( size_t  wire,
float  drift 
) const
inlineprivate

Definition at line 176 of file PointIdAlg.h.

177  {
178  return bufferPatch(wire, drift, fWireDriftPatch);
179  }
std::vector< std::vector< float > > fWireDriftPatch
Definition: PointIdAlg.h:150
bool bufferPatch(size_t wire, float drift, std::vector< std::vector< float >> &patch) const
Definition: PointIdAlg.h:154
unsigned int img::DataProviderAlg::Cryo ( ) const
inlineinherited

Pool sum of pixels in a patch around the wire/drift pixel.

Definition at line 159 of file DataProviderAlg.h.

Referenced by nnet::TrainingDataAlg::collectVtxFlags().

159 { return fCryo; }
void nnet::PointIdAlg::deleteNNet ( )
inlineprivate

Definition at line 182 of file PointIdAlg.h.

Referenced by PointIdAlg(), and ~PointIdAlg().

183  {
184  if (fNNet) delete fNNet;
185  fNNet = 0;
186  }
nnet::ModelInterface * fNNet
Definition: PointIdAlg.h:148
std::vector<float> img::DataProviderAlg::downscale ( std::size_t  dst_size,
std::vector< float > const &  adc,
size_t  tick0 
) const
inlineprotectedinherited

Definition at line 196 of file DataProviderAlg.h.

References img::DataProviderAlg::kMax, img::DataProviderAlg::kMaxMean, and img::DataProviderAlg::kMean.

Referenced by img::DataProviderAlg::patchFromOriginalView(), and img::DataProviderAlg::setWireData().

199  {
200  switch (fDownscaleMode) {
201  case img::DataProviderAlg::kMean: return downscaleMean(dst_size, adc, tick0);
202  case img::DataProviderAlg::kMaxMean: return downscaleMaxMean(dst_size, adc, tick0);
203  case img::DataProviderAlg::kMax: return downscaleMax(dst_size, adc, tick0);
204  }
205  throw cet::exception("img::DataProviderAlg") << "Downscale mode not supported." << std::endl;
206  }
std::vector< float > downscaleMaxMean(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
std::vector< float > downscaleMax(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
std::vector< float > downscaleMean(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
EDownscaleMode fDownscaleMode
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< float > img::DataProviderAlg::downscaleMax ( std::size_t  dst_size,
std::vector< float > const &  adc,
size_t  tick0 
) const
protectedinherited

Definition at line 193 of file DataProviderAlg.cxx.

References img::DataProviderAlg::fAlgView, img::DataProviderAlg::fDriftWindow, img::DataProviderAlgView::fLifetimeCorrFactors, and img::DataProviderAlg::scaleAdcSamples().

196 {
197  size_t kStop = dst_size;
198  std::vector<float> result(dst_size);
199  if (adc.size() < kStop) { kStop = adc.size(); }
200  for (size_t i = 0, k0 = 0; i < kStop; ++i, k0 += fDriftWindow) {
201  size_t k1 = k0 + fDriftWindow;
202 
203  float max_adc = adc[k0] * fAlgView.fLifetimeCorrFactors[k0 + tick0];
204  for (size_t k = k0 + 1; k < k1; ++k) {
205  float ak = adc[k] * fAlgView.fLifetimeCorrFactors[k + tick0];
206  if (ak > max_adc) max_adc = ak;
207  }
208  result[i] = max_adc;
209  }
210  scaleAdcSamples(result);
211  return result;
212 }
void scaleAdcSamples(std::vector< float > &values) const
DataProviderAlgView fAlgView
std::vector< float > fLifetimeCorrFactors
std::vector< float > img::DataProviderAlg::downscaleMaxMean ( std::size_t  dst_size,
std::vector< float > const &  adc,
size_t  tick0 
) const
protectedinherited

Definition at line 214 of file DataProviderAlg.cxx.

References img::DataProviderAlg::fAlgView, img::DataProviderAlg::fDriftWindow, img::DataProviderAlgView::fLifetimeCorrFactors, n, and img::DataProviderAlg::scaleAdcSamples().

217 {
218  size_t kStop = dst_size;
219  std::vector<float> result(dst_size);
220  if (adc.size() < kStop) { kStop = adc.size(); }
221  for (size_t i = 0, k0 = 0; i < kStop; ++i, k0 += fDriftWindow) {
222  size_t k1 = k0 + fDriftWindow;
223  size_t max_idx = k0;
224  float max_adc = adc[k0] * fAlgView.fLifetimeCorrFactors[k0 + tick0];
225  for (size_t k = k0 + 1; k < k1; ++k) {
226  float ak = adc[k] * fAlgView.fLifetimeCorrFactors[k + tick0];
227  if (ak > max_adc) {
228  max_adc = ak;
229  max_idx = k;
230  }
231  }
232 
233  size_t n = 1;
234  if (max_idx > 0) {
235  max_adc += adc[max_idx - 1] * fAlgView.fLifetimeCorrFactors[max_idx - 1 + tick0];
236  n++;
237  }
238  if (max_idx + 1 < adc.size()) {
239  max_adc += adc[max_idx + 1] * fAlgView.fLifetimeCorrFactors[max_idx + 1 + tick0];
240  n++;
241  }
242 
243  result[i] = max_adc / n;
244  }
245  scaleAdcSamples(result);
246  return result;
247 }
void scaleAdcSamples(std::vector< float > &values) const
DataProviderAlgView fAlgView
std::vector< float > fLifetimeCorrFactors
Char_t n[5]
std::vector< float > img::DataProviderAlg::downscaleMean ( std::size_t  dst_size,
std::vector< float > const &  adc,
size_t  tick0 
) const
protectedinherited

Definition at line 249 of file DataProviderAlg.cxx.

References img::DataProviderAlg::fAlgView, img::DataProviderAlg::fDriftWindow, img::DataProviderAlg::fDriftWindowInv, img::DataProviderAlgView::fLifetimeCorrFactors, and img::DataProviderAlg::scaleAdcSamples().

252 {
253  size_t kStop = dst_size;
254  std::vector<float> result(dst_size);
255  if (adc.size() < kStop) { kStop = adc.size(); }
256  for (size_t i = 0, k0 = 0; i < kStop; ++i, k0 += fDriftWindow) {
257  size_t k1 = k0 + fDriftWindow;
258 
259  float sum_adc = 0;
260  for (size_t k = k0; k < k1; ++k) {
261  if (k + tick0 < fAlgView.fLifetimeCorrFactors.size())
262  sum_adc += adc[k] * fAlgView.fLifetimeCorrFactors[k + tick0];
263  }
264  result[i] = sum_adc * fDriftWindowInv;
265  }
266  scaleAdcSamples(result);
267  return result;
268 }
void scaleAdcSamples(std::vector< float > &values) const
DataProviderAlgView fAlgView
std::vector< float > fLifetimeCorrFactors
unsigned int img::DataProviderAlg::DriftWindow ( ) const
inlineinherited

Definition at line 166 of file DataProviderAlg.h.

166 { return fDriftWindow; }
std::vector< float > nnet::PointIdAlg::flattenData2D ( std::vector< std::vector< float >> const &  patch)
static

Definition at line 309 of file PointIdAlg.cxx.

References d, and w.

310 {
311  std::vector<float> flat;
312  if (patch.empty() || patch.front().empty()) {
313  mf::LogError("DataProviderAlg") << "Patch is empty.";
314  return flat;
315  }
316 
317  flat.resize(patch.size() * patch.front().size());
318 
319  for (size_t w = 0, i = 0; w < patch.size(); ++w) {
320  auto const& wire = patch[w];
321  for (size_t d = 0; d < wire.size(); ++d, ++i) {
322  flat[i] = wire[d];
323  }
324  }
325 
326  return flat;
327 }
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
Float_t d
Definition: plot.C:235
Float_t w
Definition: plot.C:20
size_t img::DataProviderAlg::getAdcArea ( ) const
inlineinherited

Definition at line 151 of file DataProviderAlg.h.

References r.

151 { return fAdcAreaOverThr; }
double img::DataProviderAlg::getAdcSum ( ) const
inlineinherited

Definition at line 150 of file DataProviderAlg.h.

150 { return fAdcSumOverThr; }
size_t img::DataProviderAlg::getDriftIndex ( float  drift) const
inlineprotectedinherited

Definition at line 208 of file DataProviderAlg.h.

References lar::dump::vector().

Referenced by img::DataProviderAlg::poolMax().

209  {
210  if (fDownscaleFullView)
211  return (size_t)(drift * fDriftWindowInv);
212  else
213  return (size_t)drift;
214  }
std::vector<std::vector<float> > img::DataProviderAlg::getPatch ( size_t  wire,
float  drift,
size_t  patchSizeW,
size_t  patchSizeD 
) const
inlineinherited

Return patch of data centered on the wire and drift, witht the size in (downscaled) pixels givent with patchSizeW and patchSizeD. Pad with the zero-level calue if patch extends beyond the event projection.

Definition at line 120 of file DataProviderAlg.h.

124  {
125  bool ok = false;
126  std::vector<std::vector<float>> patch;
127  if (fDownscaleFullView) {
128  ok = patchFromDownsampledView(wire, drift, patchSizeW, patchSizeD, patch);
129  }
130  else {
131  ok = patchFromOriginalView(wire, drift, patchSizeW, patchSizeD, patch);
132  }
133 
134  if (ok) return patch;
135  throw cet::exception("img::DataProviderAlg") << "Patch filling failed." << std::endl;
136  }
bool patchFromOriginalView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float >> &patch) const
bool patchFromDownsampledView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float >> &patch) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
float img::DataProviderAlg::getPixelOrZero ( int  wire,
int  drift 
) const
inlineinherited

Return value from the ADC buffer, or zero if coordinates are out of the view; will scale the drift according to the downscale settings.

Definition at line 140 of file DataProviderAlg.h.

141  {
142  size_t didx = getDriftIndex(drift), widx = (size_t)wire;
143 
144  if ((widx < fAlgView.fWireDriftData.size()) && (didx < fAlgView.fNCachedDrifts)) {
145  return fAlgView.fWireDriftData[widx][didx];
146  }
147  return 0;
148  }
std::vector< std::vector< float > > fWireDriftData
DataProviderAlgView fAlgView
size_t getDriftIndex(float drift) const
bool nnet::PointIdAlg::isCurrentPatch ( unsigned int  wire,
float  drift 
) const

test if wire/drift coordinates point to the current patch (so maybe the cnn output does not need to be recalculated)

Definition at line 293 of file PointIdAlg.cxx.

References fCurrentScaledDrift, fCurrentWireIdx, img::DataProviderAlg::fDownscaleFullView, and img::DataProviderAlg::fDriftWindow.

294 {
295  if (fDownscaleFullView) {
296  size_t sd = (size_t)(drift / fDriftWindow);
297  if ((fCurrentWireIdx == wire) && (fCurrentScaledDrift == sd))
298  return true; // still within the current position
299  }
300  else {
301  if ((fCurrentWireIdx == wire) && (fCurrentScaledDrift == drift))
302  return true; // still within the current position
303  }
304 
305  return false; // not a current position
306 }
size_t fCurrentScaledDrift
Definition: PointIdAlg.h:153
size_t fCurrentWireIdx
Definition: PointIdAlg.h:153
bool nnet::PointIdAlg::isInsideFiducialRegion ( unsigned int  wire,
float  drift 
) const

Definition at line 330 of file PointIdAlg.cxx.

References img::DataProviderAlg::fAlgView, img::DataProviderAlg::fDriftWindow, img::DataProviderAlgView::fNScaledDrifts, img::DataProviderAlgView::fNWires, fPatchSizeD, and fPatchSizeW.

Referenced by nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), and nnet::EmTrackMichelId::produce().

331 {
332  size_t marginW = fPatchSizeW / 8; // fPatchSizeX/2 will make patch always completely filled
333  size_t marginD = fPatchSizeD / 8;
334 
335  size_t scaledDrift = (size_t)(drift / fDriftWindow);
336  if ((wire >= marginW) && (wire < fAlgView.fNWires - marginW) && (scaledDrift >= marginD) &&
337  (scaledDrift < fAlgView.fNScaledDrifts - marginD))
338  return true;
339  else
340  return false;
341 }
size_t fPatchSizeD
Definition: PointIdAlg.h:151
DataProviderAlgView fAlgView
size_t fPatchSizeW
Definition: PointIdAlg.h:151
bool nnet::PointIdAlg::isSamePatch ( unsigned int  wire1,
float  drift1,
unsigned int  wire2,
float  drift2 
) const

test if two wire/drift coordinates point to the same patch

Definition at line 276 of file PointIdAlg.cxx.

References img::DataProviderAlg::fDownscaleFullView, and img::DataProviderAlg::fDriftWindow.

280 {
281  if (fDownscaleFullView) {
282  size_t sd1 = (size_t)(drift1 / fDriftWindow);
283  size_t sd2 = (size_t)(drift2 / fDriftWindow);
284  if ((wire1 == wire2) && (sd1 == sd2)) return true; // the same position
285  }
286  else {
287  if ((wire1 == wire2) && ((size_t)drift1 == (size_t)drift2)) return true; // the same position
288  }
289 
290  return false; // not the same position
291 }
double img::DataProviderAlg::LifetimeCorrection ( detinfo::DetectorClocksData const &  clock_data,
detinfo::DetectorPropertiesData const &  det_prop,
double  tick 
) const
inlineinherited

Definition at line 171 of file DataProviderAlg.h.

174  {
175  return fCalorimetryAlg.LifetimeCorrection(clock_data, det_prop, tick);
176  }
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:73
calo::CalorimetryAlg fCalorimetryAlg
double LifetimeCorrection(detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, double time, double T0=0) const
unsigned int img::DataProviderAlg::NCachedDrifts ( ) const
inlineinherited

Definition at line 165 of file DataProviderAlg.h.

165 { return fAlgView.fNCachedDrifts; }
DataProviderAlgView fAlgView
unsigned int img::DataProviderAlg::NScaledDrifts ( ) const
inlineinherited

Definition at line 164 of file DataProviderAlg.h.

Referenced by nnet::PointIdTrainingData::analyze().

164 { return fAlgView.fNScaledDrifts; }
DataProviderAlgView fAlgView
unsigned int img::DataProviderAlg::NWires ( ) const
inlineinherited

Definition at line 163 of file DataProviderAlg.h.

Referenced by nnet::PointIdTrainingData::analyze().

163 { return fAlgView.fNWires; }
DataProviderAlgView fAlgView
std::vector<std::string> const& nnet::PointIdAlg::outputLabels ( ) const
inline

network output labels

Definition at line 117 of file PointIdAlg.h.

References lar::dump::vector().

Referenced by nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), and nnet::EmTrackMichelId::produce().

117 { return fNNetOutputs; }
std::vector< std::string > fNNetOutputs
Definition: PointIdAlg.h:147
std::vector<float> nnet::PointIdAlg::patchData1D ( ) const
inline

Definition at line 131 of file PointIdAlg.h.

132  {
134  } // flat vector made of the patch data, wire after wire
std::vector< std::vector< float > > fWireDriftPatch
Definition: PointIdAlg.h:150
static std::vector< float > flattenData2D(std::vector< std::vector< float >> const &patch)
Definition: PointIdAlg.cxx:309
std::vector<std::vector<float> > const& nnet::PointIdAlg::patchData2D ( ) const
inline

Definition at line 130 of file PointIdAlg.h.

130 { return fWireDriftPatch; }
std::vector< std::vector< float > > fWireDriftPatch
Definition: PointIdAlg.h:150
bool img::DataProviderAlg::patchFromDownsampledView ( size_t  wire,
float  drift,
size_t  size_w,
size_t  size_d,
std::vector< std::vector< float >> &  patch 
) const
protectedinherited

Definition at line 436 of file DataProviderAlg.cxx.

References d, img::DataProviderAlg::fAdcZero, img::DataProviderAlg::fAlgView, img::DataProviderAlg::fDriftWindow, trkf::fill(), img::DataProviderAlgView::fWireDriftData, and w.

441 {
442  int halfSizeW = size_w / 2;
443  int halfSizeD = size_d / 2;
444 
445  int w0 = wire - halfSizeW;
446  int w1 = wire + halfSizeW;
447 
448  size_t sd = (size_t)(drift / fDriftWindow);
449  int d0 = sd - halfSizeD;
450  int d1 = sd + halfSizeD;
451 
452  int wsize = fAlgView.fWireDriftData.size();
453  for (int w = w0, wpatch = 0; w < w1; ++w, ++wpatch) {
454  auto& dst = patch[wpatch];
455  if ((w >= 0) && (w < wsize)) {
456  auto& src = fAlgView.fWireDriftData[w];
457  int dsize = src.size();
458  for (int d = d0, dpatch = 0; d < d1; ++d, ++dpatch) {
459  if ((d >= 0) && (d < dsize)) { dst[dpatch] = src[d]; }
460  else {
461  dst[dpatch] = fAdcZero;
462  }
463  }
464  }
465  else {
466  std::fill(dst.begin(), dst.end(), fAdcZero);
467  }
468  }
469 
470  return true;
471 }
std::vector< std::vector< float > > fWireDriftData
Float_t d
Definition: plot.C:235
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
DataProviderAlgView fAlgView
Float_t w
Definition: plot.C:20
bool img::DataProviderAlg::patchFromOriginalView ( size_t  wire,
float  drift,
size_t  size_w,
size_t  size_d,
std::vector< std::vector< float >> &  patch 
) const
protectedinherited

Definition at line 473 of file DataProviderAlg.cxx.

References d, img::DataProviderAlg::downscale(), img::DataProviderAlg::fAdcZero, img::DataProviderAlg::fAlgView, img::DataProviderAlg::fDriftWindow, trkf::fill(), img::DataProviderAlgView::fWireDriftData, util::size(), tmp, and w.

478 {
479  int dsize = fDriftWindow * size_d;
480  int halfSizeW = size_w / 2;
481  int halfSizeD = dsize / 2;
482 
483  int w0 = wire - halfSizeW;
484  int w1 = wire + halfSizeW;
485 
486  int d0 = int(drift) - halfSizeD;
487  int d1 = int(drift) + halfSizeD;
488 
489  if (d0 < 0) d0 = 0;
490 
491  std::vector<float> tmp(dsize);
492  int wsize = fAlgView.fWireDriftData.size();
493  for (int w = w0, wpatch = 0; w < w1; ++w, ++wpatch) {
494  if ((w >= 0) && (w < wsize)) {
495  auto& src = fAlgView.fWireDriftData[w];
496  int src_size = src.size();
497  for (int d = d0, dpatch = 0; d < d1; ++d, ++dpatch) {
498  if ((d >= 0) && (d < src_size)) { tmp[dpatch] = src[d]; }
499  else {
500  tmp[dpatch] = fAdcZero;
501  }
502  }
503  }
504  else {
505  std::fill(tmp.begin(), tmp.end(), fAdcZero);
506  }
507  patch[wpatch] = downscale(patch[wpatch].size(), tmp, d0);
508  }
509 
510  return true;
511 }
std::vector< std::vector< float > > fWireDriftData
std::vector< float > downscale(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
Float_t tmp
Definition: plot.C:35
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
Float_t d
Definition: plot.C:235
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
DataProviderAlgView fAlgView
Float_t w
Definition: plot.C:20
unsigned int img::DataProviderAlg::Plane ( ) const
inlineinherited
float img::DataProviderAlg::poolMax ( int  wire,
int  drift,
size_t  r = 0 
) const
inherited

Pool max value in a patch around the wire/drift pixel.

Definition at line 142 of file DataProviderAlg.cxx.

References col, d, img::DataProviderAlg::fAlgView, img::DataProviderAlg::fDownscaleFullView, img::DataProviderAlg::fDriftWindow, img::DataProviderAlgView::fNCachedDrifts, img::DataProviderAlgView::fNWires, img::DataProviderAlgView::fWireDriftData, img::DataProviderAlg::getDriftIndex(), r, and w.

Referenced by pma::ProjectionMatchingAlg::validate_on_adc(), and pma::ProjectionMatchingAlg::validate_on_adc_test().

143 {
144  size_t rw = r, rd = r;
145  if (!fDownscaleFullView) { rd *= fDriftWindow; }
146 
147  size_t didx = getDriftIndex(drift);
148  int d0 = didx - rd;
149  if (d0 < 0) { d0 = 0; }
150  int d1 = didx + rd;
151  if (d1 >= (int)fAlgView.fNCachedDrifts) { d1 = fAlgView.fNCachedDrifts - 1; }
152 
153  int w0 = wire - rw;
154  if (w0 < 0) { w0 = 0; }
155  int w1 = wire + rw;
156  if (w1 >= (int)fAlgView.fNWires) { w1 = fAlgView.fNWires - 1; }
157 
158  float adc, max_adc = 0;
159  for (int w = w0; w <= w1; ++w) {
160  auto const* col = fAlgView.fWireDriftData[w].data();
161  for (int d = d0; d <= d1; ++d) {
162  adc = col[d];
163  if (adc > max_adc) { max_adc = adc; }
164  }
165  }
166 
167  return max_adc;
168 }
TRandom r
Definition: spectrum.C:23
std::vector< std::vector< float > > fWireDriftData
Int_t col[ntarg]
Definition: Style.C:29
Float_t d
Definition: plot.C:235
DataProviderAlgView fAlgView
Float_t w
Definition: plot.C:20
size_t getDriftIndex(float drift) const
float nnet::PointIdAlg::predictIdValue ( unsigned int  wire,
float  drift,
size_t  outIdx = 0 
) const

calculate single-value prediction (2-class probability) for [wire, drift] point

Definition at line 218 of file PointIdAlg.cxx.

References bufferPatch(), fNNet, fWireDriftPatch, and nnet::ModelInterface::Run().

219 {
220  float result = 0.;
221 
222  if (!bufferPatch(wire, drift)) {
223  mf::LogError("PointIdAlg") << "Patch buffering failed.";
224  return result;
225  }
226 
227  if (fNNet) {
228  auto out = fNNet->Run(fWireDriftPatch);
229  if (!out.empty()) { result = out[outIdx]; }
230  else {
231  mf::LogError("PointIdAlg") << "Problem with applying model to input.";
232  }
233  }
234 
235  return result;
236 }
std::vector< std::vector< float > > fWireDriftPatch
Definition: PointIdAlg.h:150
bool bufferPatch(size_t wire, float drift, std::vector< std::vector< float >> &patch) const
Definition: PointIdAlg.h:154
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
virtual std::vector< float > Run(std::vector< std::vector< float >> const &inp2d)=0
nnet::ModelInterface * fNNet
Definition: PointIdAlg.h:148
std::vector< float > nnet::PointIdAlg::predictIdVector ( unsigned int  wire,
float  drift 
) const

calculate multi-class probabilities for [wire, drift] point

Definition at line 239 of file PointIdAlg.cxx.

References bufferPatch(), fNNet, fWireDriftPatch, and nnet::ModelInterface::Run().

Referenced by nnet::ParticleDecayId::DetectDecay().

240 {
241  std::vector<float> result;
242 
243  if (!bufferPatch(wire, drift)) {
244  mf::LogError("PointIdAlg") << "Patch buffering failed.";
245  return result;
246  }
247 
248  if (fNNet) {
249  result = fNNet->Run(fWireDriftPatch);
250  if (result.empty()) { mf::LogError("PointIdAlg") << "Problem with applying model to input."; }
251  }
252 
253  return result;
254 }
std::vector< std::vector< float > > fWireDriftPatch
Definition: PointIdAlg.h:150
bool bufferPatch(size_t wire, float drift, std::vector< std::vector< float >> &patch) const
Definition: PointIdAlg.h:154
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
virtual std::vector< float > Run(std::vector< std::vector< float >> const &inp2d)=0
nnet::ModelInterface * fNNet
Definition: PointIdAlg.h:148
std::vector< std::vector< float > > nnet::PointIdAlg::predictIdVectors ( std::vector< std::pair< unsigned int, float >>  points) const

Definition at line 257 of file PointIdAlg.cxx.

References bufferPatch(), fNNet, fPatchSizeD, fPatchSizeW, and nnet::ModelInterface::Run().

Referenced by nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), and nnet::EmTrackMichelId::produce().

259 {
260  if (points.empty() || !fNNet) { return std::vector<std::vector<float>>(); }
261 
262  std::vector<std::vector<std::vector<float>>> inps(
263  points.size(), std::vector<std::vector<float>>(fPatchSizeW, std::vector<float>(fPatchSizeD)));
264  for (size_t i = 0; i < points.size(); ++i) {
265  unsigned int wire = points[i].first;
266  float drift = points[i].second;
267  if (!bufferPatch(wire, drift, inps[i])) {
268  throw cet::exception("PointIdAlg") << "Patch buffering failed" << std::endl;
269  }
270  }
271 
272  return fNNet->Run(inps);
273 }
bool bufferPatch(size_t wire, float drift, std::vector< std::vector< float >> &patch) const
Definition: PointIdAlg.h:154
virtual std::vector< float > Run(std::vector< std::vector< float >> const &inp2d)=0
size_t fPatchSizeD
Definition: PointIdAlg.h:151
nnet::ModelInterface * fNNet
Definition: PointIdAlg.h:148
size_t fPatchSizeW
Definition: PointIdAlg.h:151
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void nnet::PointIdAlg::resizePatch ( )
private

Definition at line 210 of file PointIdAlg.cxx.

References fPatchSizeD, fPatchSizeW, fWireDriftPatch, and r.

Referenced by PointIdAlg().

211 {
213  for (auto& r : fWireDriftPatch)
214  r.resize(fPatchSizeD);
215 }
TRandom r
Definition: spectrum.C:23
std::vector< std::vector< float > > fWireDriftPatch
Definition: PointIdAlg.h:150
size_t fPatchSizeD
Definition: PointIdAlg.h:151
size_t fPatchSizeW
Definition: PointIdAlg.h:151
img::DataProviderAlgView img::DataProviderAlg::resizeView ( detinfo::DetectorClocksData const &  clock_data,
detinfo::DetectorPropertiesData const &  det_prop,
size_t  wires,
size_t  drifts 
)
protectedvirtualinherited

Reimplemented in nnet::TrainingDataAlg.

Definition at line 111 of file DataProviderAlg.cxx.

References img::DataProviderAlg::fAdcZero, img::DataProviderAlg::fCalibrateLifetime, img::DataProviderAlg::fCalorimetryAlg, img::DataProviderAlg::fDownscaleFullView, img::DataProviderAlg::fDriftWindow, img::DataProviderAlgView::fLifetimeCorrFactors, img::DataProviderAlgView::fNCachedDrifts, img::DataProviderAlgView::fNDrifts, img::DataProviderAlgView::fNScaledDrifts, img::DataProviderAlgView::fNWires, img::DataProviderAlgView::fWireChannels, img::DataProviderAlgView::fWireDriftData, raw::InvalidChannelID, and calo::CalorimetryAlg::LifetimeCorrection().

Referenced by nnet::TrainingDataAlg::resizeView(), and img::DataProviderAlg::setWireDriftData().

116 {
118  result.fNWires = wires;
119  result.fNDrifts = drifts;
120  result.fNScaledDrifts = drifts / fDriftWindow;
121  result.fNCachedDrifts = fDownscaleFullView ? result.fNScaledDrifts : drifts;
122 
123  result.fWireChannels.resize(wires, raw::InvalidChannelID);
124 
125  result.fWireDriftData.resize(wires, std::vector<float>(result.fNCachedDrifts, fAdcZero));
126 
127  result.fLifetimeCorrFactors.resize(drifts);
128  if (fCalibrateLifetime) {
129  for (size_t t = 0; t < drifts; ++t) {
130  result.fLifetimeCorrFactors[t] = fCalorimetryAlg.LifetimeCorrection(clock_data, det_prop, t);
131  }
132  }
133  else {
134  for (size_t t = 0; t < drifts; ++t) {
135  result.fLifetimeCorrFactors[t] = 1.0;
136  }
137  }
138  return result;
139 }
std::vector< std::vector< float > > fWireDriftData
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
Definition: RawTypes.h:31
std::vector< raw::ChannelID_t > fWireChannels
calo::CalorimetryAlg fCalorimetryAlg
std::vector< float > fLifetimeCorrFactors
double LifetimeCorrection(detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, double time, double T0=0) const
std::optional< std::vector< float > > img::DataProviderAlg::setWireData ( std::vector< float > const &  adc,
size_t  wireIdx 
) const
protectedinherited

Definition at line 270 of file DataProviderAlg.cxx.

References img::DataProviderAlg::downscale(), img::DataProviderAlg::fAlgView, img::DataProviderAlg::fDownscaleFullView, and img::DataProviderAlgView::fWireDriftData.

Referenced by img::DataProviderAlg::setWireDriftData().

272 {
273  if (wireIdx >= fAlgView.fWireDriftData.size()) { return std::nullopt; }
274  auto& wData = fAlgView.fWireDriftData[wireIdx];
275 
276  if (fDownscaleFullView) {
277  if (!adc.empty()) { return downscale(wData.size(), adc, 0); }
278  return std::nullopt;
279  }
280  if (adc.empty()) { return std::nullopt; }
281  if (adc.size() <= wData.size()) { return adc; }
282  return std::vector<float>(adc.begin(), adc.begin() + wData.size());
283 }
std::vector< std::vector< float > > fWireDriftData
std::vector< float > downscale(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
DataProviderAlgView fAlgView
bool img::DataProviderAlg::setWireDriftData ( const detinfo::DetectorClocksData clock_data,
const detinfo::DetectorPropertiesData det_prop,
const std::vector< recob::Wire > &  wires,
unsigned int  plane,
unsigned int  tpc,
unsigned int  cryo 
)
inherited

Definition at line 286 of file DataProviderAlg.cxx.

References img::DataProviderAlg::addCoherentNoise(), img::DataProviderAlg::addWhiteNoise(), img::DataProviderAlg::applyBlur(), geo::WireReadoutGeom::ChannelToWire(), img::DataProviderAlg::fAdcAreaOverThr, img::DataProviderAlg::fAdcSumOverThr, img::DataProviderAlg::fAdcSumThr, img::DataProviderAlg::fAlgView, img::DataProviderAlg::fCryo, img::DataProviderAlg::fPlane, img::DataProviderAlg::fTPC, img::DataProviderAlgView::fWireChannels, img::DataProviderAlgView::fWireDriftData, img::DataProviderAlg::fWireReadoutGeom, detinfo::DetectorPropertiesData::NumberTimeSamples(), geo::WireReadoutGeom::Nwires(), img::DataProviderAlg::Plane(), img::DataProviderAlg::resizeView(), and img::DataProviderAlg::setWireData().

Referenced by nnet::ParticleDecayId::DetectDecay(), nnet::EmTrackClusterId2out::produce(), nnet::EmTrackClusterId::produce(), nnet::EmTrackMichelId::produce(), nnet::TrainingDataAlg::setDataEventData(), and nnet::TrainingDataAlg::setEventData().

292 {
293  mf::LogInfo("DataProviderAlg") << "Create image for cryo:" << cryo << " tpc:" << tpc
294  << " plane:" << plane;
295 
296  fCryo = cryo;
297  fTPC = tpc;
298  fPlane = plane;
299 
300  fAdcSumOverThr = 0;
301  fAdcAreaOverThr = 0;
302 
303  size_t nwires = fWireReadoutGeom->Nwires({cryo, tpc, plane});
304  size_t ndrifts = det_prop.NumberTimeSamples();
305 
306  fAlgView = resizeView(clock_data, det_prop, nwires, ndrifts);
307 
308  auto const& channelStatus =
310 
311  bool allWrong = true;
312  for (auto const& wire : wires) {
313  auto wireChannelNumber = wire.Channel();
314  if (!channelStatus.IsGood(wireChannelNumber)) { continue; }
315 
316  size_t w_idx = 0;
317  for (auto const& id : fWireReadoutGeom->ChannelToWire(wireChannelNumber)) {
318  if ((id.Plane == plane) && (id.TPC == tpc) && (id.Cryostat == cryo)) {
319  w_idx = id.Wire;
320 
321  auto adc = wire.Signal();
322  if (adc.size() < ndrifts) {
323  mf::LogWarning("DataProviderAlg") << "Wire ADC vector size lower than NumberTimeSamples.";
324  continue; // not critical, maybe other wires are OK, so continue
325  }
326  auto wire_data = setWireData(adc, w_idx);
327  if (!wire_data) {
328  mf::LogWarning("DataProviderAlg") << "Wire data not set.";
329  continue; // also not critical, try to set other wires
330  }
331  fAlgView.fWireDriftData[w_idx] = *wire_data;
332  for (auto v : adc) {
333  if (v >= fAdcSumThr) {
334  fAdcSumOverThr += v;
335  fAdcAreaOverThr++;
336  }
337  }
338 
339  fAlgView.fWireChannels[w_idx] = wireChannelNumber;
340  allWrong = false;
341  }
342  }
343  }
344  if (allWrong) {
345  mf::LogError("DataProviderAlg")
346  << "Wires data not set in the cryo:" << cryo << " tpc:" << tpc << " plane:" << plane;
347  return false;
348  }
349 
350  applyBlur();
351  addWhiteNoise();
353 
354  return true;
355 }
std::optional< std::vector< float > > setWireData(std::vector< float > const &adc, size_t wireIdx) const
unsigned int Nwires(PlaneID const &planeid) const
Returns the total number of wires in the specified plane.
std::vector< std::vector< float > > fWireDriftData
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
virtual DataProviderAlgView resizeView(detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, size_t wires, size_t drifts)
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
unsigned int Plane() const
geo::WireReadoutGeom const * fWireReadoutGeom
std::vector< raw::ChannelID_t > fWireChannels
DataProviderAlgView fAlgView
virtual std::vector< WireID > ChannelToWire(raw::ChannelID_t channel) const =0
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
unsigned int img::DataProviderAlg::TPC ( ) const
inlineinherited

Definition at line 160 of file DataProviderAlg.h.

Referenced by nnet::TrainingDataAlg::collectVtxFlags().

160 { return fTPC; }
std::vector<float> const& img::DataProviderAlg::wireData ( size_t  widx) const
inlineinherited

Definition at line 115 of file DataProviderAlg.h.

Referenced by nnet::PointIdTrainingData::analyze().

115 { return fAlgView.fWireDriftData[widx]; }
std::vector< std::vector< float > > fWireDriftData
DataProviderAlgView fAlgView
float img::DataProviderAlg::ZeroLevel ( ) const
inlineinherited

Level of zero ADC after scaling.

Definition at line 169 of file DataProviderAlg.h.

169 { return fAdcZero; }

Member Data Documentation

calo::CalorimetryAlg img::DataProviderAlg::fCalorimetryAlg
protectedinherited
size_t nnet::PointIdAlg::fCurrentScaledDrift
mutableprivate

Definition at line 153 of file PointIdAlg.h.

Referenced by isCurrentPatch().

size_t nnet::PointIdAlg::fCurrentWireIdx
mutableprivate

Definition at line 153 of file PointIdAlg.h.

Referenced by isCurrentPatch().

EDownscaleMode img::DataProviderAlg::fDownscaleMode
protectedinherited

Definition at line 180 of file DataProviderAlg.h.

Referenced by img::DataProviderAlg::DataProviderAlg().

float img::DataProviderAlg::fDriftWindowInv
protectedinherited
geo::Geometry const* img::DataProviderAlg::fGeometry
protectedinherited
nnet::ModelInterface* nnet::PointIdAlg::fNNet
private

Definition at line 148 of file PointIdAlg.h.

Referenced by PointIdAlg(), predictIdValue(), predictIdVector(), and predictIdVectors().

std::string nnet::PointIdAlg::fNNetModelFilePath
private

Definition at line 146 of file PointIdAlg.h.

Referenced by PointIdAlg().

std::vector<std::string> nnet::PointIdAlg::fNNetOutputs
private

Definition at line 147 of file PointIdAlg.h.

Referenced by PointIdAlg().

size_t nnet::PointIdAlg::fPatchSizeD
private

Definition at line 151 of file PointIdAlg.h.

Referenced by isInsideFiducialRegion(), predictIdVectors(), and resizePatch().

size_t nnet::PointIdAlg::fPatchSizeW
private

Definition at line 151 of file PointIdAlg.h.

Referenced by isInsideFiducialRegion(), predictIdVectors(), and resizePatch().

std::vector<std::vector<float> > nnet::PointIdAlg::fWireDriftPatch
mutableprivate

Definition at line 150 of file PointIdAlg.h.

Referenced by predictIdValue(), predictIdVector(), and resizePatch().

geo::WireReadoutGeom const* img::DataProviderAlg::fWireReadoutGeom
protectedinherited

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