LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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
20 #include "fhiclcpp/types/Atom.h"
22 #include "fhiclcpp/types/Table.h"
23 
24 // LArSoft includes
30 
31 #include "CLHEP/Random/JamesRandom.h" // for testing on noise, not used by any reco
32 
33 // ROOT & C++
34 #include <memory>
35 //#include <functional>
36 
37 namespace img
38 {
39  class DataProviderAlg;
40 }
41 
47 {
48 public:
49  enum EDownscaleMode { kMax = 1, kMaxMean = 2, kMean = 3 };
50 
51  struct Config
52  {
53  using Name = fhicl::Name;
55 
57  Name("CalorimetryAlg"), Comment("Used to eliminate amplitude variation due to electron lifetime.")
58  };
59 
60  fhicl::Atom<float> AdcMax { Name("AdcMax"), Comment("Saturation max value") };
61  fhicl::Atom<float> AdcMin { Name("AdcMin"), Comment("Saturation min value") };
62  fhicl::Atom<float> OutMax { Name("OutMax"), Comment("Output max value") };
63  fhicl::Atom<float> OutMin { Name("OutMin"), Comment("Output min value") };
64 
66  Name("CalibrateAmpl"), Comment("Calibrate ADC values with CalAmpConstants")
67  };
68 
70  Name("CalibrateLifetime"), Comment("Calibrate ADC values with the electron lifetime")
71  };
72 
74  Name("DriftWindow"), Comment("Downsampling window (in drift ticks).")
75  };
76 
78  Name("DownscaleFn"), Comment("Downsampling function")
79  };
80 
82  Name("DownscaleFullView"), Comment("Downsample full view (faster / lower location precision)")
83  };
84 
86  Name("BlurKernel"), Comment("Blur kernel in wire direction")
87  };
88 
90  Name("NoiseSigma"), Comment("White noise sigma")
91  };
92 
94  Name("CoherentSigma"), Comment("Coherent noise sigma")
95  };
96  };
97 
99  DataProviderAlg(fhicl::Table<Config>(pset, {})())
100  {}
101 
102  DataProviderAlg(const Config& config);
103 
104  virtual ~DataProviderAlg(void);
105 
106  bool setWireDriftData(const std::vector<recob::Wire> & wires, // once per plane: setup ADC buffer, collect & downscale ADC's
107  unsigned int plane, unsigned int tpc, unsigned int cryo);
108 
109  std::vector<float> const & wireData(size_t widx) const { return fWireDriftData[widx]; }
110 
114  std::vector< std::vector<float> > getPatch(size_t wire, float drift, size_t patchSizeW, size_t patchSizeD) const
115  {
116  bool ok = false;
117  std::vector< std::vector<float> > patch;
118  if (fDownscaleFullView)
119  {
120  ok = patchFromDownsampledView(wire, drift, patchSizeW, patchSizeD, patch);
121  }
122  else
123  {
124  ok = patchFromOriginalView(wire, drift, patchSizeW, patchSizeD, patch);
125  }
126 
127  if (ok) return patch;
128  else
129  {
130  throw cet::exception("img::DataProviderAlg") << "Patch filling failed." << std::endl;
131  }
132  }
133 
136  float getPixelOrZero(int wire, int drift) const
137  {
138  size_t didx = getDriftIndex(drift), widx = (size_t)wire;
139 
140  if ((widx < fWireDriftData.size()) &&
141  (didx < fNCachedDrifts))
142  {
143  return fWireDriftData[widx][didx];
144  }
145  else { return 0; }
146  }
147 
148  double getAdcSum(void) const { return fAdcSumOverThr; }
149  size_t getAdcArea(void) const { return fAdcAreaOverThr; }
150 
152  float poolMax(int wire, int drift, size_t r = 0) const;
153 
155  float poolSum(int wire, int drift, size_t r = 0) const;
156 
157  unsigned int Cryo(void) const { return fCryo; }
158  unsigned int TPC(void) const { return fTPC; }
159  unsigned int Plane(void) const { return fPlane; }
160 
161  unsigned int NWires(void) const { return fNWires; }
162  unsigned int NScaledDrifts(void) const { return fNScaledDrifts; }
163  unsigned int NCachedDrifts(void) const { return fNCachedDrifts; }
164  unsigned int DriftWindow(void) const { return fDriftWindow; }
165 
167  float ZeroLevel(void) const { return fAdcZero; }
168 
169  double LifetimeCorrection(double tick) const { return fCalorimetryAlg.LifetimeCorrection(tick); }
170 
171 protected:
172  unsigned int fCryo, fTPC, fPlane;
174 
175  std::vector< raw::ChannelID_t > fWireChannels; // wire channels (may need this connection...), InvalidChannelID if not used
176  std::vector< std::vector<float> > fWireDriftData; // 2D data for entire projection, drifts scaled down
177  std::vector<float> fLifetimeCorrFactors; // precalculated correction factors along full drift
178 
180  //std::function<void (std::vector<float> &, std::vector<float> const &, size_t)> fnDownscale;
181 
182  size_t fDriftWindow;
185 
186  void downscaleMax(std::vector<float> & dst, std::vector<float> const & adc, size_t tick0) const;
187  void downscaleMaxMean(std::vector<float> & dst, std::vector<float> const & adc, size_t tick0) const;
188  void downscaleMean(std::vector<float> & dst, std::vector<float> const & adc, size_t tick0) const;
189  void downscale(std::vector<float> & dst, std::vector<float> const & adc, size_t tick0) const
190  {
191  switch (fDownscaleMode)
192  {
193  case img::DataProviderAlg::kMean: downscaleMean(dst, adc, tick0); break;
194  case img::DataProviderAlg::kMaxMean: downscaleMaxMean(dst, adc, tick0); break;
195  case img::DataProviderAlg::kMax: downscaleMax(dst, adc, tick0); break;
196  default:throw cet::exception("img::DataProviderAlg") << "Downscale mode not supported." << std::endl; break;
197  }
198  }
199 
200  size_t getDriftIndex(float drift) const
201  {
202  if (fDownscaleFullView) return (size_t)(drift * fDriftWindowInv);
203  else return (size_t)drift;
204  }
205 
206  bool setWireData(std::vector<float> const & adc, size_t wireIdx);
207 
208  bool patchFromDownsampledView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector<float> > & patch) const;
209  bool patchFromOriginalView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector<float> > & patch) const;
210 
211  virtual void resizeView(size_t wires, size_t drifts);
212 
213  // Calorimetry needed to equalize ADC amplitude along drift:
215 
216  // Geometry and detector properties:
219 
220 private:
221  float scaleAdcSample(float val) const;
222  void scaleAdcSamples(std::vector< float > & values) const;
223  std::vector<float> fAmplCalibConst;
225 
229 
230  CLHEP::HepJamesRandom fRndEngine;
231 
232  void applyBlur();
233  std::vector<float> fBlurKernel; // blur not applied if empty
234 
235  void addWhiteNoise();
236  float fNoiseSigma; // noise not added if sigma=0
237 
238  void addCoherentNoise();
239  float fCoherentSigma; // noise not added if sigma=0
240 };
241 // ------------------------------------------------------
242 // ------------------------------------------------------
243 // ------------------------------------------------------
244 
245 #endif
size_t getAdcArea(void) const
virtual void resizeView(size_t wires, size_t drifts)
std::vector< float > fLifetimeCorrFactors
geo::GeometryCore const * fGeometry
fhicl::Atom< float > AdcMax
double LifetimeCorrection(double tick) const
std::vector< raw::ChannelID_t > fWireChannels
fhicl::Atom< float > CoherentSigma
float poolSum(int wire, int drift, size_t r=0) const
Pool sum of pixels in a patch around the wire/drift pixel.
void downscaleMaxMean(std::vector< float > &dst, std::vector< float > const &adc, size_t tick0) const
std::vector< std::vector< float > > fWireDriftData
std::vector< std::vector< float > > getPatch(size_t wire, float drift, size_t patchSizeW, size_t patchSizeD) const
bool patchFromDownsampledView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float > > &patch) const
fhicl::Atom< unsigned int > DriftWindow
unsigned int NScaledDrifts(void) const
DataProviderAlg(const fhicl::ParameterSet &pset)
unsigned int NWires(void) const
unsigned int Cryo(void) const
bool setWireDriftData(const std::vector< recob::Wire > &wires, unsigned int plane, unsigned int tpc, unsigned int cryo)
bool setWireData(std::vector< float > const &adc, size_t wireIdx)
std::vector< float > fBlurKernel
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Access the description of detector geometry.
fhicl::Sequence< float > BlurKernel
detinfo::DetectorProperties const * fDetProp
fhicl::Atom< bool > DownscaleFullView
parameter set interface
void downscaleMax(std::vector< float > &dst, std::vector< float > const &adc, size_t tick0) const
unsigned int fNCachedDrifts
unsigned int NCachedDrifts(void) const
unsigned int TPC(void) const
void scaleAdcSamples(std::vector< float > &values) const
unsigned int Plane(void) const
float scaleAdcSample(float val) const
Description of geometry of one entire detector.
fhicl::Atom< float > AdcMin
calo::CalorimetryAlg fCalorimetryAlg
fhicl::Atom< bool > CalibrateLifetime
float ZeroLevel(void) const
Level of zero ADC after scaling.
unsigned int fNScaledDrifts
float getPixelOrZero(int wire, int drift) const
fhicl::Table< calo::CalorimetryAlg::Config > CalorimetryAlg
std::vector< float > fAmplCalibConst
fhicl::Atom< std::string > DownscaleFn
Declaration of basic channel signal object.
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.
bool patchFromOriginalView(size_t wire, float drift, size_t size_w, size_t size_d, std::vector< std::vector< float > > &patch) const
void downscale(std::vector< float > &dst, std::vector< float > const &adc, size_t tick0) const
double LifetimeCorrection(double time, double T0=0) const
size_t getDriftIndex(float drift) const
art framework interface to geometry description
void downscaleMean(std::vector< float > &dst, std::vector< float > const &adc, size_t tick0) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
virtual ~DataProviderAlg(void)
fhicl::Atom< float > NoiseSigma
std::vector< float > const & wireData(size_t widx) const
unsigned int DriftWindow(void) const
double getAdcSum(void) const