LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 <math.h>
13 #include <iostream>
14 #include <fstream>
15 #include <algorithm>
16 #include <map>
17 #include <tuple>
18 
19 // Framework includes
21 #include "fhiclcpp/ParameterSet.h"
30 
31 // LArSoft includes
42 
47 #include <TTree.h>
48 #include <TMatrixT.h>
49 
50 
51 #include <vector>
52 #include <string>
53 
54 
55 // ROOT includes
56 #include "TVectorD.h"
57 #include "TFile.h"
58 #include "TGeoManager.h"
59 #include "TF1.h"
60 #include "TGraph.h"
61 #include "TMath.h"
62 #include "TPrincipal.h"
63 #include "TDatabasePDG.h"
64 
65 class StitchAlg;
66 
67 namespace trkf {
68 
69  class TrackStitcher : public art::EDProducer {
70 
71  public:
72 
73  explicit TrackStitcher(fhicl::ParameterSet const& pset);
74  virtual ~TrackStitcher();
75 
77  void produce(art::Event& evt);
78  void beginJob();
79  void endJob();
80  void reconfigure(fhicl::ParameterSet const& p);
81 
82  private:
83 
87  std::string fTrackModuleLabel;// label for input collection
88  std::string fSpptModuleLabel;// label for input collection
89  bool fStizatch; // CommonComponentStitch
91 
92  }; // class TrackStitcher
93 
94 } // end namespace for declarations
95 
96 namespace trkf {
97 
98  //-------------------------------------------------
100  fStitchAlg(pset.get< fhicl::ParameterSet >("StitchAlg"))
101  {
102 
103  this->reconfigure(pset);
104 
105  produces< std::vector<recob::Track> >();
106  produces<std::vector<art::PtrVector<recob::Track> > >();
107  produces<art::Assns<recob::Track, recob::Hit> >();
108  produces<art::Assns<recob::Track, recob::SpacePoint> >();
109  produces<art::Assns<recob::SpacePoint, recob::Hit> >();
110 
111  }
112 
113  //-------------------------------------------------
115  {
116  fTrackModuleLabel = pset.get< std::string >("TrackModuleLabel");
117  fSpptModuleLabel = pset.get< std::string >("SpptModuleLabel");
118  fStizatch = pset.get< bool > ("CommonComponentStitch",true);
119  fStitchAlg.reconfigure(pset.get< fhicl::ParameterSet >("StitchAlg"));
120  }
121 
122  //-------------------------------------------------
124  {
125  }
126 
127 
128  //-------------------------------------------------
130  {
132  }
133 
134  //-------------------------------------------------
136  {
137  }
138 
139 
140  //------------------------------------------------------------------------------------//
142  {
143 
144  // get services
146 
148  // Make a std::unique_ptr<> for the thing you want to put into the event
150  // tcol is the collection of new tracks
151  std::unique_ptr<std::vector<recob::Track> > tcol(new std::vector<recob::Track>);
152  std::unique_ptr<art::PtrVector<recob::SpacePoint> > scol(new art::PtrVector<recob::SpacePoint>);
153  // tvcol is the collection of vectors that comprise each tcol
154  std::unique_ptr<std::vector< art::PtrVector<recob::Track> > > tvcol(new std::vector< art::PtrVector<recob::Track> >);
155  std::unique_ptr< art::Assns<recob::Track, recob::Hit> > thassn(new art::Assns<recob::Track, recob::Hit>);
156  std::unique_ptr< art::Assns<recob::Track, recob::SpacePoint> > tsptassn(new art::Assns<recob::Track, recob::SpacePoint>);
157  std::unique_ptr< art::Assns<recob::SpacePoint, recob::Hit > > spthassn(new art::Assns<recob::SpacePoint, recob::Hit>);
158 
159 
160  // Get the original Spacepoints. Trackers other than CosmicTracker wrote the
161  // SpacePoints as a PtrVec of vecs. If they look like that, flatten into one vec.
162 
164  try
165  {
166  mf::LogWarning("TrackStitcher") << "Trying to read Track3DKalmanXYZ-style PtrVector of std::vector of SpacePoints" << std::endl;
168  evt.getByLabel(fSpptModuleLabel, sppth);
169  for (size_t ii=0; ii<sppth->size() ;ii++)
170  for (size_t jj=0; jj<sppth->at(ii).size() ;ii++)
171  {
172  art::Ptr<recob::SpacePoint> sptmp(sppth->at(ii).at(jj));
173  scol->push_back(sptmp );
174  }
175  }
176  catch(...)
177  {
178  mf::LogWarning("TrackStitcher") << "Trying instead to read CosmicTracker-style already-flattened vector of SpacePoints" << std::endl;
180  evt.getByLabel(fSpptModuleLabel, sppthf);
181  for (size_t ii=0; ii<sppthf->size() ;ii++)
182  {
183  art::Ptr<recob::SpacePoint> sptmpf(sppthf,ii);
184  scol->push_back(sptmpf);
185  }
186 
187  }
188 
189 
190  // Find the best match for each track's Head and Tail.
192  // walk through each vertex of one track to its match on another, and so on and stitch 'em.
194  // search composite tracks and stitch further if there are components in common. Do it until all are stitched.
195  bool stizatch(fStizatch);
196  while (stizatch)
197  {
198  stizatch = fStitchAlg.CommonComponentStitch();
199  }
200  mf::LogVerbatim("TrackStitcher.beginning") << "There are " << fStitchAlg.ftListHandle->size() << " Tracks in this event before stitching.";
201 
202  fStitchAlg.GetTracks(*tcol);
204 
205  if (tcol->size()!=tvcol->size())
206  throw cet::exception("TrackStitcher") << "Tracks and TrackComposites do not match: "<<tcol->size()<<" vs "<<tvcol->size()<<"\n";
207 
208  std::vector<size_t> spIndices(scol->size());
209  // create spIndices, index array for searching into original scol SpacePoints.
210  for ( size_t ii=0; ii<scol->size(); ii++ )
211  {spIndices[ii]=ii;}
212 
213  for (size_t ii=0; ii<tvcol->size(); ii++)
214  {
216  // Now make the Assns of relevant Hits to stitched Track
217  util::CreateAssn(*this, evt, *tcol, hits, *thassn, ii);
219  // Now make the Assns of relevant Sppts to stitched Track
220  util::CreateAssn(*this, evt, *tcol, sppts, *tsptassn, ii);
221 
222  // Now Assns of sppts to hits. For this Sppt
223  // I call the function to bring back the vec of associated Hits and the vector of
224  // pairs of iterators that allow to pull those Hits needed from each Sppt.
225  std::vector<std::pair<std::vector<art::Ptr<recob::Hit> >::const_iterator, std::vector<art::Ptr<recob::Hit> >::const_iterator > > pits;
226 
227  std::vector<art::Ptr<recob::Hit>> hitsFromSppts;
228  art::FindManyP<recob::Hit> hitAssns(sppts, evt, fSpptModuleLabel);
229 
230  size_t start(0), finish(0);
231  for (unsigned int ii=0; ii < sppts.size(); ++ii )
232  {
233  hitsFromSppts.insert(hitsFromSppts.end(),hitAssns.at(ii).begin(), hitAssns.at(ii).end());
234  finish = start+(size_t)(hitAssns.at(ii).end() - hitAssns.at(ii).begin());
235  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());
236  pits.push_back(pithittmp);
237  start += (finish+1);
238  }
239  // std::cout << "TrackStitcher_module: scol->size() is " << scol->size() << std::endl;
240  // std::cout << "TrackStitcher_module: sppts.size() is " << sppts.size() << std::endl;
241  for ( size_t jj=0; jj<sppts.size(); jj++ )
242  {
243  // find jjth sppt in the list of scol. Meaning, find kkth element of sppth.
244  size_t ll(scol->size());
245  // this gives indices into the vector of original spacepoints in which
246  // to look for our sppts.
247  size_t off(0);
248  for ( auto& kk : spIndices )
249  {
250  const art::Ptr<recob::SpacePoint> spptnc(scol->at(kk));
251  if ( spptnc != sppts.at(jj)) { off++; continue;}
252  ll = kk;
253  // std::cout << "TrackStitcher_module: index into spacepoints for which to write out sppt-hit Assns is " << ll << std::endl;
254  // drop this one for future searches, since we've used it.
255 
256  break;
257  }
258  if (ll<scol->size())
259  {
260  std::vector <art::Ptr <recob::Hit> > hitsThisSppt;
261  hitsThisSppt.insert(hitsThisSppt.begin(),pits.at(jj).first,pits.at(jj).second);
262  util::CreateAssn(*this, evt, scol->at(ll), hitsThisSppt, *spthassn);
263  }
264  }
265  }
266 
267 
268  mf::LogVerbatim("TrackStitcher.end") << "There are " << tvcol->size() << " Tracks in this event after stitching.";
269  evt.put(std::move(tcol));
270  evt.put(std::move(tvcol));
271  // Add Hit-to-Track and Sppt-to-Track Assns.
272  evt.put(std::move(thassn));
273  evt.put(std::move(tsptassn));
274  evt.put(std::move(spthassn));
275 
276  }
277 
279  {
280 
282  art::FindManyP<recob::Hit> hitAssns(tcomp, evtGHFCT, fTrackModuleLabel);
283 
284  for (unsigned int ii=0; ii < tcomp.size(); ++ii )
285  {
286  hits.insert(hits.end(),hitAssns.at(ii).begin(), hitAssns.at(ii).end());
287  }
288 
289 
290  // const art::PtrVector<recob::Hit> chits(hits);
291  return hits;
292  }
293 
295  {
296 
298  art::FindManyP<recob::SpacePoint> spptAssns(tcomp, evtGHFCT, fTrackModuleLabel);
299  for (unsigned int ii=0; ii < tcomp.size(); ++ii )
300  {
301  sppts.insert(sppts.end(),spptAssns.at(ii).begin(), spptAssns.at(ii).end());
302  }
303 
304  // const art::PtrVector<recob::Hit> chits(hits);
305  return sppts;
306  }
307 
309  {
310 
311  std::vector<art::Ptr<recob::Hit>> hits;
312  art::FindManyP<recob::Hit> hitAssns(sppts, evtGHFCT, fSpptModuleLabel);
313 
314  size_t start(0), finish(0);
315  for (unsigned int ii=0; ii < sppts.size(); ++ii )
316  {
317  hits.insert(hits.end(),hitAssns.at(ii).begin(), hitAssns.at(ii).end());
318  finish = start+(size_t)(hitAssns.at(ii).end() - hitAssns.at(ii).begin());
319  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());
320  pithit.push_back(pithittmp);
321  start += (finish+1);
322  }
323 
324  return hits;
325  }
326 
328 
329 
330 } // end namespace
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
art::Handle< std::vector< recob::Track > > ftListHandle
Definition: StitchAlg.h:43
void FindHeadsAndTails(const art::Event &e, const std::string &t)
Definition: StitchAlg.cxx:54
bool CommonComponentStitch()
Definition: StitchAlg.cxx:488
Declaration of signal hit object.
TrackStitcher(fhicl::ParameterSet const &pset)
const 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)
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
const art::PtrVector< recob::Hit > GetHitsFromComponentTracks(const art::PtrVector< recob::Track > &, const art::Event &evt)
void hits()
Definition: readHits.C:15
intermediate_table::const_iterator const_iterator
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
Provides recob::Track data product.
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:231
iterator end()
Definition: PtrVector.h:237
void reconfigure(fhicl::ParameterSet const &pset)
Definition: StitchAlg.cxx:46
const art::PtrVector< recob::SpacePoint > GetSpacePointsFromComponentTracks(const art::PtrVector< recob::Track > &, const art::Event &evt)
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.
void GetTrackComposites(std::vector< art::PtrVector< recob::Track > > &c)
Definition: StitchAlg.h:42
reference at(size_type n)
Definition: PtrVector.h:365
size_type size() const
Definition: PtrVector.h:308
void reconfigure(fhicl::ParameterSet const &p)
Encapsulate the geometry of a wire.
void produce(art::Event &evt)
iterator insert(iterator position, Ptr< U > const &p)
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
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
TCEvent evt
Definition: DataStructs.cxx:5
Tools and modules for checking out the basics of the Monte Carlo.
art framework interface to geometry description
void GetTracks(std::vector< recob::Track > &t)
Definition: StitchAlg.h:43
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33