LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
VertexMatch_module.cc
Go to the documentation of this file.
1 //
3 // \file VertexMatch_module.cc
4 //
5 // \author joshua.spitz@yale.edu
6 //
7 // This algorithm is designed to match vertices found with a dedicated vertex finder
8 // (HarrisVertexFinder) and those found with the HoughLineFinder. A weak vertex is a
9 // vertex that has been found using a dedicated vertex finding algorithm only. A strong
10 // vertex is a vertex that has been found using a dedicated vertex finding algorithm and
11 // matched to a crossing of two or more HoughLineFinder lines.
13 #ifndef VERTEXMATCH_H
14 #define VERTEXMATCH_H
15 extern "C" {
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 }
19 #include <stdint.h>
20 #include <sstream>
21 #include <fstream>
22 #include <math.h>
23 #include <algorithm>
24 #include <vector>
25 #include <string>
26 #include <map>
27 
28 // Framework includes
33 #include "fhiclcpp/ParameterSet.h"
41 
50 
51 // ROOT includes
52 #include <TCanvas.h>
53 #include "TDatabasePDG.h"
54 #include "TSystem.h"
55 #include "TMath.h"
56 
57 namespace vertex {
58 
59  class VertexMatch : public art::EDProducer {
60 
61  public:
62 
63  explicit VertexMatch(fhicl::ParameterSet const& pset);
64  virtual ~VertexMatch();
65  void produce(art::Event& evt);
66 
67  private:
68  std::string fVertexModuleLabel;
69  std::string fHoughModuleLabel;
70  double fMaxDistance;
71  protected:
72 
73 
74 
75  };
76 }
77 
78 //------------------------------------------------------------------------------
80  : fVertexModuleLabel(pset.get< std::string >("VertexModuleLabel"))
81  , fHoughModuleLabel (pset.get< std::string >("HoughModuleLabel"))
82  , fMaxDistance (pset.get< double >("MaxDistance"))
83 {
84  produces< std::vector<recob::EndPoint2D> >();
85  produces< art::Assns<recob::EndPoint2D, recob::Hit> >();
86 }
87 
88 //------------------------------------------------------------------------------
90 {
91 }
92 
93 //------------------------------------------------------------------------------
94 bool sort_pred(const std::pair<art::Ptr<recob::Hit>,double>& left,
95  const std::pair<art::Ptr<recob::Hit>,double>& right)
96 {
97  return left.first < right.first;
98 }
99 
100 //------------------------------------------------------------------------------
101 bool sort_pred2(const std::pair<art::Ptr<recob::Hit>,double>& left,
102  const std::pair<art::Ptr<recob::Hit>,double>& right)
103 {
104  return left.second < right.second;
105 }
106 
107 //------------------------------------------------------------------------------
109 {
110 
112  evt.getByLabel(fVertexModuleLabel,vertexListHandle);
113 
115  evt.getByLabel(fHoughModuleLabel,houghListHandle);
116 
117  std::unique_ptr<std::vector<recob::EndPoint2D> > mvertexcol(new std::vector<recob::EndPoint2D>);
118  std::unique_ptr< art::Assns<recob::EndPoint2D, recob::Hit> > assn(new art::Assns<recob::EndPoint2D, recob::Hit>);
119 
121  //hits associated with a vertex
122  std::vector< art::Ptr<recob::Hit> > vHits;
123  art::PtrVector<recob::Hit> vertexhit;
124 
125  std::vector<double> weakvertexstrength; //strength of weak vertices
126  std::vector<double> strongvertexstrength; //strength of strong vertices
127  //hits associated with a hough line
128  std::vector< art::Ptr<recob::Hit> > hHits;
130 
131  //art::PtrVector< std::pair<recob::Hit,double> > matchedvertex;//vertices associated with a hough line
132 
133  std::vector< std::pair<art::Ptr<recob::Hit>, double> > matchedvertex;
134 
135  art::PtrVector<recob::Hit> strongvertex;
136  std::vector< std::pair<art::Ptr<recob::Hit>, double> > strongestvertex; //the strongest strong vertex
137 
139 
140  for(size_t ii = 0; ii < vertexListHandle->size(); ++ii){
141  art::Ptr<recob::EndPoint2D> vertex(vertexListHandle, ii);
142  vertIn.push_back(vertex);
143  }
144 
145  art::FindManyP<recob::Hit> fmh(vertexListHandle, evt, fVertexModuleLabel);
146 
148  for(size_t ii = 0; ii < houghListHandle->size(); ++ii){
149  art::Ptr<recob::Cluster> cluster(houghListHandle, ii);
150  houghIn.push_back(cluster);
151  }
152 
153  art::FindManyP<recob::Hit> fmhh(houghListHandle, evt, fHoughModuleLabel);
154 
155  uint32_t channel = 0;
156  uint32_t wire = 0;
157  geo::PlaneID planeID(999,999,999);
158  geo::WireID wireID;
159  double slope,intercept,distance;
160  double starttime, endtime;
161  int startwire, endwire;
162  double strength; //the strength of a strong vertex
163 
164  for(auto const& pid : geom->IteratePlaneIDs() ){
165  //create the vector of vertex hits
168  for(size_t v = 0; v < vertIn.size(); ++v){
169  // vHits = (*vertexIter)->Hits(p,-1);
170  // if(vHits.size() > 0){
171  // vertexhit.insert(vertexhit.end(),vHits.begin(),vHits.end());
172  // weakvertexstrength.push_back((*vertexIter)->Strength());
173  // }
174  vHits = fmh.at(v);
175  if(vHits.size() > 0){
176  art::PtrVector<recob::Hit>::const_iterator vertexhitIter = vHits.begin();
177  while (vertexhitIter != vHits.end()){
178  vertexhit.push_back((*vertexhitIter));
179  weakvertexstrength.push_back((*vertexIter)->Strength());
180  vertexhitIter++;
181  }
182  }
183 
184  }// end loop over vertIn
185 
186 
187  if(vHits.size() == 0)
188  continue;
189 
190  vHits.clear();
191  //loop over vector of hough lines and find the vertex hits that are associated with the hough line(s)
192  houghIter = houghIn.begin();
193  size_t ctr = 0;
194  while(houghIter!= houghIn.end()){
195  houghhit.clear();
196  hHits.clear();
197  planeID.Plane = 999;
198  distance = -1.;
199  //create vector of hits associated with hough line
200  hHits = fmhh.at(ctr);
201 
202  if(hHits.size() > 0){
203 
204  art::PtrVector<recob::Hit>::const_iterator hitIter = hHits.begin();
205  while (hitIter!=hHits.end()){
206  houghhit.push_back((*hitIter));
207  hitIter++;
208  }
209  }
210  if(houghhit.size()){
211  wire = houghhit[0]->WireID().Wire;
212  wireID = houghhit[0]->WireID(); //for update to EndPoint2D ... WK 4/22/13
213  planeID = wireID.planeID();
214  }
215  if(pid == planeID){
216  slope = std::atan((*houghIter)->StartAngle());
217  intercept=(*houghIter)->StartTick() - slope*(*houghIter)->StartWire();
218  for(unsigned int i=0;i < vertexhit.size(); i++){
219 
220  distance=-1;
221  wire = vertexhit[i]->WireID().Wire;
222  wireID = vertexhit[i]->WireID(); //for update to EndPoint2D ... WK 4/22/13
223 
224  starttime=(*houghIter)->StartTick();
225  endtime=(*houghIter)->EndTick();
226  startwire=(*houghIter)->StartWire();
227  endwire=(*houghIter)->EndWire();
228 
229  //require the vertices found with HarrisVertexFinder to match up with the endpoints
230  //(within a window) of a Hough line. A strong vertex matches up with at least two Hough lines.
231  if(((std::abs((int)(wire-startwire))<fMaxDistance*.0743)
232  ||(std::abs((int)(wire-endwire))<fMaxDistance*.0743)
233  )
234  &&((std::abs(vertexhit[i]->PeakTime()-starttime)<fMaxDistance)
235  ||(std::abs(vertexhit[i]->PeakTime()-endtime)<fMaxDistance)
236  ))
237  distance=(std::abs(vertexhit[i]->PeakTime()-slope*(double)wire-intercept)/(std::sqrt(pow(.0743*slope,2)+1)));
238 
239  if(distance<(fMaxDistance+vertexhit[i]->RMS())&&distance>-1)
240  matchedvertex.emplace_back(vertexhit[i],
241  weakvertexstrength[i]*std::sqrt(std::pow(std::abs(endwire-startwire)*.0743,2)
242  +std::pow(std::abs(endtime-starttime),2)));
243  //ala strongestvertex.push_back(std::pair<art::PtrVector<recob::Hit>,double>(matchedvertex[i].first,strength));
244  }
245  }
246 
247  if(vertexhit.size() == 0 || houghhit.size() == 0){
248  houghIter++;
249  ++ctr;
250  continue;
251  }
252 
253  if(vertexIter!=vertIn.end()) vertexIter++;
254  if(houghIter!=houghIn.end()){
255  houghIter++;
256  ++ctr;
257  }
258  }
259 
260  //sort matchedvertex vector to make it easy to find duplicate entries (strong vertices)
261  std::sort(matchedvertex.rbegin(), matchedvertex.rend(),sort_pred);
262 
263  // the "strength" of a strong vertex is defined as
264  // (HarrisVertexStrength*LengthofHoughLine)_1+(HarrisVertexStrength*LengthofHoughLine)_2+...
265  // ...+(HarrisVertexStrength*LengthofHoughLine)_n, where n is the number of vertices
266  // associated with a Hough Line
267  for(unsigned int i=0;i < matchedvertex.size(); i++){
268  strength=matchedvertex[i].second;
269 
270  for(unsigned int n=1;n < matchedvertex.size() && i>=n; n++)
271  if(matchedvertex[i].first==matchedvertex[i-n].first)
272  strength+=matchedvertex[i-n].second;
273 
274  strongvertexstrength.push_back(strength);
275  //make sure there is more than one Hough Line associated with the vertex
276 
277  if(strength>matchedvertex[i].second)
278  strongestvertex.emplace_back(matchedvertex[i].first,strength);
279  }
280 
281 
282  //sort the strength of the strong vertices to find the strongest vertex
283  std::sort(strongestvertex.rbegin(), strongestvertex.rend(),sort_pred2);
284  for(unsigned int i=0;i < matchedvertex.size(); i++){
285  // I think this is grabbing first item in pair, itself a pointer then grabbing first
286  // (.begin()) one of those. EC, 18-Oct-2010.
287  channel=(matchedvertex[i].first)->Channel();
288 
289  // strongvertex, despite name, is a hit vector.
290  strongvertex.push_back(matchedvertex[i].first);
291 
292  //find the strong vertices, those vertices that have been
293  // associated with more than one hough line
294  int id = 0;
295  if(i > 0){
296  if(matchedvertex[i].first==matchedvertex[i-1].first){
297  if(strongvertex[0]==(strongestvertex[0].first)&&strongestvertex.size()>0)
298  id = 4;//the strongest strong vertex is given a vertex id=4
299  else
300  id = 3;//strong vertices are given vertex id=3
301  }
302  }
303  else{
304  // weak vertices that have been associated with an endpoint of
305  // a single Hough line are given vertex id=2
306  id = 2;
307  }
308 
309  // strongvertex is a collection of hits
310  double totalQ = 0.;
311  for(size_t h = 0; h < strongvertex.size(); ++h) totalQ += strongvertex[h]->Integral();
312 
313  recob::EndPoint2D vertex((matchedvertex[i].first)->PeakTime(),
314  wireID,
315  strongvertexstrength[i],
316  id,
317  geom->View(channel),
318  totalQ);
319 
320  mvertexcol->push_back(vertex);
321 
322  util::CreateAssn(*this, evt, *(mvertexcol.get()), strongvertex, *(assn.get()));
323 
324  strongvertex.clear();
325 
326  }
327 
328  strongestvertex.clear();
329  matchedvertex.clear();
330  vertexhit.clear();
331  }// end loop over planeIDs
332 
333  evt.put(std::move(mvertexcol));
334  evt.put(std::move(assn));
335 }
336 
337 namespace vertex{
338 
340 
341 } // end of vertex namespace
342 
343 #endif // VERTEXMATCH_H
PlaneID const & planeID() const
Definition: geo_types.h:355
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:112
Encapsulate the construction of a single cyostat.
iterator begin()
Definition: PtrVector.h:223
Declaration of signal hit object.
The data type to uniquely identify a Plane.
Definition: geo_types.h:250
STL namespace.
Cluster finding and building.
bool sort_pred2(const std::pair< art::Ptr< recob::Hit >, double > &left, const std::pair< art::Ptr< recob::Hit >, double > &right)
IteratorBox< plane_id_iterator,&GeometryCore::begin_plane_id,&GeometryCore::end_plane_id > IteratePlaneIDs() const
Enables ranged-for loops on all plane IDs of the detector.
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
void produce(art::Event &evt)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
std::string fVertexModuleLabel
iterator end()
Definition: PtrVector.h:237
bool sort_pred(const std::pair< art::Ptr< recob::Hit >, double > &left, const std::pair< art::Ptr< recob::Hit >, double > &right)
bool CreateAssn(PRODUCER const &prod, art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t indx=UINT_MAX)
Creates a single one-to-one association.
PlaneID_t Plane
Index of the plane within its TPC.
Definition: geo_types.h:258
Declaration of cluster object.
View_t View(geo::PlaneID const &pid) const
Returns the view (wire orientation) on the channels of specified TPC plane.
size_type size() const
Definition: PtrVector.h:308
data_t::const_iterator const_iterator
Definition: PtrVector.h:61
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
Utility object to perform functions of association.
Encapsulate the construction of a single detector plane.
bool getByLabel(std::string const &label, std::string const &productInstanceName, Handle< PROD > &result) const
Definition: DataViewImpl.h:344
VertexMatch(fhicl::ParameterSet const &pset)
Char_t n[5]
void clear()
Definition: PtrVector.h:537
art framework interface to geometry description
Encapsulate the construction of a single detector plane.
vertex reconstruction