LArSoft  v06_85_00
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 //
22 //
47 //
48 //
50 public:
52  // The compiler-generated destructor is fine for non-base
53  // classes without bare pointers or other resource use.
54  //
55  // Plugins should not be copied or assigned.
60  // Required functions.
61  void produce(art::Event & e) override;
62 private:
63  std::unique_ptr<trkmkr::TrackMaker> trackMaker_;
72 };
73 //
75  : trackMaker_{art::make_tool<trkmkr::TrackMaker>(p.get<fhicl::ParameterSet>("trackMaker"))}
76  , pfpInputTag{p.get<art::InputTag>("inputCollection")}
77  , doTrackFitHitInfo_{p.get<bool>("doTrackFitHitInfo")}
78  , doSpacePoints_{p.get<bool>("doSpacePoints")}
79  , spacePointsFromTrajP_{p.get<bool>("spacePointsFromTrajP")}
80  , trackFromPF_{p.get<bool>("trackFromPF")}
81  , showerFromPF_{p.get<bool>("showerFromPF")}
82 {
83  //
84  if (p.has_key("trackInputTag")) trkInputTag = p.get<art::InputTag>("trackInputTag");
85  else trkInputTag = pfpInputTag;
86  if (p.has_key("showerInputTag")) shwInputTag = p.get<art::InputTag>("showerInputTag");
87  else shwInputTag = pfpInputTag;
88  produces<std::vector<recob::Track> >();
89  produces<art::Assns<recob::Track, recob::Hit> >();
90  produces<art::Assns<recob::PFParticle, recob::Track> >();
91  if (doTrackFitHitInfo_) produces<std::vector<std::vector<recob::TrackFitHitInfo> > >();
92  if (doSpacePoints_) {
93  produces<std::vector<recob::SpacePoint> >();
94  produces<art::Assns<recob::Hit, recob::SpacePoint> >();
95  }
96 }
97 //
99 {
100  // Output collections
101  auto outputTracks = std::make_unique<std::vector<recob::Track> >();
102  auto outputHits = std::make_unique<art::Assns<recob::Track, recob::Hit> >();
103  auto outputPfpTAssn = std::make_unique<art::Assns<recob::PFParticle, recob::Track> >();
104  auto outputHitInfo = std::make_unique<std::vector<std::vector<recob::TrackFitHitInfo> > >();
105  auto outputSpacePoints = std::make_unique<std::vector<recob::SpacePoint> >();
106  auto outputHitSpacePointAssn = std::make_unique<art::Assns<recob::Hit, recob::SpacePoint> >();
107  //
108  // PtrMakers for Assns
109  art::PtrMaker<recob::Track> trackPtrMaker(e, *this);
110  art::PtrMaker<recob::SpacePoint>* spacePointPtrMaker = nullptr;
111  if (doSpacePoints_) spacePointPtrMaker = new art::PtrMaker<recob::SpacePoint>(e, *this);
112  //
113  // Input from event
114  art::ValidHandle<std::vector<recob::PFParticle> > inputPfps = e.getValidHandle<std::vector<recob::PFParticle> >(pfpInputTag);
115  const auto assocTracks = std::unique_ptr<art::FindManyP<recob::Track> >(new art::FindManyP<recob::Track>(inputPfps, e, pfpInputTag));
116  const auto assocShowers = std::unique_ptr<art::FindManyP<recob::Shower> >(new art::FindManyP<recob::Shower>(inputPfps, e, pfpInputTag));
117  auto const& tkHitsAssn = *e.getValidHandle<art::Assns<recob::Track, recob::Hit> >(trkInputTag);
118  const auto& trackHitsGroups = util::associated_groups(tkHitsAssn);
119  //
120  auto const& pfClustersAssn = *e.getValidHandle<art::Assns<recob::PFParticle, recob::Cluster> >(pfpInputTag);
121  const auto& pfpClusterGroups = util::associated_groups(pfClustersAssn);
122  auto const& clHitsAssn = *e.getValidHandle<art::Assns<recob::Cluster, recob::Hit> >(shwInputTag);
123  const auto& clusterHitsGroups = util::associated_groups(clHitsAssn);
124  //
125  // Initialize tool for this event
126  trackMaker_->initEvent(e);
127  //
128  // Loop over pfps to fit
129  for (unsigned int iPfp = 0; iPfp < inputPfps->size(); ++iPfp) {
130  //
131  const art::Ptr<recob::PFParticle> pfp(inputPfps, iPfp);
132  //
133  if (trackFromPF_) {
134  // Tracks associated to PFParticles
135  const std::vector<art::Ptr<recob::Track> >& tracks = assocTracks->at(iPfp);
136  // Loop over tracks to refit
137  for (art::Ptr<recob::Track> const& track: tracks) {
138  //
139  // Get track and its hits
140  std::vector<art::Ptr<recob::Hit> > inHits;
141  decltype(auto) hitsRange = util::groupByIndex(trackHitsGroups, track.key());
142  for (art::Ptr<recob::Hit> const& hit: hitsRange) inHits.push_back(hit);
143  //
144  // Declare output objects
145  recob::Track outTrack;
146  std::vector<art::Ptr<recob::Hit> > outHits;
147  trkmkr::OptionalOutputs optionals;
148  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
150  //
151  // Invoke tool to fit track and fill output objects
152  bool fitok = trackMaker_->makeTrack(track, inHits, outTrack, outHits, optionals);
153  if (!fitok) continue;
154  //
155  // Check that the requirement Nhits == Npoints is satisfied
156  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
157  if (outTrack.NumberTrajectoryPoints()!=outHits.size()) {
158  throw cet::exception("TrackProducerFromPFParticle") << "Produced recob::Track required to have 1-1 correspondance between hits and points.\n";
159  }
160  //
161  // Fill output collections, including Assns
162  outputTracks->emplace_back(std::move(outTrack));
163  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size()-1);
164  outputPfpTAssn->addSingle(pfp, aptr);
165  unsigned int ip = 0;
166  for (auto const& trhit: outHits) {
167  outputHits->addSingle(aptr, trhit);
168  //
169  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
170  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
171  const double fXYZ[3] = {tp.X(),tp.Y(),tp.Z()};
172  const double fErrXYZ[6] = {0};
173  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
174  outputSpacePoints->emplace_back(std::move(sp));
175  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
176  outputHitSpacePointAssn->addSingle(trhit, apsp);
177  }
178  ip++;
179  }
181  auto osp = optionals.spacePointHitPairs();
182  for (auto it = osp.begin(); it!=osp.end(); ++it ) {
183  outputSpacePoints->emplace_back(std::move(it->first));
184  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
185  outputHitSpacePointAssn->addSingle(it->second,apsp);
186  }
187  }
188  if (doTrackFitHitInfo_) {
189  outputHitInfo->emplace_back(optionals.trackFitHitInfos());
190  }
191  }
192  }
193  //
194  if (showerFromPF_) {
195  //
196  // Showers associated to PFParticles
197  const std::vector<art::Ptr<recob::Shower> >& showers = assocShowers->at(iPfp);
198  // if there is more than one shower the logic below to get the hits does not work! this works, at least for uboone
199  if (showers.size()!=1) continue;
200  //
201  // Get hits for shower (through the chain pfp->clusters->hits)
202  std::vector<art::Ptr<recob::Hit> > inHits;
203  decltype(auto) clustersRange = util::groupByIndex(pfpClusterGroups, pfp.key());
204  for (art::Ptr<recob::Cluster> const& cluster: clustersRange) {
205  decltype(auto) hitsRange = util::groupByIndex(clusterHitsGroups, cluster.key());
206  for (art::Ptr<recob::Hit> const& hit: hitsRange) inHits.push_back(hit);
207  }
208  // Loop over showers to refit (should be only one)
209  for (unsigned int iShower = 0; iShower < showers.size(); ++iShower) {
210  //
211  // Get the shower and convert/hack it into a trajectory so that the fit is initialized
212  art::Ptr<recob::Shower> shower = showers[iShower++];
213  recob::tracking::Point_t pos(shower->ShowerStart().X(),shower->ShowerStart().Y(),shower->ShowerStart().Z());
214  recob::tracking::Vector_t dir(shower->Direction().X(),shower->Direction().Y(),shower->Direction().Z());
215  std::vector<recob::tracking::Point_t> p;
216  std::vector<recob::tracking::Vector_t> d;
217  for (unsigned int i=0; i<inHits.size(); ++i) {
218  p.push_back(pos);
219  d.push_back(dir);
220  }
221  recob::TrackTrajectory traj(std::move(p), std::move(d), recob::TrackTrajectory::Flags_t(p.size()), false);
222  //
223  // Declare output objects
224  recob::Track outTrack;
225  std::vector<art::Ptr<recob::Hit> > outHits;
226  trkmkr::OptionalOutputs optionals;
227  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
229  //
230  // Invoke tool to fit track and fill output objects
231  bool fitok = trackMaker_->makeTrack(traj, iShower, inHits, outTrack, outHits, optionals);
232  if (!fitok) continue;
233  //
234  // Check that the requirement Nhits == Npoints is satisfied
235  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
236  if (outTrack.NumberTrajectoryPoints()!=outHits.size()) {
237  throw cet::exception("TrackProducerFromPFParticle") << "Produced recob::Track required to have 1-1 correspondance between hits and points.\n";
238  }
239  //
240  // Fill output collections, including Assns
241  outputTracks->emplace_back(std::move(outTrack));
242  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size()-1);
243  outputPfpTAssn->addSingle(pfp, aptr);
244  unsigned int ip = 0;
245  for (auto const& trhit: outHits) {
246  outputHits->addSingle(aptr, trhit);
247  //
248  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
249  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
250  const double fXYZ[3] = {tp.X(),tp.Y(),tp.Z()};
251  const double fErrXYZ[6] = {0};
252  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
253  outputSpacePoints->emplace_back(std::move(sp));
254  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
255  outputHitSpacePointAssn->addSingle(trhit, apsp);
256  }
257  ip++;
258  }
260  auto osp = optionals.spacePointHitPairs();
261  for (auto it = osp.begin(); it!=osp.end(); ++it ) {
262  outputSpacePoints->emplace_back(std::move(it->first));
263  const art::Ptr<recob::SpacePoint> apsp = (*spacePointPtrMaker)(outputSpacePoints->size()-1);
264  outputHitSpacePointAssn->addSingle(it->second,apsp);
265  }
266  }
267  if (doTrackFitHitInfo_) {
268  outputHitInfo->emplace_back(optionals.trackFitHitInfos());
269  }
270  }
271  }
272  //
273  }
274  //
275  // Put collections in the event
276  e.put(std::move(outputTracks));
277  e.put(std::move(outputHits));
278  e.put(std::move(outputPfpTAssn));
279  if (doTrackFitHitInfo_) {
280  e.put(std::move(outputHitInfo));
281  }
282  if (doSpacePoints_) {
283  e.put(std::move(outputSpacePoints));
284  e.put(std::move(outputHitSpacePointAssn));
285  }
286  if (doSpacePoints_) delete spacePointPtrMaker;
287 }
288 //
key_type key() const
Definition: Ptr.h:356
const TVector3 & ShowerStart() const
Definition: Shower.h:192
void initTrackFitInfos()
initialize the output vector of TrackFitHitInfos
Definition: TrackMaker.h:102
geo::Point_t Point_t
Type for representation of position in physical 3D space.
Definition: TrackingTypes.h:20
const recob::TrackTrajectory & Trajectory() const
Access to the stored recob::TrackTrajectory.
Definition: Track.h:105
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:109
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:42
A trajectory in space reconstructed from hits.
T get(std::string const &key) const
Definition: ParameterSet.h:231
geo::Vector_t Vector_t
Type for representation of momenta in 3D space.
Definition: TrackingTypes.h:23
Float_t d
Definition: plot.C:237
Point_t const & LocationAtPoint(size_t i) const
Returns the position at the specified trajectory point.
Definition: Trajectory.h:255
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< 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
Float_t track
Definition: plot.C:34
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:51
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33