All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackProducerFromPFParticle_module.cc
Go to the documentation of this file.
1 #include "art/Framework/Core/EDProducer.h"
2 #include "art/Framework/Core/ModuleMacros.h"
3 #include "art/Framework/Principal/Event.h"
4 #include "art/Framework/Principal/Handle.h"
5 #include "art/Framework/Principal/Run.h"
6 #include "art/Framework/Principal/SubRun.h"
7 #include "art/Persistency/Common/PtrMaker.h"
8 #include "art/Utilities/make_tool.h"
9 #include "canvas/Persistency/Common/FindManyP.h"
10 #include "canvas/Utilities/InputTag.h"
11 #include "cetlib_except/exception.h"
12 #include "fhiclcpp/ParameterSet.h"
13 #include "messagefacility/MessageLogger/MessageLogger.h"
14 
15 #include <memory>
16 
25 
26 /**
27  * @file larreco/TrackFinder/TrackProducerFromPFParticle_module.cc
28  * @class TrackProducerFromPFParticle
29  *
30  * @brief Produce a reco::Track collection, as a result of the fit of an existing recob::PFParticle collection.
31  *
32  * This producer takes an input an existing recob::PFParticle collection and refits the associated tracks;
33  * it can make track fits of the associated showers as well, but this is experimental - do it at your own risk.
34  * The mandatory outputs are: the resulting recob::Track collection, the associated hits, and the association
35  * between the input PFParticle and the output Track.
36  * Optional outputs are recob::TrackFitHitInfo and recob::SpacePoint collections, plus the Assns of SpacePoints to Hits.
37  * An option is provided to create SpacePoints from the TrajectoryPoints in the Track.
38  * The fit is performed by an user-defined tool, which must inherit from larreco/TrackFinder/TrackMaker.
39  *
40  * Parameters: trackMaker (fhicl::ParameterSet for the trkmkr::TrackMaker tool used to do the fit), inputCollection (art::InputTag of the input recob::Track collection),
41  * doTrackFitHitInfo (bool to decide whether to produce recob::TrackFitHitInfo's), doSpacePoints (bool to decide whether to produce recob::SpacePoint's),
42  * spacePointsFromTrajP (bool to decide whether the produced recob::SpacePoint's are taken from the recob::tracking::TrajectoryPoint_t's of the fitted recob::Track),
43  * trackFromPF (bool to decide whether to fit the recob::Track associated to the recob::PFParticle), and
44  * showerFromPF (bool to decide whether to fit the recob::Shower associated to the recob::PFParticle - this option is intended to mitigate possible problems due to tracks being mis-identified as showers)
45  * seedFromPF (bool to decide whether to fit the recob::PFParticle using the associated seed)
46  *
47  * @author G. Cerati (FNAL, MicroBooNE)
48  * @date 2017
49  * @version 1.0
50  */
51 //
52 //
53 class TrackProducerFromPFParticle : public art::EDProducer {
54 public:
55  explicit TrackProducerFromPFParticle(fhicl::ParameterSet const& p);
56  // The compiler-generated destructor is fine for non-base
57  // classes without bare pointers or other resource use.
58  //
59  // Plugins should not be copied or assigned.
64 
65 private:
66  // Required functions.
67  void produce(art::Event& e) override;
68  std::unique_ptr<trkmkr::TrackMaker> trackMaker_;
69  art::InputTag pfpInputTag;
70  art::InputTag trkInputTag;
71  art::InputTag shwInputTag;
72  art::InputTag clsInputTag;
79 };
80 //
82  : EDProducer{p}
83  , trackMaker_{art::make_tool<trkmkr::TrackMaker>(p.get<fhicl::ParameterSet>("trackMaker"))}
84  , pfpInputTag{p.get<art::InputTag>("inputCollection")}
85  , doTrackFitHitInfo_{p.get<bool>("doTrackFitHitInfo")}
86  , doSpacePoints_{p.get<bool>("doSpacePoints")}
87  , spacePointsFromTrajP_{p.get<bool>("spacePointsFromTrajP")}
88  , trackFromPF_{p.get<bool>("trackFromPF")}
89  , showerFromPF_{p.get<bool>("showerFromPF")}
90  , seedFromPF_{p.get<bool>("seedFromPF")}
91 {
92  //
93  if (p.has_key("trackInputTag"))
94  trkInputTag = p.get<art::InputTag>("trackInputTag");
95  else
96  trkInputTag = pfpInputTag;
97  if (p.has_key("showerInputTag"))
98  shwInputTag = p.get<art::InputTag>("showerInputTag");
99  else
100  shwInputTag = pfpInputTag;
101  if (p.has_key("clusterInputTag"))
102  clsInputTag = p.get<art::InputTag>("clusterInputTag");
103  else
104  clsInputTag = pfpInputTag;
105  produces<std::vector<recob::Track>>();
106  produces<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta>>();
107  produces<art::Assns<recob::PFParticle, recob::Track>>();
108  if (doTrackFitHitInfo_) produces<std::vector<std::vector<recob::TrackFitHitInfo>>>();
109  if (doSpacePoints_) {
110  produces<std::vector<recob::SpacePoint>>();
111  produces<art::Assns<recob::Hit, recob::SpacePoint>>();
112  }
113 }
114 //
115 void
117 {
118  // Output collections
119  auto outputTracks = std::make_unique<std::vector<recob::Track>>();
120  auto outputHits = std::make_unique<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta>>();
121  auto outputPfpTAssn = std::make_unique<art::Assns<recob::PFParticle, recob::Track>>();
122  auto outputHitInfo = std::make_unique<std::vector<std::vector<recob::TrackFitHitInfo>>>();
123  auto outputSpacePoints = std::make_unique<std::vector<recob::SpacePoint>>();
124  auto outputHitSpacePointAssn = std::make_unique<art::Assns<recob::Hit, recob::SpacePoint>>();
125  //
126  // PtrMakers for Assns
127  art::PtrMaker<recob::Track> trackPtrMaker(e);
128  art::PtrMaker<recob::SpacePoint>* spacePointPtrMaker = nullptr;
129  if (doSpacePoints_) spacePointPtrMaker = new art::PtrMaker<recob::SpacePoint>(e);
130  //
131  // Input from event
132  art::ValidHandle<std::vector<recob::PFParticle>> inputPfps =
133  e.getValidHandle<std::vector<recob::PFParticle>>(pfpInputTag);
134  std::unique_ptr<art::FindManyP<recob::Track>> assocTracks;
135  art::Assns<recob::Track, recob::Hit> tkHitsAssn;
136  std::unique_ptr<art::FindManyP<recob::Shower>> assocShowers;
137  std::unique_ptr<art::FindManyP<recob::Seed>> assocSeeds;
138  if (trackFromPF_) {
139  assocTracks = std::unique_ptr<art::FindManyP<recob::Track>>(
140  new art::FindManyP<recob::Track>(inputPfps, e, trkInputTag));
141  tkHitsAssn = *e.getValidHandle<art::Assns<recob::Track, recob::Hit>>(trkInputTag);
142  }
143  if (showerFromPF_)
144  assocShowers = std::unique_ptr<art::FindManyP<recob::Shower>>(
145  new art::FindManyP<recob::Shower>(inputPfps, e, shwInputTag));
146  if (seedFromPF_)
147  assocSeeds = std::unique_ptr<art::FindManyP<recob::Seed>>(
148  new art::FindManyP<recob::Seed>(inputPfps, e, pfpInputTag));
149  const auto& trackHitsGroups = util::associated_groups(tkHitsAssn);
150 
151  std::unique_ptr<art::FindManyP<recob::Cluster>> assocClusters =
152  std::unique_ptr<art::FindManyP<recob::Cluster>>(
153  new art::FindManyP<recob::Cluster>(inputPfps, e, pfpInputTag));
154  auto const& clHitsAssn = *e.getValidHandle<art::Assns<recob::Cluster, recob::Hit>>(clsInputTag);
155  const auto& clusterHitsGroups = util::associated_groups(clHitsAssn);
156 
157  // Initialize tool for this event
158  trackMaker_->initEvent(e);
159 
160  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(e);
161 
162  // Loop over pfps to fit
163  for (unsigned int iPfp = 0; iPfp < inputPfps->size(); ++iPfp) {
164  const art::Ptr<recob::PFParticle> pfp(inputPfps, iPfp);
165  if (trackFromPF_) {
166  // Tracks associated to PFParticles
167  const std::vector<art::Ptr<recob::Track>>& tracks = assocTracks->at(iPfp);
168  // Loop over tracks to refit
169  for (art::Ptr<recob::Track> const& track : tracks) {
170 
171  // Get track and its hits
172  std::vector<art::Ptr<recob::Hit>> inHits;
173  decltype(auto) hitsRange = util::groupByIndex(trackHitsGroups, track.key());
174  for (art::Ptr<recob::Hit> const& hit : hitsRange)
175  inHits.push_back(hit);
176 
177  // Declare output objects
178  recob::Track outTrack;
179  std::vector<art::Ptr<recob::Hit>> outHits;
180  trkmkr::OptionalOutputs optionals;
181  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
183 
184  // Invoke tool to fit track and fill output objects
185  bool fitok = trackMaker_->makeTrack(detProp, track, inHits, outTrack, outHits, optionals);
186  if (!fitok) continue;
187 
188  // Check that the requirement Nhits == Npoints is satisfied
189  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
190  if (outTrack.NumberTrajectoryPoints() != outHits.size()) {
191  throw cet::exception("TrackProducerFromPFParticle")
192  << "Produced recob::Track required to have 1-1 correspondance between hits and "
193  "points.\n";
194  }
195 
196  // Fill output collections, including Assns
197  outputTracks->emplace_back(std::move(outTrack));
198  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size() - 1);
199  outputPfpTAssn->addSingle(pfp, aptr);
200  unsigned int ip = 0;
201  for (auto const& trhit : outHits) {
202  recob::TrackHitMeta metadata(
203  outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(),
204  -std::numeric_limits<double>::max());
205  outputHits->addSingle(aptr, trhit, metadata);
206 
207  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
208  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
209  const double fXYZ[3] = {tp.X(), tp.Y(), tp.Z()};
210  const double fErrXYZ[6] = {0};
211  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
212  outputSpacePoints->emplace_back(std::move(sp));
213  const art::Ptr<recob::SpacePoint> apsp =
214  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
215  outputHitSpacePointAssn->addSingle(trhit, apsp);
216  }
217  ip++;
218  }
220  auto osp = optionals.spacePointHitPairs();
221  for (auto it = osp.begin(); it != osp.end(); ++it) {
222  outputSpacePoints->emplace_back(std::move(it->first));
223  const art::Ptr<recob::SpacePoint> apsp =
224  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
225  outputHitSpacePointAssn->addSingle(it->second, apsp);
226  }
227  }
228  if (doTrackFitHitInfo_) { outputHitInfo->emplace_back(optionals.trackFitHitInfos()); }
229  }
230  }
231  //
232  if (showerFromPF_) {
233  //
234  // Showers associated to PFParticles
235  const std::vector<art::Ptr<recob::Shower>>& showers = assocShowers->at(iPfp);
236  // if there is more than one shower the logic below to get the hits does not work! this works, at least for uboone
237  if (showers.size() != 1) continue;
238  //
239  // Get hits for shower (through the chain pfp->clusters->hits)
240  std::vector<art::Ptr<recob::Hit>> inHits;
241  const std::vector<art::Ptr<recob::Cluster>> clustersRange = assocClusters->at(iPfp);
242  for (art::Ptr<recob::Cluster> const& cluster : clustersRange) {
243  // for hits we use groupByIndex since it preserves the order (and we can use it since each cluster must have associated hits)
244  decltype(auto) hitsRange = util::groupByIndex(clusterHitsGroups, cluster.key());
245  for (art::Ptr<recob::Hit> const& hit : hitsRange)
246  inHits.push_back(hit);
247  }
248  // Loop over showers to refit (should be only one)
249  for (unsigned int iShower = 0; iShower < showers.size(); ++iShower) {
250  //
251  // Get the shower and convert/hack it into a trajectory so that the fit is initialized
252  art::Ptr<recob::Shower> shower = showers[iShower];
254  shower->ShowerStart().X(), shower->ShowerStart().Y(), shower->ShowerStart().Z());
256  shower->Direction().X(), shower->Direction().Y(), shower->Direction().Z());
257  float mom = 1.;
258  if (shower->Energy().size() == 3) mom = shower->Energy()[2] * 0.001;
259  std::vector<recob::tracking::Point_t> p;
260  std::vector<recob::tracking::Vector_t> d;
261  for (unsigned int i = 0; i < inHits.size(); ++i) {
262  p.push_back(pos);
263  d.push_back(mom * dir);
264  }
266  std::move(p), std::move(d), recob::TrackTrajectory::Flags_t(p.size()), false);
267  //
268  // Declare output objects
269  recob::Track outTrack;
270  std::vector<art::Ptr<recob::Hit>> outHits;
271  trkmkr::OptionalOutputs optionals;
272  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
274  //
275  // Invoke tool to fit track and fill output objects
276  bool fitok =
277  trackMaker_->makeTrack(detProp, traj, iPfp, inHits, outTrack, outHits, optionals);
278  if (!fitok) continue;
279  //
280  // Check that the requirement Nhits == Npoints is satisfied
281  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
282  if (outTrack.NumberTrajectoryPoints() != outHits.size()) {
283  throw cet::exception("TrackProducerFromPFParticle")
284  << "Produced recob::Track required to have 1-1 correspondance between hits and "
285  "points.\n";
286  }
287  //
288  // Fill output collections, including Assns
289  outputTracks->emplace_back(std::move(outTrack));
290  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size() - 1);
291  outputPfpTAssn->addSingle(pfp, aptr);
292  unsigned int ip = 0;
293  for (auto const& trhit : outHits) {
294  recob::TrackHitMeta metadata(
295  outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(),
296  -std::numeric_limits<double>::max());
297  outputHits->addSingle(aptr, trhit, metadata);
298  //
299  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
300  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
301  const double fXYZ[3] = {tp.X(), tp.Y(), tp.Z()};
302  const double fErrXYZ[6] = {0};
303  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
304  outputSpacePoints->emplace_back(std::move(sp));
305  const art::Ptr<recob::SpacePoint> apsp =
306  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
307  outputHitSpacePointAssn->addSingle(trhit, apsp);
308  }
309  ip++;
310  }
312  auto osp = optionals.spacePointHitPairs();
313  for (auto it = osp.begin(); it != osp.end(); ++it) {
314  outputSpacePoints->emplace_back(std::move(it->first));
315  const art::Ptr<recob::SpacePoint> apsp =
316  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
317  outputHitSpacePointAssn->addSingle(it->second, apsp);
318  }
319  }
320  if (doTrackFitHitInfo_) { outputHitInfo->emplace_back(optionals.trackFitHitInfos()); }
321  }
322  }
323  //
324  //
325  if (seedFromPF_) {
326  //
327  // Seeds associated to PFParticles
328  const std::vector<art::Ptr<recob::Seed>>& seeds = assocSeeds->at(iPfp);
329  // if there is more than one seed the logic below to get the hits does not work! this works, at least for uboone
330  if (seeds.size() != 1) continue;
331  //
332  // Get hits for pfp (through the chain pfp->clusters->hits)
333  std::vector<art::Ptr<recob::Hit>> inHits;
334  const std::vector<art::Ptr<recob::Cluster>> clustersRange = assocClusters->at(iPfp);
335  for (art::Ptr<recob::Cluster> const& cluster : clustersRange) {
336  // for hits we use groupByIndex since it preserves the order (and we can use it since each cluster must have associated hits)
337  decltype(auto) hitsRange = util::groupByIndex(clusterHitsGroups, cluster.key());
338  for (art::Ptr<recob::Hit> const& hit : hitsRange)
339  inHits.push_back(hit);
340  }
341  if (inHits.size() < 4) continue;
342  // Loop over seeds should be only one)
343  for (unsigned int iS = 0; iS < seeds.size(); ++iS) {
344  //
345  // Get the seed and convert/hack it into a trajectory so that the fit is initialized
346  art::Ptr<recob::Seed> seed = seeds[iS];
347  double p0[3], pe[3];
348  seed->GetPoint(p0, pe);
349  double d0[3], de[3];
350  seed->GetDirection(d0, de);
351  recob::tracking::Point_t pos(p0[0], p0[1], p0[2]);
352  recob::tracking::Vector_t dir(d0[0], d0[1], d0[2]);
353  std::vector<recob::tracking::Point_t> p;
354  std::vector<recob::tracking::Vector_t> d;
355  for (unsigned int i = 0; i < inHits.size(); ++i) {
356  p.push_back(pos);
357  d.push_back(dir);
358  }
360  std::move(p), std::move(d), recob::TrackTrajectory::Flags_t(p.size()), false);
361  //
362  // Declare output objects
363  recob::Track outTrack;
364  std::vector<art::Ptr<recob::Hit>> outHits;
365  trkmkr::OptionalOutputs optionals;
366  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
368  //
369  // Invoke tool to fit track and fill output objects
370  bool fitok =
371  trackMaker_->makeTrack(detProp, traj, iPfp, inHits, outTrack, outHits, optionals);
372  if (!fitok) continue;
373  //
374  // Check that the requirement Nhits == Npoints is satisfied
375  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
376  if (outTrack.NumberTrajectoryPoints() != outHits.size()) {
377  throw cet::exception("TrackProducerFromPFParticle")
378  << "Produced recob::Track required to have 1-1 correspondance between hits and "
379  "points.\n";
380  }
381  //
382  // Fill output collections, including Assns
383  outputTracks->emplace_back(std::move(outTrack));
384  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size() - 1);
385  outputPfpTAssn->addSingle(pfp, aptr);
386  unsigned int ip = 0;
387  for (auto const& trhit : outHits) {
388  recob::TrackHitMeta metadata(
389  outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(),
390  -std::numeric_limits<double>::max());
391  outputHits->addSingle(aptr, trhit, metadata);
392  //
393  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
394  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
395  const double fXYZ[3] = {tp.X(), tp.Y(), tp.Z()};
396  const double fErrXYZ[6] = {0};
397  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
398  outputSpacePoints->emplace_back(std::move(sp));
399  const art::Ptr<recob::SpacePoint> apsp =
400  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
401  outputHitSpacePointAssn->addSingle(trhit, apsp);
402  }
403  ip++;
404  }
406  auto osp = optionals.spacePointHitPairs();
407  for (auto it = osp.begin(); it != osp.end(); ++it) {
408  outputSpacePoints->emplace_back(std::move(it->first));
409  const art::Ptr<recob::SpacePoint> apsp =
410  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
411  outputHitSpacePointAssn->addSingle(it->second, apsp);
412  }
413  }
414  if (doTrackFitHitInfo_) { outputHitInfo->emplace_back(optionals.trackFitHitInfos()); }
415  }
416  }
417  //
418  }
419  //
420  // Put collections in the event
421  e.put(std::move(outputTracks));
422  e.put(std::move(outputHits));
423  e.put(std::move(outputPfpTAssn));
424  if (doTrackFitHitInfo_) { e.put(std::move(outputHitInfo)); }
425  if (doSpacePoints_) {
426  e.put(std::move(outputSpacePoints));
427  e.put(std::move(outputHitSpacePointAssn));
428  }
429  if (doSpacePoints_) delete spacePointPtrMaker;
430 }
431 //
432 DEFINE_ART_MODULE(TrackProducerFromPFParticle)
void initTrackFitInfos()
initialize the output vector of TrackFitHitInfos
Definition: TrackMaker.h:161
ClusterModuleLabel join with tracks
process_name cluster
Definition: cheaterreco.fcl:51
pdgs p
Definition: selectors.fcl:22
size_t NumberTrajectoryPoints() const
Various functions related to the presence and the number of (valid) points.
Class to keep data related to recob::Hit associated with recob::Track.
process_name use argoneut_mc_hitfinder track
Data related to recob::Hit associated with recob::Track.The purpose is to collect several variables t...
Definition: TrackHitMeta.h:43
process_name hit
Definition: cheaterreco.fcl:51
process_name shower
Definition: cheaterreco.fcl:51
TrackProducerFromPFParticle(fhicl::ParameterSet const &p)
auto associated_groups(A const &assns)
Helper functions to access associations in order.
std::unique_ptr< trkmkr::TrackMaker > trackMaker_
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< Coord_t >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space. See recob::tracking::Coord_t for more details on the ...
Definition: TrackingTypes.h:29
A trajectory in space reconstructed from hits.
unsigned int seed
std::vector< PointFlags_t > Flags_t
Type of point flag list.
Declaration of cluster object.
Produce a reco::Track collection, as a result of the fit of an existing recob::PFParticle collection...
tuple dir
Definition: dropbox.py:28
std::vector< SpHitPair > spacePointHitPairs()
get the output vector of SpHitPair by releasing and moving
Definition: TrackMaker.h:195
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:14
auto groupByIndex(Groups &&groups, std::size_t index) -> decltype(auto)
Returns the group within groups with the specified index.
std::vector< recob::TrackFitHitInfo > trackFitHitInfos()
get the output vector of TrackFitHitInfos by releasing and moving
Definition: TrackMaker.h:185
Helper functions to access associations in order.
do i e
TrackProducerFromPFParticle & operator=(TrackProducerFromPFParticle const &)=delete
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< Coord_t >, ROOT::Math::GlobalCoordinateSystemTag > Point_t
Type for representation of position in physical 3D space. See recob::tracking::Coord_t for more detai...
Definition: TrackingTypes.h:26
void initSpacePoints()
initialize the output vector of SpHitPair
Definition: TrackMaker.h:167
Struct holding optional TrackMaker outputs.
Definition: TrackMaker.h:125
auto const detProp
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track: