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