LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
KGTrack.cxx
Go to the documentation of this file.
1 
11 #include <cmath>
12 #include <iomanip>
13 
22 
23 #include "cetlib_except/exception.h"
24 
25 namespace trkf {
26 
27  KGTrack::KGTrack(int prefplane) : fPrefPlane(prefplane) {}
28 
31  {
32  if (!isValid()) throw cet::exception("KGTrack") << "Starting track is not valid.\n";
33 
34  // Return track.
35 
36  return (*fTrackMap.begin()).second;
37  }
38 
41  {
43 
44  if (!isValid()) throw cet::exception("KGTrack") << "Ending track is not valid.\n";
45 
46  // Return track.
47 
48  return (*fTrackMap.rbegin()).second;
49  }
50 
53  {
55 
56  if (!isValid()) throw cet::exception("KGTrack") << "Starting track is not valid.\n";
57 
58  // Return track.
59 
60  return (*fTrackMap.begin()).second;
61  }
62 
65  {
67 
68  if (!isValid()) throw cet::exception("KGTrack") << "Ending track is not valid.\n";
69 
70  // Return track.
71 
72  return (*fTrackMap.rbegin()).second;
73  }
74 
76  void KGTrack::addTrack(const KHitTrack& trh)
77  {
78  if (!trh.isValid()) throw cet::exception("KGTrack") << "Adding invalid track to KGTrack.\n";
79  fTrackMap.insert(std::make_pair(trh.getPath() + trh.getHit()->getPredDistance(), trh));
80  }
81 
89  {
90  std::multimap<double, KHitTrack> newmap;
91 
92  // Loop over old track map.
93 
94  bool first = true;
95  double s0 = 0.;
97  ++i) {
98  KHitTrack& trh = (*i).second;
99  if (first) {
100  first = false;
101  s0 = trh.getPath();
102  }
103  double s = trh.getPath() - s0;
104  trh.setPath(s);
105  newmap.insert(std::make_pair(s, trh));
106  }
107 
108  // Update data member track map.
109 
110  fTrackMap.swap(newmap);
111  }
112 
121  int id) const
122  {
123 
124  // Make propagator for propating to standard track surface.
125 
126  PropXYZPlane prop(detProp, 0., false);
127 
128  // Fill collections of trajectory points and direction vectors.
129 
130  std::vector<recob::tracking::Point_t> xyz;
131  std::vector<recob::tracking::Vector_t> pxpypz;
132  std::vector<recob::tracking::SMatrixSym55> cov;
133  std::vector<recob::TrajectoryPointFlags> outFlags;
134 
135  xyz.reserve(fTrackMap.size());
136  pxpypz.reserve(fTrackMap.size());
137  outFlags.reserve(fTrackMap.size());
138 
139  // Loop over KHitTracks.
140 
141  int ndof = 0;
142  float totChi2 = 0.;
143  unsigned int n = 0;
145  itr != fTrackMap.end();
146  ++itr, ++n) {
147  const KHitTrack& trh = (*itr).second;
148 
149  // Get position.
150 
151  double pos[3];
152  trh.getPosition(pos);
153  xyz.push_back({pos[0], pos[1], pos[2]});
154 
155  // Get momentum vector.
156  // Fill direction unit vector and momentum.
157 
158  double mom[3];
159  trh.getMomentum(mom);
160  double p = std::sqrt(mom[0] * mom[0] + mom[1] * mom[1] + mom[2] * mom[2]);
161  if (p == 0.) throw cet::exception("KGTrack") << __func__ << ": null momentum\n";
162  pxpypz.push_back({mom[0], mom[1], mom[2]});
163 
164  ndof += 1;
165  totChi2 += trh.getChisq();
166  outFlags.emplace_back(n, recob::TrajectoryPointFlags::makeMask());
167 
168  // Fill error matrix.
169 
171 
172  // Construct surface perpendicular to track momentun, and
173  // propagate track to that surface (zero distance).
174 
175  const std::shared_ptr<const Surface> psurf(
176  new SurfXYZPlane(pos[0], pos[1], pos[2], mom[0], mom[1], mom[2]));
177  KETrack tre(trh);
178  std::optional<double> dist = prop.err_prop(tre, psurf, Propagator::UNKNOWN, false);
179  if (!dist) throw cet::exception("KGTrack") << __func__ << ": error propagation failed\n";
180  for (int i = 0; i < 5; ++i) {
181  for (int j = 0; j < 5; ++j)
182  covar(i, j) = tre.getError()(i, j);
183  }
184 
185  // Only save first and last error matrix.
186 
187  if (cov.size() < 2)
188  cov.push_back(covar);
189  else
190  cov.back() = covar;
191  }
192 
193  // Fill track.
194 
195  ndof = ndof - 4; //fit measures 4 parameters: position and direction on plane
196  if (xyz.size() >= 2) {
197  track = recob::Track(std::move(xyz),
198  std::move(pxpypz),
199  std::move(outFlags),
200  true,
201  this->startTrack().PdgCode(),
202  totChi2,
203  ndof,
204  std::move(cov.front()),
205  std::move(cov.back()),
206  id);
207  }
208  }
209 
217  std::vector<unsigned int>& hittpindex) const
218  {
219  hits.reserve(hits.size() + fTrackMap.size());
220 
221  // Loop over KHitTracks and fill hits belonging to this track.
222 
223  unsigned int counter = 0; //Index of corresponding trajectory point
225  it != fTrackMap.end();
226  ++it) {
227  const KHitTrack& track = (*it).second;
228  ++counter;
229  // Extrack Hit from track.
230  const std::shared_ptr<const KHitBase>& hit = track.getHit();
231  if (const KHitWireX* phit = dynamic_cast<const KHitWireX*>(&*hit)) {
232  const art::Ptr<recob::Hit> prhit = phit->getHit();
233  if (!prhit.isNull()) {
234  hits.push_back(prhit);
235  hittpindex.push_back(counter - 1);
236  }
237  }
238  else if (const KHitWireLine* phit = dynamic_cast<const KHitWireLine*>(&*hit)) {
239  const art::Ptr<recob::Hit> prhit = phit->getHit();
240  if (!prhit.isNull()) {
241  hits.push_back(prhit);
242  hittpindex.push_back(counter - 1);
243  }
244  }
245  }
246  }
247 
251  std::ostream& KGTrack::Print(std::ostream& out) const
252  {
253 
254  int n = 0;
255 
256  double oldxyz[3] = {0., 0., 0.};
257  double len = 0.;
258  bool first = true;
259  for (auto const& ele : fTrackMap) {
260  double s = ele.first;
261  const KHitTrack& trh = ele.second;
262  double xyz[3];
263  double mom[3];
264  trh.getPosition(xyz);
265  trh.getMomentum(mom);
266  double tmom = std::sqrt(mom[0] * mom[0] + mom[1] * mom[1] + mom[2] * mom[2]);
267  if (tmom != 0.) {
268  mom[0] /= tmom;
269  mom[1] /= tmom;
270  mom[2] /= tmom;
271  }
272  if (!first) {
273  double dx = xyz[0] - oldxyz[0];
274  double dy = xyz[1] - oldxyz[1];
275  double dz = xyz[2] - oldxyz[2];
276  len += std::sqrt(dx * dx + dy * dy + dz * dz);
277  }
278  const KHitBase& hit = *(trh.getHit());
279  int plane = hit.getMeasPlane();
280  std::ios_base::fmtflags f = out.flags();
281  out << "State " << std::setw(4) << n << ", path=" << std::setw(8) << std::fixed
282  << std::setprecision(2) << s << ", length=" << std::setw(8) << len
283  << ", x=" << std::setw(8) << xyz[0] << ", y=" << std::setw(8) << xyz[1]
284  << ", z=" << std::setw(8) << xyz[2] << ", dx=" << std::setw(8) << mom[0]
285  << ", dy=" << std::setw(8) << mom[1] << ", dz=" << std::setw(8) << mom[2]
286  << ", plane=" << std::setw(1) << plane << "\n";
287  out.flags(f);
288 
289  oldxyz[0] = xyz[0];
290  oldxyz[1] = xyz[1];
291  oldxyz[2] = xyz[2];
292 
293  ++n;
294  first = false;
295  }
296  return out;
297  }
298 
300  std::ostream& operator<<(std::ostream& out, const KGTrack& trg)
301  {
302  return trg.Print(out);
303  }
304 
305 } // end namespace trkf
intermediate_table::iterator iterator
const TrackError & getError() const
Track error matrix.
Definition: KETrack.h:52
void reserve(size_type n)
Definition: PtrVector.h:337
const std::shared_ptr< const KHitBase > & getHit() const
Measurement.
Definition: KHitTrack.h:51
void fillHits(art::PtrVector< recob::Hit > &hits, std::vector< unsigned int > &hittpindex) const
Fill a PtrVector of Hits.
Definition: KGTrack.cxx:216
General planar surface.
static constexpr Mask_t makeMask(Flags...flags)
Returns a bit mask with only the specified bit set.
void recalibrate()
Recalibrate track map.
Definition: KGTrack.cxx:88
int getMeasPlane() const
Measurement plane index.
Definition: KHitBase.h:85
const KHitTrack & endTrack() const
Track at end point.
Definition: KGTrack.cxx:40
Kalman filter wire-time measurement on a SurfWireX surface.
ROOT::Math::SMatrix< Double32_t, 5, 5, ROOT::Math::MatRepSym< Double32_t, 5 >> SMatrixSym55
void addTrack(const KHitTrack &trh)
Add track.
Definition: KGTrack.cxx:76
intermediate_table::const_iterator const_iterator
TFile f
Definition: plotHisto.C:6
void fillTrack(detinfo::DetectorPropertiesData const &detProp, recob::Track &track, int id) const
Fill a recob::Track.
Definition: KGTrack.cxx:119
void hits()
Definition: readHits.C:15
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:295
std::ostream & operator<<(std::ostream &out, const KGTrack &trg)
Output operator.
Definition: KGTrack.cxx:300
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:435
void getPosition(double xyz[3]) const
Get position of track.
Definition: KTrack.cxx:157
double getPath() const
Propagation distance.
Definition: KFitTrack.h:61
Propagate to SurfXYZPlane surface.
KGTrack(int prefplane)
Definition: KGTrack.cxx:27
std::optional< double > err_prop(KETrack &tre, const std::shared_ptr< const Surface > &psurf, PropDirection dir, bool doDedx, KTrack *ref=0, TrackMatrix *prop_matrix=0) const
Propagate with error, but without noise.
Definition: Propagator.cxx:343
bool isNull() const noexcept
Definition: Ptr.h:211
Provides recob::Track data product.
Set of flags pertaining a point of the track.
size_type size() const
Definition: PtrVector.h:302
Detector simulation of raw signals on wires.
std::multimap< double, KHitTrack > fTrackMap
KHitTrack collection, indexed by path distance.
Definition: KGTrack.h:108
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
void getMomentum(double mom[3]) const
Get momentum vector of track.
Definition: KTrack.cxx:201
std::ostream & Print(std::ostream &out) const
Printout.
Definition: KGTrack.cxx:251
Kalman filter wire-time measurement on a SurfWireLine surface.
void setPath(double path)
Set propagation distance.
Definition: KFitTrack.h:67
Char_t n[5]
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
Definition: Track.h:992
A collection of KHitTracks.
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:82
bool isValid() const
Validity flag.
Definition: KGTrack.h:66
Float_t track
Definition: plot.C:35
double getChisq() const
Fit chisquare.
Definition: KFitTrack.h:62
const KHitTrack & startTrack() const
Track at start point.
Definition: KGTrack.cxx:30
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
bool isValid() const
Test if track is valid.
Definition: KTrack.cxx:81