LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DataProviderAlg.h
Go to the documentation of this file.
1 // 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 //
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"
23 #include "fhiclcpp/types/Table.h"
24 
25 // LArSoft includes
26 namespace geo {
27  class GeometryCore;
28 }
31 
32 #include "CLHEP/Random/JamesRandom.h" // for testing on noise, not used by any reco
33 
34 // ROOT & C++
35 #include <iostream>
36 #include <memory>
37 #include <optional>
38 #include <vector>
39 
40 namespace detinfo {
41  class DetectorClocksData;
42  class DetectorPropertiesData;
43 }
44 
45 namespace img {
46  class DataProviderAlg;
48  unsigned int fNWires;
49  unsigned int fNDrifts;
50  unsigned int fNScaledDrifts;
51  unsigned int fNCachedDrifts;
52  std::vector<raw::ChannelID_t> fWireChannels;
53  std::vector<std::vector<float>> fWireDriftData;
54  std::vector<float> fLifetimeCorrFactors;
55  };
56 }
57 
63 public:
64  enum EDownscaleMode { kMax = 1, kMaxMean = 2, kMean = 3 };
65 
66  struct Config {
67  using Name = fhicl::Name;
69 
71  Name("CalorimetryAlg"),
72  Comment("Used to eliminate amplitude variation due to electron lifetime.")};
73 
74  fhicl::Atom<float> AdcMax{Name("AdcMax"), Comment("Saturation max value")};
75  fhicl::Atom<float> AdcMin{Name("AdcMin"), Comment("Saturation min value")};
76  fhicl::Atom<float> OutMax{Name("OutMax"), Comment("Output max value")};
77  fhicl::Atom<float> OutMin{Name("OutMin"), Comment("Output min value")};
78 
79  fhicl::Atom<bool> CalibrateAmpl{Name("CalibrateAmpl"),
80  Comment("Calibrate ADC values with CalAmpConstants")};
81 
82  fhicl::Atom<bool> CalibrateLifetime{Name("CalibrateLifetime"),
83  Comment("Calibrate ADC values with the electron lifetime")};
84 
85  fhicl::Atom<unsigned int> DriftWindow{Name("DriftWindow"),
86  Comment("Downsampling window (in drift ticks).")};
87 
88  fhicl::Atom<std::string> DownscaleFn{Name("DownscaleFn"), Comment("Downsampling function")};
89 
90  fhicl::Atom<bool> DownscaleFullView{
91  Name("DownscaleFullView"),
92  Comment("Downsample full view (faster / lower location precision)")};
93 
94  fhicl::Sequence<float> BlurKernel{Name("BlurKernel"), Comment("Blur kernel in wire direction")};
95 
96  fhicl::Atom<float> NoiseSigma{Name("NoiseSigma"), Comment("White noise sigma")};
97 
98  fhicl::Atom<float> CoherentSigma{Name("CoherentSigma"), Comment("Coherent noise sigma")};
99  };
100 
102  : DataProviderAlg(fhicl::Table<Config>(pset, {})())
103  {}
104 
105  DataProviderAlg(const Config& config);
106 
107  virtual ~DataProviderAlg();
108 
109  bool setWireDriftData(const detinfo::DetectorClocksData& clock_data,
110  const detinfo::DetectorPropertiesData& det_prop,
111  const std::vector<recob::Wire>&
112  wires, // once per plane: setup ADC buffer, collect & downscale ADC's
113  unsigned int plane,
114  unsigned int tpc,
115  unsigned int cryo);
116 
117  std::vector<float> const& wireData(size_t widx) const { return fAlgView.fWireDriftData[widx]; }
118 
122  std::vector<std::vector<float>> getPatch(size_t wire,
123  float drift,
124  size_t patchSizeW,
125  size_t patchSizeD) const
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  }
139 
142  float getPixelOrZero(int wire, int drift) const
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  }
151 
152  double getAdcSum() const { return fAdcSumOverThr; }
153  size_t getAdcArea() const { return fAdcAreaOverThr; }
154 
156  float poolMax(int wire, int drift, size_t r = 0) const;
157 
159  //float poolSum(int wire, int drift, size_t r = 0) const;
160 
161  unsigned int Cryo() const { return fCryo; }
162  unsigned int TPC() const { return fTPC; }
163  unsigned int Plane() const { return fPlane; }
164 
165  unsigned int NWires() const { return fAlgView.fNWires; }
166  unsigned int NScaledDrifts() const { return fAlgView.fNScaledDrifts; }
167  unsigned int NCachedDrifts() const { return fAlgView.fNCachedDrifts; }
168  unsigned int DriftWindow() const { return fDriftWindow; }
169 
171  float ZeroLevel() const { return fAdcZero; }
172 
174  detinfo::DetectorPropertiesData const& det_prop,
175  double tick) const
176  {
177  return fCalorimetryAlg.LifetimeCorrection(clock_data, det_prop, tick);
178  }
179 
180 protected:
183  //std::function<void (std::vector<float> &, std::vector<float> const &, size_t)> fnDownscale;
184 
185  size_t fDriftWindow;
188 
189  std::vector<float> downscaleMax(std::size_t dst_size,
190  std::vector<float> const& adc,
191  size_t tick0) const;
192  std::vector<float> downscaleMaxMean(std::size_t dst_size,
193  std::vector<float> const& adc,
194  size_t tick0) const;
195  std::vector<float> downscaleMean(std::size_t dst_size,
196  std::vector<float> const& adc,
197  size_t tick0) const;
198  std::vector<float> downscale(std::size_t dst_size,
199  std::vector<float> const& adc,
200  size_t tick0) const
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  }
209 
210  size_t getDriftIndex(float drift) const
211  {
212  if (fDownscaleFullView)
213  return (size_t)(drift * fDriftWindowInv);
214  else
215  return (size_t)drift;
216  }
217 
218  std::optional<std::vector<float>> setWireData(std::vector<float> const& adc,
219  size_t wireIdx) const;
220 
221  bool patchFromDownsampledView(size_t wire,
222  float drift,
223  size_t size_w,
224  size_t size_d,
225  std::vector<std::vector<float>>& patch) const;
226  bool patchFromOriginalView(size_t wire,
227  float drift,
228  size_t size_w,
229  size_t size_d,
230  std::vector<std::vector<float>>& patch) const;
231 
232  virtual DataProviderAlgView resizeView(detinfo::DetectorClocksData const& clock_data,
233  detinfo::DetectorPropertiesData const& det_prop,
234  size_t wires,
235  size_t drifts);
236 
237  // Calorimetry needed to equalize ADC amplitude along drift:
239 
240  // Geometry and detector properties:
242 
243 private:
244  float scaleAdcSample(float val) const;
245  void scaleAdcSamples(std::vector<float>& values) const;
246  std::vector<float> fAmplCalibConst;
247  bool fCalibrateAmpl, fCalibrateLifetime;
248  unsigned int fCryo = 9999, fTPC = 9999, fPlane = 9999;
249  float fAdcMax, fAdcMin, fAdcScale, fAdcOffset, fAdcZero;
250  double fAdcSumOverThr, fAdcSumThr;
252 
253  CLHEP::HepJamesRandom fRndEngine;
254 
255  void applyBlur();
256  std::vector<float> fBlurKernel; // blur not applied if empty
257 
258  void addWhiteNoise();
259  float fNoiseSigma; // noise not added if sigma=0
260 
261  void addCoherentNoise();
262  float fCoherentSigma; // noise not added if sigma=0
263 };
264 // ------------------------------------------------------
265 // ------------------------------------------------------
266 // ------------------------------------------------------
267 
268 #endif
TRandom r
Definition: spectrum.C:23
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
std::vector< float > downscale(std::size_t dst_size, std::vector< float > const &adc, size_t tick0) const
DataProviderAlg(const fhicl::ParameterSet &pset)
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:289
decltype(auto) values(Coll &&coll)
Range-for loop helper iterating across the values of the specified collection.
parameter set interface
std::vector< raw::ChannelID_t > fWireChannels
tick_as<> tick
Tick number, represented by std::ptrdiff_t.
Definition: electronics.h:73
double getAdcSum() const
General LArSoft Utilities.
Description of geometry of one entire detector.
Definition: GeometryCore.h:119
DataProviderAlgView fAlgView
calo::CalorimetryAlg fCalorimetryAlg
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
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
Namespace collecting geometry-related classes utilities.
size_t getAdcArea() const
size_t getDriftIndex(float drift) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
std::vector< float > const & wireData(size_t widx) const
unsigned int TPC() const
map< int, array< map< int, double >, 2 >> Table
Definition: plot.C:18