LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
PointIdAlg.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 // Point Identification Algorithm
9 //
10 // Run CNN or MLP trained to classify a point in 2D projection. Various features can be
11 // recognized, depending on the net model/weights used.
12 //
14 
15 #ifndef PointIdAlg_h
16 #define PointIdAlg_h
17 
18 // Framework includes
22 
23 // LArSoft includes
29 
33 
34 // ROOT & C++
35 #include <memory>
36 
37 namespace nnet
38 {
39  class ModelInterface;
40  class KerasModelInterface;
41  class TfModelInterface;
42  class PointIdAlg;
43  class TrainingDataAlg;
44 }
45 
50 {
51 public:
52  virtual ~ModelInterface(void) { }
53 
54  virtual std::vector<float> Run(std::vector< std::vector<float> > const & inp2d) = 0;
55  virtual std::vector< std::vector<float> > Run(std::vector< std::vector< std::vector<float> > > const & inps, int samples = -1);
56 
57 protected:
58  ModelInterface(void) { }
59 
60  std::string findFile(const char* fileName) const;
61 };
62 // ------------------------------------------------------
63 
65 {
66 public:
67  KerasModelInterface(const char* modelFileName);
68 
69  std::vector<float> Run(std::vector< std::vector<float> > const & inp2d) override;
70 
71 private:
72  keras::KerasModel m; // network model
73 };
74 // ------------------------------------------------------
75 
77 {
78 public:
79  TfModelInterface(const char* modelFileName);
80 
81  std::vector< std::vector<float> > Run(std::vector< std::vector< std::vector<float> > > const & inps, int samples = -1) override;
82  std::vector<float> Run(std::vector< std::vector<float> > const & inp2d) override;
83 
84 private:
85  std::unique_ptr<tf::Graph> g; // network graph
86 };
87 // ------------------------------------------------------
88 
90 {
91 public:
92 
94  {
95  using Name = fhicl::Name;
97 
98  fhicl::Atom<std::string> NNetModelFile {
99  Name("NNetModelFile"), Comment("Neural net model to apply.")
100  };
102  Name("NNetOutputs"), Comment("Labels of the network outputs.")
103  };
104 
106  Name("PatchSizeW"), Comment("How many wires in patch.")
107  };
108 
110  Name("PatchSizeD"), Comment("How many downsampled ADC entries in patch")
111  };
112  };
113 
115  PointIdAlg(fhicl::Table<Config>(pset, {})())
116  {}
117 
118  PointIdAlg(const Config& config);
119 
120  ~PointIdAlg(void) override;
121 
123  std::vector< std::string > const & outputLabels(void) const { return fNNetOutputs; }
124 
126  float predictIdValue(unsigned int wire, float drift, size_t outIdx = 0) const;
127 
129  std::vector<float> predictIdVector(unsigned int wire, float drift) const;
130 
131  std::vector< std::vector<float> > predictIdVectors(std::vector< std::pair<unsigned int, float> > points) const;
132 
133  static std::vector<float> flattenData2D(std::vector< std::vector<float> > const & patch);
134 
135  std::vector< std::vector<float> > const & patchData2D(void) const { return fWireDriftPatch; }
136  std::vector<float> patchData1D(void) const { return flattenData2D(fWireDriftPatch); } // flat vector made of the patch data, wire after wire
137 
138  bool isInsideFiducialRegion(unsigned int wire, float drift) const;
139 
142  bool isCurrentPatch(unsigned int wire, float drift) const;
143 
145  bool isSamePatch(unsigned int wire1, float drift1, unsigned int wire2, float drift2) const;
146 
147 private:
148  std::string fNNetModelFilePath;
149  std::vector< std::string > fNNetOutputs;
151 
152  mutable std::vector< std::vector<float> > fWireDriftPatch; // patch data around the identified point
153  size_t fPatchSizeW, fPatchSizeD;
154 
155  mutable size_t fCurrentWireIdx, fCurrentScaledDrift;
156  bool bufferPatch(size_t wire, float drift, std::vector< std::vector<float> > & patch) const
157  {
158  if (fDownscaleFullView)
159  {
160  size_t sd = (size_t)(drift / fDriftWindow);
161  if ((fCurrentWireIdx == wire) && (fCurrentScaledDrift == sd))
162  return true; // still within the current position
163 
164  fCurrentWireIdx = wire; fCurrentScaledDrift = sd;
165 
166  return patchFromDownsampledView(wire, drift, fPatchSizeW, fPatchSizeD, patch);
167  }
168  else
169  {
170  if ((fCurrentWireIdx == wire) && (fCurrentScaledDrift == drift))
171  return true; // still within the current position
172 
173  fCurrentWireIdx = wire; fCurrentScaledDrift = drift;
174 
175  return patchFromOriginalView(wire, drift, fPatchSizeW, fPatchSizeD, patch);
176  }
177  }
178  bool bufferPatch(size_t wire, float drift) const { return bufferPatch(wire, drift, fWireDriftPatch); }
179  void resizePatch(void);
180 
181  void deleteNNet(void) { if (fNNet) delete fNNet; fNNet = 0; }
182 };
183 // ------------------------------------------------------
184 // ------------------------------------------------------
185 // ------------------------------------------------------
186 
188 {
189 public:
190 
191  enum EMask
192  {
193  kNone = 0,
194  kPdgMask = 0x00000FFF, // pdg code mask
195  kTypeMask = 0x0000F000, // track type mask
196  kVtxMask = 0xFFFF0000 // vertex flags
197  };
198 
199  enum ETrkType
200  {
201  kDelta = 0x1000, // delta electron
202  kMichel = 0x2000, // Michel electron
203  kPriEl = 0x4000, // primary electron
204  kPriMu = 0x8000 // primary muon
205  };
206 
207  enum EVtxId
208  {
209  kNuNC = 0x0010000, kNuCC = 0x0020000, kNuPri = 0x0040000, // nu interaction type
210  kNuE = 0x0100000, kNuMu = 0x0200000, kNuTau = 0x0400000, // nu flavor
211  kHadr = 0x1000000, // hadronic inelastic scattering
212  kPi0 = 0x2000000, // pi0 produced in this vertex
213  kDecay = 0x4000000, // point of particle decay
214  kConv = 0x8000000, // gamma conversion
215  kElectronEnd = 0x10000000 // clear end of an electron
216  };
217 
219  {
220  using Name = fhicl::Name;
222 
224  Name("WireLabel"),
225  Comment("Tag of recob::Wire.")
226  };
227 
229  Name("HitLabel"),
230  Comment("Tag of recob::Hit.")
231  };
232 
234  Name("TrackLabel"),
235  Comment("Tag of recob::Track.")
236  };
237 
239  Name("SimulationLabel"),
240  Comment("Tag of simulation producer.")
241  };
242 
243  fhicl::Atom< bool > SaveVtxFlags {
244  Name("SaveVtxFlags"),
245  Comment("Include (or not) vertex info in PDG map.")
246  };
247 
249  Name("AdcDelayTicks"),
250  Comment("ADC pulse peak delay in ticks (non-zero for not deconvoluted waveforms).")
251  };
252  };
253 
255  TrainingDataAlg(fhicl::Table<Config>(pset, {})())
256  {}
257 
258  TrainingDataAlg(const Config& config);
259 
260  ~TrainingDataAlg(void) override;
261 
262  void reconfigure(const Config& config);
263 
264  bool saveSimInfo() const { return fSaveSimInfo; }
265 
266  bool setEventData(const art::Event& event, // collect & downscale ADC's, charge deposits, pdg labels
267  unsigned int plane, unsigned int tpc, unsigned int cryo);
268 
269  bool setDataEventData(const art::Event& event, // collect & downscale ADC's, charge deposits, pdg labels
270  unsigned int plane, unsigned int tpc, unsigned int cryo);
271 
272 
273  bool findCrop(float max_e_cut, unsigned int & w0, unsigned int & w1, unsigned int & d0, unsigned int & d1) const;
274 
275  double getEdepTot(void) const { return fEdepTot; } // [GeV]
276  std::vector<float> const & wireEdep(size_t widx) const { return fWireDriftEdep[widx]; }
277  std::vector<int> const & wirePdg(size_t widx) const { return fWireDriftPdg[widx]; }
278 
279 protected:
280 
281  void resizeView(size_t wires, size_t drifts) override;
282 
283 private:
284 
285  struct WireDrift // used to find MCParticle start/end 2D projections
286  {
287  size_t Wire;
288  int Drift;
289  int TPC;
290  int Cryo;
291  };
292 
293  WireDrift getProjection(const TLorentzVector& tvec, unsigned int plane) const;
294 
295  bool setWireEdepsAndLabels(
296  std::vector<float> const & edeps,
297  std::vector<int> const & pdgs,
298  size_t wireIdx);
299 
300  void collectVtxFlags(
301  std::unordered_map< size_t, std::unordered_map< int, int > > & wireToDriftToVtxFlags,
302  const std::unordered_map< int, const simb::MCParticle* > & particleMap,
303  unsigned int plane) const;
304 
305  static float particleRange2(const simb::MCParticle & particle)
306  {
307  float dx = particle.EndX() - particle.Vx();
308  float dy = particle.EndY() - particle.Vy();
309  float dz = particle.EndZ() - particle.Vz();
310  return dx*dx + dy*dy + dz*dz;
311  }
312  bool isElectronEnd(
313  const simb::MCParticle & particle,
314  const std::unordered_map< int, const simb::MCParticle* > & particleMap) const;
315 
316  bool isMuonDecaying(
317  const simb::MCParticle & particle,
318  const std::unordered_map< int, const simb::MCParticle* > & particleMap) const;
319 
320  double fEdepTot; // [GeV]
321  std::vector< std::vector<float> > fWireDriftEdep;
322  std::vector< std::vector<int> > fWireDriftPdg;
323 
330 
331  unsigned int fAdcDelay;
332 
333  std::vector<size_t> fEventsPerBin;
334 };
335 // ------------------------------------------------------
336 // ------------------------------------------------------
337 // ------------------------------------------------------
338 
339 #endif
bool bufferPatch(size_t wire, float drift, std::vector< std::vector< float > > &patch) const
Definition: PointIdAlg.h:156
std::vector< float > const & wireEdep(size_t widx) const
Definition: PointIdAlg.h:276
std::vector< float > patchData1D(void) const
Definition: PointIdAlg.h:136
double EndZ() const
Definition: MCParticle.h:232
std::vector< std::vector< int > > fWireDriftPdg
Definition: PointIdAlg.h:322
bool saveSimInfo() const
Definition: PointIdAlg.h:264
unsigned int fAdcDelay
Definition: PointIdAlg.h:331
std::vector< std::vector< float > > const & patchData2D(void) const
Definition: PointIdAlg.h:135
void deleteNNet(void)
Definition: PointIdAlg.h:181
bool bufferPatch(size_t wire, float drift) const
Definition: PointIdAlg.h:178
art::InputTag fTrackModuleLabel
Definition: PointIdAlg.h:326
Declaration of signal hit object.
static const int kNuTau
virtual std::vector< float > Run(std::vector< std::vector< float > > const &inp2d)=0
Particle class.
double EndY() const
Definition: MCParticle.h:231
no compression
Definition: RawTypes.h:9
std::vector< std::string > fNNetOutputs
Definition: PointIdAlg.h:149
art::InputTag fWireProducerLabel
Definition: PointIdAlg.h:324
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
std::vector< std::string > const & outputLabels(void) const
network output labels
Definition: PointIdAlg.h:123
std::vector< std::vector< float > > fWireDriftPatch
Definition: PointIdAlg.h:152
Provides recob::Track data product.
parameter set interface
std::vector< size_t > fEventsPerBin
Definition: PointIdAlg.h:333
keras::KerasModel m
Definition: PointIdAlg.h:72
double getEdepTot(void) const
Definition: PointIdAlg.h:275
double Vx(const int i=0) const
Definition: MCParticle.h:225
virtual ~ModelInterface(void)
Definition: PointIdAlg.h:52
std::string findFile(const char *fileName) const
Definition: PointIdAlg.cxx:51
static float particleRange2(const simb::MCParticle &particle)
Definition: PointIdAlg.h:305
double Vz(const int i=0) const
Definition: MCParticle.h:227
nnet::ModelInterface * fNNet
Definition: PointIdAlg.h:150
std::unique_ptr< tf::Graph > g
Definition: PointIdAlg.h:85
size_t fPatchSizeW
Definition: PointIdAlg.h:153
art::InputTag fSimulationProducerLabel
Definition: PointIdAlg.h:327
std::string fNNetModelFilePath
Definition: PointIdAlg.h:148
std::vector< int > const & wirePdg(size_t widx) const
Definition: PointIdAlg.h:277
double EndX() const
Definition: MCParticle.h:230
std::vector< std::vector< float > > fWireDriftEdep
Definition: PointIdAlg.h:321
art::InputTag fHitProducerLabel
Definition: PointIdAlg.h:325
double Vy(const int i=0) const
Definition: MCParticle.h:226
static const int kNuMu
Event finding and building.
TrainingDataAlg(const fhicl::ParameterSet &pset)
Definition: PointIdAlg.h:254
size_t fCurrentWireIdx
Definition: PointIdAlg.h:155
PointIdAlg(const fhicl::ParameterSet &pset)
Definition: PointIdAlg.h:114