DataProviderAlg.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Class: PointIdAlg
3 // Authors: D.Stefan (Dorota.Stefan@ncbj.gov.pl), from DUNE, CERN/NCBJ, since May 2016
4 // R.Sulej (Robert.Sulej@cern.ch), from DUNE, FNAL/NCBJ, since May 2016
5 // P.Plonski, from DUNE, WUT, since May 2016
6 //
7 //
8 // Algorithm for making 2D image-like data from recob::Wire's. Used by CNN codes for training data
9 // preparation and application of trained models to new data. Also used by PMA to keep images of
10 // 2D projections used for the track validation.
11 //
12 ////////////////////////////////////////////////////////////////////////////////////////////////////
13 
14 #ifndef DataProviderAlg_h
15 #define DataProviderAlg_h
16 
17 // Framework includes
18 #include "fhiclcpp/types/Atom.h"
20 #include "fhiclcpp/types/Table.h"
21 
22 // LArSoft includes
26 
27 #include "CLHEP/Random/JamesRandom.h" // for testing on noise, not used by any reco
28 
29 // ROOT & C++
30 #include <memory>
31 
32 namespace detinfo {
33  class DetectorClocksData;
34  class DetectorPropertiesData;
35 }
36 
37 namespace img {
38  class DataProviderAlg;
40  unsigned int fNWires;
41  unsigned int fNDrifts;
42  unsigned int fNScaledDrifts;
43  unsigned int fNCachedDrifts;
44  std::vector<raw::ChannelID_t> fWireChannels;
45  std::vector<std::vector<float>> fWireDriftData;
46  std::vector<float> fLifetimeCorrFactors;
47  };
48 }
49 
50 /// Base class providing data for training / running image based classifiers. It can be used
51 /// also for any other algorithms where 2D projection image is useful. Currently the image
52 /// is 32-bit fp / pixel, as sson as have time will template it so e.g. byte pixels would
53 /// be possible.
55 public:
56  enum EDownscaleMode { kMax = 1, kMaxMean = 2, kMean = 3 };
57 
58  struct Config {
59  using Name = fhicl::Name;
61 
63  Name("CalorimetryAlg"),
64  Comment("Used to eliminate amplitude variation due to electron lifetime.")};
65 
66  fhicl::Atom<float> AdcMax{Name("AdcMax"), Comment("Saturation max value")};
67  fhicl::Atom<float> AdcMin{Name("AdcMin"), Comment("Saturation min value")};
68  fhicl::Atom<float> OutMax{Name("OutMax"), Comment("Output max value")};
69  fhicl::Atom<float> OutMin{Name("OutMin"), Comment("Output min value")};
70 
71  fhicl::Atom<bool> CalibrateAmpl{Name("CalibrateAmpl"),
72  Comment("Calibrate ADC values with CalAmpConstants")};
73 
74  fhicl::Atom<bool> CalibrateLifetime{Name("CalibrateLifetime"),
75  Comment("Calibrate ADC values with the electron lifetime")};
76 
77  fhicl::Atom<unsigned int> DriftWindow{Name("DriftWindow"),
78  Comment("Downsampling window (in drift ticks).")};
79 
80  fhicl::Atom<std::string> DownscaleFn{Name("DownscaleFn"), Comment("Downsampling function")};
81 
82  fhicl::Atom<bool> DownscaleFullView{
83  Name("DownscaleFullView"),
84  Comment("Downsample full view (faster / lower location precision)")};
85 
86  fhicl::Sequence<float> BlurKernel{Name("BlurKernel"), Comment("Blur kernel in wire direction")};
87 
88  fhicl::Atom<float> NoiseSigma{Name("NoiseSigma"), Comment("White noise sigma")};
89 
90  fhicl::Atom<float> CoherentSigma{Name("CoherentSigma"), Comment("Coherent noise sigma")};
91  };
92 
94  : DataProviderAlg(fhicl::Table<Config>(pset, {})())
95  {}
96 
98 
99  virtual ~DataProviderAlg();
100 
101 
102  bool setWireDriftData(const detinfo::DetectorClocksData& clock_data,
103  const detinfo::DetectorPropertiesData& det_prop,
104  const std::vector<recob::Wire>&
105  wires, // once per plane: setup ADC buffer, collect & downscale ADC's
106  unsigned int plane,
107  unsigned int tpc,
108  unsigned int cryo);
109 
110  std::vector<float> const&
111  wireData(size_t widx) const
112  {
113  return fAlgView.fWireDriftData[widx];
114  }
115 
116  /// Return patch of data centered on the wire and drift, witht the size in (downscaled) pixels givent
117  /// with patchSizeW and patchSizeD. Pad with the zero-level calue if patch extends beyond the event
118  /// projection.
119  std::vector<std::vector<float>>
120  getPatch(size_t wire, float drift, size_t patchSizeW, size_t patchSizeD) const
121  {
122  bool ok = false;
123  std::vector<std::vector<float>> patch;
124  if (fDownscaleFullView) {
125  ok = patchFromDownsampledView(wire, drift, patchSizeW, patchSizeD, patch);
126  }
127  else {
128  ok = patchFromOriginalView(wire, drift, patchSizeW, patchSizeD, patch);
129  }
130 
131  if (ok)
132  return patch;
133  throw cet::exception("img::DataProviderAlg") << "Patch filling failed." << std::endl;
134  }
135 
136  /// Return value from the ADC buffer, or zero if coordinates are out of the view;
137  /// will scale the drift according to the downscale settings.
138  float
139  getPixelOrZero(int wire, int drift) const
140  {
141  size_t didx = getDriftIndex(drift), widx = (size_t)wire;
142 
143  if ((widx < fAlgView.fWireDriftData.size()) && (didx < fAlgView.fNCachedDrifts)) {
144  return fAlgView.fWireDriftData[widx][didx];
145  }
146  return 0;
147  }
148 
149  double
150  getAdcSum() const
151  {
152  return fAdcSumOverThr;
153  }
154  size_t
155  getAdcArea() const
156  {
157  return fAdcAreaOverThr;
158  }
159 
160  /// Pool max value in a patch around the wire/drift pixel.
161  float poolMax(int wire, int drift, size_t r = 0) const;
162 
163  /// Pool sum of pixels in a patch around the wire/drift pixel.
164  //float poolSum(int wire, int drift, size_t r = 0) const;
165 
166  unsigned int
167  Cryo() const
168  {
169  return fCryo;
170  }
171  unsigned int
172  TPC() const
173  {
174  return fTPC;
175  }
176  unsigned int
177  Plane() const
178  {
179  return fPlane;
180  }
181 
182  unsigned int
183  NWires() const
184  {
185  return fAlgView.fNWires;
186  }
187  unsigned int
189  {
190  return fAlgView.fNScaledDrifts;
191  }
192  unsigned int
194  {
195  return fAlgView.fNCachedDrifts;
196  }
197  unsigned int
198  DriftWindow() const
199  {
200  return fDriftWindow;
201  }
202 
203  /// Level of zero ADC after scaling.
204  float
205  ZeroLevel() const
206  {
207  return fAdcZero;
208  }
209 
210  double
212  detinfo::DetectorPropertiesData const& det_prop,
213  double tick) const
214  {
215  return fCalorimetryAlg.LifetimeCorrection(clock_data, det_prop, tick);
216  }
217 
218 protected:
221  //std::function<void (std::vector<float> &, std::vector<float> const &, size_t)> fnDownscale;
222 
223  size_t fDriftWindow;
226 
227  std::vector<float> downscaleMax(std::size_t dst_size,
228  std::vector<float> const& adc,
229  size_t tick0) const;
230  std::vector<float> downscaleMaxMean(std::size_t dst_size,
231  std::vector<float> const& adc,
232  size_t tick0) const;
233  std::vector<float> downscaleMean(std::size_t dst_size,
234  std::vector<float> const& adc,
235  size_t tick0) const;
236  std::vector<float>
237  downscale(std::size_t dst_size, std::vector<float> const& adc, size_t tick0) const
238  {
239  switch (fDownscaleMode) {
240  case img::DataProviderAlg::kMean: return downscaleMean(dst_size, adc, tick0);
241  case img::DataProviderAlg::kMaxMean: return downscaleMaxMean(dst_size, adc, tick0);
242  case img::DataProviderAlg::kMax: return downscaleMax(dst_size, adc, tick0);
243  }
244  throw cet::exception("img::DataProviderAlg") << "Downscale mode not supported." << std::endl;
245  }
246 
247  size_t
248  getDriftIndex(float drift) const
249  {
250  if (fDownscaleFullView)
251  return (size_t)(drift * fDriftWindowInv);
252  else
253  return (size_t)drift;
254  }
255 
256  std::optional<std::vector<float>> setWireData(std::vector<float> const& adc,
257  size_t wireIdx) const;
258 
259  bool patchFromDownsampledView(size_t wire,
260  float drift,
261  size_t size_w,
262  size_t size_d,
263  std::vector<std::vector<float>>& patch) const;
264  bool patchFromOriginalView(size_t wire,
265  float drift,
266  size_t size_w,
267  size_t size_d,
268  std::vector<std::vector<float>>& patch) const;
269 
270  virtual DataProviderAlgView resizeView(detinfo::DetectorClocksData const& clock_data,
271  detinfo::DetectorPropertiesData const& det_prop,
272  size_t wires,
273  size_t drifts);
274 
275  // Calorimetry needed to equalize ADC amplitude along drift:
277 
278  // Geometry and detector properties:
280 
281 private:
282  float scaleAdcSample(float val) const;
283  void scaleAdcSamples(std::vector<float>& values) const;
284  std::vector<float> fAmplCalibConst;
285  bool fCalibrateAmpl, fCalibrateLifetime;
286  unsigned int fCryo = 9999, fTPC = 9999, fPlane = 9999;
287  float fAdcMax, fAdcMin, fAdcScale, fAdcOffset, fAdcZero;
288  double fAdcSumOverThr, fAdcSumThr;
290 
291  CLHEP::HepJamesRandom fRndEngine;
292 
293  void applyBlur();
294  std::vector<float> fBlurKernel; // blur not applied if empty
295 
296  void addWhiteNoise();
297  float fNoiseSigma; // noise not added if sigma=0
298 
299  void addCoherentNoise();
300  float fCoherentSigma; // noise not added if sigma=0
301 };
302 // ------------------------------------------------------
303 // ------------------------------------------------------
304 // ------------------------------------------------------
305 
306 #endif
geo::GeometryCore const * fGeometry
unsigned int NCachedDrifts() const
std::vector< std::vector< float > > fWireDriftData
double LifetimeCorrection(detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, double tick) const
unsigned int DriftWindow() const
struct vector vector
ChannelGroupService::Name Name
std::vector< float > downscale(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
int16_t adc
Definition: CRTFragment.hh:202
const int kMax
DataProviderAlg(const fhicl::ParameterSet &pset)
unsigned int Plane() const
def addWhiteNoise(a, sigma)
Definition: utils.py:100
def applyBlur(a, kernel)
Definition: utils.py:84
std::vector< float > fBlurKernel
unsigned int NWires() const
static Config * config
Definition: config.cpp:1054
std::vector< raw::ChannelID_t > fWireChannels
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
double getAdcSum() const
Q_UINT16 values[128]
General LArSoft Utilities.
Description of geometry of one entire detector.
DataProviderAlgView fAlgView
def addCoherentNoise(a, sigma)
Definition: utils.py:109
calo::CalorimetryAlg fCalorimetryAlg
#define Comment
Contains all timing reference information for the detector.
std::vector< float > fLifetimeCorrFactors
unsigned int Cryo() const
Pool sum of pixels in a patch around the wire/drift pixel.
float getPixelOrZero(int wire, int drift) const
std::vector< float > fAmplCalibConst
unsigned int NScaledDrifts() const
Access the description of detector geometry.
Declaration of basic channel signal object.
std::vector< std::vector< float > > getPatch(size_t wire, float drift, size_t patchSizeW, size_t patchSizeD) const
float ZeroLevel() const
Level of zero ADC after scaling.
EDownscaleMode fDownscaleMode
CLHEP::HepJamesRandom fRndEngine
size_t getAdcArea() const
size_t getDriftIndex(float drift) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
std::vector< float > const & wireData(size_t widx) const
unsigned int TPC() const