LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
img::DataProviderAlg Class Reference

#include "DataProviderAlg.h"

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

Classes

struct  Config
 

Public Types

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

Public Member Functions

 DataProviderAlg (const fhicl::ParameterSet &pset)
 
 DataProviderAlg (const Config &config)
 
virtual ~DataProviderAlg ()
 
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
 

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::GeometryCore const * fGeometry
 

Private Member Functions

float scaleAdcSample (float val) const
 
void scaleAdcSamples (std::vector< float > &values) const
 
void applyBlur ()
 
void addWhiteNoise ()
 
void addCoherentNoise ()
 

Private Attributes

std::vector< float > fAmplCalibConst
 
bool fCalibrateAmpl
 
bool fCalibrateLifetime
 
unsigned int fCryo = 9999
 
unsigned int fTPC = 9999
 
unsigned int fPlane = 9999
 
float fAdcMax
 
float fAdcMin
 
float fAdcScale
 
float fAdcOffset
 
float fAdcZero
 
double fAdcSumOverThr
 
double fAdcSumThr
 
size_t fAdcAreaOverThr
 
CLHEP::HepJamesRandom fRndEngine
 
std::vector< float > fBlurKernel
 
float fNoiseSigma
 
float fCoherentSigma
 

Detailed Description

Base class providing data for training / running image based classifiers. It can be used also for any other algorithms where 2D projection image is useful. Currently the image is 32-bit fp / pixel, as sson as have time will template it so e.g. byte pixels would be possible.

Definition at line 62 of file DataProviderAlg.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

img::DataProviderAlg::DataProviderAlg ( const fhicl::ParameterSet pset)
inline

Definition at line 101 of file DataProviderAlg.h.

103  {}
DataProviderAlg(const fhicl::ParameterSet &pset)
img::DataProviderAlg::DataProviderAlg ( const Config config)

Definition at line 32 of file DataProviderAlg.cxx.

References calo::CalorimetryAlg::ElectronsFromADCPeak(), fAdcAreaOverThr, fAdcMax, fAdcMin, fAdcOffset, fAdcScale, fAdcSumOverThr, fAdcSumThr, fAdcZero, fAmplCalibConst, fBlurKernel, fCalibrateAmpl, fCalibrateLifetime, fCalorimetryAlg, fCoherentSigma, fDownscaleFullView, fDownscaleMode, fDriftWindow, fDriftWindowInv, fGeometry, fNoiseSigma, art::ServiceHandle< T, SCOPE >::get(), kMax, kMaxMean, kMean, geo::GeometryCore::MaxPlanes(), and ~DataProviderAlg().

33  : fAlgView{}
35  , fDriftWindow(10)
36  , fCalorimetryAlg(config.CalorimetryAlg())
38  , fAdcSumOverThr(0)
39  , fAdcSumThr(10)
40  , // set fixed threshold of 10 ADC counts for counting the sum
42  , fNoiseSigma(0)
43  , fCoherentSigma(0)
44 {
45  fCalibrateLifetime = config.CalibrateLifetime();
46  fCalibrateAmpl = config.CalibrateAmpl();
47 
49  if (fCalibrateAmpl) {
50  mf::LogInfo("DataProviderAlg") << "Using calibration constants:";
51  for (size_t p = 0; p < fAmplCalibConst.size(); ++p) {
52  try {
54  mf::LogInfo("DataProviderAlg") << " plane:" << p << " const:" << 1.0 / fAmplCalibConst[p];
55  }
56  catch (...) {
57  fAmplCalibConst[p] = 1.0;
58  }
59  }
60  }
61  else {
62  mf::LogInfo("DataProviderAlg") << "No plane-to-plane calibration.";
63  for (size_t p = 0; p < fAmplCalibConst.size(); ++p) {
64  fAmplCalibConst[p] = 1.0;
65  }
66  }
67 
68  fDriftWindow = config.DriftWindow();
69  fDownscaleFullView = config.DownscaleFullView();
71 
72  std::string mode_str = config.DownscaleFn();
73  mf::LogVerbatim("DataProviderAlg") << "Downscale mode is: " << mode_str;
74  if (mode_str == "maxpool") {
75  //fnDownscale = [this](std::vector<float> & dst, std::vector<float> const & adc, size_t tick0) { downscaleMax(dst, adc, tick0); };
77  }
78  else if (mode_str == "maxmean") {
79  //fnDownscale = [this](std::vector<float> & dst, std::vector<float> const & adc, size_t tick0) { downscaleMaxMean(dst, adc, tick0); };
81  }
82  else if (mode_str == "mean") {
83  //fnDownscale = [this](std::vector<float> & dst, std::vector<float> const & adc, size_t tick0) { downscaleMean(dst, adc, tick0); };
85  }
86  else {
87  mf::LogError("DataProviderAlg") << "Downscale mode string not recognized, set to max pooling.";
88  //fnDownscale = [this](std::vector<float> & dst, std::vector<float> const & adc, size_t tick0) { downscaleMax(dst, adc, tick0); };
90  }
91 
92  fAdcMax = config.AdcMax();
93  fAdcMin = config.AdcMin();
94  fAdcOffset = config.OutMin();
95  fAdcScale = (config.OutMax() - fAdcOffset) / (fAdcMax - fAdcMin);
96  fAdcZero = fAdcOffset + fAdcScale * (0 - fAdcMin); // level of zero ADC after scaling
97 
98  if (fAdcMax <= fAdcMin) {
99  throw cet::exception("img::DataProviderAlg") << "Misconfigured: AdcMax <= AdcMin" << std::endl;
100  }
101  if (fAdcScale == 0) {
102  throw cet::exception("img::DataProviderAlg") << "Misconfigured: OutMax == OutMin" << std::endl;
103  }
104 
105  fBlurKernel = config.BlurKernel();
106  fNoiseSigma = config.NoiseSigma();
107  fCoherentSigma = config.CoherentSigma();
108 }
geo::GeometryCore const * fGeometry
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
T * get() const
Definition: ServiceHandle.h:69
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
std::vector< float > fBlurKernel
double ElectronsFromADCPeak(double adc, unsigned short plane) const
unsigned int MaxPlanes() const
Returns the largest number of planes among all TPCs in this detector.
DataProviderAlgView fAlgView
calo::CalorimetryAlg fCalorimetryAlg
std::vector< float > fAmplCalibConst
EDownscaleMode fDownscaleMode
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
img::DataProviderAlg::~DataProviderAlg ( )
virtualdefault

Referenced by DataProviderAlg().

Member Function Documentation

void img::DataProviderAlg::addCoherentNoise ( )
private

Definition at line 535 of file DataProviderAlg.cxx.

References d, fAlgView, fCoherentSigma, fDownscaleFullView, fDriftWindow, img::DataProviderAlgView::fNCachedDrifts, fRndEngine, img::DataProviderAlgView::fWireDriftData, noise(), scaleAdcSample(), and w.

Referenced by setWireDriftData().

536 {
537  if (fCoherentSigma == 0) return;
538 
539  double effectiveSigma = scaleAdcSample(fCoherentSigma);
540  if (fDownscaleFullView) effectiveSigma /= fDriftWindow;
541 
542  CLHEP::RandGauss gauss(fRndEngine);
543  std::vector<double> amps1(fAlgView.fWireDriftData.size());
544  std::vector<double> amps2(1 + (fAlgView.fWireDriftData.size() / 32));
545  gauss.fireArray(amps1.size(), amps1.data(), 1., 0.1); // 10% wire-wire ampl. variation
546  gauss.fireArray(amps2.size(), amps2.data(), 1., 0.1); // 10% group-group ampl. variation
547 
548  double group_amp = 1.0;
549  std::vector<double> noise(fAlgView.fNCachedDrifts);
550  for (size_t w = 0; w < fAlgView.fWireDriftData.size(); ++w) {
551  if ((w & 31) == 0) {
552  group_amp = amps2[w >> 5]; // div by 32
553  gauss.fireArray(fAlgView.fNCachedDrifts, noise.data(), 0., effectiveSigma);
554  } // every 32 wires
555 
556  auto& wire = fAlgView.fWireDriftData[w];
557  for (size_t d = 0; d < wire.size(); ++d) {
558  wire[d] += group_amp * amps1[w] * noise[d];
559  }
560  }
561 }
std::vector< std::vector< float > > fWireDriftData
unsigned int noise()
Definition: chem4.cc:261
Float_t d
Definition: plot.C:235
float scaleAdcSample(float val) const
DataProviderAlgView fAlgView
CLHEP::HepJamesRandom fRndEngine
Float_t w
Definition: plot.C:20
void img::DataProviderAlg::addWhiteNoise ( )
private

Definition at line 517 of file DataProviderAlg.cxx.

References d, fAlgView, fDownscaleFullView, fDriftWindow, img::DataProviderAlgView::fNCachedDrifts, fNoiseSigma, fRndEngine, img::DataProviderAlgView::fWireDriftData, noise(), and scaleAdcSample().

Referenced by setWireDriftData().

518 {
519  if (fNoiseSigma == 0) return;
520 
521  double effectiveSigma = scaleAdcSample(fNoiseSigma);
522  if (fDownscaleFullView) effectiveSigma /= fDriftWindow;
523 
524  CLHEP::RandGauss gauss(fRndEngine);
525  std::vector<double> noise(fAlgView.fNCachedDrifts);
526  for (auto& wire : fAlgView.fWireDriftData) {
527  gauss.fireArray(fAlgView.fNCachedDrifts, noise.data(), 0., effectiveSigma);
528  for (size_t d = 0; d < wire.size(); ++d) {
529  wire[d] += noise[d];
530  }
531  }
532 }
std::vector< std::vector< float > > fWireDriftData
unsigned int noise()
Definition: chem4.cc:261
Float_t d
Definition: plot.C:235
float scaleAdcSample(float val) const
DataProviderAlgView fAlgView
CLHEP::HepJamesRandom fRndEngine
void img::DataProviderAlg::applyBlur ( )
private

Definition at line 414 of file DataProviderAlg.cxx.

References d, fAlgView, fBlurKernel, img::DataProviderAlgView::fWireDriftData, sum, and w.

Referenced by setWireDriftData().

415 {
416  if (fBlurKernel.size() < 2) return;
417 
418  size_t margin_left = (fBlurKernel.size() - 1) >> 1,
419  margin_right = fBlurKernel.size() - margin_left - 1;
420 
421  std::vector<std::vector<float>> src(fAlgView.fWireDriftData.size());
422  for (size_t w = 0; w < fAlgView.fWireDriftData.size(); ++w) {
423  src[w] = fAlgView.fWireDriftData[w];
424  }
425 
426  for (size_t w = margin_left; w < fAlgView.fWireDriftData.size() - margin_right; ++w) {
427  for (size_t d = 0; d < fAlgView.fWireDriftData[w].size(); ++d) {
428  float sum = 0;
429  for (size_t i = 0; i < fBlurKernel.size(); ++i) {
430  sum += fBlurKernel[i] * src[w + i - margin_left][d];
431  }
433  }
434  }
435 }
std::vector< std::vector< float > > fWireDriftData
std::vector< float > fBlurKernel
Float_t d
Definition: plot.C:235
DataProviderAlgView fAlgView
Double_t sum
Definition: plot.C:31
Float_t w
Definition: plot.C:20
unsigned int img::DataProviderAlg::Cryo ( ) const
inline

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

Definition at line 161 of file DataProviderAlg.h.

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

161 { return fCryo; }
std::vector<float> img::DataProviderAlg::downscale ( std::size_t  dst_size,
std::vector< float > const &  adc,
size_t  tick0 
) const
inlineprotected

Definition at line 198 of file DataProviderAlg.h.

References kMax, kMaxMean, and kMean.

Referenced by patchFromOriginalView(), and setWireData().

201  {
202  switch (fDownscaleMode) {
203  case img::DataProviderAlg::kMean: return downscaleMean(dst_size, adc, tick0);
204  case img::DataProviderAlg::kMaxMean: return downscaleMaxMean(dst_size, adc, tick0);
205  case img::DataProviderAlg::kMax: return downscaleMax(dst_size, adc, tick0);
206  }
207  throw cet::exception("img::DataProviderAlg") << "Downscale mode not supported." << std::endl;
208  }
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
protected

Definition at line 196 of file DataProviderAlg.cxx.

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

199 {
200  size_t kStop = dst_size;
201  std::vector<float> result(dst_size);
202  if (adc.size() < kStop) { kStop = adc.size(); }
203  for (size_t i = 0, k0 = 0; i < kStop; ++i, k0 += fDriftWindow) {
204  size_t k1 = k0 + fDriftWindow;
205 
206  float max_adc = adc[k0] * fAlgView.fLifetimeCorrFactors[k0 + tick0];
207  for (size_t k = k0 + 1; k < k1; ++k) {
208  float ak = adc[k] * fAlgView.fLifetimeCorrFactors[k + tick0];
209  if (ak > max_adc) max_adc = ak;
210  }
211  result[i] = max_adc;
212  }
213  scaleAdcSamples(result);
214  return result;
215 }
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
protected

Definition at line 217 of file DataProviderAlg.cxx.

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

220 {
221  size_t kStop = dst_size;
222  std::vector<float> result(dst_size);
223  if (adc.size() < kStop) { kStop = adc.size(); }
224  for (size_t i = 0, k0 = 0; i < kStop; ++i, k0 += fDriftWindow) {
225  size_t k1 = k0 + fDriftWindow;
226  size_t max_idx = k0;
227  float max_adc = adc[k0] * fAlgView.fLifetimeCorrFactors[k0 + tick0];
228  for (size_t k = k0 + 1; k < k1; ++k) {
229  float ak = adc[k] * fAlgView.fLifetimeCorrFactors[k + tick0];
230  if (ak > max_adc) {
231  max_adc = ak;
232  max_idx = k;
233  }
234  }
235 
236  size_t n = 1;
237  if (max_idx > 0) {
238  max_adc += adc[max_idx - 1] * fAlgView.fLifetimeCorrFactors[max_idx - 1 + tick0];
239  n++;
240  }
241  if (max_idx + 1 < adc.size()) {
242  max_adc += adc[max_idx + 1] * fAlgView.fLifetimeCorrFactors[max_idx + 1 + tick0];
243  n++;
244  }
245 
246  result[i] = max_adc / n;
247  }
248  scaleAdcSamples(result);
249  return result;
250 }
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
protected

Definition at line 252 of file DataProviderAlg.cxx.

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

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

Definition at line 168 of file DataProviderAlg.h.

168 { return fDriftWindow; }
size_t img::DataProviderAlg::getAdcArea ( ) const
inline

Definition at line 153 of file DataProviderAlg.h.

References r.

153 { return fAdcAreaOverThr; }
double img::DataProviderAlg::getAdcSum ( ) const
inline

Definition at line 152 of file DataProviderAlg.h.

152 { return fAdcSumOverThr; }
size_t img::DataProviderAlg::getDriftIndex ( float  drift) const
inlineprotected

Definition at line 210 of file DataProviderAlg.h.

References lar::dump::vector().

Referenced by poolMax().

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

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 122 of file DataProviderAlg.h.

126  {
127  bool ok = false;
128  std::vector<std::vector<float>> patch;
129  if (fDownscaleFullView) {
130  ok = patchFromDownsampledView(wire, drift, patchSizeW, patchSizeD, patch);
131  }
132  else {
133  ok = patchFromOriginalView(wire, drift, patchSizeW, patchSizeD, patch);
134  }
135 
136  if (ok) return patch;
137  throw cet::exception("img::DataProviderAlg") << "Patch filling failed." << std::endl;
138  }
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
inline

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 142 of file DataProviderAlg.h.

143  {
144  size_t didx = getDriftIndex(drift), widx = (size_t)wire;
145 
146  if ((widx < fAlgView.fWireDriftData.size()) && (didx < fAlgView.fNCachedDrifts)) {
147  return fAlgView.fWireDriftData[widx][didx];
148  }
149  return 0;
150  }
std::vector< std::vector< float > > fWireDriftData
DataProviderAlgView fAlgView
size_t getDriftIndex(float drift) const
double img::DataProviderAlg::LifetimeCorrection ( detinfo::DetectorClocksData const &  clock_data,
detinfo::DetectorPropertiesData const &  det_prop,
double  tick 
) const
inline

Definition at line 173 of file DataProviderAlg.h.

176  {
177  return fCalorimetryAlg.LifetimeCorrection(clock_data, det_prop, tick);
178  }
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
inline

Definition at line 167 of file DataProviderAlg.h.

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

Definition at line 166 of file DataProviderAlg.h.

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

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

Definition at line 165 of file DataProviderAlg.h.

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

165 { return fAlgView.fNWires; }
DataProviderAlgView fAlgView
bool img::DataProviderAlg::patchFromDownsampledView ( size_t  wire,
float  drift,
size_t  size_w,
size_t  size_d,
std::vector< std::vector< float >> &  patch 
) const
protected

Definition at line 439 of file DataProviderAlg.cxx.

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

444 {
445  int halfSizeW = size_w / 2;
446  int halfSizeD = size_d / 2;
447 
448  int w0 = wire - halfSizeW;
449  int w1 = wire + halfSizeW;
450 
451  size_t sd = (size_t)(drift / fDriftWindow);
452  int d0 = sd - halfSizeD;
453  int d1 = sd + halfSizeD;
454 
455  int wsize = fAlgView.fWireDriftData.size();
456  for (int w = w0, wpatch = 0; w < w1; ++w, ++wpatch) {
457  auto& dst = patch[wpatch];
458  if ((w >= 0) && (w < wsize)) {
459  auto& src = fAlgView.fWireDriftData[w];
460  int dsize = src.size();
461  for (int d = d0, dpatch = 0; d < d1; ++d, ++dpatch) {
462  if ((d >= 0) && (d < dsize)) { dst[dpatch] = src[d]; }
463  else {
464  dst[dpatch] = fAdcZero;
465  }
466  }
467  }
468  else {
469  std::fill(dst.begin(), dst.end(), fAdcZero);
470  }
471  }
472 
473  return true;
474 }
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
protected

Definition at line 476 of file DataProviderAlg.cxx.

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

481 {
482  int dsize = fDriftWindow * size_d;
483  int halfSizeW = size_w / 2;
484  int halfSizeD = dsize / 2;
485 
486  int w0 = wire - halfSizeW;
487  int w1 = wire + halfSizeW;
488 
489  int d0 = int(drift) - halfSizeD;
490  int d1 = int(drift) + halfSizeD;
491 
492  if (d0 < 0) d0 = 0;
493 
494  std::vector<float> tmp(dsize);
495  int wsize = fAlgView.fWireDriftData.size();
496  for (int w = w0, wpatch = 0; w < w1; ++w, ++wpatch) {
497  if ((w >= 0) && (w < wsize)) {
498  auto& src = fAlgView.fWireDriftData[w];
499  int src_size = src.size();
500  for (int d = d0, dpatch = 0; d < d1; ++d, ++dpatch) {
501  if ((d >= 0) && (d < src_size)) { tmp[dpatch] = src[d]; }
502  else {
503  tmp[dpatch] = fAdcZero;
504  }
505  }
506  }
507  else {
508  std::fill(tmp.begin(), tmp.end(), fAdcZero);
509  }
510  patch[wpatch] = downscale(patch[wpatch].size(), tmp, d0);
511  }
512 
513  return true;
514 }
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
inline
float img::DataProviderAlg::poolMax ( int  wire,
int  drift,
size_t  r = 0 
) const

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

Definition at line 145 of file DataProviderAlg.cxx.

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

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

146 {
147  size_t rw = r, rd = r;
148  if (!fDownscaleFullView) { rd *= fDriftWindow; }
149 
150  size_t didx = getDriftIndex(drift);
151  int d0 = didx - rd;
152  if (d0 < 0) { d0 = 0; }
153  int d1 = didx + rd;
154  if (d1 >= (int)fAlgView.fNCachedDrifts) { d1 = fAlgView.fNCachedDrifts - 1; }
155 
156  int w0 = wire - rw;
157  if (w0 < 0) { w0 = 0; }
158  int w1 = wire + rw;
159  if (w1 >= (int)fAlgView.fNWires) { w1 = fAlgView.fNWires - 1; }
160 
161  float adc, max_adc = 0;
162  for (int w = w0; w <= w1; ++w) {
163  auto const* col = fAlgView.fWireDriftData[w].data();
164  for (int d = d0; d <= d1; ++d) {
165  adc = col[d];
166  if (adc > max_adc) { max_adc = adc; }
167  }
168  }
169 
170  return max_adc;
171 }
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
img::DataProviderAlgView img::DataProviderAlg::resizeView ( detinfo::DetectorClocksData const &  clock_data,
detinfo::DetectorPropertiesData const &  det_prop,
size_t  wires,
size_t  drifts 
)
protectedvirtual

Reimplemented in nnet::TrainingDataAlg.

Definition at line 114 of file DataProviderAlg.cxx.

References fAdcZero, fCalibrateLifetime, fCalorimetryAlg, fDownscaleFullView, 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 setWireDriftData().

119 {
121  result.fNWires = wires;
122  result.fNDrifts = drifts;
123  result.fNScaledDrifts = drifts / fDriftWindow;
124  result.fNCachedDrifts = fDownscaleFullView ? result.fNScaledDrifts : drifts;
125 
126  result.fWireChannels.resize(wires, raw::InvalidChannelID);
127 
128  result.fWireDriftData.resize(wires, std::vector<float>(result.fNCachedDrifts, fAdcZero));
129 
130  result.fLifetimeCorrFactors.resize(drifts);
131  if (fCalibrateLifetime) {
132  for (size_t t = 0; t < drifts; ++t) {
133  result.fLifetimeCorrFactors[t] = fCalorimetryAlg.LifetimeCorrection(clock_data, det_prop, t);
134  }
135  }
136  else {
137  for (size_t t = 0; t < drifts; ++t) {
138  result.fLifetimeCorrFactors[t] = 1.0;
139  }
140  }
141  return result;
142 }
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
float img::DataProviderAlg::scaleAdcSample ( float  val) const
private

Definition at line 361 of file DataProviderAlg.cxx.

References fAdcMax, fAdcMin, fAdcOffset, fAdcScale, fAmplCalibConst, and fPlane.

Referenced by addCoherentNoise(), addWhiteNoise(), and scaleAdcSamples().

362 {
363  val *= fAmplCalibConst[fPlane]; // prescale by plane-to-plane calibration factors
364 
365  if (val < fAdcMin) { val = fAdcMin; } // saturate
366  else if (val > fAdcMax) {
367  val = fAdcMax;
368  }
369 
370  return fAdcOffset +
371  fAdcScale *
372  (val - fAdcMin); // shift and scale to the output range, shift to the output min
373 }
std::vector< float > fAmplCalibConst
void img::DataProviderAlg::scaleAdcSamples ( std::vector< float > &  values) const
private

Definition at line 375 of file DataProviderAlg.cxx.

References fAdcMax, fAdcMin, fAdcOffset, fAdcScale, fAmplCalibConst, fPlane, scaleAdcSample(), and util::size().

Referenced by downscaleMax(), downscaleMaxMean(), and downscaleMean().

376 {
377  float calib = fAmplCalibConst[fPlane];
378  auto* data = values.data();
379 
380  size_t k = 0, size4 = values.size() >> 2, size = values.size();
381  for (size_t i = 0; i < size4; ++i) // vectorize if you can
382  {
383  data[k] *= calib; // prescale by plane-to-plane calibration factors
384  data[k + 1] *= calib;
385  data[k + 2] *= calib;
386  data[k + 3] *= calib;
387 
388  if (data[k] < fAdcMin) { data[k] = fAdcMin; } // saturate min
389  if (data[k + 1] < fAdcMin) { data[k + 1] = fAdcMin; }
390  if (data[k + 2] < fAdcMin) { data[k + 2] = fAdcMin; }
391  if (data[k + 3] < fAdcMin) { data[k + 3] = fAdcMin; }
392 
393  if (data[k] > fAdcMax) { data[k] = fAdcMax; } // saturate max
394  if (data[k + 1] > fAdcMax) { data[k + 1] = fAdcMax; }
395  if (data[k + 2] > fAdcMax) { data[k + 2] = fAdcMax; }
396  if (data[k + 3] > fAdcMax) { data[k + 3] = fAdcMax; }
397 
398  data[k] = fAdcOffset +
399  fAdcScale *
400  (data[k] - fAdcMin); // shift and scale to the output range, shift to the output min
401  data[k + 1] = fAdcOffset + fAdcScale * (data[k + 1] - fAdcMin);
402  data[k + 2] = fAdcOffset + fAdcScale * (data[k + 2] - fAdcMin);
403  data[k + 3] = fAdcOffset + fAdcScale * (data[k + 3] - fAdcMin);
404 
405  k += 4;
406  }
407  while (k < size) {
408  data[k] = scaleAdcSample(data[k]);
409  ++k;
410  } // do the tail
411 }
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
Definition: OpHitAlg.h:19
float scaleAdcSample(float val) const
std::vector< float > fAmplCalibConst
std::optional< std::vector< float > > img::DataProviderAlg::setWireData ( std::vector< float > const &  adc,
size_t  wireIdx 
) const
protected

Definition at line 273 of file DataProviderAlg.cxx.

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

Referenced by setWireDriftData().

275 {
276  if (wireIdx >= fAlgView.fWireDriftData.size()) { return std::nullopt; }
277  auto& wData = fAlgView.fWireDriftData[wireIdx];
278 
279  if (fDownscaleFullView) {
280  if (!adc.empty()) { return downscale(wData.size(), adc, 0); }
281  return std::nullopt;
282  }
283  if (adc.empty()) { return std::nullopt; }
284  if (adc.size() <= wData.size()) { return adc; }
285  return std::vector<float>(adc.begin(), adc.begin() + wData.size());
286 }
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 
)

Definition at line 289 of file DataProviderAlg.cxx.

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

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

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

Definition at line 162 of file DataProviderAlg.h.

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

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

Definition at line 117 of file DataProviderAlg.h.

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

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

Level of zero ADC after scaling.

Definition at line 171 of file DataProviderAlg.h.

171 { return fAdcZero; }

Member Data Documentation

size_t img::DataProviderAlg::fAdcAreaOverThr
private

Definition at line 251 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), and setWireDriftData().

float img::DataProviderAlg::fAdcMax
private

Definition at line 249 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), scaleAdcSample(), and scaleAdcSamples().

float img::DataProviderAlg::fAdcMin
private

Definition at line 249 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), scaleAdcSample(), and scaleAdcSamples().

float img::DataProviderAlg::fAdcOffset
private

Definition at line 249 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), scaleAdcSample(), and scaleAdcSamples().

float img::DataProviderAlg::fAdcScale
private

Definition at line 249 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), scaleAdcSample(), and scaleAdcSamples().

double img::DataProviderAlg::fAdcSumOverThr
private

Definition at line 250 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), and setWireDriftData().

double img::DataProviderAlg::fAdcSumThr
private

Definition at line 250 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), and setWireDriftData().

float img::DataProviderAlg::fAdcZero
private
std::vector<float> img::DataProviderAlg::fAmplCalibConst
private

Definition at line 246 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), scaleAdcSample(), and scaleAdcSamples().

std::vector<float> img::DataProviderAlg::fBlurKernel
private

Definition at line 256 of file DataProviderAlg.h.

Referenced by applyBlur(), and DataProviderAlg().

bool img::DataProviderAlg::fCalibrateAmpl
private

Definition at line 247 of file DataProviderAlg.h.

Referenced by DataProviderAlg().

bool img::DataProviderAlg::fCalibrateLifetime
private

Definition at line 247 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), and resizeView().

calo::CalorimetryAlg img::DataProviderAlg::fCalorimetryAlg
protected

Definition at line 238 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), and resizeView().

float img::DataProviderAlg::fCoherentSigma
private

Definition at line 262 of file DataProviderAlg.h.

Referenced by addCoherentNoise(), and DataProviderAlg().

unsigned int img::DataProviderAlg::fCryo = 9999
private

Definition at line 248 of file DataProviderAlg.h.

Referenced by setWireDriftData().

EDownscaleMode img::DataProviderAlg::fDownscaleMode
protected

Definition at line 182 of file DataProviderAlg.h.

Referenced by DataProviderAlg().

float img::DataProviderAlg::fDriftWindowInv
protected

Definition at line 187 of file DataProviderAlg.h.

Referenced by DataProviderAlg(), and downscaleMean().

geo::GeometryCore const* img::DataProviderAlg::fGeometry
protected
float img::DataProviderAlg::fNoiseSigma
private

Definition at line 259 of file DataProviderAlg.h.

Referenced by addWhiteNoise(), and DataProviderAlg().

unsigned int img::DataProviderAlg::fPlane = 9999
private

Definition at line 248 of file DataProviderAlg.h.

Referenced by scaleAdcSample(), scaleAdcSamples(), and setWireDriftData().

CLHEP::HepJamesRandom img::DataProviderAlg::fRndEngine
private

Definition at line 253 of file DataProviderAlg.h.

Referenced by addCoherentNoise(), and addWhiteNoise().

unsigned int img::DataProviderAlg::fTPC = 9999
private

Definition at line 248 of file DataProviderAlg.h.

Referenced by setWireDriftData().


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