LArSoft  v10_04_05
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
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 
61 public:
62  enum EDownscaleMode { kMax = 1, kMaxMean = 2, kMean = 3 };
63 
64  struct Config {
65  using Name = fhicl::Name;
67 
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 
100  : DataProviderAlg(fhicl::Table<Config>(pset, {})())
101  {}
102 
103  DataProviderAlg(const Config& config);
104 
105  virtual ~DataProviderAlg();
106 
107  bool setWireDriftData(const detinfo::DetectorClocksData& clock_data,
108  const detinfo::DetectorPropertiesData& det_prop,
109  const std::vector<recob::Wire>&
110  wires, // once per plane: setup ADC buffer, collect & downscale ADC's
111  unsigned int plane,
112  unsigned int tpc,
113  unsigned int cryo);
114 
115  std::vector<float> const& wireData(size_t widx) const { return fAlgView.fWireDriftData[widx]; }
116 
120  std::vector<std::vector<float>> getPatch(size_t wire,
121  float drift,
122  size_t patchSizeW,
123  size_t patchSizeD) const
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  }
137 
140  float getPixelOrZero(int wire, int drift) const
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  }
149 
150  double getAdcSum() const { return fAdcSumOverThr; }
151  size_t getAdcArea() const { return fAdcAreaOverThr; }
152 
154  float poolMax(int wire, int drift, size_t r = 0) const;
155 
157  //float poolSum(int wire, int drift, size_t r = 0) const;
158 
159  unsigned int Cryo() const { return fCryo; }
160  unsigned int TPC() const { return fTPC; }
161  unsigned int Plane() const { return fPlane; }
162 
163  unsigned int NWires() const { return fAlgView.fNWires; }
164  unsigned int NScaledDrifts() const { return fAlgView.fNScaledDrifts; }
165  unsigned int NCachedDrifts() const { return fAlgView.fNCachedDrifts; }
166  unsigned int DriftWindow() const { return fDriftWindow; }
167 
169  float ZeroLevel() const { return fAdcZero; }
170 
172  detinfo::DetectorPropertiesData const& det_prop,
173  double tick) const
174  {
175  return fCalorimetryAlg.LifetimeCorrection(clock_data, det_prop, tick);
176  }
177 
178 protected:
181  //std::function<void (std::vector<float> &, std::vector<float> const &, size_t)> fnDownscale;
182 
183  size_t fDriftWindow;
186 
187  std::vector<float> downscaleMax(std::size_t dst_size,
188  std::vector<float> const& adc,
189  size_t tick0) const;
190  std::vector<float> downscaleMaxMean(std::size_t dst_size,
191  std::vector<float> const& adc,
192  size_t tick0) const;
193  std::vector<float> downscaleMean(std::size_t dst_size,
194  std::vector<float> const& adc,
195  size_t tick0) const;
196  std::vector<float> downscale(std::size_t dst_size,
197  std::vector<float> const& adc,
198  size_t tick0) const
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  }
207 
208  size_t getDriftIndex(float drift) const
209  {
210  if (fDownscaleFullView)
211  return (size_t)(drift * fDriftWindowInv);
212  else
213  return (size_t)drift;
214  }
215 
216  std::optional<std::vector<float>> setWireData(std::vector<float> const& adc,
217  size_t wireIdx) const;
218 
219  bool patchFromDownsampledView(size_t wire,
220  float drift,
221  size_t size_w,
222  size_t size_d,
223  std::vector<std::vector<float>>& patch) const;
224  bool patchFromOriginalView(size_t wire,
225  float drift,
226  size_t size_w,
227  size_t size_d,
228  std::vector<std::vector<float>>& patch) const;
229 
230  virtual DataProviderAlgView resizeView(detinfo::DetectorClocksData const& clock_data,
231  detinfo::DetectorPropertiesData const& det_prop,
232  size_t wires,
233  size_t drifts);
234 
235  // Calorimetry needed to equalize ADC amplitude along drift:
237 
238  // Geometry and detector properties:
241 
242 private:
243  float scaleAdcSample(float val) const;
244  void scaleAdcSamples(std::vector<float>& values) const;
245  std::vector<float> fAmplCalibConst;
246  bool fCalibrateAmpl, fCalibrateLifetime;
247  unsigned int fCryo = 9999, fTPC = 9999, fPlane = 9999;
248  float fAdcMax, fAdcMin, fAdcScale, fAdcOffset, fAdcZero;
249  double fAdcSumOverThr, fAdcSumThr;
251 
252  CLHEP::HepJamesRandom fRndEngine;
253 
254  void applyBlur();
255  std::vector<float> fBlurKernel; // blur not applied if empty
256 
257  void addWhiteNoise();
258  float fNoiseSigma; // noise not added if sigma=0
259 
260  void addCoherentNoise();
261  float fCoherentSigma; // noise not added if sigma=0
262 };
263 // ------------------------------------------------------
264 // ------------------------------------------------------
265 // ------------------------------------------------------
266 
267 #endif
TRandom r
Definition: spectrum.C:23
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
geo::Geometry const * fGeometry
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
geo::WireReadoutGeom const * fWireReadoutGeom
Interface for a class providing readout channel mapping to geometry.
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.
The geometry of one entire detector, as served by art.
Definition: Geometry.h:42
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
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