LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
TrackProducerFromTrack_module.cc
Go to the documentation of this file.
9 #include "cetlib_except/exception.h"
10 #include "fhiclcpp/ParameterSet.h"
12 
13 #include <memory>
14 
19 
40 //
41 //
43 public:
45 
46  // Plugins should not be copied or assigned.
51 
52 private:
53  void produce(art::Event& e) override;
54  std::unique_ptr<trkmkr::TrackMaker> trackMaker_;
59 };
60 
62  : EDProducer{p}
63  , trackMaker_{art::make_tool<trkmkr::TrackMaker>(p.get<fhicl::ParameterSet>("trackMaker"))}
64  , trackInputTag{p.get<art::InputTag>("inputCollection")}
65  , doTrackFitHitInfo_{p.get<bool>("doTrackFitHitInfo")}
66  , doSpacePoints_{p.get<bool>("doSpacePoints")}
67  , spacePointsFromTrajP_{p.get<bool>("spacePointsFromTrajP")}
68 {
69  produces<std::vector<recob::Track>>();
70  produces<art::Assns<recob::Track, recob::Hit>>();
71  if (doTrackFitHitInfo_) produces<std::vector<std::vector<recob::TrackFitHitInfo>>>();
72  if (doSpacePoints_) {
73  produces<std::vector<recob::SpacePoint>>();
74  produces<art::Assns<recob::Hit, recob::SpacePoint>>();
75  }
76 }
77 
79 {
80  // Output collections
81  auto outputTracks = std::make_unique<std::vector<recob::Track>>();
82  auto outputHits = std::make_unique<art::Assns<recob::Track, recob::Hit>>();
83  auto outputHitInfo = std::make_unique<std::vector<std::vector<recob::TrackFitHitInfo>>>();
84  auto outputSpacePoints = std::make_unique<std::vector<recob::SpacePoint>>();
85  auto outputHitSpacePointAssn = std::make_unique<art::Assns<recob::Hit, recob::SpacePoint>>();
86 
87  // PtrMakers for Assns
88  art::PtrMaker<recob::Track> trackPtrMaker(e);
89  art::PtrMaker<recob::SpacePoint> spacePointPtrMaker(e);
90 
91  // Input from event
92  auto const inputTracks = e.getValidHandle<std::vector<recob::Track>>(trackInputTag);
94  const auto& tracksWithHits = util::associated_groups(tkHitsAssn);
95 
96  // Initialize tool for this event
97  trackMaker_->initEvent(e);
98 
99  auto const detProp = art::ServiceHandle<detinfo::DetectorPropertiesService const>()->DataFor(e);
100 
101  // Loop over tracks to refit
102  unsigned int iTrack = 0;
103  for (auto hitsRange : tracksWithHits) {
104 
105  // Get track and its hits
106  art::Ptr<recob::Track> track(inputTracks, iTrack++);
107  std::vector<art::Ptr<recob::Hit>> inHits;
108  for (art::Ptr<recob::Hit> const& hit : hitsRange)
109  inHits.push_back(hit);
110 
111  // Declare output objects
112  recob::Track outTrack;
113  std::vector<art::Ptr<recob::Hit>> outHits;
114  trkmkr::OptionalOutputs optionals;
115  if (doTrackFitHitInfo_) optionals.initTrackFitInfos();
117 
118  // Invoke tool to fit track and fill output objects
119  bool fitok = trackMaker_->makeTrack(detProp, track, inHits, outTrack, outHits, optionals);
120  if (!fitok) continue;
121 
122  // Check that the requirement Nhits == Npoints is satisfied
123  // We also require the hits to the in the same order as the points (this cannot be enforced, can it?)
124  if (outTrack.NumberTrajectoryPoints() != outHits.size()) {
125  throw cet::exception("TrackProducerFromTrack")
126  << "Produced recob::Track required to have 1-1 correspondance between hits and "
127  "points.\noutTrack.NumberTrajectoryPoints()="
128  << outTrack.NumberTrajectoryPoints() << " outHits.size()=" << outHits.size() << "\n";
129  }
130 
131  // Fill output collections, including Assns
132  outputTracks->emplace_back(std::move(outTrack));
133  const art::Ptr<recob::Track> aptr = trackPtrMaker(outputTracks->size() - 1);
134  unsigned int ip = 0;
135  for (auto const& trhit : outHits) {
136  outputHits->addSingle(aptr, trhit);
137 
138  if (doSpacePoints_ && spacePointsFromTrajP_ && outputTracks->back().HasValidPoint(ip)) {
139  auto& tp = outputTracks->back().Trajectory().LocationAtPoint(ip);
140  const double fXYZ[3] = {tp.X(), tp.Y(), tp.Z()};
141  const double fErrXYZ[6] = {0};
142  recob::SpacePoint sp(fXYZ, fErrXYZ, -1.);
143  outputSpacePoints->emplace_back(std::move(sp));
144  const art::Ptr<recob::SpacePoint> apsp = spacePointPtrMaker(outputSpacePoints->size() - 1);
145  outputHitSpacePointAssn->addSingle(trhit, apsp);
146  }
147  ip++;
148  }
150  auto osp = optionals.spacePointHitPairs();
151  for (auto it = osp.begin(); it != osp.end(); ++it) {
152  outputSpacePoints->emplace_back(std::move(it->first));
153  const art::Ptr<recob::SpacePoint> apsp = spacePointPtrMaker(outputSpacePoints->size() - 1);
154  outputHitSpacePointAssn->addSingle(it->second, apsp);
155  }
156  }
157  if (doTrackFitHitInfo_) { outputHitInfo->emplace_back(optionals.trackFitHitInfos()); }
158  }
159 
160  // Put collections in the event
161  e.put(std::move(outputTracks));
162  e.put(std::move(outputHits));
163  if (doTrackFitHitInfo_) { e.put(std::move(outputHitInfo)); }
164  if (doSpacePoints_) {
165  e.put(std::move(outputSpacePoints));
166  e.put(std::move(outputHitSpacePointAssn));
167  }
168 }
169 
TrackProducerFromTrack & operator=(TrackProducerFromTrack const &)=delete
void initTrackFitInfos()
initialize the output vector of TrackFitHitInfos
Definition: TrackMaker.h:140
const recob::TrackTrajectory & Trajectory() const
Access to the stored recob::TrackTrajectory.
Definition: Track.h:132
EDProducer(fhicl::ParameterSet const &pset)
Definition: EDProducer.cc:6
size_t NumberTrajectoryPoints() const
Various functions related to the presence and the number of (valid) points.
Definition: Track.h:136
Produce a reco::Track collection, as a result of the fit of an existing recob::Track collection...
std::unique_ptr< trkmkr::TrackMaker > trackMaker_
PutHandle< PROD > put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: Event.h:77
auto associated_groups(A const &assns)
Helper functions to access associations in order.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
T LocationAtPoint(unsigned int p) const
Position at point p. Use e.g. as:
Detector simulation of raw signals on wires.
void produce(art::Event &e) override
std::vector< SpHitPair > spacePointHitPairs()
get the output vector of SpHitPair by releasing and moving
Definition: TrackMaker.h:160
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
Helper functions to access associations in order.
TrackProducerFromTrack(fhicl::ParameterSet const &p)
Float_t e
Definition: plot.C:35
Float_t track
Definition: plot.C:35
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