LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
TrackStitcher_module.cc
Go to the documentation of this file.
1 //
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.
10 
11 // C++ includes
12 #include <ostream>
13 #include <utility>
14 
15 // Framework includes
21 #include "fhiclcpp/ParameterSet.h"
23 
24 // LArSoft includes
31 
35 
36 #include <string>
37 #include <vector>
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 
49  const art::Event& evt);
52  const art::Event& evt);
53  std::vector<art::Ptr<recob::Hit>> GetHitsFromAssdSpacePoints(
55  const art::Event& evt,
58  std::string fTrackModuleLabel; // label for input collection
59  std::string fSpptModuleLabel; // label for input collection
60  bool fStizatch; // CommonComponentStitch
62 
63  }; // class TrackStitcher
64 
65 } // end namespace for declarations
66 
67 namespace trkf {
68 
69  //-------------------------------------------------
71  : EDProducer{pset}, fStitchAlg(pset.get<fhicl::ParameterSet>("StitchAlg"))
72  {
73  fTrackModuleLabel = pset.get<std::string>("TrackModuleLabel");
74  fSpptModuleLabel = pset.get<std::string>("SpptModuleLabel");
75  fStizatch = pset.get<bool>("CommonComponentStitch", true);
76 
77  produces<std::vector<recob::Track>>();
78  produces<std::vector<art::PtrVector<recob::Track>>>();
79  produces<art::Assns<recob::Track, recob::Hit>>();
80  produces<art::Assns<recob::Track, recob::SpacePoint>>();
81  produces<art::Assns<recob::SpacePoint, recob::Hit>>();
82  }
83 
84  //------------------------------------------------------------------------------------//
86  {
87 
88  // get services
90 
92  // Make a std::unique_ptr<> for the thing you want to put into the event
94  // tcol is the collection of new tracks
95  std::unique_ptr<std::vector<recob::Track>> tcol(new std::vector<recob::Track>);
96  std::unique_ptr<art::PtrVector<recob::SpacePoint>> scol(new art::PtrVector<recob::SpacePoint>);
97  // tvcol is the collection of vectors that comprise each tcol
98  std::unique_ptr<std::vector<art::PtrVector<recob::Track>>> tvcol(
100  std::unique_ptr<art::Assns<recob::Track, recob::Hit>> thassn(
102  std::unique_ptr<art::Assns<recob::Track, recob::SpacePoint>> tsptassn(
104  std::unique_ptr<art::Assns<recob::SpacePoint, recob::Hit>> spthassn(
106 
107  // Get the original Spacepoints. Trackers other than CosmicTracker wrote the
108  // SpacePoints as a PtrVec of vecs. If they look like that, flatten into one vec.
109 
111  try {
112  mf::LogWarning("TrackStitcher")
113  << "Trying to read Track3DKalmanXYZ-style PtrVector of std::vector of SpacePoints"
114  << std::endl;
116  evt.getByLabel(fSpptModuleLabel, sppth);
117  for (size_t ii = 0; ii < sppth->size(); ii++)
118  for (size_t jj = 0; jj < sppth->at(ii).size(); ii++) {
119  art::Ptr<recob::SpacePoint> sptmp(sppth->at(ii).at(jj));
120  scol->push_back(sptmp);
121  }
122  }
123  catch (...) {
124  mf::LogWarning("TrackStitcher")
125  << "Trying instead to read CosmicTracker-style already-flattened vector of SpacePoints"
126  << std::endl;
128  evt.getByLabel(fSpptModuleLabel, sppthf);
129  for (size_t ii = 0; ii < sppthf->size(); ii++) {
130  art::Ptr<recob::SpacePoint> sptmpf(sppthf, ii);
131  scol->push_back(sptmpf);
132  }
133  }
134 
135  // Find the best match for each track's Head and Tail.
137  // walk through each vertex of one track to its match on another, and so on and stitch 'em.
139  // search composite tracks and stitch further if there are components in common. Do it until all are stitched.
140  bool stizatch(fStizatch);
141  while (stizatch) {
142  stizatch = fStitchAlg.CommonComponentStitch();
143  }
144  mf::LogVerbatim("TrackStitcher.beginning") << "There are " << fStitchAlg.ftListHandle->size()
145  << " Tracks in this event before stitching.";
146 
147  fStitchAlg.GetTracks(*tcol);
149 
150  if (tcol->size() != tvcol->size())
151  throw cet::exception("TrackStitcher")
152  << "Tracks and TrackComposites do not match: " << tcol->size() << " vs " << tvcol->size()
153  << "\n";
154 
155  std::vector<size_t> spIndices(scol->size());
156  // create spIndices, index array for searching into original scol SpacePoints.
157  for (size_t ii = 0; ii < scol->size(); ii++) {
158  spIndices[ii] = ii;
159  }
160 
161  for (size_t ii = 0; ii < tvcol->size(); ii++) {
163  // Now make the Assns of relevant Hits to stitched Track
164  util::CreateAssn(evt, *tcol, hits, *thassn, ii);
166  GetSpacePointsFromComponentTracks(tvcol->at(ii), evt));
167  // Now make the Assns of relevant Sppts to stitched Track
168  util::CreateAssn(evt, *tcol, sppts, *tsptassn, ii);
169 
170  // Now Assns of sppts to hits. For this Sppt
171  // I call the function to bring back the vec of associated Hits and the vector of
172  // pairs of iterators that allow to pull those Hits needed from each Sppt.
173  std::vector<std::pair<std::vector<art::Ptr<recob::Hit>>::const_iterator,
174  std::vector<art::Ptr<recob::Hit>>::const_iterator>>
175  pits;
176 
177  std::vector<art::Ptr<recob::Hit>> hitsFromSppts;
178  art::FindManyP<recob::Hit> hitAssns(sppts, evt, fSpptModuleLabel);
179 
180  size_t start(0), finish(0);
181  for (unsigned int ii = 0; ii < sppts.size(); ++ii) {
182  hitsFromSppts.insert(hitsFromSppts.end(), hitAssns.at(ii).begin(), hitAssns.at(ii).end());
183  finish = start + (size_t)(hitAssns.at(ii).end() - hitAssns.at(ii).begin());
184  std::pair<std::vector<art::Ptr<recob::Hit>>::const_iterator,
185  std::vector<art::Ptr<recob::Hit>>::const_iterator>
186  pithittmp(hitAssns.at(ii).begin(), hitAssns.at(ii).end());
187  pits.push_back(pithittmp);
188  start += (finish + 1);
189  }
190  // std::cout << "TrackStitcher_module: scol->size() is " << scol->size() << std::endl;
191  // std::cout << "TrackStitcher_module: sppts.size() is " << sppts.size() << std::endl;
192  for (size_t jj = 0; jj < sppts.size(); jj++) {
193  // find jjth sppt in the list of scol. Meaning, find kkth element of sppth.
194  size_t ll(scol->size());
195  // this gives indices into the vector of original spacepoints in which
196  // to look for our sppts.
197  size_t off(0);
198  for (auto& kk : spIndices) {
199  const art::Ptr<recob::SpacePoint> spptnc(scol->at(kk));
200  if (spptnc != sppts.at(jj)) {
201  off++;
202  continue;
203  }
204  ll = kk;
205  // std::cout << "TrackStitcher_module: index into spacepoints for which to write out sppt-hit Assns is " << ll << std::endl;
206  // drop this one for future searches, since we've used it.
207 
208  break;
209  }
210  if (ll < scol->size()) {
211  std::vector<art::Ptr<recob::Hit>> hitsThisSppt;
212  hitsThisSppt.insert(hitsThisSppt.begin(), pits.at(jj).first, pits.at(jj).second);
213  util::CreateAssn(evt, scol->at(ll), hitsThisSppt, *spthassn);
214  }
215  }
216  }
217 
218  mf::LogVerbatim("TrackStitcher.end")
219  << "There are " << tvcol->size() << " Tracks in this event after stitching.";
220  evt.put(std::move(tcol));
221  evt.put(std::move(tvcol));
222  // Add Hit-to-Track and Sppt-to-Track Assns.
223  evt.put(std::move(thassn));
224  evt.put(std::move(tsptassn));
225  evt.put(std::move(spthassn));
226  }
227 
229  const art::PtrVector<recob::Track>& tcomp,
230  const art::Event& evtGHFCT)
231  {
232 
234  art::FindManyP<recob::Hit> hitAssns(tcomp, evtGHFCT, fTrackModuleLabel);
235 
236  for (unsigned int ii = 0; ii < tcomp.size(); ++ii) {
237  hits.insert(hits.end(), hitAssns.at(ii).begin(), hitAssns.at(ii).end());
238  }
239 
240  // const art::PtrVector<recob::Hit> chits(hits);
241  return hits;
242  }
243 
245  const art::PtrVector<recob::Track>& tcomp,
246  const art::Event& evtGHFCT)
247  {
248 
250  art::FindManyP<recob::SpacePoint> spptAssns(tcomp, evtGHFCT, fTrackModuleLabel);
251  for (unsigned int ii = 0; ii < tcomp.size(); ++ii) {
252  sppts.insert(sppts.end(), spptAssns.at(ii).begin(), spptAssns.at(ii).end());
253  }
254 
255  // const art::PtrVector<recob::Hit> chits(hits);
256  return sppts;
257  }
258 
259  std::vector<art::Ptr<recob::Hit>> TrackStitcher::GetHitsFromAssdSpacePoints(
261  const art::Event& evtGHFCT,
264  {
265 
266  std::vector<art::Ptr<recob::Hit>> hits;
267  art::FindManyP<recob::Hit> hitAssns(sppts, evtGHFCT, fSpptModuleLabel);
268 
269  size_t start(0), finish(0);
270  for (unsigned int ii = 0; ii < sppts.size(); ++ii) {
271  hits.insert(hits.end(), hitAssns.at(ii).begin(), hitAssns.at(ii).end());
272  finish = start + (size_t)(hitAssns.at(ii).end() - hitAssns.at(ii).begin());
273  std::pair<std::vector<art::Ptr<recob::Hit>>::const_iterator,
274  std::vector<art::Ptr<recob::Hit>>::const_iterator>
275  pithittmp(hitAssns.at(ii).begin(), hitAssns.at(ii).end());
276  pithit.push_back(pithittmp);
277  start += (finish + 1);
278  }
279 
280  return hits;
281  }
282 
284 
285 } // end namespace
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
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:47
art::Handle< std::vector< recob::Track > > ftListHandle
Definition: StitchAlg.h:49
void FindHeadsAndTails(const art::Event &e, const std::string &t)
Definition: StitchAlg.cxx:43
bool CommonComponentStitch()
Definition: StitchAlg.cxx:472
Declaration of signal hit object.
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
art::PtrVector< recob::Hit > GetHitsFromComponentTracks(const art::PtrVector< recob::Track > &, const art::Event &evt)
intermediate_table::const_iterator const_iterator
TrackStitcher(fhicl::ParameterSet const &pset)
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
void hits()
Definition: readHits.C:15
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
iterator end()
Definition: PtrVector.h:231
Provides recob::Track data product.
reference at(size_type n)
Definition: PtrVector.h:359
size_type size() const
Definition: PtrVector.h:302
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.
iterator insert(iterator position, Ptr< U > const &p)
art::PtrVector< recob::SpacePoint > GetSpacePointsFromComponentTracks(const art::PtrVector< recob::Track > &, const art::Event &evt)
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Utility object to perform functions of association.
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void GetTrackComposites(std::vector< art::PtrVector< recob::Track >> &c) const
Definition: StitchAlg.h:43
TCEvent evt
Definition: DataStructs.cxx:8
art framework interface to geometry description
void produce(art::Event &evt) override
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33