LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
PMAlgTrackMaker_module.cc
Go to the documentation of this file.
1 // Class: PMAlgTrackMaker
3 // Module Type: producer
4 // File: PMAlgTrackMaker_module.cc
5 // Authors D.Stefan (Dorota.Stefan@ncbj.gov.pl), from DUNE, CERN/NCBJ, since May 2015
6 // R.Sulej (Robert.Sulej@cern.ch), from DUNE, FNAL/NCBJ, since May 2015
7 // L.Whitehead (leigh.howard.whitehead@cern.ch), from DUNE, CERN, since Jan 2017
8 //
9 // Creates 3D tracks and vertices using Projection Matching Algorithm,
10 // please see RecoAlg/ProjectionMatchingAlg.h for basics of the PMA algorithm and its settings.
11 //
12 // Progress:
13 // May-June 2015: track finding and validation, growing tracks by iterative merging of matching
14 // clusters, no attempts to build multi-track structures, however cosmic tracking
15 // works fine as they are sets of independent tracks
16 // June-July 2015: merging track parts within a single tpc and stitching tracks across tpc's
17 // August 2015: optimization of track-vertex structures (so 3D vertices are also produced)
18 // November 2015: use track-shower splitting at 2D level, then tag low-E EM cascades in 3D
19 // note: the splitter is not finished and not as good as we want it
20 // January 2016: output of track-vertex finding as a tree of PFParticles, refined vertexing
21 // code, put vertex at front of each track, flip whole track structures to
22 // selected, direction (beam/down/dQdx), use any pattern reco stored in
23 // PFParticles as a source of associated clusters
24 // Mar-Apr 2016: kinks finding, EM shower direction reconstructed for PFPaarticles tagged as
25 // electrons
26 // July 2016: redesign module: extract trajectory fitting-only to separate module, move
27 // tracking functionality to algorithm classes
28 // ~Jan-May 2017: track stitching on APA and CPA, cosmics tagging
29 // July 2017: validation on 2D ADC image
30 //
32 
40 
41 #include "fhiclcpp/types/Atom.h"
42 #include "fhiclcpp/types/Table.h"
45 
47 
48 // LArSoft includes
65 
67 
72 
73 #include <memory>
74 
75 namespace trkf {
76 
78 public:
79 
80  struct Config {
81  using Name = fhicl::Name;
83 
85  Name("ProjectionMatchingAlg")
86  };
87 
89  Name("PMAlgTracking")
90  };
91 
93  Name("PMAlgVertexing")
94  };
95 
97  Name("PMAlgCosmicTagging")
98  };
99 
101  Name("PMAlgStitching")
102  };
103 
105  Name("SaveOnlyBranchingVtx"),
106  Comment("use true to save only vertices interconnecting many tracks, otherwise vertex is added to the front of each track")
107  };
108 
110  Name("SavePmaNodes"),
111  Comment("save track nodes (only for algorithm development purposes)")
112  };
113 
115  Name("HitModuleLabel"),
116  Comment("tag of unclustered hits, which were used to validate tracks")
117  };
118 
120  Name("WireModuleLabel"),
121  Comment("tag of recob::Wire producer.")
122  };
123 
125  Name("ClusterModuleLabel"),
126  Comment("tag of cluster collection, these clusters are used for track building")
127  };
128 
130  Name("EmClusterModuleLabel"),
131  Comment("EM-like clusters, will be excluded from tracking if provided")
132  };
133  };
135 
136  explicit PMAlgTrackMaker(Parameters const& config);
137 
138  PMAlgTrackMaker(PMAlgTrackMaker const &) = delete;
139  PMAlgTrackMaker(PMAlgTrackMaker &&) = delete;
140  PMAlgTrackMaker & operator = (PMAlgTrackMaker const &) = delete;
142 
143  void produce(art::Event & e) override;
144 
145 private:
146  // will try to get EM- and track-like values from various lenght MVA vectors
147  template <size_t N> bool init(const art::Event & evt, pma::PMAlgTracker & pmalgTracker) const;
148 
149  // calculate EM/track value for hits in track, in its best 2D projection
150  // (tracks are built starting from track-like cluster, some electrons
151  // still may look track-like)
152  template <size_t N> int getPdgFromCnnOnHits(const art::Event& evt, const pma::Track3D& trk) const;
153 
154  // convert to a vector of LArSoft's cosmic tags
155  std::vector<anab::CosmicTagID_t> getCosmicTag(const pma::Track3D::ETag pmaTag) const;
156 
157  // ******************** fcl parameters **********************
158  art::InputTag fHitModuleLabel; // tag for hits collection (used for trk validation)
159  art::InputTag fWireModuleLabel; // tag for recob::Wire collection (used for trk validation)
160  art::InputTag fCluModuleLabel; // tag for input cluster collection
161  art::InputTag fEmModuleLabel; // tag for em-like cluster collection
162 
168 
169  bool fSaveOnlyBranchingVtx; // for debugging, save only vertices which connect many tracks
170  bool fSavePmaNodes; // for debugging, save only track nodes
171 
172  // ********** instance names (collections, assns) ************
173  static const std::string kKinksName; // kinks on tracks
174  static const std::string kNodesName; // pma nodes
175 
176  // *********************** geometry **************************
178 
179  // histograms created only for the calibration of the ADC-based track validation mode
181 };
182 // -------------------------------------------------------------
183 const std::string PMAlgTrackMaker::kKinksName = "kink";
184 const std::string PMAlgTrackMaker::kNodesName = "node";
185 // -------------------------------------------------------------
186 
188  fHitModuleLabel(config().HitModuleLabel()),
189  fWireModuleLabel(config().WireModuleLabel()),
192 
193  fPmaConfig(config().ProjectionMatchingAlg()),
194  fPmaTrackerConfig(config().PMAlgTracking()),
196  fPmaVtxConfig(config().PMAlgVertexing()),
197  fPmaStitchConfig(config().PMAlgStitching()),
198 
200  fSavePmaNodes(config().SavePmaNodes()),
201 
202  fGeom( &*(art::ServiceHandle<geo::Geometry>()))
203 {
204  produces< std::vector<recob::Track> >();
205  produces< std::vector<recob::SpacePoint> >();
206  produces< std::vector<recob::Vertex> >(); // no instance name for interaction vertices
207  produces< std::vector<recob::Vertex> >(kKinksName); // collection of kinks on tracks
208  produces< std::vector<recob::Vertex> >(kNodesName); // collection of pma nodes
209  produces< std::vector<anab::T0> >();
210  produces< std::vector<anab::CosmicTag> >(); // Cosmic ray tags
211 
212  produces< art::Assns<recob::Track, recob::Hit> >(); // ****** REMEMBER to remove when FindMany improved ******
213  produces< art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta> >();
214 
215  produces< art::Assns<recob::Track, recob::SpacePoint> >();
216  produces< art::Assns<recob::SpacePoint, recob::Hit> >();
217  produces< art::Assns<recob::Vertex, recob::Track> >(); // no instance name for assns of tracks to interaction vertices
218  produces< art::Assns<recob::Track, recob::Vertex> >(kKinksName); // assns of kinks to tracks
219  produces< art::Assns<recob::Track, anab::T0> >();
220  produces< art::Assns<recob::Track, anab::CosmicTag> >(); // Cosmic ray tags associated to tracks
221 
222  produces< std::vector<recob::PFParticle> >();
223  produces< art::Assns<recob::PFParticle, recob::Cluster> >();
224  produces< art::Assns<recob::PFParticle, recob::Vertex> >();
225  produces< art::Assns<recob::PFParticle, recob::Track> >();
226 
227  if (fPmaTrackerConfig.Validation() == "calib") // create histograms only in the calibration mode
228  {
230  for (size_t p = 0; p < fGeom->MaxPlanes(); ++p)
231  {
232  std::ostringstream ss1; ss1 << "adc_plane_" << p ;
233  fAdcInPassingPoints.push_back( tfs->make<TH1F>((ss1.str() + "_passing").c_str(), "max adc around the point on track", 100., 0., 5.) );
234  fAdcInRejectedPoints.push_back( tfs->make<TH1F>((ss1.str() + "_rejected").c_str(), "max adc around spurious point ", 100., 0., 5.) );
235  }
236  }
237 }
238 // ------------------------------------------------------
239 
240 template <size_t N>
242 {
243  int pdg = 0;
245  {
247  if (hitResults)
248  {
249  int trkLikeIdx = hitResults->getIndex("track");
250  int emLikeIdx = hitResults->getIndex("em");
251  if ((trkLikeIdx < 0) || (emLikeIdx < 0))
252  {
253  throw cet::exception("PMAlgTrackMaker") << "No em/track labeled columns in MVA data products." << std::endl;
254  }
255 
256  size_t nh[3] = { 0, 0, 0 };
257  for (size_t hidx = 0; hidx < trk.size(); ++hidx) { ++nh[trk[hidx]->View2D()]; }
258 
259  size_t best_view = 2; // collection
260  if ((nh[0] >= nh[1]) && (nh[0] > 1.25 * nh[2])) best_view = 0; // ind1
261  if ((nh[1] >= nh[0]) && (nh[1] > 1.25 * nh[2])) best_view = 1; // ind2
262 
263  std::vector< art::Ptr<recob::Hit> > trkHitPtrList;
264  trkHitPtrList.reserve(nh[best_view]);
265  for (size_t hidx = 0; hidx < trk.size(); ++hidx)
266  {
267  if (trk[hidx]->View2D() == best_view) { trkHitPtrList.emplace_back(trk[hidx]->Hit2DPtr()); }
268  }
269  auto vout = hitResults->getOutput(trkHitPtrList);
270  double trk_like = -1, trk_or_em = vout[trkLikeIdx] + vout[emLikeIdx];
271  if (trk_or_em > 0)
272  {
273  trk_like = vout[trkLikeIdx] / trk_or_em;
274  if (trk_like < fPmaTrackerConfig.TrackLikeThreshold()) pdg = 11; // tag if EM-like
275  // (don't set pdg for track-like, for the moment don't like the idea of using "13")
276  }
277  //std::cout << "trk:" << best_view << ":" << trk.size() << ":" << trkHitPtrList.size() << " p=" << trk_like << std::endl;
278  }
279  }
280  return pdg;
281 }
282 
283 template <size_t N>
284 bool PMAlgTrackMaker::init(const art::Event & evt, pma::PMAlgTracker & pmalgTracker) const
285 {
287  if (!cluResults) { return false; }
288 
289  int trkLikeIdx = cluResults->getIndex("track");
290  int emLikeIdx = cluResults->getIndex("em");
291  if ((trkLikeIdx < 0) || (emLikeIdx < 0)) { return false; }
292 
293  const art::FindManyP< recob::Hit > hitsFromClusters(cluResults->dataHandle(), evt, cluResults->dataTag());
294  const auto & cnnOuts = cluResults->outputs();
295  std::vector< float > trackLike(cnnOuts.size());
296  for (size_t i = 0; i < cnnOuts.size(); ++i)
297  {
298  double trkOrEm = cnnOuts[i][trkLikeIdx] + cnnOuts[i][emLikeIdx];
299  if (trkOrEm > 0) { trackLike[i] = cnnOuts[i][trkLikeIdx] / trkOrEm; }
300  else { trackLike[i] = 0; }
301  }
302  pmalgTracker.init(hitsFromClusters, trackLike);
303  return true;
304 }
305 
306 std::vector<anab::CosmicTagID_t> PMAlgTrackMaker::getCosmicTag(const pma::Track3D::ETag pmaTag) const
307 {
308  std::vector<anab::CosmicTagID_t> anabTags;
309 
313  if (pmaTag & pma::Track3D::kGeometry_XX) { anabTags.push_back(anab::CosmicTagID_t::kGeometry_XX); }
314  if (pmaTag & pma::Track3D::kGeometry_YY) { anabTags.push_back(anab::CosmicTagID_t::kGeometry_YY); }
315  if (pmaTag & pma::Track3D::kGeometry_ZZ) { anabTags.push_back(anab::CosmicTagID_t::kGeometry_ZZ); }
316  if (pmaTag & pma::Track3D::kGeometry_YZ) { anabTags.push_back(anab::CosmicTagID_t::kGeometry_YZ); }
317  if (pmaTag & pma::Track3D::kGeometry_Y) { anabTags.push_back(anab::CosmicTagID_t::kGeometry_Y); }
318 
319  if (anabTags.empty()) { anabTags.push_back(anab::CosmicTagID_t::kUnknown); }
320 
321  return anabTags;
322 }
323 
325 {
326  // ---------------- Create data products --------------------------
327  auto tracks = std::make_unique< std::vector< recob::Track > >();
328  auto allsp = std::make_unique< std::vector< recob::SpacePoint > >();
329  auto vtxs = std::make_unique< std::vector< recob::Vertex > >(); // interaction vertices
330  auto kinks = std::make_unique< std::vector< recob::Vertex > >(); // kinks on tracks (no new particles start in kinks)
331  auto nodes = std::make_unique< std::vector< recob::Vertex > >(); // pma nodes
332  auto t0s = std::make_unique< std::vector< anab::T0 > >();
333  auto cosmicTags = std::make_unique< std::vector< anab::CosmicTag > >();
334 
335  auto trk2hit_oldway = std::make_unique< art::Assns< recob::Track, recob::Hit > >(); // ****** REMEMBER to remove when FindMany improved ******
336  auto trk2hit = std::make_unique< art::Assns< recob::Track, recob::Hit, recob::TrackHitMeta > >();
337 
338  auto trk2sp = std::make_unique< art::Assns< recob::Track, recob::SpacePoint > >();
339  auto trk2t0 = std::make_unique< art::Assns< recob::Track, anab::T0 > >();
340  auto trk2ct = std::make_unique< art::Assns< recob::Track, anab::CosmicTag > >();
341 
342  auto sp2hit = std::make_unique< art::Assns< recob::SpacePoint, recob::Hit > >();
343  auto vtx2trk = std::make_unique< art::Assns< recob::Vertex, recob::Track > >(); // one or more tracks (particles) start in the vertex
344  auto trk2kink = std::make_unique< art::Assns< recob::Track, recob::Vertex > >(); // one or more kinks on the track
345 
346  auto pfps = std::make_unique< std::vector< recob::PFParticle > >();
347 
348  auto pfp2clu = std::make_unique< art::Assns<recob::PFParticle, recob::Cluster> >();
349  auto pfp2vtx = std::make_unique< art::Assns<recob::PFParticle, recob::Vertex> >();
350  auto pfp2trk = std::make_unique< art::Assns< recob::PFParticle, recob::Track > >();
351 
352 
353  // --------------------- Wires & Hits -----------------------------
354  auto wireHandle = evt.getValidHandle< std::vector<recob::Wire> >(fWireModuleLabel);
355  auto allHitListHandle = evt.getValidHandle< std::vector<recob::Hit> >(fHitModuleLabel);
356  std::vector< art::Ptr<recob::Hit> > allhitlist;
357  art::fill_ptr_vector(allhitlist, allHitListHandle);
358 
359 
360  // -------------- PMA Tracker for this event ----------------------
361  auto pmalgTracker = pma::PMAlgTracker(allhitlist, *wireHandle,
363 
364  size_t mvaLength = 0;
365  if (fEmModuleLabel != "") // ----------- Exclude EM parts ---------
366  {
367  auto cluListHandle = evt.getValidHandle< std::vector<recob::Cluster> >(fCluModuleLabel);
368  auto splitCluHandle = evt.getValidHandle< std::vector<recob::Cluster> >(fEmModuleLabel);
369 
370  art::FindManyP< recob::Hit > hitsFromClusters(cluListHandle, evt, fCluModuleLabel);
371  art::FindManyP< recob::Hit > hitsFromEmParts(splitCluHandle, evt, fEmModuleLabel);
372  pmalgTracker.init(hitsFromClusters, hitsFromEmParts);
373  }
374  else if (fPmaTrackerConfig.TrackLikeThreshold() > 0) // --- CNN EM/trk separation ----
375  {
376  // try to dig out 4- or 3-output MVA data product
377  if (init<4>(evt, pmalgTracker) ) { mvaLength = 4; } // e.g.: EM / track / Michel / none
378  else if (init<3>(evt, pmalgTracker)) { mvaLength = 3; } // e.g.: EM / track / none
379  else if (init<2>(evt, pmalgTracker)) { mvaLength = 2; } // just EM / track (LArIAT starts with this style)
380  else
381  {
382  throw cet::exception("PMAlgTrackMaker") << "No EM/track MVA data products." << std::endl;
383  }
384  }
385  else // ------------------------ Use ALL clusters -----------------
386  {
387  auto cluListHandle = evt.getValidHandle< std::vector<recob::Cluster> >(fCluModuleLabel);
388  art::FindManyP< recob::Hit > hitsFromClusters(cluListHandle, evt, fCluModuleLabel);
389  pmalgTracker.init(hitsFromClusters);
390  }
391 
392 
393  // ------------------ Do the job here: ----------------------------
394  int retCode = pmalgTracker.build();
395  // ----------------------------------------------------------------
396  switch (retCode)
397  {
398  case -2: mf::LogError("Summary") << "problem"; break;
399  case -1: mf::LogWarning("Summary") << "no input"; break;
400  case 0: mf::LogVerbatim("Summary") << "no tracks done"; break;
401  default:
402  if (retCode < 0) mf::LogVerbatim("Summary") << "unknown result";
403  else if (retCode == 1) mf::LogVerbatim("Summary") << retCode << " track ready";
404  else mf::LogVerbatim("Summary") << retCode << " tracks ready";
405  break;
406  }
407 
408  // ---------- Translate output to data products: ------------------
409  auto const & result = pmalgTracker.result();
410  if (!result.empty()) // ok, there is something to save
411  {
412  size_t spStart = 0, spEnd = 0;
413  double sp_pos[3], sp_err[6];
414  for (size_t i = 0; i < 3; ++i) sp_pos[i] = 0.0;
415  for (size_t i = 0; i < 6; ++i) sp_err[i] = 1.0;
416 
417  auto const make_pfpptr = art::PtrMaker<recob::PFParticle>(evt, *this);
418  auto const make_trkptr = art::PtrMaker<recob::Track>(evt, *this); // PtrMaker Step #1
419  auto const make_vtxptr = art::PtrMaker<recob::Vertex>(evt, *this);
420  auto const make_kinkptr = art::PtrMaker<recob::Vertex>(evt, *this, kKinksName);
421  auto const make_t0ptr = art::PtrMaker<anab::T0>(evt, *this);
422  auto const make_ctptr = art::PtrMaker<anab::CosmicTag>(evt, *this);
423 
424  tracks->reserve(result.size());
425  for (size_t trkIndex = 0; trkIndex < result.size(); ++trkIndex)
426  {
427  pma::Track3D* trk = result[trkIndex].Track();
428 
429  trk->SelectHits(); // just in case, set all to enabled
430  unsigned int itpc = trk->FrontTPC(), icryo = trk->FrontCryo();
431  pma::dedx_map dedx_tmp;
432  if (fGeom->TPC(itpc, icryo).HasPlane(geo::kU)) { trk->CompleteMissingWires(geo::kU); trk->GetRawdEdxSequence(dedx_tmp, geo::kU, 1); }
433  if (fGeom->TPC(itpc, icryo).HasPlane(geo::kV)) { trk->CompleteMissingWires(geo::kV); trk->GetRawdEdxSequence(dedx_tmp, geo::kV, 1); }
434  if (fGeom->TPC(itpc, icryo).HasPlane(geo::kX)) { trk->CompleteMissingWires(geo::kX); trk->GetRawdEdxSequence(dedx_tmp, geo::kX, 1); }
435  if (fGeom->TPC(itpc, icryo).HasPlane(geo::kY)) { trk->CompleteMissingWires(geo::kY); trk->GetRawdEdxSequence(dedx_tmp, geo::kY, 1); }
436  if (fGeom->TPC(itpc, icryo).HasPlane(geo::kZ)) { trk->CompleteMissingWires(geo::kZ); trk->GetRawdEdxSequence(dedx_tmp, geo::kZ, 1); }
437 
438  //gc: make sure no tracks are created with less than 2 points
439  if (trk->size()<2) continue;
440 
441  int pdg = 0;
442  if (mvaLength == 4) pdg = getPdgFromCnnOnHits<4>(evt, *(result[trkIndex].Track()));
443  else if (mvaLength == 3) pdg = getPdgFromCnnOnHits<3>(evt, *(result[trkIndex].Track()));
444  else if (mvaLength == 2) pdg = getPdgFromCnnOnHits<2>(evt, *(result[trkIndex].Track()));
445  //else mf::LogInfo("PMAlgTrackMaker") << "Not using PID from CNN.";
446 
447  tracks->push_back(pma::convertFrom(*trk, trkIndex, pdg));
448 
449  auto const trkPtr = make_trkptr(tracks->size() - 1); // PtrMaker Step #2
450 
451  if (trk->HasT0())
452  {
453  // TriggBits=3 means from 3d reco (0,1,2 mean something else)
454  t0s->push_back(anab::T0(trk->GetT0(), 0, 3, tracks->back().ID()));
455 
456  auto const t0Ptr = make_t0ptr(t0s->size() - 1); // PtrMaker Step #3
457  trk2t0->addSingle(trkPtr, t0Ptr);
458  }
459 
460  // Check if this is a cosmic ray and create an association if it is.
462  // Get the track end points
463  std::vector<float> trkEnd0;
464  std::vector<float> trkEnd1;
465  // Get the drift direction, but don't care about the sign
466  // Also need to subtract 1 due to the definition.
467  int driftDir = abs(fGeom->TPC(trk->FrontTPC(), trk->FrontCryo()).DetectDriftDirection()) - 1;
468 
469  for(int i = 0; i < 3; ++i){
470  // Get the drift direction and apply the opposite of the drift shift in order to
471  // give the CosmicTag the drift coordinate assuming T0 = T_beam as it requests.
472  double shift = 0.0;
473  if(i == driftDir){
474  shift = trk->Nodes()[0]->GetDriftShift();
475  }
476  trkEnd0.push_back(trk->Nodes()[0]->Point3D()[i] - shift);
477  trkEnd1.push_back(trk->Nodes()[trk->Nodes().size()-1]->Point3D()[i] - shift);
478  }
479  // Make the tag object. For now, let's say this is very likely a cosmic (3rd argument = 1).
480  // Set the type of cosmic to the value saved in pma::Track.
481  auto tags = getCosmicTag(trk->GetTag());
482  for (const auto t : tags)
483  {
484  cosmicTags->emplace_back(trkEnd0, trkEnd1, 1, t);
485  auto const cosmicPtr = make_ctptr(cosmicTags->size()-1);
486  trk2ct->addSingle(trkPtr,cosmicPtr);
487  }
488  }
489 
490  //gc: save associated hits in the same order as trajectory points
491  for (size_t h = 0, cnt = 0; h < trk->size(); h++)
492  {
493  pma::Hit3D* h3d = (*trk)[h];
494  if (!h3d->IsEnabled()) continue;
495 
496  recob::TrackHitMeta metadata(cnt++, h3d->Dx());
497  trk2hit->addSingle(trkPtr, h3d->Hit2DPtr(), metadata);
498  trk2hit_oldway->addSingle(trkPtr, h3d->Hit2DPtr()); // ****** REMEMBER to remove when FindMany improved ******
499  }
500 
502  spStart = allsp->size();
503  //rs: so make also associations to space points in the order of trajectory points
504  for (size_t h = 0; h < trk->size(); ++h)
505  {
506  pma::Hit3D* h3d = (*trk)[h];
507  if (!h3d->IsEnabled()) continue;
508 
509  double hx = h3d->Point3D().X();
510  double hy = h3d->Point3D().Y();
511  double hz = h3d->Point3D().Z();
512 
513  if ((h == 0) ||
514  (std::fabs(sp_pos[0] - hx) > 1.0e-5) ||
515  (std::fabs(sp_pos[1] - hy) > 1.0e-5) ||
516  (std::fabs(sp_pos[2] - hz) > 1.0e-5))
517  {
518  if (sp_hits.size()) // hits assigned to the previous sp
519  {
520  util::CreateAssn(*this, evt, *allsp, sp_hits, *sp2hit);
521  sp_hits.clear();
522  }
523  sp_pos[0] = hx; sp_pos[1] = hy; sp_pos[2] = hz;
524  allsp->push_back(recob::SpacePoint(sp_pos, sp_err, 1.0));
525  }
526  sp_hits.push_back(h3d->Hit2DPtr());
527  }
528 
529  if (sp_hits.size()) // hits assigned to the last sp
530  {
531  util::CreateAssn(*this, evt, *allsp, sp_hits, *sp2hit);
532  }
533  spEnd = allsp->size();
534 
535  if (spEnd > spStart) util::CreateAssn(*this, evt, *tracks, *allsp, *trk2sp, spStart, spEnd);
536  }
537 
538  auto vsel = pmalgTracker.getVertices(fSaveOnlyBranchingVtx); // vtx pos's with vector of connected track idxs
539  auto ksel = pmalgTracker.getKinks(); // pairs of kink position - associated track idx
540  std::map< size_t, art::Ptr<recob::Vertex> > frontVtxs; // front vertex ptr for each track index
541 
542  if (fPmaTrackerConfig.RunVertexing()) // save vertices and vtx-trk assns
543  {
544  double xyz[3];
545  for (auto const & v : vsel)
546  {
547  xyz[0] = v.first.X(); xyz[1] = v.first.Y(); xyz[2] = v.first.Z();
548  mf::LogVerbatim("Summary")
549  << " vtx:" << xyz[0] << ":" << xyz[1] << ":" << xyz[2]
550  << " (" << v.second.size() << " tracks)";
551 
552  size_t vidx = vtxs->size();
553  vtxs->push_back(recob::Vertex(xyz, vidx));
554 
555  auto const vptr = make_vtxptr(vidx);
556  if (!v.second.empty())
557  {
558  for (const auto & vEntry : v.second)
559  {
560  size_t tidx = vEntry.first;
561  bool isFront = vEntry.second;
562 
563  if (isFront) frontVtxs[tidx] = vptr; // keep ptr of the front vtx
564 
565  auto const tptr = make_trkptr(tidx);
566  vtx2trk->addSingle(vptr, tptr);
567  }
568  }
569  else mf::LogWarning("PMAlgTrackMaker") << "No tracks found at this vertex.";
570  }
571  mf::LogVerbatim("Summary") << vtxs->size() << " vertices ready";
572 
573  for (auto const & k : ksel)
574  {
575  xyz[0] = k.first.X(); xyz[1] = k.first.Y(); xyz[2] = k.first.Z();
576  mf::LogVerbatim("Summary") << " kink:" << xyz[0] << ":" << xyz[1] << ":" << xyz[2];
577 
578  size_t kidx = kinks->size();
579  size_t tidx = k.second; // track idx on which this kink was found
580 
581  kinks->push_back(recob::Vertex(xyz, tidx)); // save index of track (will have color of trk in evd)
582 
583  auto const tptr = make_trkptr(tidx);
584  auto const kptr = make_kinkptr(kidx);
585  trk2kink->addSingle(tptr, kptr);
586  }
587  mf::LogVerbatim("Summary") << ksel.size() << " kinks ready";
588  }
589 
590  if (fSavePmaNodes)
591  {
592  double xyz[3];
593  for (size_t t = 0; t < result.size(); ++t)
594  {
595  auto const & trk = *(result[t].Track());
596  for (auto const * node : trk.Nodes())
597  {
598  xyz[0] = node->Point3D().X(); xyz[1] = node->Point3D().Y(); xyz[2] = node->Point3D().Z();
599  nodes->push_back(recob::Vertex(xyz, t));
600  }
601  }
602  }
603 
604  for (size_t t = 0; t < result.size(); ++t)
605  {
606  size_t parentIdx = recob::PFParticle::kPFParticlePrimary;
607  if (result[t].Parent() >= 0) parentIdx = (size_t)result[t].Parent();
608 
609  std::vector< size_t > daughterIdxs;
610  for (size_t idx : result[t].Daughters()) { daughterIdxs.push_back(idx); }
611 
612  size_t pfpidx = pfps->size();
613  pfps->emplace_back((*tracks)[t].ParticleId(), pfpidx, parentIdx, daughterIdxs);
614 
615  auto const pfpptr = make_pfpptr(pfpidx);
616  auto const tptr = make_trkptr(t);
617  pfp2trk->addSingle(pfpptr, tptr);
618 
619  // add assns to FRONT vertex of each particle
621  {
622  art::Ptr<recob::Vertex> vptr = frontVtxs[t];
623  if (!vptr.isNull()) pfp2vtx->addSingle(pfpptr, vptr);
624  else mf::LogWarning("PMAlgTrackMaker") << "Front vertex for PFParticle is missing.";
625  }
626  }
627  mf::LogVerbatim("Summary") << pfps->size() << " PFParticles created for reconstructed tracks.";
628  mf::LogVerbatim("Summary") << "Adding " << result.parents().size() << " primary PFParticles.";
629  for (size_t t = 0; t < result.parents().size(); ++t)
630  {
631  std::vector< size_t > daughterIdxs;
632  for (size_t idx : result.parents()[t].Daughters()) { daughterIdxs.push_back(idx); }
633 
634  size_t pfpidx = pfps->size();
635  size_t parentIdx = recob::PFParticle::kPFParticlePrimary;
636  pfps->emplace_back(0, pfpidx, parentIdx, daughterIdxs);
637 
638  // add assns to END vertex of primary
639  if (fPmaTrackerConfig.RunVertexing() && !daughterIdxs.empty())
640  {
641  auto const pfpptr = make_pfpptr(pfpidx);
642  art::Ptr<recob::Vertex> vptr = frontVtxs[daughterIdxs.front()]; // same vertex for all daughters
643  if (!vptr.isNull()) pfp2vtx->addSingle(pfpptr, vptr);
644  else mf::LogWarning("PMAlgTrackMaker") << "Front vertex for PFParticle is missing.";
645  }
646  }
647  mf::LogVerbatim("Summary") << pfps->size() << " PFParticles created in total.";
648  }
649 
650  // for (const auto & ct : *cosmicTags) { std::cout << "Cosmic tag: " << ct << std::endl; }
651 
652  evt.put(std::move(tracks));
653  evt.put(std::move(allsp));
654  evt.put(std::move(vtxs));
655  evt.put(std::move(kinks), kKinksName);
656  evt.put(std::move(nodes), kNodesName);
657  evt.put(std::move(t0s));
658  evt.put(std::move(cosmicTags));
659 
660  evt.put(std::move(trk2hit_oldway)); // ****** REMEMBER to remove when FindMany improved ******
661  evt.put(std::move(trk2hit));
662  evt.put(std::move(trk2sp));
663  evt.put(std::move(trk2t0));
664  evt.put(std::move(trk2ct));
665 
666  evt.put(std::move(sp2hit));
667  evt.put(std::move(vtx2trk));
668  evt.put(std::move(trk2kink), kKinksName);
669 
670  evt.put(std::move(pfps));
671  evt.put(std::move(pfp2clu));
672  evt.put(std::move(pfp2vtx));
673  evt.put(std::move(pfp2trk));
674 }
675 // ------------------------------------------------------
676 // ------------------------------------------------------
677 // ------------------------------------------------------
678 
680 
681 } // namespace trkf
682 
PMAlgTrackMaker & operator=(PMAlgTrackMaker const &)=delete
bool SelectHits(float fraction=1.0F)
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
bool HasPlane(unsigned int iplane) const
Returns whether a plane with index iplane is present in this TPC.
Definition: TPCGeo.h:155
bool HasTagFlag(ETag value) const
Definition: PmaTrack3D.h:54
static const std::string kKinksName
bool HasT0(void) const
Definition: PmaTrack3D.h:218
void produce(art::Event &e) override
recob::Track convertFrom(const pma::Track3D &src, unsigned int tidx, int pdg=0)
fhicl::Atom< art::InputTag > EmClusterModuleLabel
unsigned int FrontTPC(void) const
Definition: PmaTrack3D.h:104
static constexpr size_t kPFParticlePrimary
Define index to signify primary particle.
Definition: PFParticle.h:61
Planes which measure V.
Definition: geo_types.h:77
Unknown view.
Definition: geo_types.h:83
bool IsEnabled(void) const
Definition: PmaHit3D.h:82
Declaration of signal hit object.
fhicl::Atom< art::InputTag > HitModuleLabel
fhicl::Table< pma::PMAlgVertexing::Config > PMAlgVertexing
Planes which measure X direction.
Definition: geo_types.h:81
fhicl::Table< pma::PMAlgCosmicTagger::Config > PMAlgCosmicTagging
std::vector< pma::Node3D * > const & Nodes(void) const
Definition: PmaTrack3D.h:232
fhicl::Table< pma::ProjectionMatchingAlg::Config > ProjectionMatchingAlg
fhicl::Atom< std::string > Validation
fhicl::Atom< art::InputTag > ClusterModuleLabel
Class to keep data related to recob::Hit associated with recob::Track.
Planes which measure Z direction.
Definition: geo_types.h:79
fhicl::Atom< art::InputTag > WireModuleLabel
Data related to recob::Hit associated with recob::Track.The purpose is to collect several variables t...
Definition: TrackHitMeta.h:43
fhicl::Atom< bool > RunVertexing
Definition: T0.h:19
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
art::Ptr< recob::Hit > const & Hit2DPtr(void) const
Definition: PmaHit3D.h:46
Definition of vertex object for LArSoft.
Definition: Vertex.h:35
Planes which measure Y direction.
Definition: geo_types.h:80
std::vector< anab::CosmicTagID_t > getCosmicTag(const pma::Track3D::ETag pmaTag) const
geo::GeometryCore const * fGeom
#define nodes
double GetT0(void) const
Definition: PmaTrack3D.h:215
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
Planes which measure U.
Definition: geo_types.h:76
pma::PMAlgStitching::Config fPmaStitchConfig
unsigned int MaxPlanes() const
Returns the largest number of planes among all TPCs in this detector.
std::vector< TH1F * > fAdcInRejectedPoints
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
pma::ProjectionMatchingAlg::Config fPmaConfig
Provides recob::Track data product.
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
pma::PMAlgVertexing::Config fPmaVtxConfig
int getPdgFromCnnOnHits(const art::Event &evt, const pma::Track3D &trk) const
size_t CompleteMissingWires(unsigned int view)
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.
pma::PMAlgCosmicTagger::Config fPmaTaggingConfig
Description of geometry of one entire detector.
Declaration of cluster object.
unsigned int FrontCryo(void) const
Definition: PmaTrack3D.h:105
size_type size() const
Definition: PtrVector.h:308
Encapsulate the geometry of a wire.
TVector3 const & Point3D(void) const
Definition: PmaHit3D.h:48
T * make(ARGS...args) const
fhicl::Atom< float > TrackLikeThreshold
Utility object to perform functions of association.
Encapsulate the construction of a single detector plane.
void init(const art::FindManyP< recob::Hit > &hitsFromClusters)
fhicl::Table< pma::PMAlgTracker::Config > PMAlgTracking
bool init(const art::Event &evt, pma::PMAlgTracker &pmalgTracker) const
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
fhicl::Table< pma::PMAlgStitching::Config > PMAlgStitching
double GetRawdEdxSequence(std::map< size_t, std::vector< double > > &dedx, unsigned int view=geo::kZ, unsigned int skip=0, bool inclDisabled=false) const
Definition: PmaTrack3D.cxx:990
HLT enums.
std::vector< TH1F * > fAdcInPassingPoints
double Dx(void) const
Definition: PmaHit3D.h:67
TPCGeo const & TPC(unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified TPC.
std::map< size_t, std::vector< double > > dedx_map
Definition: Utilities.h:31
ETag GetTag(void) const
Definition: PmaTrack3D.h:53
bool isNull() const
Definition: Ptr.h:328
pma::PMAlgTracker::Config fPmaTrackerConfig
TCEvent evt
Definition: DataStructs.cxx:5
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:464
Float_t e
Definition: plot.C:34
size_t size() const
Definition: PmaTrack3D.h:76
Namespace collecting geometry-related classes utilities.
void clear()
Definition: PtrVector.h:537
PMAlgTrackMaker(Parameters const &config)
static std::unique_ptr< MVAReader > create(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:110
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Encapsulate the construction of a single detector plane.
static const std::string kNodesName