LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
KGTrack.cxx
Go to the documentation of this file.
1 
11 #include <cmath>
12 #include <iomanip>
19 #include "cetlib_except/exception.h"
20 
21 namespace trkf {
22 
24  KGTrack::KGTrack(int prefplane) :
25  fPrefPlane(prefplane)
26  {}
27 
30  {}
31 
34  {
36 
37  if(!isValid())
38  throw cet::exception("KGTrack") << "Starting track is not valid.\n";
39 
40  // Return track.
41 
42  return (*fTrackMap.begin()).second;
43  }
44 
47  {
49 
50  if(!isValid())
51  throw cet::exception("KGTrack") << "Ending track is not valid.\n";
52 
53  // Return track.
54 
55  return (*fTrackMap.rbegin()).second;
56  }
57 
60  {
62 
63  if(!isValid())
64  throw cet::exception("KGTrack") << "Starting track is not valid.\n";
65 
66  // Return track.
67 
68  return (*fTrackMap.begin()).second;
69  }
70 
73  {
75 
76  if(!isValid())
77  throw cet::exception("KGTrack") << "Ending track is not valid.\n";
78 
79  // Return track.
80 
81  return (*fTrackMap.rbegin()).second;
82  }
83 
85  void KGTrack::addTrack(const KHitTrack& trh) {
86  if(!trh.isValid())
87  throw cet::exception("KGTrack") << "Adding invalid track to KGTrack.\n";
88  fTrackMap.insert(std::make_pair(trh.getPath() + trh.getHit()->getPredDistance(), trh));
89  }
90 
98  {
99  std::multimap<double, KHitTrack> newmap;
100 
101  // Loop over old track map.
102 
103  bool first = true;
104  double s0 = 0.;
106  i != fTrackMap.end(); ++i) {
107  KHitTrack& trh = (*i).second;
108  if(first) {
109  first = false;
110  s0 = trh.getPath();
111  }
112  double s = trh.getPath() - s0;
113  trh.setPath(s);
114  newmap.insert(std::make_pair(s, trh));
115  }
116 
117  // Update data member track map.
118 
119  fTrackMap.swap(newmap);
120  }
121 
129  int id) const
130  {
131 
132  // Make propagator for propating to standard track surface.
133 
134  PropXYZPlane prop(0., false);
135 
136  // Fill collections of trajectory points and direction vectors.
137 
138  std::vector<recob::tracking::Point_t> xyz;
139  std::vector<recob::tracking::Vector_t> pxpypz;
140  std::vector<recob::tracking::SMatrixSym55> cov;
141  std::vector<recob::TrajectoryPointFlags> outFlags;
142 
143  xyz.reserve(fTrackMap.size());
144  pxpypz.reserve(fTrackMap.size());
145  outFlags.reserve(fTrackMap.size());
146 
147  // Loop over KHitTracks.
148 
149  int ndof = 0;
150  float totChi2 = 0.;
151  unsigned int n = 0;
153  itr != fTrackMap.end(); ++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.)
169  throw cet::exception("KGTrack") << __func__ << ": null momentum\n";
170  pxpypz.push_back({mom[0], mom[1], mom[2]});
171 
172  ndof += 1;
173  totChi2 += trh.getChisq();
174  outFlags.emplace_back(n, recob::TrajectoryPointFlags::makeMask());
175 
176  // Fill error matrix.
177 
179 
180  // Construct surface perpendicular to track momentun, and
181  // propagate track to that surface (zero distance).
182 
183  const std::shared_ptr<const Surface> psurf(new SurfXYZPlane(pos[0], pos[1], pos[2],
184  mom[0], mom[1], mom[2]));
185  KETrack tre(trh);
186  boost::optional<double> dist = prop.err_prop(tre, psurf, Propagator::UNKNOWN, false);
187  if (!dist.is_initialized())
188  throw cet::exception("KGTrack") << __func__ << ": error propagation failed\n";
189  for(int i=0; i<5; ++i) {
190  for(int j=0; j<5; ++j)
191  covar(i,j) = tre.getError()(i,j);
192  }
193 
194  // Only save first and last error matrix.
195 
196  if(cov.size() < 2)
197  cov.push_back(covar);
198  else
199  cov.back() = covar;
200 
201  }
202 
203  // Fill track.
204 
205  ndof = ndof - 4; //fit measures 4 parameters: position and direction on plane
206  if(xyz.size() >= 2) {
207  track = recob::Track(std::move(xyz), std::move(pxpypz), std::move(outFlags),
208  true, this->startTrack().PdgCode(), totChi2, ndof,
209  std::move(cov.front()), std::move(cov.back()), id);
210  }
211  }
212 
220  std::vector<unsigned int>& hittpindex) const
221  {
222  hits.reserve(hits.size() + fTrackMap.size());
223 
224  // Loop over KHitTracks and fill hits belonging to this track.
225 
226  unsigned int counter = 0; //Index of corresponding trajectory point
228  it != fTrackMap.end(); ++it) {
229  const KHitTrack& track = (*it).second;
230  ++counter;
231  // Extrack Hit from track.
232  const std::shared_ptr<const KHitBase>& hit = track.getHit();
233  if(const KHitWireX* phit = dynamic_cast<const KHitWireX*>(&*hit)) {
234  const art::Ptr<recob::Hit> prhit = phit->getHit();
235  if(!prhit.isNull()){
236  hits.push_back(prhit);
237  hittpindex.push_back(counter-1);
238  }
239  }
240  else if(const KHitWireLine* phit = dynamic_cast<const KHitWireLine*>(&*hit)) {
241  const art::Ptr<recob::Hit> prhit = phit->getHit();
242  if(!prhit.isNull()){
243  hits.push_back(prhit);
244  hittpindex.push_back(counter-1);
245  }
246  }
247  }
248  }
249 
253  std::ostream& KGTrack::Print(std::ostream& out) const {
254 
255  int n = 0;
256 
257  double oldxyz[3] = {0., 0., 0.};
258  double len = 0.;
259  bool first = true;
260  for(auto const& ele : fTrackMap) {
261  double s = ele.first;
262  const KHitTrack& trh = ele.second;
263  double xyz[3];
264  double mom[3];
265  trh.getPosition(xyz);
266  trh.getMomentum(mom);
267  double tmom = std::sqrt(mom[0]*mom[0] + mom[1]*mom[1] + mom[2]*mom[2]);
268  if(tmom != 0.) {
269  mom[0] /= tmom;
270  mom[1] /= tmom;
271  mom[2] /= tmom;
272  }
273  if(!first) {
274  double dx = xyz[0] - oldxyz[0];
275  double dy = xyz[1] - oldxyz[1];
276  double dz = xyz[2] - oldxyz[2];
277  len += std::sqrt(dx*dx + dy*dy + dz*dz);
278  }
279  const KHitBase& hit = *(trh.getHit());
280  int plane = hit.getMeasPlane();
281  std::ios_base::fmtflags f = out.flags();
282  out << "State " << std::setw(4) << n
283  << ", path=" << std::setw(8) << std::fixed << std::setprecision(2) << s
284  << ", length=" << std::setw(8) << len
285  << ", x=" << std::setw(8) << xyz[0]
286  << ", y=" << std::setw(8) << xyz[1]
287  << ", z=" << std::setw(8) << xyz[2]
288  << ", dx=" << std::setw(8) << mom[0]
289  << ", dy=" << std::setw(8) << mom[1]
290  << ", dz=" << std::setw(8) << mom[2]
291  << ", plane=" << std::setw(1) << plane
292  << "\n";
293  out.flags(f);
294 
295  oldxyz[0] = xyz[0];
296  oldxyz[1] = xyz[1];
297  oldxyz[2] = xyz[2];
298 
299  ++n;
300  first = false;
301  }
302  return out;
303  }
304 
306  std::ostream& operator<<(std::ostream& out, const KGTrack& trg)
307  {
308  return trg.Print(out);
309  }
310 
311 } // end namespace trkf
const TrackError & getError() const
Track error matrix.
Definition: KETrack.h:54
Float_t s
Definition: plot.C:23
void reserve(size_type n)
Definition: PtrVector.h:343
const std::shared_ptr< const KHitBase > & getHit() const
Measurement.
Definition: KHitTrack.h:53
void fillTrack(recob::Track &track, int id) const
Fill a recob::Track.
Definition: KGTrack.cxx:128
void fillHits(art::PtrVector< recob::Hit > &hits, std::vector< unsigned int > &hittpindex) const
Fill a PtrVector of Hits.
Definition: KGTrack.cxx:219
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:97
int getMeasPlane() const
Measurement plane index.
Definition: KHitBase.h:84
const KHitTrack & endTrack() const
Track at end point.
Definition: KGTrack.cxx:46
intermediate_table::iterator iterator
Kalman filter wire-time measurement on a SurfWireX surface.
void addTrack(const KHitTrack &trh)
Add track.
Definition: KGTrack.cxx:85
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > SMatrixSym55
Definition: TrackingTypes.h:57
boost::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:354
TFile f
Definition: plotHisto.C:6
void hits()
Definition: readHits.C:15
intermediate_table::const_iterator const_iterator
std::ostream & operator<<(std::ostream &out, const KGTrack &trg)
Output operator.
Definition: KGTrack.cxx:306
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
void getPosition(double xyz[3]) const
Get position of track.
Definition: KTrack.cxx:170
double getPath() const
Propagation distance.
Definition: KFitTrack.h:66
Propagate to SurfXYZPlane surface.
KGTrack(int prefplane)
Constructor.
Definition: KGTrack.cxx:24
size_type size() const
Definition: PtrVector.h:308
Detector simulation of raw signals on wires.
std::multimap< double, KHitTrack > fTrackMap
KHitTrack collection, indexed by path distance.
Definition: KGTrack.h:111
~KGTrack()
Destructor.
Definition: KGTrack.cxx:29
void getMomentum(double mom[3]) const
Get momentum vector of track.
Definition: KTrack.cxx:217
std::ostream & Print(std::ostream &out) const
Printout.
Definition: KGTrack.cxx:253
Kalman filter wire-time measurement on a SurfWireLine surface.
void setPath(double path)
Set propagation distance.
Definition: KFitTrack.h:72
Char_t n[5]
TrackCollectionProxyElement< TrackCollProxy > Track
Proxy to an element of a proxy collection of recob::Track objects.
Definition: Track.h:1035
A collection of KHitTracks.
bool isNull() const
Definition: Ptr.h:328
bool isValid() const
Validity flag.
Definition: KGTrack.h:66
Float_t track
Definition: plot.C:34
double getChisq() const
Fit chisquare.
Definition: KFitTrack.h:67
const KHitTrack & startTrack() const
Track at start point.
Definition: KGTrack.cxx:33
art framework interface to geometry description
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
bool isValid() const
Test if track is valid.
Definition: KTrack.cxx:90