All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KGTrack.cxx
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////
2 ///
3 /// \file KGTrack.cxx
4 ///
5 /// \brief A collection of KHitTracks.
6 ///
7 /// \author H. Greenlee
8 ///
9 ////////////////////////////////////////////////////////////////////////
10 
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 
29  /// Track at start point.
30  const KHitTrack&
32  {
33  if (!isValid()) throw cet::exception("KGTrack") << "Starting track is not valid.\n";
34 
35  // Return track.
36 
37  return (*fTrackMap.begin()).second;
38  }
39 
40  /// Track at end point.
41  const KHitTrack&
43  {
44  /// Throw exception if track is not valid.
45 
46  if (!isValid()) throw cet::exception("KGTrack") << "Ending track is not valid.\n";
47 
48  // Return track.
49 
50  return (*fTrackMap.rbegin()).second;
51  }
52 
53  /// Modifiable track at start point.
54  KHitTrack&
56  {
57  /// Throw exception if track is not valid.
58 
59  if (!isValid()) throw cet::exception("KGTrack") << "Starting track is not valid.\n";
60 
61  // Return track.
62 
63  return (*fTrackMap.begin()).second;
64  }
65 
66  /// Modifiable track at end point.
67  KHitTrack&
69  {
70  /// Throw exception if track is not valid.
71 
72  if (!isValid()) throw cet::exception("KGTrack") << "Ending track is not valid.\n";
73 
74  // Return track.
75 
76  return (*fTrackMap.rbegin()).second;
77  }
78 
79  /// Add track.
80  void
82  {
83  if (!trh.isValid()) throw cet::exception("KGTrack") << "Adding invalid track to KGTrack.\n";
84  fTrackMap.insert(std::make_pair(trh.getPath() + trh.getHit()->getPredDistance(), trh));
85  }
86 
87  /// Recalibrate track map.
88  ///
89  /// Loop over contents of track map. Copy each KHitTrack into a new multimap track map.
90  /// Offset the distance stored in the KHitTracks such that the minimum distance is zero.
91  /// Also update multimap keys to agree with distance stored in track.
92  ///
93  void
95  {
96  std::multimap<double, KHitTrack> newmap;
97 
98  // Loop over old track map.
99 
100  bool first = true;
101  double s0 = 0.;
102  for (std::multimap<double, KHitTrack>::iterator i = fTrackMap.begin(); i != fTrackMap.end();
103  ++i) {
104  KHitTrack& trh = (*i).second;
105  if (first) {
106  first = false;
107  s0 = trh.getPath();
108  }
109  double s = trh.getPath() - s0;
110  trh.setPath(s);
111  newmap.insert(std::make_pair(s, trh));
112  }
113 
114  // Update data member track map.
115 
116  fTrackMap.swap(newmap);
117  }
118 
119  /// Fill a recob::Track.
120  ///
121  /// Arguments:
122  ///
123  /// track - Track to fill.
124  ///
125  void
128  int id) const
129  {
130 
131  // Make propagator for propating to standard track surface.
132 
133  PropXYZPlane prop(detProp, 0., false);
134 
135  // Fill collections of trajectory points and direction vectors.
136 
137  std::vector<recob::tracking::Point_t> xyz;
138  std::vector<recob::tracking::Vector_t> pxpypz;
139  std::vector<recob::tracking::SMatrixSym55> cov;
140  std::vector<recob::TrajectoryPointFlags> outFlags;
141 
142  xyz.reserve(fTrackMap.size());
143  pxpypz.reserve(fTrackMap.size());
144  outFlags.reserve(fTrackMap.size());
145 
146  // Loop over KHitTracks.
147 
148  int ndof = 0;
149  float totChi2 = 0.;
150  unsigned int n = 0;
151  for (std::multimap<double, KHitTrack>::const_iterator itr = fTrackMap.begin();
152  itr != fTrackMap.end();
153  ++itr, ++n) {
154  const KHitTrack& trh = (*itr).second;
155 
156  // Get position.
157 
158  double pos[3];
159  trh.getPosition(pos);
160  xyz.push_back({pos[0], pos[1], pos[2]});
161 
162  // Get momentum vector.
163  // Fill direction unit vector and momentum.
164 
165  double mom[3];
166  trh.getMomentum(mom);
167  double p = std::sqrt(mom[0] * mom[0] + mom[1] * mom[1] + mom[2] * mom[2]);
168  if (p == 0.) throw cet::exception("KGTrack") << __func__ << ": null momentum\n";
169  pxpypz.push_back({mom[0], mom[1], mom[2]});
170 
171  ndof += 1;
172  totChi2 += trh.getChisq();
173  outFlags.emplace_back(n, recob::TrajectoryPointFlags::makeMask());
174 
175  // Fill error matrix.
176 
178 
179  // Construct surface perpendicular to track momentun, and
180  // propagate track to that surface (zero distance).
181 
182  const std::shared_ptr<const Surface> psurf(
183  new SurfXYZPlane(pos[0], pos[1], pos[2], mom[0], mom[1], mom[2]));
184  KETrack tre(trh);
185  std::optional<double> dist = prop.err_prop(tre, psurf, Propagator::UNKNOWN, false);
186  if (!dist) throw cet::exception("KGTrack") << __func__ << ": error propagation failed\n";
187  for (int i = 0; i < 5; ++i) {
188  for (int j = 0; j < 5; ++j)
189  covar(i, j) = tre.getError()(i, j);
190  }
191 
192  // Only save first and last error matrix.
193 
194  if (cov.size() < 2)
195  cov.push_back(covar);
196  else
197  cov.back() = covar;
198  }
199 
200  // Fill track.
201 
202  ndof = ndof - 4; //fit measures 4 parameters: position and direction on plane
203  if (xyz.size() >= 2) {
204  track = recob::Track(std::move(xyz),
205  std::move(pxpypz),
206  std::move(outFlags),
207  true,
208  this->startTrack().PdgCode(),
209  totChi2,
210  ndof,
211  std::move(cov.front()),
212  std::move(cov.back()),
213  id);
214  }
215  }
216 
217  /// Fill a PtrVector of Hits.
218  ///
219  /// Arguments:
220  ///
221  /// hits - Hit vector to fill.
222  ///
223  void
224  KGTrack::fillHits(art::PtrVector<recob::Hit>& hits, std::vector<unsigned int>& hittpindex) const
225  {
226  hits.reserve(hits.size() + fTrackMap.size());
227 
228  // Loop over KHitTracks and fill hits belonging to this track.
229 
230  unsigned int counter = 0; //Index of corresponding trajectory point
231  for (std::multimap<double, KHitTrack>::const_iterator it = fTrackMap.begin();
232  it != fTrackMap.end();
233  ++it) {
234  const KHitTrack& track = (*it).second;
235  ++counter;
236  // Extrack Hit from track.
237  const std::shared_ptr<const KHitBase>& hit = track.getHit();
238  if (const KHitWireX* phit = dynamic_cast<const KHitWireX*>(&*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  else if (const KHitWireLine* phit = dynamic_cast<const KHitWireLine*>(&*hit)) {
246  const art::Ptr<recob::Hit> prhit = phit->getHit();
247  if (!prhit.isNull()) {
248  hits.push_back(prhit);
249  hittpindex.push_back(counter - 1);
250  }
251  }
252  }
253  }
254 
255  ///
256  /// Printout
257  ///
258  std::ostream&
259  KGTrack::Print(std::ostream& out) const
260  {
261 
262  int n = 0;
263 
264  double oldxyz[3] = {0., 0., 0.};
265  double len = 0.;
266  bool first = true;
267  for (auto const& ele : fTrackMap) {
268  double s = ele.first;
269  const KHitTrack& trh = ele.second;
270  double xyz[3];
271  double mom[3];
272  trh.getPosition(xyz);
273  trh.getMomentum(mom);
274  double tmom = std::sqrt(mom[0] * mom[0] + mom[1] * mom[1] + mom[2] * mom[2]);
275  if (tmom != 0.) {
276  mom[0] /= tmom;
277  mom[1] /= tmom;
278  mom[2] /= tmom;
279  }
280  if (!first) {
281  double dx = xyz[0] - oldxyz[0];
282  double dy = xyz[1] - oldxyz[1];
283  double dz = xyz[2] - oldxyz[2];
284  len += std::sqrt(dx * dx + dy * dy + dz * dz);
285  }
286  const KHitBase& hit = *(trh.getHit());
287  int plane = hit.getMeasPlane();
288  std::ios_base::fmtflags f = out.flags();
289  out << "State " << std::setw(4) << n << ", path=" << std::setw(8) << std::fixed
290  << std::setprecision(2) << s << ", length=" << std::setw(8) << len
291  << ", x=" << std::setw(8) << xyz[0] << ", y=" << std::setw(8) << xyz[1]
292  << ", z=" << std::setw(8) << xyz[2] << ", dx=" << std::setw(8) << mom[0]
293  << ", dy=" << std::setw(8) << mom[1] << ", dz=" << std::setw(8) << mom[2]
294  << ", plane=" << std::setw(1) << plane << "\n";
295  out.flags(f);
296 
297  oldxyz[0] = xyz[0];
298  oldxyz[1] = xyz[1];
299  oldxyz[2] = xyz[2];
300 
301  ++n;
302  first = false;
303  }
304  return out;
305  }
306 
307  /// Output operator.
308  std::ostream&
309  operator<<(std::ostream& out, const KGTrack& trg)
310  {
311  return trg.Print(out);
312  }
313 
314 } // end namespace trkf
const TrackError & getError() const
Track error matrix.
Definition: KETrack.h:53
const std::shared_ptr< const KHitBase > & getHit() const
Measurement.
Definition: KHitTrack.h:53
void fillHits(art::PtrVector< recob::Hit > &hits, std::vector< unsigned int > &hittpindex) const
Fill a PtrVector of Hits.
Definition: KGTrack.cxx:224
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:94
int getMeasPlane() const
Measurement plane index.
Definition: KHitBase.h:98
const KHitTrack & endTrack() const
Track at end point.
Definition: KGTrack.cxx:42
ROOT::Math::SMatrix< Double32_t, 5, 5, ROOT::Math::MatRepSym< Double32_t, 5 > > SMatrixSym55
Definition: TrackingTypes.h:85
Kalman filter wire-time measurement on a SurfWireX surface.
pdgs p
Definition: selectors.fcl:22
void addTrack(const KHitTrack &trh)
Add track.
Definition: KGTrack.cxx:81
process_name use argoneut_mc_hitfinder track
process_name hit
Definition: cheaterreco.fcl:51
void fillTrack(detinfo::DetectorPropertiesData const &detProp, recob::Track &track, int id) const
Fill a recob::Track.
Definition: KGTrack.cxx:126
auto counter(T begin, T end)
Returns an object to iterate values from begin to end in a range-for loop.
Definition: counter.h:285
std::ostream & operator<<(std::ostream &out, const KGTrack &trg)
Output operator.
Definition: KGTrack.cxx:309
void getPosition(double xyz[3]) const
Get position of track.
Definition: KTrack.cxx:171
double getPath() const
Propagation distance.
Definition: KFitTrack.h:66
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:346
Set of flags pertaining a point of the track.
Provides recob::Track data product.
std::multimap< double, KHitTrack > fTrackMap
KHitTrack collection, indexed by path distance.
Definition: KGTrack.h:136
constexpr double dist(const TReal *x, const TReal *y, const unsigned int dimension)
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
void getMomentum(double mom[3]) const
Get momentum vector of track.
Definition: KTrack.cxx:218
std::ostream & Print(std::ostream &out) const
Printout.
Definition: KGTrack.cxx:259
Kalman filter wire-time measurement on a SurfWireLine surface.
void setPath(double path)
Set propagation distance.
Definition: KFitTrack.h:72
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
A collection of KHitTracks.
bool isValid() const
Validity flag.
Definition: KGTrack.h:79
double getChisq() const
Fit chisquare.
Definition: KFitTrack.h:67
const KHitTrack & startTrack() const
Track at start point.
Definition: KGTrack.cxx:31
auto const detProp
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a &quot;fitted&quot; track:
bool isValid() const
Test if track is valid.
Definition: KTrack.cxx:91