LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
TrackProducerFromPFParticle_module.cc
Go to the documentation of this file.
11 #include "cetlib_except/exception.h"
12 #include "fhiclcpp/ParameterSet.h"
14 
15 #include <memory>
16 
25 
51 //
52 //
54 public:
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_;
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
97  if (p.has_key("showerInputTag"))
98  shwInputTag = p.get<art::InputTag>("showerInputTag");
99  else
101  if (p.has_key("clusterInputTag"))
102  clsInputTag = p.get<art::InputTag>("clusterInputTag");
103  else
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 //
116 {
117  // Output collections
118  auto outputTracks = std::make_unique<std::vector<recob::Track>>();
119  auto outputHits = std::make_unique<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta>>();
120  auto outputPfpTAssn = std::make_unique<art::Assns<recob::PFParticle, recob::Track>>();
121  auto outputHitInfo = std::make_unique<std::vector<std::vector<recob::TrackFitHitInfo>>>();
122  auto outputSpacePoints = std::make_unique<std::vector<recob::SpacePoint>>();
123  auto outputHitSpacePointAssn = std::make_unique<art::Assns<recob::Hit, recob::SpacePoint>>();
124  //
125  // PtrMakers for Assns
126  art::PtrMaker<recob::Track> trackPtrMaker(e);
127  art::PtrMaker<recob::SpacePoint>* spacePointPtrMaker = nullptr;
128  if (doSpacePoints_) spacePointPtrMaker = new art::PtrMaker<recob::SpacePoint>(e);
129  //
130  // Input from event
132  e.getValidHandle<std::vector<recob::PFParticle>>(pfpInputTag);
133  std::unique_ptr<art::FindManyP<recob::Track>> assocTracks;
135  std::unique_ptr<art::FindManyP<recob::Shower>> assocShowers;
136  std::unique_ptr<art::FindManyP<recob::Seed>> assocSeeds;
137  if (trackFromPF_) {
138  assocTracks = std::unique_ptr<art::FindManyP<recob::Track>>(
139  new art::FindManyP<recob::Track>(inputPfps, e, trkInputTag));
140  tkHitsAssn = *e.getValidHandle<art::Assns<recob::Track, recob::Hit>>(trkInputTag);
141  }
142  if (showerFromPF_)
143  assocShowers = std::unique_ptr<art::FindManyP<recob::Shower>>(
144  new art::FindManyP<recob::Shower>(inputPfps, e, shwInputTag));
145  if (seedFromPF_)
146  assocSeeds = std::unique_ptr<art::FindManyP<recob::Seed>>(
147  new art::FindManyP<recob::Seed>(inputPfps, e, pfpInputTag));
148  const auto& trackHitsGroups = util::associated_groups(tkHitsAssn);
149 
150  std::unique_ptr<art::FindManyP<recob::Cluster>> assocClusters =
151  std::unique_ptr<art::FindManyP<recob::Cluster>>(
152  new art::FindManyP<recob::Cluster>(inputPfps, e, pfpInputTag));
153  auto const& clHitsAssn = *e.getValidHandle<art::Assns<recob::Cluster, recob::Hit>>(clsInputTag);
154  const auto& clusterHitsGroups = util::associated_groups(clHitsAssn);
155 
156  // Initialize tool for this event
157  trackMaker_->initEvent(e);
158 
159  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(e);
160 
161  // Loop over pfps to fit
162  for (unsigned int iPfp = 0; iPfp < inputPfps->size(); ++iPfp) {
163  const art::Ptr<recob::PFParticle> pfp(inputPfps, iPfp);
164  if (trackFromPF_) {
165  // Tracks associated to PFParticles
166  const std::vector<art::Ptr<recob::Track>>& tracks = assocTracks->at(iPfp);
167  // Loop over tracks to refit
168  for (art::Ptr<recob::Track> const& track : tracks) {
169 
170  // Get track and its hits
171  std::vector<art::Ptr<recob::Hit>> inHits;
172  decltype(auto) hitsRange = util::groupByIndex(trackHitsGroups, track.key());
173  for (art::Ptr<recob::Hit> const& hit : hitsRange)
174  inHits.push_back(hit);
175 
176  // Declare output objects
177  recob::Track outTrack;
178  std::vector<art::Ptr<recob::Hit>> outHits;
179  trkmkr::OptionalOutputs optionals;
180  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
182 
183  // Invoke tool to fit track and fill output objects
184  bool fitok = trackMaker_->makeTrack(detProp, track, inHits, outTrack, outHits, optionals);
185  if (!fitok) continue;
186 
187  // Check that the requirement Nhits == Npoints is satisfied
188  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
189  if (outTrack.NumberTrajectoryPoints() != outHits.size()) {
190  throw cet::exception("TrackProducerFromPFParticle")
191  << "Produced recob::Track required to have 1-1 correspondance between hits and "
192  "points.\n";
193  }
194 
195  // Fill output collections, including Assns
196  outputTracks->emplace_back(std::move(outTrack));
197  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size() - 1);
198  outputPfpTAssn->addSingle(pfp, aptr);
199  unsigned int ip = 0;
200  for (auto const& trhit : outHits) {
201  recob::TrackHitMeta metadata(
202  outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(),
203  -std::numeric_limits<double>::max());
204  outputHits->addSingle(aptr, trhit, metadata);
205 
206  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
207  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
208  const double fXYZ[3] = {tp.X(), tp.Y(), tp.Z()};
209  const double fErrXYZ[6] = {0};
210  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
211  outputSpacePoints->emplace_back(std::move(sp));
212  const art::Ptr<recob::SpacePoint> apsp =
213  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
214  outputHitSpacePointAssn->addSingle(trhit, apsp);
215  }
216  ip++;
217  }
219  auto osp = optionals.spacePointHitPairs();
220  for (auto it = osp.begin(); it != osp.end(); ++it) {
221  outputSpacePoints->emplace_back(std::move(it->first));
222  const art::Ptr<recob::SpacePoint> apsp =
223  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
224  outputHitSpacePointAssn->addSingle(it->second, apsp);
225  }
226  }
227  if (doTrackFitHitInfo_) { outputHitInfo->emplace_back(optionals.trackFitHitInfos()); }
228  }
229  }
230  //
231  if (showerFromPF_) {
232  //
233  // Showers associated to PFParticles
234  const std::vector<art::Ptr<recob::Shower>>& showers = assocShowers->at(iPfp);
235  // if there is more than one shower the logic below to get the hits does not work! this works, at least for uboone
236  if (showers.size() != 1) continue;
237  //
238  // Get hits for shower (through the chain pfp->clusters->hits)
239  std::vector<art::Ptr<recob::Hit>> inHits;
240  const std::vector<art::Ptr<recob::Cluster>> clustersRange = assocClusters->at(iPfp);
241  for (art::Ptr<recob::Cluster> const& cluster : clustersRange) {
242  // for hits we use groupByIndex since it preserves the order (and we can use it since each cluster must have associated hits)
243  decltype(auto) hitsRange = util::groupByIndex(clusterHitsGroups, cluster.key());
244  for (art::Ptr<recob::Hit> const& hit : hitsRange)
245  inHits.push_back(hit);
246  }
247  // Loop over showers to refit (should be only one)
248  for (unsigned int iShower = 0; iShower < showers.size(); ++iShower) {
249  //
250  // Get the shower and convert/hack it into a trajectory so that the fit is initialized
251  art::Ptr<recob::Shower> shower = showers[iShower];
253  shower->ShowerStart().X(), shower->ShowerStart().Y(), shower->ShowerStart().Z());
255  shower->Direction().X(), shower->Direction().Y(), shower->Direction().Z());
256  float mom = 1.;
257  if (shower->Energy().size() == 3) mom = shower->Energy()[2] * 0.001;
258  std::vector<recob::tracking::Point_t> p;
259  std::vector<recob::tracking::Vector_t> d;
260  for (unsigned int i = 0; i < inHits.size(); ++i) {
261  p.push_back(pos);
262  d.push_back(mom * dir);
263  }
265  std::move(p), std::move(d), recob::TrackTrajectory::Flags_t(p.size()), false);
266  //
267  // Declare output objects
268  recob::Track outTrack;
269  std::vector<art::Ptr<recob::Hit>> outHits;
270  trkmkr::OptionalOutputs optionals;
271  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
273  //
274  // Invoke tool to fit track and fill output objects
275  bool fitok =
276  trackMaker_->makeTrack(detProp, traj, iPfp, inHits, outTrack, outHits, optionals);
277  if (!fitok) continue;
278  //
279  // Check that the requirement Nhits == Npoints is satisfied
280  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
281  if (outTrack.NumberTrajectoryPoints() != outHits.size()) {
282  throw cet::exception("TrackProducerFromPFParticle")
283  << "Produced recob::Track required to have 1-1 correspondance between hits and "
284  "points.\n";
285  }
286  //
287  // Fill output collections, including Assns
288  outputTracks->emplace_back(std::move(outTrack));
289  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size() - 1);
290  outputPfpTAssn->addSingle(pfp, aptr);
291  unsigned int ip = 0;
292  for (auto const& trhit : outHits) {
293  recob::TrackHitMeta metadata(
294  outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(),
295  -std::numeric_limits<double>::max());
296  outputHits->addSingle(aptr, trhit, metadata);
297  //
298  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
299  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
300  const double fXYZ[3] = {tp.X(), tp.Y(), tp.Z()};
301  const double fErrXYZ[6] = {0};
302  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
303  outputSpacePoints->emplace_back(std::move(sp));
304  const art::Ptr<recob::SpacePoint> apsp =
305  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
306  outputHitSpacePointAssn->addSingle(trhit, apsp);
307  }
308  ip++;
309  }
311  auto osp = optionals.spacePointHitPairs();
312  for (auto it = osp.begin(); it != osp.end(); ++it) {
313  outputSpacePoints->emplace_back(std::move(it->first));
314  const art::Ptr<recob::SpacePoint> apsp =
315  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
316  outputHitSpacePointAssn->addSingle(it->second, apsp);
317  }
318  }
319  if (doTrackFitHitInfo_) { outputHitInfo->emplace_back(optionals.trackFitHitInfos()); }
320  }
321  }
322  //
323  //
324  if (seedFromPF_) {
325  //
326  // Seeds associated to PFParticles
327  const std::vector<art::Ptr<recob::Seed>>& seeds = assocSeeds->at(iPfp);
328  // if there is more than one seed the logic below to get the hits does not work! this works, at least for uboone
329  if (seeds.size() != 1) continue;
330  //
331  // Get hits for pfp (through the chain pfp->clusters->hits)
332  std::vector<art::Ptr<recob::Hit>> inHits;
333  const std::vector<art::Ptr<recob::Cluster>> clustersRange = assocClusters->at(iPfp);
334  for (art::Ptr<recob::Cluster> const& cluster : clustersRange) {
335  // for hits we use groupByIndex since it preserves the order (and we can use it since each cluster must have associated hits)
336  decltype(auto) hitsRange = util::groupByIndex(clusterHitsGroups, cluster.key());
337  for (art::Ptr<recob::Hit> const& hit : hitsRange)
338  inHits.push_back(hit);
339  }
340  if (inHits.size() < 4) continue;
341  // Loop over seeds should be only one)
342  for (unsigned int iS = 0; iS < seeds.size(); ++iS) {
343  //
344  // Get the seed and convert/hack it into a trajectory so that the fit is initialized
345  art::Ptr<recob::Seed> seed = seeds[iS];
346  double p0[3], pe[3];
347  seed->GetPoint(p0, pe);
348  double d0[3], de[3];
349  seed->GetDirection(d0, de);
350  recob::tracking::Point_t pos(p0[0], p0[1], p0[2]);
351  recob::tracking::Vector_t dir(d0[0], d0[1], d0[2]);
352  std::vector<recob::tracking::Point_t> p;
353  std::vector<recob::tracking::Vector_t> d;
354  for (unsigned int i = 0; i < inHits.size(); ++i) {
355  p.push_back(pos);
356  d.push_back(dir);
357  }
359  std::move(p), std::move(d), recob::TrackTrajectory::Flags_t(p.size()), false);
360  //
361  // Declare output objects
362  recob::Track outTrack;
363  std::vector<art::Ptr<recob::Hit>> outHits;
364  trkmkr::OptionalOutputs optionals;
365  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
367  //
368  // Invoke tool to fit track and fill output objects
369  bool fitok =
370  trackMaker_->makeTrack(detProp, traj, iPfp, inHits, outTrack, outHits, optionals);
371  if (!fitok) continue;
372  //
373  // Check that the requirement Nhits == Npoints is satisfied
374  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
375  if (outTrack.NumberTrajectoryPoints() != outHits.size()) {
376  throw cet::exception("TrackProducerFromPFParticle")
377  << "Produced recob::Track required to have 1-1 correspondance between hits and "
378  "points.\n";
379  }
380  //
381  // Fill output collections, including Assns
382  outputTracks->emplace_back(std::move(outTrack));
383  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size() - 1);
384  outputPfpTAssn->addSingle(pfp, aptr);
385  unsigned int ip = 0;
386  for (auto const& trhit : outHits) {
387  recob::TrackHitMeta metadata(
388  outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(),
389  -std::numeric_limits<double>::max());
390  outputHits->addSingle(aptr, trhit, metadata);
391  //
392  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
393  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
394  const double fXYZ[3] = {tp.X(), tp.Y(), tp.Z()};
395  const double fErrXYZ[6] = {0};
396  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
397  outputSpacePoints->emplace_back(std::move(sp));
398  const art::Ptr<recob::SpacePoint> apsp =
399  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
400  outputHitSpacePointAssn->addSingle(trhit, apsp);
401  }
402  ip++;
403  }
405  auto osp = optionals.spacePointHitPairs();
406  for (auto it = osp.begin(); it != osp.end(); ++it) {
407  outputSpacePoints->emplace_back(std::move(it->first));
408  const art::Ptr<recob::SpacePoint> apsp =
409  (*spacePointPtrMaker)(outputSpacePoints->size() - 1);
410  outputHitSpacePointAssn->addSingle(it->second, apsp);
411  }
412  }
413  if (doTrackFitHitInfo_) { outputHitInfo->emplace_back(optionals.trackFitHitInfos()); }
414  }
415  }
416  //
417  }
418  //
419  // Put collections in the event
420  e.put(std::move(outputTracks));
421  e.put(std::move(outputHits));
422  e.put(std::move(outputPfpTAssn));
423  if (doTrackFitHitInfo_) { e.put(std::move(outputHitInfo)); }
424  if (doSpacePoints_) {
425  e.put(std::move(outputSpacePoints));
426  e.put(std::move(outputHitSpacePointAssn));
427  }
428  if (doSpacePoints_) delete spacePointPtrMaker;
429 }
430 //
const TVector3 & ShowerStart() const
Definition: Shower.h:197
void initTrackFitInfos()
initialize the output vector of TrackFitHitInfos
Definition: TrackMaker.h:140
void GetPoint(double *Pt, double *Err) const
Definition: Seed.cxx:89
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
const std::vector< double > & Energy() const
Definition: Shower.h:206
size_t NumberTrajectoryPoints() const
Various functions related to the presence and the number of (valid) points.
Definition: Track.h:136
Class to keep data related to recob::Hit associated with recob::Track.
Data related to recob::Hit associated with recob::Track.The purpose is to collect several variables t...
Definition: TrackHitMeta.h:43
Cluster finding and building.
TrackProducerFromPFParticle(fhicl::ParameterSet const &p)
auto associated_groups(A const &assns)
Helper functions to access associations in order.
std::unique_ptr< trkmkr::TrackMaker > trackMaker_
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
long seed
Definition: chem4.cc:67
A trajectory in space reconstructed from hits.
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:31
Float_t d
Definition: plot.C:235
const TVector3 & Direction() const
Definition: Shower.h:188
std::vector< PointFlags_t > Flags_t
Type of point flag list.
Declaration of cluster object.
Detector simulation of raw signals on wires.
Produce a reco::Track collection, as a result of the fit of an existing recob::PFParticle collection...
std::vector< SpHitPair > spacePointHitPairs()
get the output vector of SpHitPair by releasing and moving
Definition: TrackMaker.h:160
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.
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
std::vector< recob::TrackFitHitInfo > trackFitHitInfos()
get the output vector of TrackFitHitInfos by releasing and moving
Definition: TrackMaker.h:151
TDirectory * dir
Definition: macro.C:5
Helper functions to access associations in order.
TrackProducerFromPFParticle & operator=(TrackProducerFromPFParticle const &)=delete
Float_t e
Definition: plot.C:35
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:27
Float_t track
Definition: plot.C:35
void GetDirection(double *Dir, double *Err) const
Definition: Seed.cxx:80
void initSpacePoints()
initialize the output vector of SpHitPair
Definition: TrackMaker.h:145
Struct holding optional TrackMaker outputs.
Definition: TrackMaker.h:108
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
Definition: Track.h:49
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33