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 
23 #include "fhiclcpp/types/Atom.h"
24 #include "fhiclcpp/types/Table.h"
26 
27 // LArSoft includes
40 
42 
43 #include <memory>
44 
45 namespace trkf {
46 
48  public:
49  struct Config {
50  using Name = fhicl::Name;
52 
54  Name("ProjectionMatchingAlg")};
55 
57 
59 
61  Name("HitModuleLabel"),
62  Comment("tag of unclustered hits, which were used to produce PFPs and clusters")};
63 
65  Name("PfpModuleLabel"),
66  Comment("tag of the input PFParticles and associated clusters")};
67 
69  Name("SaveOnlyBranchingVtx"),
70  Comment("use true to save only vertices interconnecting many tracks, otherwise vertex is "
71  "added to the front of each track")};
72 
74  Name("SavePmaNodes"),
75  Comment("save track nodes (only for algorithm development purposes)")};
76  };
78 
79  explicit PMAlgTrajFitter(Parameters const& config);
80 
81  PMAlgTrajFitter(PMAlgTrajFitter const&) = delete;
82  PMAlgTrajFitter(PMAlgTrajFitter&&) = delete;
83  PMAlgTrajFitter& operator=(PMAlgTrajFitter const&) = delete;
85 
86  private:
87  void produce(art::Event& e) override;
88 
89  // ******************** fcl parameters ***********************
90  art::InputTag fHitModuleLabel; // tag for unclustered hit collection
91  art::InputTag fPfpModuleLabel; // tag for PFParticle and cluster collections
92 
96 
97  bool fSaveOnlyBranchingVtx; // for debugging, save only vertices which connect many tracks
98  bool fSavePmaNodes; // for debugging, save only track nodes
99 
100  // ********** instance names (collections, assns) ************
101  static const std::string kKinksName; // kinks on tracks
102  static const std::string kNodesName; // pma nodes
103 
104  // *********************** geometry **************************
106  };
107  // -------------------------------------------------------------
110  // -------------------------------------------------------------
111 
113  : EDProducer{config}
114  , fHitModuleLabel(config().HitModuleLabel())
115  , fPfpModuleLabel(config().PfpModuleLabel())
116  ,
117 
118  fPmaConfig(config().ProjectionMatchingAlg())
119  , fPmaFitterConfig(config().PMAlgFitting())
120  , fPmaVtxConfig(config().PMAlgVertexing())
121  ,
122 
123  fSaveOnlyBranchingVtx(config().SaveOnlyBranchingVtx())
124  , fSavePmaNodes(config().SavePmaNodes())
125  {
126  produces<std::vector<recob::Track>>();
127  produces<std::vector<recob::SpacePoint>>();
128  produces<std::vector<recob::Vertex>>(); // no instance name for interaction vertices
129  produces<std::vector<recob::Vertex>>(kKinksName); // collection of kinks on tracks
130  produces<std::vector<recob::Vertex>>(kNodesName); // collection of pma nodes
131 
133  recob::Hit>>(); // ****** REMEMBER to remove when FindMany improved ******
134  produces<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta>>();
135 
136  produces<art::Assns<recob::Track, recob::SpacePoint>>();
137  produces<art::Assns<recob::SpacePoint, recob::Hit>>();
138  produces<
140  recob::Track>>(); // no instance name for assns of tracks to interaction vertices
141  produces<art::Assns<recob::Track, recob::Vertex>>(kKinksName); // assns of kinks to tracks
142 
143  produces<art::Assns<recob::PFParticle, recob::Track>>();
144  }
145  // ------------------------------------------------------
146 
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<
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<
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;
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 =
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 
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 
423 
424 } // namespace trkf
fhicl::Table< pma::ProjectionMatchingAlg::Config > ProjectionMatchingAlg
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:175
static QCString result
ProductID getProductID(std::string const &instance_name="") const
Definition: DataViewImpl.h:338
size_t Self() const
Returns the index of this particle.
Definition: PFParticle.h:92
recob::Track convertFrom(const pma::Track3D &src, unsigned int tidx, int pdg=0)
std::string string
Definition: nybbler.cc:12
Planes which measure V.
Definition: geo_types.h:130
bool IsEnabled() const noexcept
Definition: PmaHit3D.h:161
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.h:20
ChannelGroupService::Name Name
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
Class to keep data related to recob::Hit associated with recob::Track.
TVector3 const & Point3D() const
Definition: PmaHit3D.h: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
uint size() const
Definition: qcstring.h:201
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
void produce(art::Event &e) override
art framework interface to geometry description
Planes which measure U.
Definition: geo_types.h:129
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
const double e
fhicl::Table< pma::PMAlgFitter::Config > PMAlgFitting
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
#define nodes
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:435
static Config * config
Definition: config.cpp:1054
fhicl::Atom< bool > RunVertexing
def move(depos, offset)
Definition: depos.py:107
EDProductGetter const * productGetter(ProductID const pid) const
key_type key() const noexcept
Definition: Ptr.h:216
bool isNull() const noexcept
Definition: Ptr.h:173
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
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.
fhicl::Table< pma::PMAlgVertexing::Config > PMAlgVertexing
unsigned int FrontTPC() const
Definition: PmaTrack3D.h:145
unsigned int FrontCryo() const
Definition: PmaTrack3D.h:150
size_type size() const
Definition: PtrVector.h:302
size_t CompleteMissingWires(detinfo::DetectorPropertiesData const &detProp, unsigned int view)
Definition: tracks.py:1
pma::PMAlgVertexing::Config fPmaVtxConfig
Declaration of signal hit object.
#define Comment
static const std::string kNodesName
double Dx() const noexcept
Definition: PmaHit3D.h:130
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
pma::ProjectionMatchingAlg::Config fPmaConfig
Provides recob::Track data product.
art::Ptr< recob::Hit > const & Hit2DPtr() const
Definition: PmaHit3D.h:49
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:1036
TPCGeo const & TPC(unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified TPC.
2D representation of charge deposited in the TDC/wire plane
Definition: Hit.h:48
TCEvent evt
Definition: DataStructs.cxx:7
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
size_t size() const
Definition: PmaTrack3D.h:108
PMAlgTrajFitter(Parameters const &config)
static const std::string kKinksName
fhicl::Atom< art::InputTag > HitModuleLabel
void clear()
Definition: PtrVector.h:533
PMAlgTrajFitter & operator=(PMAlgTrajFitter const &)=delete
pma::PMAlgFitter::Config fPmaFitterConfig
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
Encapsulate the construction of a single detector plane.
fhicl::Atom< art::InputTag > PfpModuleLabel