LArSoft  v10_04_05
Liquid Argon Software toolkit - https://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 
38 #include "art_root_io/TFileService.h"
40 #include "fhiclcpp/types/Atom.h"
41 #include "fhiclcpp/types/Table.h"
43 
44 // LArSoft includes
65 
66 #include <memory>
67 
68 #include "TH1F.h"
69 
70 namespace trkf {
71 
73  public:
74  struct Config {
75  using Name = fhicl::Name;
77 
79  Name("ProjectionMatchingAlg")};
80 
82 
84 
86 
88 
90  Name("SaveOnlyBranchingVtx"),
91  Comment("use true to save only vertices interconnecting many tracks, otherwise vertex is "
92  "added to the front of each track")};
93 
95  Name("SavePmaNodes"),
96  Comment("save track nodes (only for algorithm development purposes)")};
97 
99  Name("HitModuleLabel"),
100  Comment("tag of unclustered hits, which were used to validate tracks")};
101 
103  Comment("tag of recob::Wire producer.")};
104 
106  Name("ClusterModuleLabel"),
107  Comment("tag of cluster collection, these clusters are used for track building")};
108 
110  Name("EmClusterModuleLabel"),
111  Comment("EM-like clusters, will be excluded from tracking if provided")};
112  };
114 
115  explicit PMAlgTrackMaker(Parameters const& config);
116 
117  PMAlgTrackMaker(PMAlgTrackMaker const&) = delete;
118  PMAlgTrackMaker(PMAlgTrackMaker&&) = delete;
119  PMAlgTrackMaker& operator=(PMAlgTrackMaker const&) = delete;
121 
122  private:
123  void produce(art::Event& e) override;
124 
125  // will try to get EM- and track-like values from various lenght MVA vectors
126  template <size_t N>
127  bool init(const art::Event& evt, pma::PMAlgTracker& pmalgTracker) const;
128 
129  // calculate EM/track value for hits in track, in its best 2D projection
130  // (tracks are built starting from track-like cluster, some electrons
131  // still may look track-like)
132  template <size_t N>
133  int getPdgFromCnnOnHits(const art::Event& evt, const pma::Track3D& trk) const;
134 
135  // convert to a vector of LArSoft's cosmic tags
136  std::vector<anab::CosmicTagID_t> getCosmicTag(const pma::Track3D::ETag pmaTag) const;
137 
138  // ******************** fcl parameters **********************
139  art::InputTag fHitModuleLabel; // tag for hits collection (used for trk validation)
140  art::InputTag fWireModuleLabel; // tag for recob::Wire collection (used for trk validation)
141  art::InputTag fCluModuleLabel; // tag for input cluster collection
142  art::InputTag fEmModuleLabel; // tag for em-like cluster collection
143 
149 
150  bool fSaveOnlyBranchingVtx; // for debugging, save only vertices which connect many tracks
151  bool fSavePmaNodes; // for debugging, save only track nodes
152 
153  // ********** instance names (collections, assns) ************
154  static const std::string kKinksName; // kinks on tracks
155  static const std::string kNodesName; // pma nodes
156 
157  // *********************** geometry **************************
159 
160  // histograms created only for the calibration of the ADC-based track validation mode
162  };
163  // -------------------------------------------------------------
164  const std::string PMAlgTrackMaker::kKinksName = "kink";
165  const std::string PMAlgTrackMaker::kNodesName = "node";
166  // -------------------------------------------------------------
167 
169  : EDProducer{config}
170  , fHitModuleLabel(config().HitModuleLabel())
171  , fWireModuleLabel(config().WireModuleLabel())
172  , fCluModuleLabel(config().ClusterModuleLabel())
173  , fEmModuleLabel(config().EmClusterModuleLabel())
174  , fPmaConfig(config().ProjectionMatchingAlg())
175  , fPmaTrackerConfig(config().PMAlgTracking())
176  , fPmaTaggingConfig(config().PMAlgCosmicTagging())
177  , fPmaVtxConfig(config().PMAlgVertexing())
178  , fPmaStitchConfig(config().PMAlgStitching())
179  , fSaveOnlyBranchingVtx(config().SaveOnlyBranchingVtx())
180  , fSavePmaNodes(config().SavePmaNodes())
182  {
183  produces<std::vector<recob::Track>>();
184  produces<std::vector<recob::SpacePoint>>();
185  produces<std::vector<recob::Vertex>>(); // no instance name for interaction vertices
186  produces<std::vector<recob::Vertex>>(kKinksName); // collection of kinks on tracks
187  produces<std::vector<recob::Vertex>>(kNodesName); // collection of pma nodes
188  produces<std::vector<anab::T0>>();
189  produces<std::vector<anab::CosmicTag>>(); // Cosmic ray tags
190 
192  recob::Hit>>(); // ****** REMEMBER to remove when FindMany improved ******
193  produces<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta>>();
194 
195  produces<art::Assns<recob::Track, recob::SpacePoint>>();
196  produces<art::Assns<recob::SpacePoint, recob::Hit>>();
197  produces<
199  recob::Track>>(); // no instance name for assns of tracks to interaction vertices
200  produces<art::Assns<recob::Track, recob::Vertex>>(kKinksName); // assns of kinks to tracks
201  produces<art::Assns<recob::Track, anab::T0>>();
202  produces<art::Assns<recob::Track, anab::CosmicTag>>(); // Cosmic ray tags associated to tracks
203 
204  produces<std::vector<recob::PFParticle>>();
205  produces<art::Assns<recob::PFParticle, recob::Cluster>>();
206  produces<art::Assns<recob::PFParticle, recob::Vertex>>();
207  produces<art::Assns<recob::PFParticle, recob::Track>>();
208 
209  if (fPmaTrackerConfig.Validation() == "calib") // create histograms only in the calibration mode
210  {
212  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout>()->Get();
213  for (size_t p = 0; p < wireReadoutGeom.MaxPlanes(); ++p) {
214  std::ostringstream ss1;
215  ss1 << "adc_plane_" << p;
216  fAdcInPassingPoints.push_back(tfs->make<TH1F>(
217  (ss1.str() + "_passing").c_str(), "max adc around the point on track", 100., 0., 5.));
218  fAdcInRejectedPoints.push_back(tfs->make<TH1F>(
219  (ss1.str() + "_rejected").c_str(), "max adc around spurious point ", 100., 0., 5.));
220  }
221  }
222  }
223  // ------------------------------------------------------
224 
225  template <size_t N>
227  {
228  int pdg = 0;
231  if (hitResults) {
232  int trkLikeIdx = hitResults->getIndex("track");
233  int emLikeIdx = hitResults->getIndex("em");
234  if ((trkLikeIdx < 0) || (emLikeIdx < 0)) {
235  throw cet::exception("PMAlgTrackMaker")
236  << "No em/track labeled columns in MVA data products." << std::endl;
237  }
238 
239  size_t nh[3] = {0, 0, 0};
240  for (size_t hidx = 0; hidx < trk.size(); ++hidx) {
241  ++nh[trk[hidx]->View2D()];
242  }
243 
244  size_t best_view = 2; // collection
245  if ((nh[0] >= nh[1]) && (nh[0] > 1.25 * nh[2])) best_view = 0; // ind1
246  if ((nh[1] >= nh[0]) && (nh[1] > 1.25 * nh[2])) best_view = 1; // ind2
247 
248  std::vector<art::Ptr<recob::Hit>> trkHitPtrList;
249  trkHitPtrList.reserve(nh[best_view]);
250  for (size_t hidx = 0; hidx < trk.size(); ++hidx) {
251  if (trk[hidx]->View2D() == best_view) {
252  trkHitPtrList.emplace_back(trk[hidx]->Hit2DPtr());
253  }
254  }
255  auto vout = hitResults->getOutput(trkHitPtrList);
256  double trk_like = -1, trk_or_em = vout[trkLikeIdx] + vout[emLikeIdx];
257  if (trk_or_em > 0) {
258  trk_like = vout[trkLikeIdx] / trk_or_em;
259  if (trk_like < fPmaTrackerConfig.TrackLikeThreshold()) pdg = 11; // tag if EM-like
260  // (don't set pdg for track-like, for the moment don't like the idea of using "13")
261  }
262  //std::cout << "trk:" << best_view << ":" << trk.size() << ":" << trkHitPtrList.size() << " p=" << trk_like << std::endl;
263  }
264  }
265  return pdg;
266  }
267 
268  template <size_t N>
269  bool PMAlgTrackMaker::init(const art::Event& evt, pma::PMAlgTracker& pmalgTracker) const
270  {
272  if (!cluResults) { return false; }
273 
274  int trkLikeIdx = cluResults->getIndex("track");
275  int emLikeIdx = cluResults->getIndex("em");
276  if ((trkLikeIdx < 0) || (emLikeIdx < 0)) { return false; }
277 
278  const art::FindManyP<recob::Hit> hitsFromClusters(
279  cluResults->dataHandle(), evt, cluResults->dataTag());
280  const auto& cnnOuts = cluResults->outputs();
281  std::vector<float> trackLike(cnnOuts.size());
282  for (size_t i = 0; i < cnnOuts.size(); ++i) {
283  double trkOrEm = cnnOuts[i][trkLikeIdx] + cnnOuts[i][emLikeIdx];
284  if (trkOrEm > 0) { trackLike[i] = cnnOuts[i][trkLikeIdx] / trkOrEm; }
285  else {
286  trackLike[i] = 0;
287  }
288  }
289  pmalgTracker.init(hitsFromClusters, trackLike);
290  return true;
291  }
292 
293  std::vector<anab::CosmicTagID_t> PMAlgTrackMaker::getCosmicTag(
294  const pma::Track3D::ETag pmaTag) const
295  {
296  std::vector<anab::CosmicTagID_t> anabTags;
297 
299  anabTags.push_back(anab::CosmicTagID_t::kOutsideDrift_Partial);
300  }
303  }
304  if (pmaTag & pma::Track3D::kBeamIncompatible) {
306  }
307  if (pmaTag & pma::Track3D::kGeometry_XX) {
308  anabTags.push_back(anab::CosmicTagID_t::kGeometry_XX);
309  }
310  if (pmaTag & pma::Track3D::kGeometry_YY) {
311  anabTags.push_back(anab::CosmicTagID_t::kGeometry_YY);
312  }
313  if (pmaTag & pma::Track3D::kGeometry_ZZ) {
314  anabTags.push_back(anab::CosmicTagID_t::kGeometry_ZZ);
315  }
316  if (pmaTag & pma::Track3D::kGeometry_YZ) {
317  anabTags.push_back(anab::CosmicTagID_t::kGeometry_YZ);
318  }
319  if (pmaTag & pma::Track3D::kGeometry_Y) {
320  anabTags.push_back(anab::CosmicTagID_t::kGeometry_Y);
321  }
322 
323  if (anabTags.empty()) { anabTags.push_back(anab::CosmicTagID_t::kUnknown); }
324 
325  return anabTags;
326  }
327 
329  {
330  // ---------------- Create data products --------------------------
331  auto tracks = std::make_unique<std::vector<recob::Track>>();
332  auto allsp = std::make_unique<std::vector<recob::SpacePoint>>();
333  auto vtxs = std::make_unique<std::vector<recob::Vertex>>(); // interaction vertices
334  auto kinks = std::make_unique<
335  std::vector<recob::Vertex>>(); // kinks on tracks (no new particles start in kinks)
336  auto nodes = std::make_unique<std::vector<recob::Vertex>>(); // pma nodes
337  auto t0s = std::make_unique<std::vector<anab::T0>>();
338  auto cosmicTags = std::make_unique<std::vector<anab::CosmicTag>>();
339 
340  auto trk2hit_oldway = std::make_unique<
342  recob::Hit>>(); // ****** REMEMBER to remove when FindMany improved ******
343  auto trk2hit = std::make_unique<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta>>();
344 
345  auto trk2sp = std::make_unique<art::Assns<recob::Track, recob::SpacePoint>>();
346  auto trk2t0 = std::make_unique<art::Assns<recob::Track, anab::T0>>();
347  auto trk2ct = std::make_unique<art::Assns<recob::Track, anab::CosmicTag>>();
348 
349  auto sp2hit = std::make_unique<art::Assns<recob::SpacePoint, recob::Hit>>();
350  auto vtx2trk = std::make_unique<
352  recob::Track>>(); // one or more tracks (particles) start in the vertex
353  auto trk2kink =
354  std::make_unique<art::Assns<recob::Track, recob::Vertex>>(); // one or more kinks on the track
355 
356  auto pfps = std::make_unique<std::vector<recob::PFParticle>>();
357 
358  auto pfp2clu = std::make_unique<art::Assns<recob::PFParticle, recob::Cluster>>();
359  auto pfp2vtx = std::make_unique<art::Assns<recob::PFParticle, recob::Vertex>>();
360  auto pfp2trk = std::make_unique<art::Assns<recob::PFParticle, recob::Track>>();
361 
362  // --------------------- Wires & Hits -----------------------------
363  auto wireHandle = evt.getValidHandle<std::vector<recob::Wire>>(fWireModuleLabel);
364  auto allHitListHandle = evt.getValidHandle<std::vector<recob::Hit>>(fHitModuleLabel);
365  std::vector<art::Ptr<recob::Hit>> allhitlist;
366  art::fill_ptr_vector(allhitlist, allHitListHandle);
367 
368  // -------------- PMA Tracker for this event ----------------------
369  auto pmalgTracker = pma::PMAlgTracker(allhitlist,
370  *wireHandle,
371  fPmaConfig,
378 
379  size_t mvaLength = 0;
380  if (fEmModuleLabel != "") // ----------- Exclude EM parts ---------
381  {
382  auto cluListHandle = evt.getValidHandle<std::vector<recob::Cluster>>(fCluModuleLabel);
383  auto splitCluHandle = evt.getValidHandle<std::vector<recob::Cluster>>(fEmModuleLabel);
384 
385  art::FindManyP<recob::Hit> hitsFromClusters(cluListHandle, evt, fCluModuleLabel);
386  art::FindManyP<recob::Hit> hitsFromEmParts(splitCluHandle, evt, fEmModuleLabel);
387  pmalgTracker.init(hitsFromClusters, hitsFromEmParts);
388  }
389  else if (fPmaTrackerConfig.TrackLikeThreshold() > 0) // --- CNN EM/trk separation ----
390  {
391  // try to dig out 4- or 3-output MVA data product
392  if (init<4>(evt, pmalgTracker)) { mvaLength = 4; } // e.g.: EM / track / Michel / none
393  else if (init<3>(evt, pmalgTracker)) {
394  mvaLength = 3;
395  } // e.g.: EM / track / none
396  else if (init<2>(evt, pmalgTracker)) {
397  mvaLength = 2;
398  } // just EM / track (LArIAT starts with this style)
399  else {
400  throw cet::exception("PMAlgTrackMaker") << "No EM/track MVA data products." << std::endl;
401  }
402  }
403  else // ------------------------ Use ALL clusters -----------------
404  {
405  auto cluListHandle = evt.getValidHandle<std::vector<recob::Cluster>>(fCluModuleLabel);
406  art::FindManyP<recob::Hit> hitsFromClusters(cluListHandle, evt, fCluModuleLabel);
407  pmalgTracker.init(hitsFromClusters);
408  }
409 
410  // ------------------ Do the job here: ----------------------------
411  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
412  auto const detProp =
414  int retCode = pmalgTracker.build(clockData, detProp);
415  // ----------------------------------------------------------------
416  switch (retCode) {
417  case -2: mf::LogError("Summary") << "problem"; break;
418  case -1: mf::LogWarning("Summary") << "no input"; break;
419  case 0: mf::LogVerbatim("Summary") << "no tracks done"; break;
420  default:
421  if (retCode < 0)
422  mf::LogVerbatim("Summary") << "unknown result";
423  else if (retCode == 1)
424  mf::LogVerbatim("Summary") << retCode << " track ready";
425  else
426  mf::LogVerbatim("Summary") << retCode << " tracks ready";
427  break;
428  }
429 
430  // ---------- Translate output to data products: ------------------
431  auto const& result = pmalgTracker.result();
432  if (!result.empty()) // ok, there is something to save
433  {
434  size_t spStart = 0, spEnd = 0;
435  double sp_pos[3], sp_err[6];
436  for (size_t i = 0; i < 3; ++i)
437  sp_pos[i] = 0.0;
438  for (size_t i = 0; i < 6; ++i)
439  sp_err[i] = 1.0;
440 
441  auto const make_pfpptr = art::PtrMaker<recob::PFParticle>(evt);
442  auto const make_trkptr = art::PtrMaker<recob::Track>(evt); // PtrMaker Step #1
443  auto const make_vtxptr = art::PtrMaker<recob::Vertex>(evt);
444  auto const make_kinkptr = art::PtrMaker<recob::Vertex>(evt, kKinksName);
445  auto const make_t0ptr = art::PtrMaker<anab::T0>(evt);
446  auto const make_ctptr = art::PtrMaker<anab::CosmicTag>(evt);
447 
448  tracks->reserve(result.size());
450  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout>()->Get();
451  for (size_t trkIndex = 0; trkIndex < result.size(); ++trkIndex) {
452  pma::Track3D* trk = result[trkIndex].Track();
453 
454  trk->SelectHits(); // just in case, set all to enabled
455  unsigned int itpc = trk->FrontTPC(), icryo = trk->FrontCryo();
456  pma::dedx_map dedx_tmp;
457  geo::TPCGeo const& tpcgeo = geometry->TPC({icryo, itpc});
458  auto const& tpcid = tpcgeo.ID();
459  auto has_plane = [&wireReadoutGeom, &tpcid](auto const view) {
460  return wireReadoutGeom.HasPlane(geo::PlaneID(tpcid, view));
461  };
462  if (has_plane(geo::kU)) {
463  trk->CompleteMissingWires(detProp, geo::kU);
464  trk->GetRawdEdxSequence(dedx_tmp, geo::kU, 1);
465  }
466  if (has_plane(geo::kV)) {
467  trk->CompleteMissingWires(detProp, geo::kV);
468  trk->GetRawdEdxSequence(dedx_tmp, geo::kV, 1);
469  }
470  if (has_plane(geo::kX)) {
471  trk->CompleteMissingWires(detProp, geo::kX);
472  trk->GetRawdEdxSequence(dedx_tmp, geo::kX, 1);
473  }
474  if (has_plane(geo::kY)) {
475  trk->CompleteMissingWires(detProp, geo::kY);
476  trk->GetRawdEdxSequence(dedx_tmp, geo::kY, 1);
477  }
478  if (has_plane(geo::kZ)) {
479  trk->CompleteMissingWires(detProp, geo::kZ);
480  trk->GetRawdEdxSequence(dedx_tmp, geo::kZ, 1);
481  }
482 
483  //gc: make sure no tracks are created with less than 2 points
484  if (trk->size() < 2) continue;
485 
486  int pdg = 0;
487  if (mvaLength == 4)
488  pdg = getPdgFromCnnOnHits<4>(evt, *(result[trkIndex].Track()));
489  else if (mvaLength == 3)
490  pdg = getPdgFromCnnOnHits<3>(evt, *(result[trkIndex].Track()));
491  else if (mvaLength == 2)
492  pdg = getPdgFromCnnOnHits<2>(evt, *(result[trkIndex].Track()));
493  //else mf::LogInfo("PMAlgTrackMaker") << "Not using PID from CNN.";
494 
495  tracks->push_back(pma::convertFrom(*trk, trkIndex, pdg));
496 
497  auto const trkPtr = make_trkptr(tracks->size() - 1); // PtrMaker Step #2
498 
499  if (trk->HasT0()) {
500  // TriggBits=3 means from 3d reco (0,1,2 mean something else)
501  t0s->push_back(anab::T0(trk->GetT0(), 0, 3, tracks->back().ID()));
502 
503  auto const t0Ptr = make_t0ptr(t0s->size() - 1); // PtrMaker Step #3
504  trk2t0->addSingle(trkPtr, t0Ptr);
505  }
506 
507  // Check if this is a cosmic ray and create an association if it is.
508  if (trk->HasTagFlag(pma::Track3D::kCosmic)) {
509  // Get the track end points
510  std::vector<float> trkEnd0;
511  std::vector<float> trkEnd1;
512  // Get the drift direction, but don't care about the sign
513  // Also need to subtract 1 due to the definition.
514  auto const [axis, _] = tpcgeo.DriftAxisWithSign();
515 
516  for (int i = 0; i < 3; ++i) {
517  // Get the drift direction and apply the opposite of the drift shift in order to
518  // give the CosmicTag the drift coordinate assuming T0 = T_beam as it requests.
519  double shift = 0.0;
520  if (i == to_int(axis)) { shift = trk->Nodes()[0]->GetDriftShift(); }
521  trkEnd0.push_back(trk->Nodes()[0]->Point3D()[i] - shift);
522  trkEnd1.push_back(trk->Nodes()[trk->Nodes().size() - 1]->Point3D()[i] - shift);
523  }
524  // Make the tag object. For now, let's say this is very likely a cosmic (3rd argument = 1).
525  // Set the type of cosmic to the value saved in pma::Track.
526  auto tags = getCosmicTag(trk->GetTag());
527  for (const auto t : tags) {
528  cosmicTags->emplace_back(trkEnd0, trkEnd1, 1, t);
529  auto const cosmicPtr = make_ctptr(cosmicTags->size() - 1);
530  trk2ct->addSingle(trkPtr, cosmicPtr);
531  }
532  }
533 
534  //gc: save associated hits in the same order as trajectory points
535  for (size_t h = 0, cnt = 0; h < trk->size(); h++) {
536  pma::Hit3D* h3d = (*trk)[h];
537  if (!h3d->IsEnabled()) continue;
538 
539  recob::TrackHitMeta metadata(cnt++, h3d->Dx());
540  trk2hit->addSingle(trkPtr, h3d->Hit2DPtr(), metadata);
541  trk2hit_oldway->addSingle(
542  trkPtr, h3d->Hit2DPtr()); // ****** REMEMBER to remove when FindMany improved ******
543  }
544 
546  spStart = allsp->size();
547  //rs: so make also associations to space points in the order of trajectory points
548  for (size_t h = 0; h < trk->size(); ++h) {
549  pma::Hit3D* h3d = (*trk)[h];
550  if (!h3d->IsEnabled()) continue;
551 
552  double hx = h3d->Point3D().X();
553  double hy = h3d->Point3D().Y();
554  double hz = h3d->Point3D().Z();
555 
556  if ((h == 0) || (std::fabs(sp_pos[0] - hx) > 1.0e-5) ||
557  (std::fabs(sp_pos[1] - hy) > 1.0e-5) || (std::fabs(sp_pos[2] - hz) > 1.0e-5)) {
558  if (sp_hits.size()) // hits assigned to the previous sp
559  {
560  util::CreateAssn(evt, *allsp, sp_hits, *sp2hit);
561  sp_hits.clear();
562  }
563  sp_pos[0] = hx;
564  sp_pos[1] = hy;
565  sp_pos[2] = hz;
566  allsp->push_back(recob::SpacePoint(sp_pos, sp_err, 1.0));
567  }
568  sp_hits.push_back(h3d->Hit2DPtr());
569  }
570 
571  if (sp_hits.size()) // hits assigned to the last sp
572  {
573  util::CreateAssn(evt, *allsp, sp_hits, *sp2hit);
574  }
575  spEnd = allsp->size();
576 
577  if (spEnd > spStart) util::CreateAssn(evt, *tracks, *allsp, *trk2sp, spStart, spEnd);
578  }
579 
580  auto vsel = pmalgTracker.getVertices(
581  fSaveOnlyBranchingVtx); // vtx pos's with vector of connected track idxs
582  auto ksel = pmalgTracker.getKinks(); // pairs of kink position - associated track idx
583  std::map<size_t, art::Ptr<recob::Vertex>> frontVtxs; // front vertex ptr for each track index
584 
585  if (fPmaTrackerConfig.RunVertexing()) // save vertices and vtx-trk assns
586  {
587  double xyz[3];
588  for (auto const& v : vsel) {
589  xyz[0] = v.first.X();
590  xyz[1] = v.first.Y();
591  xyz[2] = v.first.Z();
592  mf::LogVerbatim("Summary") << " vtx:" << xyz[0] << ":" << xyz[1] << ":" << xyz[2]
593  << " (" << v.second.size() << " tracks)";
594 
595  size_t vidx = vtxs->size();
596  vtxs->push_back(recob::Vertex(xyz, vidx));
597 
598  auto const vptr = make_vtxptr(vidx);
599  if (!v.second.empty()) {
600  for (const auto& vEntry : v.second) {
601  size_t tidx = vEntry.first;
602  bool isFront = vEntry.second;
603 
604  if (isFront) frontVtxs[tidx] = vptr; // keep ptr of the front vtx
605 
606  auto const tptr = make_trkptr(tidx);
607  vtx2trk->addSingle(vptr, tptr);
608  }
609  }
610  else
611  mf::LogWarning("PMAlgTrackMaker") << "No tracks found at this vertex.";
612  }
613  mf::LogVerbatim("Summary") << vtxs->size() << " vertices ready";
614 
615  for (auto const& k : ksel) {
616  xyz[0] = k.first.X();
617  xyz[1] = k.first.Y();
618  xyz[2] = k.first.Z();
619  mf::LogVerbatim("Summary") << " kink:" << xyz[0] << ":" << xyz[1] << ":" << xyz[2];
620 
621  size_t kidx = kinks->size();
622  size_t tidx = k.second; // track idx on which this kink was found
623 
624  kinks->push_back(
625  recob::Vertex(xyz, tidx)); // save index of track (will have color of trk in evd)
626 
627  auto const tptr = make_trkptr(tidx);
628  auto const kptr = make_kinkptr(kidx);
629  trk2kink->addSingle(tptr, kptr);
630  }
631  mf::LogVerbatim("Summary") << ksel.size() << " kinks ready";
632  }
633 
634  if (fSavePmaNodes) {
635  double xyz[3];
636  for (size_t t = 0; t < result.size(); ++t) {
637  auto const& trk = *(result[t].Track());
638  for (auto const* node : trk.Nodes()) {
639  xyz[0] = node->Point3D().X();
640  xyz[1] = node->Point3D().Y();
641  xyz[2] = node->Point3D().Z();
642  nodes->push_back(recob::Vertex(xyz, t));
643  }
644  }
645  }
646 
647  for (size_t t = 0; t < result.size(); ++t) {
648  size_t parentIdx = recob::PFParticle::kPFParticlePrimary;
649  if (result[t].Parent() >= 0) parentIdx = (size_t)result[t].Parent();
650 
651  std::vector<size_t> daughterIdxs;
652  for (size_t idx : result[t].Daughters()) {
653  daughterIdxs.push_back(idx);
654  }
655 
656  size_t pfpidx = pfps->size();
657  pfps->emplace_back((*tracks)[t].ParticleId(), pfpidx, parentIdx, daughterIdxs);
658 
659  auto const pfpptr = make_pfpptr(pfpidx);
660  auto const tptr = make_trkptr(t);
661  pfp2trk->addSingle(pfpptr, tptr);
662 
663  // add assns to FRONT vertex of each particle
665  art::Ptr<recob::Vertex> vptr = frontVtxs[t];
666  if (!vptr.isNull())
667  pfp2vtx->addSingle(pfpptr, vptr);
668  else
669  mf::LogWarning("PMAlgTrackMaker") << "Front vertex for PFParticle is missing.";
670  }
671  }
672  mf::LogVerbatim("Summary") << pfps->size()
673  << " PFParticles created for reconstructed tracks.";
674  mf::LogVerbatim("Summary") << "Adding " << result.parents().size() << " primary PFParticles.";
675  for (size_t t = 0; t < result.parents().size(); ++t) {
676  std::vector<size_t> daughterIdxs;
677  for (size_t idx : result.parents()[t].Daughters()) {
678  daughterIdxs.push_back(idx);
679  }
680 
681  size_t pfpidx = pfps->size();
682  size_t parentIdx = recob::PFParticle::kPFParticlePrimary;
683  pfps->emplace_back(0, pfpidx, parentIdx, daughterIdxs);
684 
685  // add assns to END vertex of primary
686  if (fPmaTrackerConfig.RunVertexing() && !daughterIdxs.empty()) {
687  auto const pfpptr = make_pfpptr(pfpidx);
689  frontVtxs[daughterIdxs.front()]; // same vertex for all daughters
690  if (!vptr.isNull())
691  pfp2vtx->addSingle(pfpptr, vptr);
692  else
693  mf::LogWarning("PMAlgTrackMaker") << "Front vertex for PFParticle is missing.";
694  }
695  }
696  mf::LogVerbatim("Summary") << pfps->size() << " PFParticles created in total.";
697  }
698 
699  // for (const auto & ct : *cosmicTags) { std::cout << "Cosmic tag: " << ct << std::endl; }
700 
701  evt.put(std::move(tracks));
702  evt.put(std::move(allsp));
703  evt.put(std::move(vtxs));
704  evt.put(std::move(kinks), kKinksName);
705  evt.put(std::move(nodes), kNodesName);
706  evt.put(std::move(t0s));
707  evt.put(std::move(cosmicTags));
708 
709  evt.put(std::move(trk2hit_oldway)); // ****** REMEMBER to remove when FindMany improved ******
710  evt.put(std::move(trk2hit));
711  evt.put(std::move(trk2sp));
712  evt.put(std::move(trk2t0));
713  evt.put(std::move(trk2ct));
714 
715  evt.put(std::move(sp2hit));
716  evt.put(std::move(vtx2trk));
717  evt.put(std::move(trk2kink), kKinksName);
718 
719  evt.put(std::move(pfps));
720  evt.put(std::move(pfp2clu));
721  evt.put(std::move(pfp2vtx));
722  evt.put(std::move(pfp2trk));
723  }
724  // ------------------------------------------------------
725  // ------------------------------------------------------
726  // ------------------------------------------------------
727 
729 
730 } // namespace trkf
PMAlgTrackMaker & operator=(PMAlgTrackMaker const &)=delete
bool SelectHits(float fraction=1.0F)
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
static const std::string kKinksName
void produce(art::Event &e) override
recob::Track convertFrom(const pma::Track3D &src, unsigned int tidx, int pdg=0)
fhicl::Atom< art::InputTag > EmClusterModuleLabel
static constexpr size_t kPFParticlePrimary
Define index to signify primary particle.
Definition: PFParticle.h:57
Planes which measure V.
Definition: geo_types.h:132
Unknown view.
Definition: geo_types.h:138
Declaration of signal hit object.
fhicl::Atom< art::InputTag > WireModuleLabel
bool IsEnabled() const noexcept
Definition: PmaHit3D.h:86
fhicl::Atom< art::InputTag > HitModuleLabel
fhicl::Table< pma::PMAlgVertexing::Config > PMAlgVertexing
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
Planes which measure X direction.
Definition: geo_types.h:136
The data type to uniquely identify a Plane.
Definition: geo_types.h:364
fhicl::Table< pma::PMAlgCosmicTagger::Config > PMAlgCosmicTagging
Geometry information for a single TPC.
Definition: TPCGeo.h:33
fhicl::Table< pma::ProjectionMatchingAlg::Config > ProjectionMatchingAlg
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
fhicl::Atom< std::string > Validation
fhicl::Atom< art::InputTag > ClusterModuleLabel
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:134
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
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:16
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
DriftAxis DriftAxisWithSign() const
Returns the expected drift direction based on geometry.
Definition: TPCGeo.h:78
Planes which measure Y direction.
Definition: geo_types.h:135
std::vector< anab::CosmicTagID_t > getCosmicTag(const pma::Track3D::ETag pmaTag) const
geo::GeometryCore const * fGeom
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
Planes which measure U.
Definition: geo_types.h:131
double GetT0() const
Definition: PmaTrack3D.h:257
pma::PMAlgStitching::Config fPmaStitchConfig
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
pma::ProjectionMatchingAlg::Config fPmaConfig
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:435
bool HasTagFlag(ETag value) const noexcept
Definition: PmaTrack3D.h:67
pma::PMAlgVertexing::Config fPmaVtxConfig
int getPdgFromCnnOnHits(const art::Event &evt, const pma::Track3D &trk) const
bool isNull() const noexcept
Definition: Ptr.h:211
constexpr int to_int(Coordinate const coord) noexcept
Enumerate the possible plane projections.
Definition: geo_types.h:124
unsigned int FrontTPC() const
Definition: PmaTrack3D.h:118
unsigned int FrontCryo() const
Definition: PmaTrack3D.h:119
pma::PMAlgCosmicTagger::Config fPmaTaggingConfig
Description of the physical geometry of one entire detector.
Definition: GeometryCore.h:91
Declaration of cluster object.
ETag GetTag() const noexcept
Definition: PmaTrack3D.h:66
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.
std::map< size_t, std::vector< double > > dedx_map
Definition: Utilities.h:36
fhicl::Atom< float > TrackLikeThreshold
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
double GetRawdEdxSequence(std::map< size_t, std::vector< double >> &dedx, unsigned int view=geo::kZ, unsigned int skip=0, bool inclDisabled=false) const
Utility object to perform functions of association.
Provides recob::Track data product.
void init(const art::FindManyP< recob::Hit > &hitsFromClusters)
double Dx() const noexcept
Definition: PmaHit3D.h:69
bool HasT0() const noexcept
Definition: PmaTrack3D.h:260
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
art::Ptr< recob::Hit > const & Hit2DPtr() const
Definition: PmaHit3D.h:48
std::vector< TH1F * > fAdcInRejectedPoints
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
std::vector< pma::Node3D * > const & Nodes() const noexcept
Definition: PmaTrack3D.h:274
pma::PMAlgTracker::Config fPmaTrackerConfig
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
TPCGeo const & TPC(TPCID const &tpcid=details::tpc_zero) const
Returns the specified TPC.
Definition: GeometryCore.h:448
Float_t e
Definition: plot.C:35
size_t size() const
Definition: PmaTrack3D.h:89
void clear()
Definition: PtrVector.h:533
TPCID const & ID() const
Returns the identifier of this TPC.
Definition: TPCGeo.h:147
PMAlgTrackMaker(Parameters const &config)
Definition: fwd.h:26
static std::unique_ptr< MVAReader > create(const art::Event &evt, const art::InputTag &tag)
Definition: MVAReader.h:108
art framework interface to geometry description
std::vector< TH1F * > fAdcInPassingPoints
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Encapsulate the construction of a single detector plane .
static const std::string kNodesName