LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
TrackProducerFromPFParticle_module.cc
Go to the documentation of this file.
13 #include "cetlib_except/exception.h"
14 //
15 #include <memory>
16 //
24 //
50 //
51 //
53 public:
55  // The compiler-generated destructor is fine for non-base
56  // classes without bare pointers or other resource use.
57  //
58  // Plugins should not be copied or assigned.
63  // Required functions.
64  void produce(art::Event & e) override;
65 private:
66  std::unique_ptr<trkmkr::TrackMaker> trackMaker_;
76 };
77 //
79  : trackMaker_{art::make_tool<trkmkr::TrackMaker>(p.get<fhicl::ParameterSet>("trackMaker"))}
80  , pfpInputTag{p.get<art::InputTag>("inputCollection")}
81  , doTrackFitHitInfo_{p.get<bool>("doTrackFitHitInfo")}
82  , doSpacePoints_{p.get<bool>("doSpacePoints")}
83  , spacePointsFromTrajP_{p.get<bool>("spacePointsFromTrajP")}
84  , trackFromPF_{p.get<bool>("trackFromPF")}
85  , showerFromPF_{p.get<bool>("showerFromPF")}
86  , seedFromPF_{p.get<bool>("seedFromPF")}
87 {
88  //
89  if (p.has_key("trackInputTag")) trkInputTag = p.get<art::InputTag>("trackInputTag");
90  else trkInputTag = pfpInputTag;
91  if (p.has_key("showerInputTag")) shwInputTag = p.get<art::InputTag>("showerInputTag");
92  else shwInputTag = pfpInputTag;
93  produces<std::vector<recob::Track> >();
94  produces<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta> >();
95  produces<art::Assns<recob::PFParticle, recob::Track> >();
96  if (doTrackFitHitInfo_) produces<std::vector<std::vector<recob::TrackFitHitInfo> > >();
97  if (doSpacePoints_) {
98  produces<std::vector<recob::SpacePoint> >();
99  produces<art::Assns<recob::Hit, recob::SpacePoint> >();
100  }
101 }
102 //
104 {
105  // Output collections
106  auto outputTracks = std::make_unique<std::vector<recob::Track> >();
107  auto outputHits = std::make_unique<art::Assns<recob::Track, recob::Hit, recob::TrackHitMeta> >();
108  auto outputPfpTAssn = std::make_unique<art::Assns<recob::PFParticle, recob::Track> >();
109  auto outputHitInfo = std::make_unique<std::vector<std::vector<recob::TrackFitHitInfo> > >();
110  auto outputSpacePoints = std::make_unique<std::vector<recob::SpacePoint> >();
111  auto outputHitSpacePointAssn = std::make_unique<art::Assns<recob::Hit, recob::SpacePoint> >();
112  //
113  // PtrMakers for Assns
114  art::PtrMaker<recob::Track> trackPtrMaker(e, *this);
115  art::PtrMaker<recob::SpacePoint>* spacePointPtrMaker = nullptr;
116  if (doSpacePoints_) spacePointPtrMaker = new art::PtrMaker<recob::SpacePoint>(e, *this);
117  //
118  // Input from event
119  art::ValidHandle<std::vector<recob::PFParticle> > inputPfps = e.getValidHandle<std::vector<recob::PFParticle> >(pfpInputTag);
120  std::unique_ptr<art::FindManyP<recob::Track> > assocTracks;
122  std::unique_ptr<art::FindManyP<recob::Shower> > assocShowers;
123  std::unique_ptr<art::FindManyP<recob::Seed> > assocSeeds;
124  if (trackFromPF_) {
125  assocTracks = std::unique_ptr<art::FindManyP<recob::Track> >(new art::FindManyP<recob::Track>(inputPfps, e, pfpInputTag));
126  tkHitsAssn = *e.getValidHandle<art::Assns<recob::Track, recob::Hit> >(trkInputTag);
127  }
128  if (showerFromPF_) assocShowers = std::unique_ptr<art::FindManyP<recob::Shower> >(new art::FindManyP<recob::Shower>(inputPfps, e, pfpInputTag));
129  if (seedFromPF_) assocSeeds = std::unique_ptr<art::FindManyP<recob::Seed> >(new art::FindManyP<recob::Seed>(inputPfps, e, pfpInputTag));
130  const auto& trackHitsGroups = util::associated_groups(tkHitsAssn);
131  //
132  std::unique_ptr<art::FindManyP<recob::Cluster> > assocClusters = std::unique_ptr<art::FindManyP<recob::Cluster> >(new art::FindManyP<recob::Cluster>(inputPfps, e, pfpInputTag));
133  auto const& clHitsAssn = *e.getValidHandle<art::Assns<recob::Cluster, recob::Hit> >(shwInputTag);
134  const auto& clusterHitsGroups = util::associated_groups(clHitsAssn);
135  //
136  // Initialize tool for this event
137  trackMaker_->initEvent(e);
138  //
139  // Loop over pfps to fit
140  for (unsigned int iPfp = 0; iPfp < inputPfps->size(); ++iPfp) {
141  //
142  const art::Ptr<recob::PFParticle> pfp(inputPfps, iPfp);
143  //
144  if (trackFromPF_) {
145  // Tracks associated to PFParticles
146  const std::vector<art::Ptr<recob::Track> >& tracks = assocTracks->at(iPfp);
147  // Loop over tracks to refit
148  for (art::Ptr<recob::Track> const& track: tracks) {
149  //
150  // Get track and its hits
151  std::vector<art::Ptr<recob::Hit> > inHits;
152  decltype(auto) hitsRange = util::groupByIndex(trackHitsGroups, track.key());
153  for (art::Ptr<recob::Hit> const& hit: hitsRange) inHits.push_back(hit);
154  //
155  // Declare output objects
156  recob::Track outTrack;
157  std::vector<art::Ptr<recob::Hit> > outHits;
158  trkmkr::OptionalOutputs optionals;
159  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
161  //
162  // Invoke tool to fit track and fill output objects
163  bool fitok = trackMaker_->makeTrack(track, inHits, outTrack, outHits, optionals);
164  if (!fitok) continue;
165  //
166  // Check that the requirement Nhits == Npoints is satisfied
167  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
168  if (outTrack.NumberTrajectoryPoints()!=outHits.size()) {
169  throw cet::exception("TrackProducerFromPFParticle") << "Produced recob::Track required to have 1-1 correspondance between hits and points.\n";
170  }
171  //
172  // Fill output collections, including Assns
173  outputTracks->emplace_back(std::move(outTrack));
174  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size()-1);
175  outputPfpTAssn->addSingle(pfp, aptr);
176  unsigned int ip = 0;
177  for (auto const& trhit: outHits) {
178  recob::TrackHitMeta metadata(outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(), -std::numeric_limits<double>::max());
179  outputHits->addSingle(aptr, trhit, metadata);
180  //
181  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
182  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
183  const double fXYZ[3] = {tp.X(),tp.Y(),tp.Z()};
184  const double fErrXYZ[6] = {0};
185  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
186  outputSpacePoints->emplace_back(std::move(sp));
187  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
188  outputHitSpacePointAssn->addSingle(trhit, apsp);
189  }
190  ip++;
191  }
193  auto osp = optionals.spacePointHitPairs();
194  for (auto it = osp.begin(); it!=osp.end(); ++it ) {
195  outputSpacePoints->emplace_back(std::move(it->first));
196  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
197  outputHitSpacePointAssn->addSingle(it->second,apsp);
198  }
199  }
200  if (doTrackFitHitInfo_) {
201  outputHitInfo->emplace_back(optionals.trackFitHitInfos());
202  }
203  }
204  }
205  //
206  if (showerFromPF_) {
207  //
208  // Showers associated to PFParticles
209  const std::vector<art::Ptr<recob::Shower> >& showers = assocShowers->at(iPfp);
210  // if there is more than one shower the logic below to get the hits does not work! this works, at least for uboone
211  if (showers.size()!=1) continue;
212  //
213  // Get hits for shower (through the chain pfp->clusters->hits)
214  std::vector<art::Ptr<recob::Hit> > inHits;
215  const std::vector<art::Ptr<recob::Cluster> > clustersRange = assocClusters->at(iPfp);
216  for (art::Ptr<recob::Cluster> const& cluster: clustersRange) {
217  // for hits we use groupByIndex since it preserves the order (and we can use it since each cluster must have associated hits)
218  decltype(auto) hitsRange = util::groupByIndex(clusterHitsGroups, cluster.key());
219  for (art::Ptr<recob::Hit> const& hit: hitsRange) inHits.push_back(hit);
220  }
221  // Loop over showers to refit (should be only one)
222  for (unsigned int iShower = 0; iShower < showers.size(); ++iShower) {
223  //
224  // Get the shower and convert/hack it into a trajectory so that the fit is initialized
225  art::Ptr<recob::Shower> shower = showers[iShower];
226  recob::tracking::Point_t pos(shower->ShowerStart().X(),shower->ShowerStart().Y(),shower->ShowerStart().Z());
227  recob::tracking::Vector_t dir(shower->Direction().X(),shower->Direction().Y(),shower->Direction().Z());
228  std::vector<recob::tracking::Point_t> p;
229  std::vector<recob::tracking::Vector_t> d;
230  for (unsigned int i=0; i<inHits.size(); ++i) {
231  p.push_back(pos);
232  d.push_back(dir);
233  }
234  recob::TrackTrajectory traj(std::move(p), std::move(d), recob::TrackTrajectory::Flags_t(p.size()), false);
235  //
236  // Declare output objects
237  recob::Track outTrack;
238  std::vector<art::Ptr<recob::Hit> > outHits;
239  trkmkr::OptionalOutputs optionals;
240  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
242  //
243  // Invoke tool to fit track and fill output objects
244  bool fitok = trackMaker_->makeTrack(traj, iPfp, inHits, outTrack, outHits, optionals);
245  if (!fitok) continue;
246  //
247  // Check that the requirement Nhits == Npoints is satisfied
248  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
249  if (outTrack.NumberTrajectoryPoints()!=outHits.size()) {
250  throw cet::exception("TrackProducerFromPFParticle") << "Produced recob::Track required to have 1-1 correspondance between hits and points.\n";
251  }
252  //
253  // Fill output collections, including Assns
254  outputTracks->emplace_back(std::move(outTrack));
255  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size()-1);
256  outputPfpTAssn->addSingle(pfp, aptr);
257  unsigned int ip = 0;
258  for (auto const& trhit: outHits) {
259  recob::TrackHitMeta metadata(outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(), -std::numeric_limits<double>::max());
260  outputHits->addSingle(aptr, trhit, metadata);
261  //
262  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
263  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
264  const double fXYZ[3] = {tp.X(),tp.Y(),tp.Z()};
265  const double fErrXYZ[6] = {0};
266  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
267  outputSpacePoints->emplace_back(std::move(sp));
268  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
269  outputHitSpacePointAssn->addSingle(trhit, apsp);
270  }
271  ip++;
272  }
274  auto osp = optionals.spacePointHitPairs();
275  for (auto it = osp.begin(); it!=osp.end(); ++it ) {
276  outputSpacePoints->emplace_back(std::move(it->first));
277  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
278  outputHitSpacePointAssn->addSingle(it->second,apsp);
279  }
280  }
281  if (doTrackFitHitInfo_) {
282  outputHitInfo->emplace_back(optionals.trackFitHitInfos());
283  }
284  }
285  }
286  //
287  //
288  if (seedFromPF_) {
289  //
290  // Seeds associated to PFParticles
291  const std::vector<art::Ptr<recob::Seed> >& seeds = assocSeeds->at(iPfp);
292  // if there is more than one seed the logic below to get the hits does not work! this works, at least for uboone
293  if (seeds.size()!=1) continue;
294  //
295  // Get hits for pfp (through the chain pfp->clusters->hits)
296  std::vector<art::Ptr<recob::Hit> > inHits;
297  const std::vector<art::Ptr<recob::Cluster> > clustersRange = assocClusters->at(iPfp);
298  for (art::Ptr<recob::Cluster> const& cluster: clustersRange) {
299  // for hits we use groupByIndex since it preserves the order (and we can use it since each cluster must have associated hits)
300  decltype(auto) hitsRange = util::groupByIndex(clusterHitsGroups, cluster.key());
301  for (art::Ptr<recob::Hit> const& hit: hitsRange) inHits.push_back(hit);
302  }
303  if (inHits.size()<4) continue;
304  // Loop over seeds should be only one)
305  for (unsigned int iS = 0; iS < seeds.size(); ++iS) {
306  //
307  // Get the seed and convert/hack it into a trajectory so that the fit is initialized
308  art::Ptr<recob::Seed> seed = seeds[iS];
309  double p0[3], pe[3];
310  seed->GetPoint(p0,pe);
311  double d0[3], de[3];
312  seed->GetDirection(d0,de);
313  recob::tracking::Point_t pos(p0[0],p0[1],p0[2]);
314  recob::tracking::Vector_t dir(d0[0],d0[1],d0[2]);
315  std::vector<recob::tracking::Point_t> p;
316  std::vector<recob::tracking::Vector_t> d;
317  for (unsigned int i=0; i<inHits.size(); ++i) {
318  p.push_back(pos);
319  d.push_back(dir);
320  }
321  recob::TrackTrajectory traj(std::move(p), std::move(d), recob::TrackTrajectory::Flags_t(p.size()), false);
322  //
323  // Declare output objects
324  recob::Track outTrack;
325  std::vector<art::Ptr<recob::Hit> > outHits;
326  trkmkr::OptionalOutputs optionals;
327  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
329  //
330  // Invoke tool to fit track and fill output objects
331  bool fitok = trackMaker_->makeTrack(traj, iPfp, inHits, outTrack, outHits, optionals);
332  if (!fitok) continue;
333  //
334  // Check that the requirement Nhits == Npoints is satisfied
335  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
336  if (outTrack.NumberTrajectoryPoints()!=outHits.size()) {
337  throw cet::exception("TrackProducerFromPFParticle") << "Produced recob::Track required to have 1-1 correspondance between hits and points.\n";
338  }
339  //
340  // Fill output collections, including Assns
341  outputTracks->emplace_back(std::move(outTrack));
342  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size()-1);
343  outputPfpTAssn->addSingle(pfp, aptr);
344  unsigned int ip = 0;
345  for (auto const& trhit: outHits) {
346  recob::TrackHitMeta metadata(outputTracks->back().HasValidPoint(ip) ? ip : std::numeric_limits<int>::max(), -std::numeric_limits<double>::max());
347  outputHits->addSingle(aptr, trhit, metadata);
348  //
349  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
350  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
351  const double fXYZ[3] = {tp.X(),tp.Y(),tp.Z()};
352  const double fErrXYZ[6] = {0};
353  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
354  outputSpacePoints->emplace_back(std::move(sp));
355  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
356  outputHitSpacePointAssn->addSingle(trhit, apsp);
357  }
358  ip++;
359  }
361  auto osp = optionals.spacePointHitPairs();
362  for (auto it = osp.begin(); it!=osp.end(); ++it ) {
363  outputSpacePoints->emplace_back(std::move(it->first));
364  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
365  outputHitSpacePointAssn->addSingle(it->second,apsp);
366  }
367  }
368  if (doTrackFitHitInfo_) {
369  outputHitInfo->emplace_back(optionals.trackFitHitInfos());
370  }
371  }
372  }
373  //
374  }
375  //
376  // Put collections in the event
377  e.put(std::move(outputTracks));
378  e.put(std::move(outputHits));
379  e.put(std::move(outputPfpTAssn));
380  if (doTrackFitHitInfo_) {
381  e.put(std::move(outputHitInfo));
382  }
383  if (doSpacePoints_) {
384  e.put(std::move(outputSpacePoints));
385  e.put(std::move(outputHitSpacePointAssn));
386  }
387  if (doSpacePoints_) delete spacePointPtrMaker;
388 }
389 //
const TVector3 & ShowerStart() const
Definition: Shower.h:192
void initTrackFitInfos()
initialize the output vector of TrackFitHitInfos
Definition: TrackMaker.h:102
void GetPoint(double *Pt, double *Err) const
Definition: Seed.cxx:112
auto groupByIndex(Groups &&groups, std::size_t index) -> decltype(auto)
Returns the group within groups with the specified index.
size_t NumberTrajectoryPoints() const
Various functions related to the presence and the number of (valid) points.
Definition: Track.h:105
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_
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
Int_t max
Definition: plot.C:27
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
long seed
Definition: chem4.cc:68
A trajectory in space reconstructed from hits.
T get(std::string const &key) const
Definition: ParameterSet.h:231
Float_t d
Definition: plot.C:237
const TVector3 & Direction() const
Definition: Shower.h:189
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:119
std::vector< TrajPoint > seeds
Definition: DataStructs.cxx:11
std::vector< recob::TrackFitHitInfo > trackFitHitInfos()
get the output vector of TrackFitHitInfos by releasing and moving
Definition: TrackMaker.h:114
TDirectory * dir
Definition: macro.C:5
Helper functions to access associations in order.
TrackProducerFromPFParticle & operator=(TrackProducerFromPFParticle const &)=delete
Float_t e
Definition: plot.C:34
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
Float_t track
Definition: plot.C:34
void GetDirection(double *Dir, double *Err) const
Definition: Seed.cxx:102
void initSpacePoints()
initialize the output vector of SpHitPair
Definition: TrackMaker.h:106
Struct holding optional TrackMaker outputs.
Definition: TrackMaker.h:73
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:52
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33