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