All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackStitcher_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // \file TrackStitcher
4 //
5 // \author echurch@fnal.gov
6 //
7 // This algorithm is designed to join tracks that point in roughly same direction
8 // and whose endpoints are suitably close.
9 ////////////////////////////////////////////////////////////////////////
10 
11 // C++ includes
12 #include <ostream>
13 #include <utility>
14 
15 // Framework includes
16 #include "art/Framework/Principal/Event.h"
17 #include "fhiclcpp/ParameterSet.h"
18 #include "art/Framework/Principal/Handle.h"
19 #include "canvas/Persistency/Common/Ptr.h"
20 #include "canvas/Persistency/Common/PtrVector.h"
21 #include "art/Framework/Services/Registry/ServiceHandle.h"
22 #include "messagefacility/MessageLogger/MessageLogger.h"
23 
24 // LArSoft includes
31 
32 #include "art/Framework/Core/ModuleMacros.h"
33 #include "art/Framework/Core/EDProducer.h"
34 #include "canvas/Persistency/Common/FindManyP.h"
35 
36 #include <vector>
37 #include <string>
38 
39 namespace trkf {
40 
41  class TrackStitcher : public art::EDProducer {
42  public:
43  explicit TrackStitcher(fhicl::ParameterSet const& pset);
44 
45  private:
46  void produce(art::Event& evt) override;
47 
48  art::PtrVector<recob::Hit> GetHitsFromComponentTracks(const art::PtrVector<recob::Track> &, const art::Event& evt);
49  art::PtrVector<recob::SpacePoint> GetSpacePointsFromComponentTracks(const art::PtrVector<recob::Track> &, const art::Event& evt);
50  std::vector<art::Ptr<recob::Hit>> GetHitsFromAssdSpacePoints(const art::PtrVector<recob::SpacePoint> &, const art::Event& evt, std::vector<std::pair<std::vector<art::Ptr<recob::Hit> >::const_iterator, std::vector<art::Ptr<recob::Hit> >::const_iterator > > &vpi);
51  std::string fTrackModuleLabel;// label for input collection
52  std::string fSpptModuleLabel;// label for input collection
53  bool fStizatch; // CommonComponentStitch
55 
56  }; // class TrackStitcher
57 
58 } // end namespace for declarations
59 
60 namespace trkf {
61 
62  //-------------------------------------------------
63  TrackStitcher::TrackStitcher(fhicl::ParameterSet const& pset) :
64  EDProducer{pset},
65  fStitchAlg(pset.get< fhicl::ParameterSet >("StitchAlg"))
66  {
67  fTrackModuleLabel = pset.get< std::string >("TrackModuleLabel");
68  fSpptModuleLabel = pset.get< std::string >("SpptModuleLabel");
69  fStizatch = pset.get< bool > ("CommonComponentStitch",true);
70 
71  produces< std::vector<recob::Track> >();
72  produces<std::vector<art::PtrVector<recob::Track> > >();
73  produces<art::Assns<recob::Track, recob::Hit> >();
74  produces<art::Assns<recob::Track, recob::SpacePoint> >();
75  produces<art::Assns<recob::SpacePoint, recob::Hit> >();
76 
77  }
78 
79  //------------------------------------------------------------------------------------//
80  void TrackStitcher::produce(art::Event& evt)
81  {
82 
83  // get services
84  art::ServiceHandle<geo::Geometry const> geom;
85 
86  //////////////////////////////////////////////////////
87  // Make a std::unique_ptr<> for the thing you want to put into the event
88  //////////////////////////////////////////////////////
89  // tcol is the collection of new tracks
90  std::unique_ptr<std::vector<recob::Track> > tcol(new std::vector<recob::Track>);
91  std::unique_ptr<art::PtrVector<recob::SpacePoint> > scol(new art::PtrVector<recob::SpacePoint>);
92  // tvcol is the collection of vectors that comprise each tcol
93  std::unique_ptr<std::vector< art::PtrVector<recob::Track> > > tvcol(new std::vector< art::PtrVector<recob::Track> >);
94  std::unique_ptr< art::Assns<recob::Track, recob::Hit> > thassn(new art::Assns<recob::Track, recob::Hit>);
95  std::unique_ptr< art::Assns<recob::Track, recob::SpacePoint> > tsptassn(new art::Assns<recob::Track, recob::SpacePoint>);
96  std::unique_ptr< art::Assns<recob::SpacePoint, recob::Hit > > spthassn(new art::Assns<recob::SpacePoint, recob::Hit>);
97 
98 
99  // Get the original Spacepoints. Trackers other than CosmicTracker wrote the
100  // SpacePoints as a PtrVec of vecs. If they look like that, flatten into one vec.
101 
102  art::Handle< std::vector < recob::SpacePoint > > sppth;
103  try
104  {
105  mf::LogWarning("TrackStitcher") << "Trying to read Track3DKalmanXYZ-style PtrVector of std::vector of SpacePoints" << std::endl;
106  art::Handle< std::vector < art::PtrVector <recob::SpacePoint> > > sppth;
107  evt.getByLabel(fSpptModuleLabel, sppth);
108  for (size_t ii=0; ii<sppth->size() ;ii++)
109  for (size_t jj=0; jj<sppth->at(ii).size() ;ii++)
110  {
111  art::Ptr<recob::SpacePoint> sptmp(sppth->at(ii).at(jj));
112  scol->push_back(sptmp );
113  }
114  }
115  catch(...)
116  {
117  mf::LogWarning("TrackStitcher") << "Trying instead to read CosmicTracker-style already-flattened vector of SpacePoints" << std::endl;
118  art::Handle< std::vector < recob::SpacePoint > > sppthf;
119  evt.getByLabel(fSpptModuleLabel, sppthf);
120  for (size_t ii=0; ii<sppthf->size() ;ii++)
121  {
122  art::Ptr<recob::SpacePoint> sptmpf(sppthf,ii);
123  scol->push_back(sptmpf);
124  }
125 
126  }
127 
128 
129  // Find the best match for each track's Head and Tail.
131  // walk through each vertex of one track to its match on another, and so on and stitch 'em.
133  // search composite tracks and stitch further if there are components in common. Do it until all are stitched.
134  bool stizatch(fStizatch);
135  while (stizatch)
136  {
137  stizatch = fStitchAlg.CommonComponentStitch();
138  }
139  mf::LogVerbatim("TrackStitcher.beginning") << "There are " << fStitchAlg.ftListHandle->size() << " Tracks in this event before stitching.";
140 
141  fStitchAlg.GetTracks(*tcol);
143 
144  if (tcol->size()!=tvcol->size())
145  throw cet::exception("TrackStitcher") << "Tracks and TrackComposites do not match: "<<tcol->size()<<" vs "<<tvcol->size()<<"\n";
146 
147  std::vector<size_t> spIndices(scol->size());
148  // create spIndices, index array for searching into original scol SpacePoints.
149  for ( size_t ii=0; ii<scol->size(); ii++ )
150  {spIndices[ii]=ii;}
151 
152  for (size_t ii=0; ii<tvcol->size(); ii++)
153  {
154  const art::PtrVector<recob::Hit>& hits(GetHitsFromComponentTracks(tvcol->at(ii), evt));
155  // Now make the Assns of relevant Hits to stitched Track
156  util::CreateAssn(*this, evt, *tcol, hits, *thassn, ii);
157  const art::PtrVector<recob::SpacePoint>& sppts(GetSpacePointsFromComponentTracks(tvcol->at(ii), evt));
158  // Now make the Assns of relevant Sppts to stitched Track
159  util::CreateAssn(*this, evt, *tcol, sppts, *tsptassn, ii);
160 
161  // Now Assns of sppts to hits. For this Sppt
162  // I call the function to bring back the vec of associated Hits and the vector of
163  // pairs of iterators that allow to pull those Hits needed from each Sppt.
164  std::vector<std::pair<std::vector<art::Ptr<recob::Hit> >::const_iterator, std::vector<art::Ptr<recob::Hit> >::const_iterator > > pits;
165 
166  std::vector<art::Ptr<recob::Hit>> hitsFromSppts;
167  art::FindManyP<recob::Hit> hitAssns(sppts, evt, fSpptModuleLabel);
168 
169  size_t start(0), finish(0);
170  for (unsigned int ii=0; ii < sppts.size(); ++ii )
171  {
172  hitsFromSppts.insert(hitsFromSppts.end(),hitAssns.at(ii).begin(), hitAssns.at(ii).end());
173  finish = start+(size_t)(hitAssns.at(ii).end() - hitAssns.at(ii).begin());
174  std::pair< std::vector<art::Ptr<recob::Hit> >::const_iterator, std::vector<art::Ptr<recob::Hit> >::const_iterator > pithittmp(hitAssns.at(ii).begin(),hitAssns.at(ii).end());
175  pits.push_back(pithittmp);
176  start += (finish+1);
177  }
178  // std::cout << "TrackStitcher_module: scol->size() is " << scol->size() << std::endl;
179  // std::cout << "TrackStitcher_module: sppts.size() is " << sppts.size() << std::endl;
180  for ( size_t jj=0; jj<sppts.size(); jj++ )
181  {
182  // find jjth sppt in the list of scol. Meaning, find kkth element of sppth.
183  size_t ll(scol->size());
184  // this gives indices into the vector of original spacepoints in which
185  // to look for our sppts.
186  size_t off(0);
187  for ( auto& kk : spIndices )
188  {
189  const art::Ptr<recob::SpacePoint> spptnc(scol->at(kk));
190  if ( spptnc != sppts.at(jj)) { off++; continue;}
191  ll = kk;
192  // std::cout << "TrackStitcher_module: index into spacepoints for which to write out sppt-hit Assns is " << ll << std::endl;
193  // drop this one for future searches, since we've used it.
194 
195  break;
196  }
197  if (ll<scol->size())
198  {
199  std::vector <art::Ptr <recob::Hit> > hitsThisSppt;
200  hitsThisSppt.insert(hitsThisSppt.begin(),pits.at(jj).first,pits.at(jj).second);
201  util::CreateAssn(*this, evt, scol->at(ll), hitsThisSppt, *spthassn);
202  }
203  }
204  }
205 
206 
207  mf::LogVerbatim("TrackStitcher.end") << "There are " << tvcol->size() << " Tracks in this event after stitching.";
208  evt.put(std::move(tcol));
209  evt.put(std::move(tvcol));
210  // Add Hit-to-Track and Sppt-to-Track Assns.
211  evt.put(std::move(thassn));
212  evt.put(std::move(tsptassn));
213  evt.put(std::move(spthassn));
214 
215  }
216 
217  art::PtrVector<recob::Hit> TrackStitcher::GetHitsFromComponentTracks(const art::PtrVector<recob::Track> &tcomp, const art::Event& evtGHFCT)
218  {
219 
220  art::PtrVector<recob::Hit> hits;
221  art::FindManyP<recob::Hit> hitAssns(tcomp, evtGHFCT, fTrackModuleLabel);
222 
223  for (unsigned int ii=0; ii < tcomp.size(); ++ii )
224  {
225  hits.insert(hits.end(),hitAssns.at(ii).begin(), hitAssns.at(ii).end());
226  }
227 
228 
229  // const art::PtrVector<recob::Hit> chits(hits);
230  return hits;
231  }
232 
233  art::PtrVector<recob::SpacePoint> TrackStitcher::GetSpacePointsFromComponentTracks(const art::PtrVector<recob::Track> &tcomp, const art::Event& evtGHFCT)
234  {
235 
236  art::PtrVector<recob::SpacePoint> sppts;
237  art::FindManyP<recob::SpacePoint> spptAssns(tcomp, evtGHFCT, fTrackModuleLabel);
238  for (unsigned int ii=0; ii < tcomp.size(); ++ii )
239  {
240  sppts.insert(sppts.end(),spptAssns.at(ii).begin(), spptAssns.at(ii).end());
241  }
242 
243  // const art::PtrVector<recob::Hit> chits(hits);
244  return sppts;
245  }
246 
247  std::vector<art::Ptr<recob::Hit>> TrackStitcher::GetHitsFromAssdSpacePoints(const art::PtrVector<recob::SpacePoint> &sppts, const art::Event& evtGHFCT, std::vector<std::pair<std::vector<art::Ptr<recob::Hit> >::const_iterator, std::vector<art::Ptr<recob::Hit> >::const_iterator > > &pithit)
248  {
249 
250  std::vector<art::Ptr<recob::Hit>> hits;
251  art::FindManyP<recob::Hit> hitAssns(sppts, evtGHFCT, fSpptModuleLabel);
252 
253  size_t start(0), finish(0);
254  for (unsigned int ii=0; ii < sppts.size(); ++ii )
255  {
256  hits.insert(hits.end(),hitAssns.at(ii).begin(), hitAssns.at(ii).end());
257  finish = start+(size_t)(hitAssns.at(ii).end() - hitAssns.at(ii).begin());
258  std::pair< std::vector<art::Ptr<recob::Hit> >::const_iterator, std::vector<art::Ptr<recob::Hit> >::const_iterator > pithittmp(hitAssns.at(ii).begin(),hitAssns.at(ii).end());
259  pithit.push_back(pithittmp);
260  start += (finish+1);
261  }
262 
263  return hits;
264  }
265 
266  DEFINE_ART_MODULE(TrackStitcher)
267 
268 
269 } // end namespace
std::vector< art::Ptr< recob::Hit > > GetHitsFromAssdSpacePoints(const art::PtrVector< recob::SpacePoint > &, const art::Event &evt, std::vector< std::pair< std::vector< art::Ptr< recob::Hit > >::const_iterator, std::vector< art::Ptr< recob::Hit > >::const_iterator > > &vpi)
void GetTracks(std::vector< recob::Track > &t) const
Definition: StitchAlg.h:43
art::Handle< std::vector< recob::Track > > ftListHandle
Definition: StitchAlg.h:45
void FindHeadsAndTails(const art::Event &e, const std::string &t)
Definition: StitchAlg.cxx:45
bool CommonComponentStitch()
Definition: StitchAlg.cxx:478
Declaration of signal hit object.
art::PtrVector< recob::Hit > GetHitsFromComponentTracks(const art::PtrVector< recob::Track > &, const art::Event &evt)
void GetTrackComposites(std::vector< art::PtrVector< recob::Track > > &c) const
Definition: StitchAlg.h:42
std::size_t size(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:561
TrackStitcher(fhicl::ParameterSet const &pset)
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
Provides recob::Track data product.
bool CreateAssn(art::Event &evt, std::vector< T > const &a, art::Ptr< U > const &b, art::Assns< U, T > &assn, std::string a_instance, size_t index=UINT_MAX)
Creates a single one-to-one association.
art::PtrVector< recob::SpacePoint > GetSpacePointsFromComponentTracks(const art::PtrVector< recob::Track > &, const art::Event &evt)
TCEvent evt
Definition: DataStructs.cxx:8
art framework interface to geometry description
void produce(art::Event &evt) override