LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PMAlgTrajFitter_module.cc
Go to the documentation of this file.
1 // Class: PMAlgTrajFitter
3 // Module Type: producer
4 // File: PMAlgTrajFitter_module.cc
5 // Author: D.Stefan (Dorota.Stefan@ncbj.gov.pl) and R.Sulej (Robert.Sulej@cern.ch), May 2015
6 //
7 // Creates 3D tracks using Projection Matching Algorithm, please see
8 // RecoAlg/ProjectionMatchingAlg.h for basics of the PMA algorithm and its settings.
9 //
10 // Progress:
11 // July 2016: fit-only module separated from PMAlgTrackMaker_module, common tracking
12 // functionality moved to algorithm class;
13 // note, that some part of vertexing can be still reasonable in this module:
14 // - use hierarchy of input PFP's to create vertices, join tracks, reoptimize
15 // - detect vertices/kinks on fitted trajectories
16 //
18 
23 #include "fhiclcpp/types/Atom.h"
24 #include "fhiclcpp/types/Table.h"
26 
27 // LArSoft includes
40 
42 
43 #include <memory>
44 
45 namespace trkf {
46 
48  public:
49  struct Config {
50  using Name = fhicl::Name;
52 
54  Name("ProjectionMatchingAlg")};
55 
57 
59 
61  Name("HitModuleLabel"),
62  Comment("tag of unclustered hits, which were used to produce PFPs and clusters")};
63 
65  Name("PfpModuleLabel"),
66  Comment("tag of the input PFParticles and associated clusters")};
67 
69  Name("SaveOnlyBranchingVtx"),
70  Comment("use true to save only vertices interconnecting many tracks, otherwise vertex is "
71  "added to the front of each track")};
72 
74  Name("SavePmaNodes"),
75  Comment("save track nodes (only for algorithm development purposes)")};
76  };
78 
79  explicit PMAlgTrajFitter(Parameters const& config);
80 
81  PMAlgTrajFitter(PMAlgTrajFitter const&) = delete;
82  PMAlgTrajFitter(PMAlgTrajFitter&&) = delete;
83  PMAlgTrajFitter& operator=(PMAlgTrajFitter const&) = delete;
85 
86  private:
87  void produce(art::Event& e) override;
88 
89  // ******************** fcl parameters ***********************
90  art::InputTag fHitModuleLabel; // tag for unclustered hit collection
91  art::InputTag fPfpModuleLabel; // tag for PFParticle and cluster collections
92 
96 
97  bool fSaveOnlyBranchingVtx; // for debugging, save only vertices which connect many tracks
98  bool fSavePmaNodes; // for debugging, save only track nodes
99 
100  // ********** instance names (collections, assns) ************
101  static const std::string kKinksName; // kinks on tracks
102  static const std::string kNodesName; // pma nodes
103 
104  // *********************** geometry **************************
106  };
107  // -------------------------------------------------------------
108  const std::string PMAlgTrajFitter::kKinksName = "kink";
109  const std::string PMAlgTrajFitter::kNodesName = "node";
110  // -------------------------------------------------------------
111 
113  : EDProducer{config}
114  , fHitModuleLabel(config().HitModuleLabel())
115  , fPfpModuleLabel(config().PfpModuleLabel())
116  ,
117 
118  fPmaConfig(config().ProjectionMatchingAlg())
119  , fPmaFitterConfig(config().PMAlgFitting())
120  , fPmaVtxConfig(config().PMAlgVertexing())
121  ,
122 
123  fSaveOnlyBranchingVtx(config().SaveOnlyBranchingVtx())
124  , fSavePmaNodes(config().SavePmaNodes())
125  {
126  produces<std::vector<recob::Track>>();
127  produces<std::vector<recob::SpacePoint>>();
128  produces<std::vector<recob::Vertex>>(); // no instance name for interaction vertices
129  produces<std::vector<recob::Vertex>>(kKinksName); // collection of kinks on tracks
130  produces<std::vector<recob::Vertex>>(kNodesName); // collection of pma nodes
131 
133  recob::Hit>>(); // ****** REMEMBER to remove when FindMany improved ******
134  produces<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta>>();
135 
136  produces<art::Assns<recob::Track, recob::SpacePoint>>();
137  produces<art::Assns<recob::SpacePoint, recob::Hit>>();
138  produces<
140  recob::Track>>(); // no instance name for assns of tracks to interaction vertices
141  produces<art::Assns<recob::Track, recob::Vertex>>(kKinksName); // assns of kinks to tracks
142 
143  produces<art::Assns<recob::PFParticle, recob::Track>>();
144  }
145  // ------------------------------------------------------
146 
148  {
149  // ---------------- Create data products ------------------
150  auto tracks = std::make_unique<std::vector<recob::Track>>();
151  auto allsp = std::make_unique<std::vector<recob::SpacePoint>>();
152  auto vtxs = std::make_unique<std::vector<recob::Vertex>>(); // interaction vertices
153  auto kinks = std::make_unique<
154  std::vector<recob::Vertex>>(); // kinks on tracks (no new particles start in kinks)
155  auto nodes = std::make_unique<std::vector<recob::Vertex>>(); // pma nodes
156 
157  auto trk2hit_oldway = std::make_unique<
159  recob::Hit>>(); // ****** REMEMBER to remove when FindMany improved ******
160  auto trk2hit = std::make_unique<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta>>();
161 
162  auto trk2sp = std::make_unique<art::Assns<recob::Track, recob::SpacePoint>>();
163 
164  auto sp2hit = std::make_unique<art::Assns<recob::SpacePoint, recob::Hit>>();
165  auto vtx2trk = std::make_unique<
167  recob::Track>>(); // one or more tracks (particles) start in the vertex
168  auto trk2kink =
169  std::make_unique<art::Assns<recob::Track, recob::Vertex>>(); // one or more kinks on the track
170 
171  auto pfp2trk = std::make_unique<art::Assns<recob::PFParticle, recob::Track>>();
172 
173  // ------------------- Collect inputs ---------------------
174  art::Handle<std::vector<recob::Hit>> allHitListHandle;
177  std::vector<art::Ptr<recob::Hit>> allhitlist;
178  if (!(evt.getByLabel(fHitModuleLabel,
179  allHitListHandle) && // all hits used to make clusters and PFParticles
180  evt.getByLabel(fPfpModuleLabel, cluListHandle) && // clusters associated to PFParticles
181  evt.getByLabel(fPfpModuleLabel, pfparticleHandle))) // and finally PFParticles
182  {
183  throw cet::exception("PMAlgTrajFitter")
184  << "Not all required data products found in the event." << std::endl;
185  }
186 
187  art::fill_ptr_vector(allhitlist, allHitListHandle);
188 
189  art::FindManyP<recob::Hit> hitsFromClusters(cluListHandle, evt, fPfpModuleLabel);
190  art::FindManyP<recob::Cluster> clustersFromPfps(pfparticleHandle, evt, fPfpModuleLabel);
191  art::FindManyP<recob::Vertex> vtxFromPfps(pfparticleHandle, evt, fPfpModuleLabel);
192 
193  auto const detProp =
195 
196  // -------------- PMA Fitter for this event ---------------
197  auto pmalgFitter = pma::PMAlgFitter(allhitlist,
198  *cluListHandle,
199  *pfparticleHandle,
200  hitsFromClusters,
201  clustersFromPfps,
202  vtxFromPfps,
203  fPmaConfig,
205  fPmaVtxConfig);
206 
207  // ------------------ Do the job here: --------------------
208  int retCode = pmalgFitter.build(detProp);
209  // --------------------------------------------------------
210  switch (retCode) {
211  case -2: mf::LogError("Summary") << "problem"; break;
212  case -1: mf::LogWarning("Summary") << "no input"; break;
213  case 0: mf::LogVerbatim("Summary") << "no tracks done"; break;
214  default:
215  if (retCode < 0)
216  mf::LogVerbatim("Summary") << "unknown result";
217  else if (retCode == 1)
218  mf::LogVerbatim("Summary") << retCode << " track ready";
219  else
220  mf::LogVerbatim("Summary") << retCode << " tracks ready";
221  break;
222  }
223 
224  // ---------- Translate output to data products: ----------
225  auto const& result = pmalgFitter.result();
226  if (!result.empty()) // ok, there is something to save
227  {
228  size_t spStart = 0, spEnd = 0;
229  double sp_pos[3], sp_err[6];
230  for (size_t i = 0; i < 3; i++)
231  sp_pos[i] = 1.0;
232  for (size_t i = 0; i < 6; i++)
233  sp_err[i] = 1.0;
234 
235  // use the following to create PFParticle <--> Track associations;
236  std::map<size_t, std::vector<art::Ptr<recob::Track>>> pfPartToTrackVecMap;
237 
238  //auto const make_trkptr = art::PtrMaker<recob::Track>(evt, *this); // PtrMaker Step #1
239 
240  tracks->reserve(result.size());
241  for (size_t trkIndex = 0; trkIndex < result.size(); ++trkIndex) {
242  pma::Track3D* trk = result[trkIndex].Track();
243 
244  trk->SelectHits(); // just in case, set all to enabled
245  unsigned int itpc = trk->FrontTPC(), icryo = trk->FrontCryo();
246  auto const& tpc = fGeom->TPC(geo::TPCID(icryo, itpc));
247  if (tpc.HasPlane(geo::kU)) trk->CompleteMissingWires(detProp, geo::kU);
248  if (tpc.HasPlane(geo::kV)) trk->CompleteMissingWires(detProp, geo::kV);
249  if (tpc.HasPlane(geo::kZ)) trk->CompleteMissingWires(detProp, geo::kZ);
250 
251  // gc: make sure no tracks are created with less than 2 points
252  if (trk->size() < 2) continue;
253 
254  tracks->push_back(pma::convertFrom(*trk, trkIndex));
255 
256  //auto const trkPtr = make_trkptr(tracks->size() - 1); // PtrMaker Step #2
257 
258  size_t trkIdx = tracks->size() - 1; // stuff for assns:
259  art::ProductID trkId = evt.getProductID<std::vector<recob::Track>>();
260  art::Ptr<recob::Track> trkPtr(trkId, trkIdx, evt.productGetter(trkId));
261 
262  //gc: save associated hits in the same order as trajectory points
263  for (size_t h = 0, cnt = 0; h < trk->size(); h++) {
264  pma::Hit3D* h3d = (*trk)[h];
265  if (!h3d->IsEnabled()) continue;
266 
267  recob::TrackHitMeta metadata(cnt++, h3d->Dx());
268  trk2hit->addSingle(trkPtr, h3d->Hit2DPtr(), metadata);
269  trk2hit_oldway->addSingle(
270  trkPtr, h3d->Hit2DPtr()); // ****** REMEMBER to remove when FindMany improved ******
271  }
272 
274  spStart = allsp->size();
275  for (size_t h = 0; h < trk->size(); ++h) {
276  pma::Hit3D* h3d = (*trk)[h];
277  if (!h3d->IsEnabled()) continue;
278 
279  double hx = h3d->Point3D().X();
280  double hy = h3d->Point3D().Y();
281  double hz = h3d->Point3D().Z();
282 
283  if ((h == 0) || (std::fabs(sp_pos[0] - hx) > 1.0e-5) ||
284  (std::fabs(sp_pos[1] - hy) > 1.0e-5) || (std::fabs(sp_pos[2] - hz) > 1.0e-5)) {
285  if (sp_hits.size()) // hits assigned to the previous sp
286  {
287  util::CreateAssn(evt, *allsp, sp_hits, *sp2hit);
288  sp_hits.clear();
289  }
290  sp_pos[0] = hx;
291  sp_pos[1] = hy;
292  sp_pos[2] = hz;
293  allsp->push_back(recob::SpacePoint(sp_pos, sp_err, 1.0));
294  }
295  sp_hits.push_back(h3d->Hit2DPtr());
296  }
297 
298  if (sp_hits.size()) // hits assigned to the last sp
299  {
300  util::CreateAssn(evt, *allsp, sp_hits, *sp2hit);
301  }
302  spEnd = allsp->size();
303 
304  if (spEnd > spStart) util::CreateAssn(evt, *tracks, *allsp, *trk2sp, spStart, spEnd);
305 
306  // if there is a PFParticle collection then recover PFParticle and add info to map
307  if (result[trkIndex].Key() > -1) {
308  size_t trackIdx = tracks->size() - 1;
309  art::ProductID trackId = evt.getProductID<std::vector<recob::Track>>();
310  art::Ptr<recob::Track> trackPtr(trackId, trackIdx, evt.productGetter(trackId));
311  pfPartToTrackVecMap[result[trkIndex].Key()].push_back(trackPtr);
312  }
313  }
314 
315  auto vid = evt.getProductID<std::vector<recob::Vertex>>();
316  auto kid = evt.getProductID<std::vector<recob::Vertex>>(kKinksName);
317  auto const* kinkGetter = evt.productGetter(kid);
318 
319  auto tid = evt.getProductID<std::vector<recob::Track>>();
320  auto const* trkGetter = evt.productGetter(tid);
321 
322  auto vsel = pmalgFitter.getVertices(
323  fSaveOnlyBranchingVtx); // vtx pos's with vector of connected track idxs
324  auto ksel = pmalgFitter.getKinks(); // pairs of kink position - associated track idx
325  std::map<size_t, art::Ptr<recob::Vertex>> frontVtxs; // front vertex ptr for each track index
326 
327  if (fPmaFitterConfig.RunVertexing()) // save vertices and vtx-trk assns
328  {
329  double xyz[3];
330  for (auto const& v : vsel) {
331  xyz[0] = v.first.X();
332  xyz[1] = v.first.Y();
333  xyz[2] = v.first.Z();
334  mf::LogVerbatim("Summary") << " vtx:" << xyz[0] << ":" << xyz[1] << ":" << xyz[2]
335  << " (" << v.second.size() << " tracks)";
336 
337  size_t vidx = vtxs->size();
338  vtxs->push_back(recob::Vertex(xyz, vidx));
339 
340  art::Ptr<recob::Vertex> vptr(vid, vidx, evt.productGetter(vid));
341  if (vptr.isNull()) mf::LogWarning("PMAlgTrajFitter") << "Vertex ptr is null.";
342  if (!v.second.empty()) {
343  for (const auto& vEntry : v.second) {
344  size_t tidx = vEntry.first;
345  bool isFront = vEntry.second;
346 
347  if (isFront) frontVtxs[tidx] = vptr; // keep ptr of the front vtx
348 
349  art::Ptr<recob::Track> tptr(tid, tidx, trkGetter);
350  vtx2trk->addSingle(vptr, tptr);
351  }
352  }
353  else
354  mf::LogWarning("PMAlgTrajFitter") << "No tracks found at this vertex.";
355  }
356  mf::LogVerbatim("Summary") << vtxs->size() << " vertices ready";
357 
358  for (auto const& k : ksel) {
359  xyz[0] = k.first.X();
360  xyz[1] = k.first.Y();
361  xyz[2] = k.first.Z();
362  mf::LogVerbatim("Summary") << " kink:" << xyz[0] << ":" << xyz[1] << ":" << xyz[2];
363 
364  size_t kidx = kinks->size();
365  size_t tidx = k.second; // track idx on which this kink was found
366 
367  kinks->push_back(
368  recob::Vertex(xyz, tidx)); // save index of track (will have color of trk in evd)
369 
370  art::Ptr<recob::Track> tptr(tid, tidx, trkGetter);
371  art::Ptr<recob::Vertex> kptr(kid, kidx, kinkGetter);
372  trk2kink->addSingle(tptr, kptr);
373  }
374  mf::LogVerbatim("Summary") << ksel.size() << " kinks ready";
375  }
376 
377  if (fSavePmaNodes) {
378  double xyz[3];
379  for (size_t t = 0; t < result.size(); ++t) {
380  auto const& trk = *(result[t].Track());
381  for (auto const* node : trk.Nodes()) {
382  xyz[0] = node->Point3D().X();
383  xyz[1] = node->Point3D().Y();
384  xyz[2] = node->Point3D().Z();
385  nodes->push_back(recob::Vertex(xyz, t));
386  }
387  }
388  }
389 
390  for (const auto& pfParticleItr : pfPartToTrackVecMap) {
391  art::Ptr<recob::PFParticle> pfParticle(pfparticleHandle, pfParticleItr.first);
392  mf::LogVerbatim("PMAlgTrajFitter")
393  << "PFParticle key: " << pfParticle.key() << ", self: " << pfParticle->Self()
394  << ", #tracks: " << pfParticleItr.second.size();
395 
396  if (!pfParticle.isNull())
397  util::CreateAssn(evt, pfParticle, pfParticleItr.second, *pfp2trk);
398  else
399  mf::LogError("PMAlgTrajFitter")
400  << "Error in PFParticle lookup, pfparticle index: " << pfParticleItr.first
401  << ", key: " << pfParticle.key();
402  }
403  }
404 
405  evt.put(std::move(tracks));
406  evt.put(std::move(allsp));
407  evt.put(std::move(vtxs));
408  evt.put(std::move(kinks), kKinksName);
409  evt.put(std::move(nodes), kNodesName);
410 
411  evt.put(std::move(trk2hit_oldway)); // ****** REMEMBER to remove when FindMany improved ******
412  evt.put(std::move(trk2hit));
413  evt.put(std::move(trk2sp));
414 
415  evt.put(std::move(sp2hit));
416  evt.put(std::move(vtx2trk));
417  evt.put(std::move(trk2kink), kKinksName);
418 
419  evt.put(std::move(pfp2trk));
420  }
421 
423 
424 } // namespace trkf
fhicl::Table< pma::ProjectionMatchingAlg::Config > ProjectionMatchingAlg
bool SelectHits(float fraction=1.0F)
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
size_t Self() const
Returns the index of this particle.
Definition: PFParticle.h:88
recob::Track convertFrom(const pma::Track3D &src, unsigned int tidx, int pdg=0)
ProductID getProductID(std::string const &instance_name="") const
Planes which measure V.
Definition: geo_types.h:136
Declaration of signal hit object.
bool IsEnabled() const noexcept
Definition: PmaHit3D.h:86
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
Class to keep data related to recob::Hit associated with recob::Track.
TVector3 const & Point3D() const
Definition: PmaHit3D.h:50
Planes which measure Z direction.
Definition: geo_types.h:138
Data related to recob::Hit associated with recob::Track.The purpose is to collect several variables t...
Definition: TrackHitMeta.h:43
art::ServiceHandle< geo::Geometry const > fGeom
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
void produce(art::Event &e) override
TPCGeo const & TPC(TPCID const &tpcid=tpc_zero) const
Returns the specified TPC.
Definition: GeometryCore.h:722
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
Planes which measure U.
Definition: geo_types.h:135
EDProductGetter const * productGetter(ProductID const pid) const
fhicl::Table< pma::PMAlgFitter::Config > PMAlgFitting
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:435
fhicl::Atom< bool > RunVertexing
key_type key() const noexcept
Definition: Ptr.h:166
bool isNull() const noexcept
Definition: Ptr.h:211
Provides recob::Track data product.
fhicl::Table< pma::PMAlgVertexing::Config > PMAlgVertexing
unsigned int FrontTPC() const
Definition: PmaTrack3D.h:118
unsigned int FrontCryo() const
Definition: PmaTrack3D.h:119
The data type to uniquely identify a TPC.
Definition: geo_types.h:381
Declaration of cluster object.
size_type size() const
Definition: PtrVector.h:302
size_t CompleteMissingWires(detinfo::DetectorPropertiesData const &detProp, unsigned int view)
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.
pma::PMAlgVertexing::Config fPmaVtxConfig
static const std::string kNodesName
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Utility object to perform functions of association.
double Dx() const noexcept
Definition: PmaHit3D.h:69
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
pma::ProjectionMatchingAlg::Config fPmaConfig
art::Ptr< recob::Hit > const & Hit2DPtr() const
Definition: PmaHit3D.h:48
boost::graph_traits< ModuleGraph >::vertex_descriptor Vertex
Definition: ModuleGraph.h:25
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
Definition: Track.h:992
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:46
TCEvent evt
Definition: DataStructs.cxx:8
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:306
Float_t e
Definition: plot.C:35
size_t size() const
Definition: PmaTrack3D.h:89
PMAlgTrajFitter(Parameters const &config)
static const std::string kKinksName
fhicl::Atom< art::InputTag > HitModuleLabel
void clear()
Definition: PtrVector.h:533
PMAlgTrajFitter & operator=(PMAlgTrajFitter const &)=delete
art framework interface to geometry description
pma::PMAlgFitter::Config fPmaFitterConfig
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Encapsulate the construction of a single detector plane.
fhicl::Atom< art::InputTag > PfpModuleLabel