RegCNNMapper_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file RegCNNMapper_module.cc
3 // \brief Producer module for creating RegCNN PixelMap objects modified from CVNMapper_module.cc
4 // \author Ilsoo Seong - iseong@uci.edu
5 //
6 // Modifications to interface for numu energy, prong direction
7 // - Wenjie Wu - wenjieww@uci.edu
8 ////////////////////////////////////////////////////////////////////////
9 
10 // C/C++ includes
11 #include <iostream>
12 #include <sstream>
13 
14 // ROOT includes
15 #include "TTree.h"
16 #include "TH2F.h"
17 
18 // Framework includes
22 #include "art_root_io/TFileDirectory.h"
23 #include "art_root_io/TFileService.h"
25 #include "fhiclcpp/ParameterSet.h"
29 #include "canvas/Persistency/Common/FindManyP.h"
30 
31 // LArSoft includes
35 
47 
53 
54 namespace lar_pandora{class LArPandoraHelper;}
55 
56 namespace cnn {
57 
58  class RegCNNMapper : public art::EDProducer {
59  public:
60  explicit RegCNNMapper(fhicl::ParameterSet const& pset);
61  ~RegCNNMapper();
62 
63  void produce(art::Event& evt);
64  void beginJob();
65  void endJob();
66 
67 
68  private:
69  /// Module lablel for input clusters
71 
72  /// Instance lablel for cluster pixelmaps
74 
75  /// Minimum number of hits for cluster to be converted to pixel map
76  unsigned short fMinClusterHits;
77 
78  /// parameters for 2D pixel maps
79  /// Width of pixel map in tdcs
80  unsigned short fTdcWidth;
81  /// Length of pixel map in wires
82  unsigned short fWireLength;
83  // resolution of ticks
85  // resolution of wires
87 
88  /// Maximum gap in wires at front of cluster to prevent pruning of upstream
89  /// hits
90  //unsigned int fMaxWireGap;
91 
92  /// Use unwrapped pixel maps?
93  //dla bool fUnwrappedPixelMap;
94 
95  /// select which global wire method
97  /// select how to choose center of pixel map
99  /// select how to tag prong
101 
102  /// choose to create 3D pixel maps
104  // Crop pixel size to 32x32x32
105  bool fCropped;
106  // Use Prong only pixel maps
108  // How many pixels in each dimension
109  int fUnitX;
110  int fUnitY;
111  int fUnitZ;
112  // The real space covered by one pixel (in cm)
113  double fXResolution;
114  double fYResolution;
115  double fZResolution;
116  // generate pixel map by raw charge or by reco. hit
117  bool fByHit;
118 
124 
127 
128  /// PixelMapProducer does the work for us
131 
132  };
133 
134 
135  //.......................................................................
136  RegCNNMapper::RegCNNMapper(fhicl::ParameterSet const& pset):
137  EDProducer(pset),
138  fHitsModuleLabel (pset.get<std::string> ("HitsModuleLabel")),
139  fClusterPMLabel (pset.get<std::string> ("ClusterPMLabel")),
140  fMinClusterHits (pset.get<unsigned short> ("MinClusterHits")),
141  fTdcWidth (pset.get<unsigned short> ("TdcWidth")),
142  fWireLength (pset.get<unsigned short> ("WireLength")),
143  fTimeResolution (pset.get<unsigned short> ("TimeResolution")),
144  fWireResolution (pset.get<unsigned short> ("WireResolution")),
145  fGlobalWireMethod (pset.get<int> ("GlobalWireMethod")),
146  fUseRecoVertex (pset.get<int> ("UseRecoVertex")),
147  fProngTagMethod (pset.get<int> ("ProngTagMethod")),
148  fUseThreeDMap (pset.get<int> ("UseThreeDMap")),
149  fCropped (pset.get<bool> ("Cropped")),
150  fProngOnly (pset.get<bool> ("ProngOnly")),
151  fUnitX (pset.get<int> ("UnitX")),
152  fUnitY (pset.get<int> ("UnitY")),
153  fUnitZ (pset.get<int> ("UnitZ")),
154  fXResolution (pset.get<double> ("XResolution")),
155  fYResolution (pset.get<double> ("YResolution")),
156  fZResolution (pset.get<double> ("ZResolution")),
157  fByHit (pset.get<bool> ("ByHit")),
158  fShowerModuleLabel(pset.get<std::string> ("ShowerModuleLabel")),
159  fTrackModuleLabel (pset.get<std::string> ("TrackModuleLabel")),
160  fVertexModuleLabel(pset.get<std::string> ("VertexModuleLabel")),
161  fPFParticleModuleLabel(pset.get<std::string> ("PFParticleModuleLabel")),
162  fPandoraNuVertexModuleLabel(pset.get<std::string>("PandoraNuVertexModuleLabel")),
163  fRegCNNResultLabel (pset.get<std::string> ("RegCNNResultLabel")),
164  fRegCNNModuleLabel (pset.get<std::string> ("RegCNNModuleLabel")),
165  fProducer(RegPixelMapProducer(fWireLength, fWireResolution, fTdcWidth, fTimeResolution, fGlobalWireMethod, fProngOnly, fByHit)),
166  fProducer3D(RegPixelMap3DProducer(fUnitX, fUnitY, fUnitZ, fXResolution, fYResolution, fZResolution, fCropped, fProngOnly))
167  {
168  if (fUseThreeDMap==0) {
169  produces< std::vector<cnn::RegPixelMap> >(fClusterPMLabel);
170  } else if (fUseThreeDMap==1) {
171  produces< std::vector<cnn::RegPixelMap3D> >(fClusterPMLabel);
172  } else {
173  mf::LogError("RegCNNMapper::RegCNNMapper")<<"RegCNNMapper accepts 0 or 1 for fUseThreeDMap"<<std::endl;
174  }
175  }
176 
177  //......................................................................
179  {
180  //======================================================================
181  // Clean up any memory allocated by your module
182  //======================================================================
183  }
184 
185  //......................................................................
187  {
188  }
189 
190  //......................................................................
192  {
193  }
194 
195  //......................................................................
197  {
198  std::cout<<"ievet : "<<evt.id()<<std::endl;
199 
200  std::vector< art::Ptr< recob::Hit > > hitlist;
201  auto hitListHandle = evt.getHandle< std::vector< recob::Hit > >(fHitsModuleLabel);
202  if (hitListHandle)
203  art::fill_ptr_vector(hitlist, hitListHandle);
204 
205  art::FindManyP<recob::Wire> fmwire(hitListHandle, evt, fHitsModuleLabel);
206  art::FindManyP<recob::Shower> fmshwhit(hitListHandle, evt, fShowerModuleLabel);
207  art::FindManyP<recob::Track> fmtrkhit(hitListHandle, evt, fTrackModuleLabel);
208 
209  unsigned short nhits = hitlist.size();
210 
211  // Get the vertex out of the event record
212  std::vector<art::Ptr<recob::Vertex> > vertex_list;
213  auto vertexHandle = evt.getHandle<std::vector<recob::Vertex> >(fVertexModuleLabel);
214  if (vertexHandle)
215  art::fill_ptr_vector(vertex_list, vertexHandle);
216  art::FindMany<recob::PFParticle> fmPFParticle(vertexHandle, evt, fPFParticleModuleLabel);
217 
218  art::FindManyP<recob::SpacePoint> fmSPFromHits(hitListHandle, evt, fPFParticleModuleLabel);
219 
220  //Declaring containers for things to be stored in event
221  std::unique_ptr< std::vector<cnn::RegPixelMap> >
222  pmCol(new std::vector<cnn::RegPixelMap>);
223 
224  std::unique_ptr< std::vector<cnn::RegPixelMap3D> >
225  pm3DCol(new std::vector<cnn::RegPixelMap3D>);
226 
227  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
228  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(evt, clockData);
229  if(nhits>fMinClusterHits){
230  if (fUseThreeDMap==0) {
231  RegPixelMap pm;
232  if (fUseRecoVertex==0){
233  // create pixel map based on mean of wire and ticks
234  pm = fProducer.CreateMap(clockData, detProp, hitlist, fmwire);
235  }
236  else if (fUseRecoVertex==1) {
237  // create pixel map based on the reconstructed vertex
238  // Get RegCNN Results
240  auto cnnresultListHandle = evt.getHandle<std::vector<cnn::RegCNNResult>>(itag1);
241  std::vector<float> vtx(3, -99999);
242  if (!cnnresultListHandle.failedToGet())
243  {
244  if (!cnnresultListHandle->empty())
245  {
246  const std::vector<float>& v = (*cnnresultListHandle)[0].fOutput;
247  for (unsigned int ii = 0; ii < 3; ii++){
248  vtx[ii] = v[ii];
249  std::cout<<"vertex "<<ii<<" : "<<vtx[ii]<<std::endl;
250  }
251  }
252  }
253  if (fProngTagMethod==1) {
254  pm = fProducer.CreateMap(clockData, detProp, hitlist, fmwire, fmtrkhit, vtx);
255  } else {
256  pm = fProducer.CreateMap(clockData, detProp, hitlist, fmwire, fmshwhit, vtx);
257  }
258  }
259  else if (fUseRecoVertex==2) {
260  // create pixel map based on pandora vertex
261  lar_pandora::PFParticleVector particleVector;
263  lar_pandora::VertexVector vertexVector;
264  lar_pandora::PFParticlesToVertices particlesToVertices;
265  lar_pandora::LArPandoraHelper::CollectVertices(evt, fPandoraNuVertexModuleLabel, vertexVector, particlesToVertices);
266 
267  int n_prims = 0;
268  int n_pand_particles = particleVector.size();
269  //std::cout<<"n_pand_particles "<<n_pand_particles<<std::endl;
270  double xyz_temp[3] = {0.0, 0.0, 0.0};
271  std::vector<float> vtx(3, -99999);
272  for (int ipfp = 0; ipfp< n_pand_particles; ipfp++) {
273  const art::Ptr<recob::PFParticle> particle = particleVector.at(ipfp);
274  if (!particle->IsPrimary()) continue;
275  n_prims++;
276  // Particles <-> Vertices
277  lar_pandora::PFParticlesToVertices::const_iterator vIter = particlesToVertices.find(particle);
278  if (particlesToVertices.end() != vIter) {
279  const lar_pandora::VertexVector& vertexVector = vIter->second;
280  if (!vertexVector.empty()) {
281  if (vertexVector.size()!=1)
282  std::cout<<" Warning: Found particle with more than one associated vertex "<<std::endl;
283 
284  const art::Ptr<recob::Vertex> vertex_pfp = *(vertexVector.begin());
285  vertex_pfp->XYZ(xyz_temp);
286  for (unsigned int ii=0; ii<3; ++ii) {
287  vtx[ii] = xyz_temp[ii];
288  }
289  //std::cout<<"Pandora vertex "<<vtx[0]<<", "<<vtx[1]<<", "<<vtx[2]<<std::endl;
290  }
291  }
292  }
293  if (fProngTagMethod==1) {
294  pm = fProducer.CreateMap(clockData, detProp, hitlist, fmwire, fmtrkhit, vtx);
295  } else {
296  std::cout<<"FIXME: CreateMap from fmshwhit and pandora vtx"<<std::endl;
297  pm = fProducer.CreateMap(clockData, detProp, hitlist, fmwire, fmshwhit, vtx);
298  }
299  }
300  else {
301  // create pixel map based on the reconstructed vertex on wire and tick coordinate
302  // Get RegCNN Results
304  auto cnnresultListHandle = evt.getHandle<std::vector<cnn::RegCNNResult>>(itag2);
305  std::vector<float> vtx(6, -99999);
306  if (!cnnresultListHandle.failedToGet())
307  {
308  if (!cnnresultListHandle->empty())
309  {
310  const std::vector<float>& v = (*cnnresultListHandle)[0].fOutput;
311  for (unsigned int ii = 0; ii < 6; ii++){
312  vtx[ii] = v[ii];
313  }
314  }
315  }
316  if (fProngTagMethod==1) {
317  pm = fProducer.CreateMap(clockData, detProp, hitlist, fmwire, fmtrkhit, vtx);
318  } else {
319  pm = fProducer.CreateMap(clockData, detProp, hitlist, fmwire, fmshwhit, vtx);
320  }
321  }
322  // skip if PixelMap is empty
323  if (pm.fInPM) pmCol->push_back(pm);
324  } // end if fUseThreeDMap==0
325  else if (fUseThreeDMap==1) {
327  if (fUseRecoVertex==2) {
328  std::cout<<"Making 3D pixel maps ......"<<std::endl;
329  // create pixel map based on pandora vertex
330  lar_pandora::PFParticleVector particleVector;
332  lar_pandora::VertexVector vertexVector;
333  lar_pandora::PFParticlesToVertices particlesToVertices;
334  lar_pandora::LArPandoraHelper::CollectVertices(evt, fPandoraNuVertexModuleLabel, vertexVector, particlesToVertices);
335 
336  int n_prims = 0;
337  int n_pand_particles = particleVector.size();
338  //std::cout<<"n_pand_particles "<<n_pand_particles<<std::endl;
339  double xyz_temp[3] = {0.0, 0.0, 0.0};
340  std::vector<float> vtx(3, -99999);
341  for (int ipfp = 0; ipfp< n_pand_particles; ipfp++) {
342  const art::Ptr<recob::PFParticle> particle = particleVector.at(ipfp);
343  if (!particle->IsPrimary()) continue;
344  n_prims++;
345  // Particles <-> Vertices
346  lar_pandora::PFParticlesToVertices::const_iterator vIter = particlesToVertices.find(particle);
347  if (particlesToVertices.end() != vIter) {
348  const lar_pandora::VertexVector& vertexVector = vIter->second;
349  if (!vertexVector.empty()) {
350  if (vertexVector.size()!=1)
351  std::cout<<" Warning: Found particle with more than one associated vertex "<<std::endl;
352 
353  const art::Ptr<recob::Vertex> vertex_pfp = *(vertexVector.begin());
354  vertex_pfp->XYZ(xyz_temp);
355  for (unsigned int ii=0; ii<3; ++ii) {
356  vtx[ii] = xyz_temp[ii];
357  }
358  //std::cout<<"Pandora vertex "<<vtx[0]<<", "<<vtx[1]<<", "<<vtx[2]<<std::endl;
359  }
360  }
361  }
362  if (fProngTagMethod==1) {
363  pm = fProducer3D.Create3DMap(clockData, detProp, hitlist, fmSPFromHits, fmtrkhit, vtx);
364  } else {
365  pm = fProducer3D.Create3DMap(clockData, detProp, hitlist, fmSPFromHits, fmshwhit, vtx);
366  }
367  }
368  if (pm.fInPM) pm3DCol->push_back(pm);
369  }
370 
371  //pm.Print();
372  }
373  //Boundary bound = pm.Bound();
374  //}
375  if (fUseThreeDMap==0) {
376  evt.put(std::move(pmCol), fClusterPMLabel);
377  }
378  else if (fUseThreeDMap==1) {
379  evt.put(std::move(pm3DCol), fClusterPMLabel);
380  }
381  std::cout<<"Map Complete!"<<std::endl;
382 }
383 
384  //----------------------------------------------------------------------
385 
387 } // end namespace cnn
388 ////////////////////////////////////////////////////////////////////////
void XYZ(double *xyz) const
Legacy method to access vertex position, preserved to avoid breaking code. Please try to use Vertex::...
Definition: Vertex.cxx:36
RegPixelMap3D Create3DMap(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, std::vector< art::Ptr< recob::Hit > > const &cluster, art::FindManyP< recob::SpacePoint > const &fmSPFromHits, art::FindManyP< recob::Track > const &fmtrkhit, const std::vector< float > &vtx)
int fUseThreeDMap
choose to create 3D pixel maps
RegPixelMap CreateMap(detinfo::DetectorClocksData const &clockData, detinfo::DetectorPropertiesData const &detProp, std::vector< art::Ptr< recob::Hit > > const &cluster, art::FindManyP< recob::Wire > const &fmwire)
void produce(art::Event &evt)
Producer algorithm for RegPixelMap3D, input to CVN neural net.
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
std::string fPFParticleModuleLabel
RegPixelMap, basic input to CNN neural net.
Definition: RegPixelMap.h:22
std::string fHitsModuleLabel
Module lablel for input clusters.
unsigned int fInPM
Definition: RegPixelMap.h:88
std::string fShowerModuleLabel
STL namespace.
intermediate_table::const_iterator const_iterator
Producer algorithm for RegPixelMap, input to CVN neural net.
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
Particle class.
std::string fRegCNNResultLabel
std::map< art::Ptr< recob::PFParticle >, VertexVector > PFParticlesToVertices
RegPixelMap3DProducer fProducer3D
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
LArPandoraHelper class.
int fProngTagMethod
select how to tag prong
void beginJob()
Definition: Breakpoints.cc:14
RegPixelMapProducer for RegCNN modified from PixelMapProducer.h.
std::vector< art::Ptr< recob::PFParticle > > PFParticleVector
def move(depos, offset)
Definition: depos.py:107
static void CollectVertices(const art::Event &evt, const std::string &label, VertexVector &vertexVector, PFParticlesToVertices &particlesToVertices)
Collect the reconstructed PFParticles and associated Vertices from the ART event record.
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
bool IsPrimary() const
Returns whether the particle is the root of the flow.
Definition: PFParticle.h:86
RegPixelMap3D for RegCNN modified from PixelMap.h.
static void CollectPFParticles(const art::Event &evt, const std::string &label, PFParticleVector &particleVector)
Collect the reconstructed PFParticles from the ART event record.
RegPixelMap3DProducer for RegCNN modified from PixelMapProducer.h.
int fUseRecoVertex
select how to choose center of pixel map
unsigned short fMinClusterHits
Minimum number of hits for cluster to be converted to pixel map.
Defines an enumeration for cellhit classification.
std::string fPandoraNuVertexModuleLabel
RegCNNResult for RegCNN modified from Result.h.
Declaration of signal hit object.
unsigned short fWireLength
Length of pixel map in wires.
unsigned short fTdcWidth
Provides recob::Track data product.
std::string fClusterPMLabel
Instance lablel for cluster pixelmaps.
std::string fRegCNNModuleLabel
std::vector< art::Ptr< recob::Vertex > > VertexVector
Declaration of basic channel signal object.
int fGlobalWireMethod
Use unwrapped pixel maps?
RegPixelMapProducer fProducer
PixelMapProducer does the work for us.
unsigned int fInPM
Definition: RegPixelMap3D.h:47
RegPixelMap3D, input to 3D CNN neural net.
Definition: RegPixelMap3D.h:22
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
RegPixelMap for RegCNN modified from PixelMap.h.
helper function for LArPandoraInterface producer module
int bool
Definition: qglobal.h:345
EventID id() const
Definition: Event.cc:34
QTextStream & endl(QTextStream &s)
std::string fVertexModuleLabel