All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 "cetlib_except/exception.h"
19 #include "fhiclcpp/types/Atom.h"
20 #include "fhiclcpp/types/Comment.h"
21 #include "fhiclcpp/types/Name.h"
22 #include "fhiclcpp/types/Sequence.h"
23 #include "fhiclcpp/types/Table.h"
24 
25 // LArSoft includes
26 namespace geo { class GeometryCore; }
29 
30 #include "CLHEP/Random/JamesRandom.h" // for testing on noise, not used by any reco
31 
32 // ROOT & C++
33 #include <iostream>
34 #include <memory>
35 #include <optional>
36 #include <vector>
37 
38 namespace detinfo {
39  class DetectorClocksData;
40  class DetectorPropertiesData;
41 }
42 
43 namespace img {
44  class DataProviderAlg;
46  unsigned int fNWires;
47  unsigned int fNDrifts;
48  unsigned int fNScaledDrifts;
49  unsigned int fNCachedDrifts;
50  std::vector<raw::ChannelID_t> fWireChannels;
51  std::vector<std::vector<float>> fWireDriftData;
52  std::vector<float> fLifetimeCorrFactors;
53  };
54 }
55 
56 /// Base class providing data for training / running image based classifiers. It can be used
57 /// also for any other algorithms where 2D projection image is useful. Currently the image
58 /// is 32-bit fp / pixel, as sson as have time will template it so e.g. byte pixels would
59 /// be possible.
61 public:
62  enum EDownscaleMode { kMax = 1, kMaxMean = 2, kMean = 3 };
63 
64  struct Config {
65  using Name = fhicl::Name;
66  using Comment = fhicl::Comment;
67 
68  fhicl::Table<calo::CalorimetryAlg::Config> CalorimetryAlg{
69  Name("CalorimetryAlg"),
70  Comment("Used to eliminate amplitude variation due to electron lifetime.")};
71 
72  fhicl::Atom<float> AdcMax{Name("AdcMax"), Comment("Saturation max value")};
73  fhicl::Atom<float> AdcMin{Name("AdcMin"), Comment("Saturation min value")};
74  fhicl::Atom<float> OutMax{Name("OutMax"), Comment("Output max value")};
75  fhicl::Atom<float> OutMin{Name("OutMin"), Comment("Output min value")};
76 
77  fhicl::Atom<bool> CalibrateAmpl{Name("CalibrateAmpl"),
78  Comment("Calibrate ADC values with CalAmpConstants")};
79 
80  fhicl::Atom<bool> CalibrateLifetime{Name("CalibrateLifetime"),
81  Comment("Calibrate ADC values with the electron lifetime")};
82 
83  fhicl::Atom<unsigned int> DriftWindow{Name("DriftWindow"),
84  Comment("Downsampling window (in drift ticks).")};
85 
86  fhicl::Atom<std::string> DownscaleFn{Name("DownscaleFn"), Comment("Downsampling function")};
87 
88  fhicl::Atom<bool> DownscaleFullView{
89  Name("DownscaleFullView"),
90  Comment("Downsample full view (faster / lower location precision)")};
91 
92  fhicl::Sequence<float> BlurKernel{Name("BlurKernel"), Comment("Blur kernel in wire direction")};
93 
94  fhicl::Atom<float> NoiseSigma{Name("NoiseSigma"), Comment("White noise sigma")};
95 
96  fhicl::Atom<float> CoherentSigma{Name("CoherentSigma"), Comment("Coherent noise sigma")};
97  };
98 
99  DataProviderAlg(const fhicl::ParameterSet& pset)
100  : DataProviderAlg(fhicl::Table<Config>(pset, {})())
101  {}
102 
103  DataProviderAlg(const Config& config);
104 
105  virtual ~DataProviderAlg();
106 
107 
108  bool setWireDriftData(const detinfo::DetectorClocksData& clock_data,
109  const detinfo::DetectorPropertiesData& det_prop,
110  const std::vector<recob::Wire>&
111  wires, // once per plane: setup ADC buffer, collect & downscale ADC's
112  unsigned int plane,
113  unsigned int tpc,
114  unsigned int cryo);
115 
116  std::vector<float> const&
117  wireData(size_t widx) const
118  {
119  return fAlgView.fWireDriftData[widx];
120  }
121 
122  /// Return patch of data centered on the wire and drift, witht the size in (downscaled) pixels givent
123  /// with patchSizeW and patchSizeD. Pad with the zero-level calue if patch extends beyond the event
124  /// projection.
125  std::vector<std::vector<float>>
126  getPatch(size_t wire, float drift, size_t patchSizeW, size_t patchSizeD) const
127  {
128  bool ok = false;
129  std::vector<std::vector<float>> patch;
130  if (fDownscaleFullView) {
131  ok = patchFromDownsampledView(wire, drift, patchSizeW, patchSizeD, patch);
132  }
133  else {
134  ok = patchFromOriginalView(wire, drift, patchSizeW, patchSizeD, patch);
135  }
136 
137  if (ok)
138  return patch;
139  throw cet::exception("img::DataProviderAlg") << "Patch filling failed." << std::endl;
140  }
141 
142  /// Return value from the ADC buffer, or zero if coordinates are out of the view;
143  /// will scale the drift according to the downscale settings.
144  float
145  getPixelOrZero(int wire, int drift) const
146  {
147  size_t didx = getDriftIndex(drift), widx = (size_t)wire;
148 
149  if ((widx < fAlgView.fWireDriftData.size()) && (didx < fAlgView.fNCachedDrifts)) {
150  return fAlgView.fWireDriftData[widx][didx];
151  }
152  return 0;
153  }
154 
155  double
156  getAdcSum() const
157  {
158  return fAdcSumOverThr;
159  }
160  size_t
161  getAdcArea() const
162  {
163  return fAdcAreaOverThr;
164  }
165 
166  /// Pool max value in a patch around the wire/drift pixel.
167  float poolMax(int wire, int drift, size_t r = 0) const;
168 
169  /// Pool sum of pixels in a patch around the wire/drift pixel.
170  //float poolSum(int wire, int drift, size_t r = 0) const;
171 
172  unsigned int
173  Cryo() const
174  {
175  return fCryo;
176  }
177  unsigned int
178  TPC() const
179  {
180  return fTPC;
181  }
182  unsigned int
183  Plane() const
184  {
185  return fPlane;
186  }
187 
188  unsigned int
189  NWires() const
190  {
191  return fAlgView.fNWires;
192  }
193  unsigned int
195  {
196  return fAlgView.fNScaledDrifts;
197  }
198  unsigned int
200  {
201  return fAlgView.fNCachedDrifts;
202  }
203  unsigned int
204  DriftWindow() const
205  {
206  return fDriftWindow;
207  }
208 
209  /// Level of zero ADC after scaling.
210  float
211  ZeroLevel() const
212  {
213  return fAdcZero;
214  }
215 
216  double
218  detinfo::DetectorPropertiesData const& det_prop,
219  double tick) const
220  {
221  return fCalorimetryAlg.LifetimeCorrection(clock_data, det_prop, tick);
222  }
223 
224 protected:
227  //std::function<void (std::vector<float> &, std::vector<float> const &, size_t)> fnDownscale;
228 
229  size_t fDriftWindow;
232 
233  std::vector<float> downscaleMax(std::size_t dst_size,
234  std::vector<float> const& adc,
235  size_t tick0) const;
236  std::vector<float> downscaleMaxMean(std::size_t dst_size,
237  std::vector<float> const& adc,
238  size_t tick0) const;
239  std::vector<float> downscaleMean(std::size_t dst_size,
240  std::vector<float> const& adc,
241  size_t tick0) const;
242  std::vector<float>
243  downscale(std::size_t dst_size, std::vector<float> const& adc, size_t tick0) const
244  {
245  switch (fDownscaleMode) {
246  case img::DataProviderAlg::kMean: return downscaleMean(dst_size, adc, tick0);
247  case img::DataProviderAlg::kMaxMean: return downscaleMaxMean(dst_size, adc, tick0);
248  case img::DataProviderAlg::kMax: return downscaleMax(dst_size, adc, tick0);
249  }
250  throw cet::exception("img::DataProviderAlg") << "Downscale mode not supported." << std::endl;
251  }
252 
253  size_t
254  getDriftIndex(float drift) const
255  {
256  if (fDownscaleFullView)
257  return (size_t)(drift * fDriftWindowInv);
258  else
259  return (size_t)drift;
260  }
261 
262  std::optional<std::vector<float>> setWireData(std::vector<float> const& adc,
263  size_t wireIdx) const;
264 
265  bool patchFromDownsampledView(size_t wire,
266  float drift,
267  size_t size_w,
268  size_t size_d,
269  std::vector<std::vector<float>>& patch) const;
270  bool patchFromOriginalView(size_t wire,
271  float drift,
272  size_t size_w,
273  size_t size_d,
274  std::vector<std::vector<float>>& patch) const;
275 
277  detinfo::DetectorPropertiesData const& det_prop,
278  size_t wires,
279  size_t drifts);
280 
281  // Calorimetry needed to equalize ADC amplitude along drift:
283 
284  // Geometry and detector properties:
286 
287 private:
288  float scaleAdcSample(float val) const;
289  void scaleAdcSamples(std::vector<float>& values) const;
290  std::vector<float> fAmplCalibConst;
292  unsigned int fCryo = 9999, fTPC = 9999, fPlane = 9999;
296 
297  CLHEP::HepJamesRandom fRndEngine;
298 
299  void applyBlur();
300  std::vector<float> fBlurKernel; // blur not applied if empty
301 
302  void addWhiteNoise();
303  float fNoiseSigma; // noise not added if sigma=0
304 
305  void addCoherentNoise();
306  float fCoherentSigma; // noise not added if sigma=0
307 };
308 // ------------------------------------------------------
309 // ------------------------------------------------------
310 // ------------------------------------------------------
311 
312 #endif
geo::GeometryCore const * fGeometry
fhicl::Atom< float > AdcMax
std::optional< std::vector< float > > setWireData(std::vector< float > const &adc, size_t wireIdx) const
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
fhicl::Atom< float > CoherentSigma
virtual ~DataProviderAlg()
virtual DataProviderAlgView resizeView(detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, size_t wires, size_t drifts)
fhicl::Atom< unsigned int > DriftWindow
unsigned int DriftWindow() const
std::vector< float > downscale(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
DataProviderAlg(const fhicl::ParameterSet &pset)
std::vector< float > downscaleMaxMean(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
unsigned int Plane() const
std::vector< float > fBlurKernel
unsigned int NWires() const
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
fhicl::Sequence< float > BlurKernel
std::vector< float > downscaleMax(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
fhicl::Atom< bool > DownscaleFullView
std::vector< float > downscaleMean(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
void scaleAdcSamples(std::vector< float > &values) const
std::vector< raw::ChannelID_t > fWireChannels
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:75
BEGIN_PROLOG vertical distance to the surface Name
double getAdcSum() const
float scaleAdcSample(float val) const
Description of geometry of one entire detector.
fhicl::Atom< float > AdcMin
DataProviderAlgView fAlgView
calo::CalorimetryAlg fCalorimetryAlg
bool patchFromOriginalView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float >> &patch) const
fhicl::Atom< bool > CalibrateLifetime
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)
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
fhicl::Table< calo::CalorimetryAlg::Config > CalorimetryAlg
std::vector< float > fAmplCalibConst
unsigned int NScaledDrifts() const
fhicl::Atom< std::string > DownscaleFn
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.
bool patchFromDownsampledView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float >> &patch) const
fhicl::Atom< bool > CalibrateAmpl
EDownscaleMode fDownscaleMode
fhicl::Atom< float > OutMin
fhicl::Atom< float > OutMax
CLHEP::HepJamesRandom fRndEngine
float poolMax(int wire, int drift, size_t r=0) const
Pool max value in a patch around the wire/drift pixel.
double LifetimeCorrection(detinfo::DetectorClocksData const &clock_data, detinfo::DetectorPropertiesData const &det_prop, double time, double T0=0) const
size_t getAdcArea() const
esac echo uname r
size_t getDriftIndex(float drift) const
fhicl::Atom< float > NoiseSigma
std::vector< float > const & wireData(size_t widx) const
unsigned int TPC() const