12 auto position = traj.
Vertex();
16 position = traj.
End();
20 auto trackStateCov = (flipDirection ? covEnd : covVtx );
22 return fitTrack(position, direction, trackStateCov,
hits, traj.
Flags(), tkID, pval, pdgid, outTrack, outHits, optionals);
27 const int tkID,
const double pval,
const int pdgid,
30 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__;
39 bool reverseHits =
false;
40 std::vector<HitState> hitstatev;
41 std::vector<recob::TrajectoryPointFlags::Mask_t> hitflagsv;
43 if (!inputok)
return false;
46 std::vector<KFTrackState> fwdPrdTkState;
47 std::vector<KFTrackState> fwdUpdTkState;
48 std::vector<unsigned int> hitstateidx;
49 std::vector<unsigned int> rejectedhsidx;
50 std::vector<unsigned int> sortedtksidx;
53 bool fitok =
doFitWork(trackState, hitstatev, hitflagsv, fwdPrdTkState, fwdUpdTkState, hitstateidx, rejectedhsidx, sortedtksidx);
55 mf::LogWarning(
"TrackKalmanFitter") <<
"Trying to recover with skipNegProp = false and cleanZigzag = false\n";
56 fitok =
doFitWork(trackState, hitstatev, hitflagsv, fwdPrdTkState, fwdUpdTkState, hitstateidx, rejectedhsidx, sortedtksidx,
false);
59 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failed for track with ID=" << tkID <<
"\n";
64 bool fillok =
fillResult(
hits, tkID, pdgid, reverseHits, hitstatev, hitflagsv, fwdPrdTkState, fwdUpdTkState, hitstateidx, rejectedhsidx, sortedtksidx, outTrack, outHits, optionals);
69 const double pval,
const int pdgid)
const {
72 trackStateCov(0, 0) = 1000.;
73 trackStateCov(1, 1) = 1000.;
74 trackStateCov(2, 2) = 0.25;
75 trackStateCov(3, 3) = 0.25;
76 trackStateCov(4, 4) = 10.;
77 }
else trackStateCov*=100.;
79 SVector5 trackStatePar(0.,0.,0.,0.,1./pval);
80 return KFTrackState(trackStatePar, trackStateCov,
Plane(position,direction),
true, pdgid);
85 std::vector<HitState>& hitstatev, std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv)
const {
95 if (
dumpLevel_>1) std::cout <<
"distF=" << distF <<
" distB=" << distB << std::endl;
97 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__;
100 reverseHits = distB<distF;
102 if (
dumpLevel_>1) std::cout <<
"flags.size()=" << flags.size() <<
" hits.size()=" <<
hits.size() <<
" reverseHits=" << reverseHits << std::endl;
106 const int fsize = flags.size();
107 const int beg = (reverseHits ?
hits.size()-1 : 0);
108 const int end = (reverseHits ? -1 :
hits.size());
109 for (
int ihit = beg; ihit!=
end; (reverseHits ? ihit-- : ihit++)) {
111 double t =
hit->PeakTime();
112 double terr = (
useRMS_ ?
hit->RMS() :
hit->SigmaPeakTime() );
117 if (fsize>0 && ihit<fsize) hitflagsv.push_back( flags[ihit].mask() );
120 if (
dumpLevel_>2) std::cout <<
"pushed flag mask=" << hitflagsv.back() << std::endl;
136 std::vector<KFTrackState>& fwdPrdTkState, std::vector<KFTrackState>& fwdUpdTkState,
137 std::vector<unsigned int>& hitstateidx, std::vector<unsigned int>& rejectedhsidx, std::vector<unsigned int>& sortedtksidx,
138 bool applySkipClean)
const {
140 fwdPrdTkState.clear();
141 fwdUpdTkState.clear();
143 rejectedhsidx.clear();
144 sortedtksidx.clear();
146 fwdPrdTkState.reserve(hitstatev.size());
147 fwdUpdTkState.reserve(hitstatev.size());
148 hitstateidx.reserve(hitstatev.size());
153 std::vector<std::vector<unsigned int> > hitsInPlanes(nplanes);
154 for (
unsigned int ihit = 0; ihit<hitstatev.size(); ihit++) {
155 hitsInPlanes[hitstatev[ihit].wireId().Plane].push_back(ihit);
158 std::vector<unsigned int> iterHitsInPlanes(nplanes,0);
159 for (
unsigned int p = 0; p<hitstatev.size(); ++p) {
160 if (
dumpLevel_>1) std::cout << std::endl <<
"processing hit #" << p << std::endl;
162 std::cout <<
"compute distance from state=" << std::endl; trackState.
dump();
165 double min_dist = DBL_MAX;
167 for (
unsigned int iplane = 0; iplane<nplanes; ++iplane) {
169 for (
unsigned int& ih = iterHitsInPlanes[iplane]; ih<hitsInPlanes[iplane].size(); ++ih) {
170 if (
dumpLevel_>1) std::cout <<
"iplane=" << iplane <<
" nplanes=" << nplanes <<
" iterHitsInPlanes[iplane]=" << iterHitsInPlanes[iplane] <<
" hitsInPlanes[iplane].size()=" << hitsInPlanes[iplane].size() << std::endl;
171 unsigned int ihit = hitsInPlanes[iplane][ih];
172 const auto& hitstate = hitstatev[ihit];
173 const auto& hitflags = hitflagsv[ihit];
178 rejectedhsidx.push_back(ihit);
184 if (
dumpLevel_>1) std::cout <<
"distance to plane " << iplane <<
" wire=" << hitstate.wireId().Wire <<
" = " << dist <<
", min_dist=" <<
std::min(min_dist,999.) <<
" min_plane=" << min_plane <<
" success=" << success << std::endl;
186 rejectedhsidx.push_back(ihit);
190 rejectedhsidx.push_back(ihit);
200 if (
dumpLevel_>1) std::cout <<
"pick min_dist=" << min_dist <<
" min_plane=" << min_plane << std::endl;
202 if (min_plane<0)
continue;
203 const unsigned int ihit = hitsInPlanes[min_plane][iterHitsInPlanes[min_plane]];
204 const auto& hitstate = hitstatev[ihit];
206 auto& hitflags = hitflagsv[ihit];
207 iterHitsInPlanes[min_plane]++;
212 if (
dumpLevel_>1) std::cout <<
"attempt backward prop" << std::endl;
216 std::cout <<
"hit state " << std::endl; hitstate.
dump();
217 std::cout <<
"propagation result=" << propok << std::endl;
218 std::cout <<
"propagated state " << std::endl; trackState.
dump();
219 std::cout <<
"propagated planarity=" << hitstate.plane().direction().Dot(hitstate.plane().position()-trackState.
position()) << std::endl;
222 hitstateidx.push_back(ihit);
223 fwdPrdTkState.push_back(trackState);
232 fwdUpdTkState.push_back(trackState);
239 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__;
243 std::cout <<
"updated state " << std::endl; trackState.
dump();
245 fwdUpdTkState.push_back(trackState);
247 if (
dumpLevel_>0) std::cout <<
"WARNING: forward propagation failed. Skip this hit..." << std::endl;
248 rejectedhsidx.push_back(ihit);
253 for (
unsigned int ihit=0; ihit<hitstatev.size(); ++ihit) {
254 const auto& hitstate = hitstatev[ihit];
256 std::cout << std::endl <<
"processing hit #" << ihit << std::endl;
259 auto& hitflags = hitflagsv[ihit];
262 rejectedhsidx.push_back(ihit);
268 if (dist<0. || success==
false) {
269 if (
dumpLevel_>0) std::cout <<
"WARNING: negative propagation distance. Skip this hit..." << std::endl;;
270 rejectedhsidx.push_back(ihit);
279 hitstateidx.push_back(ihit);
280 fwdPrdTkState.push_back(trackState);
289 fwdUpdTkState.push_back(trackState);
296 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__;
299 fwdUpdTkState.push_back(trackState);
301 if (
dumpLevel_>0) std::cout <<
"WARNING: forward propagation failed. Skip this hit..." << std::endl;;
302 rejectedhsidx.push_back(ihit);
308 if (
dumpLevel_>2) assert( rejectedhsidx.size()+hitstateidx.size() == hitstatev.size() );
310 std::cout <<
"TRACK AFTER FWD" << std::endl;
319 for (
int itk = fwdPrdTkState.size()-1; itk>=0; itk--) {
320 auto& fwdPrdTrackState = fwdPrdTkState[itk];
321 auto& fwdUpdTrackState = fwdUpdTkState[itk];
322 const auto& hitstate = hitstatev[hitstateidx[itk]];
323 auto& hitflags = hitflagsv[hitstateidx[itk]];
325 std::cout << std::endl <<
"processing backward hit #" << itk << std::endl;
333 std::cout <<
"propagation result=" << propok << std::endl;
334 std::cout <<
"propagated state " << std::endl; trackState.
dump();
335 std::cout <<
"propagated planarity=" << hitstate.plane().direction().Dot(hitstate.plane().position()-trackState.
position()) << std::endl;
339 bool pcombok = fwdPrdTrackState.combineWithTrackState(trackState.
trackState());
341 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__;
345 std::cout <<
"combined prd state " << std::endl; fwdPrdTrackState.dump();
348 bool ucombok = fwdUpdTrackState.combineWithTrackState(trackState.
trackState());
350 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__;
354 std::cout <<
"combined upd state " << std::endl; fwdUpdTrackState.dump();
360 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__;
364 std::cout <<
"updated state " << std::endl; trackState.
dump();
373 if (
dumpLevel_>0) std::cout <<
"WARNING: backward propagation failed. Skip this hit..." << std::endl;;
379 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__ <<
" ";
384 sortOutput(hitstatev, fwdUpdTkState, hitstateidx, rejectedhsidx, sortedtksidx, applySkipClean);
385 if (sortedtksidx.size()<4) {
386 mf::LogWarning(
"TrackKalmanFitter") <<
"Fit failure at " << __FILE__ <<
" " << __LINE__ <<
" ";
390 if (
dumpLevel_>2) assert( rejectedhsidx.size()+sortedtksidx.size() == hitstatev.size() );
395 std::vector<unsigned int>& hitstateidx, std::vector<unsigned int>& rejectedhsidx,
396 std::vector<unsigned int>& sortedtksidx,
bool applySkipClean)
const {
401 std::vector<std::vector<unsigned int> > tracksInPlanes(nplanes);
402 for (
unsigned int p = 0; p<hitstateidx.size(); ++p) {
403 const auto& hitstate = hitstatev[hitstateidx[p]];
404 tracksInPlanes[hitstate.wireId().Plane].push_back(p);
407 std::vector<unsigned int> iterTracksInPlanes;
408 for (
auto it : tracksInPlanes) iterTracksInPlanes.push_back(0);
409 auto pos = fwdUpdTkState.front().position();
410 auto dir = fwdUpdTkState.front().momentum();
411 for (
unsigned int p = 0; p<fwdUpdTkState.size(); ++p) {
413 double min_dotp = DBL_MAX;
414 for (
unsigned int iplane = 0; iplane<iterTracksInPlanes.size(); ++iplane) {
415 for (
unsigned int& itk = iterTracksInPlanes[iplane]; itk<tracksInPlanes[iplane].size(); ++itk) {
416 auto& trackstate = fwdUpdTkState[tracksInPlanes[iplane][iterTracksInPlanes[iplane]]];
417 auto& tmppos = trackstate.position();
418 const double dotp =
dir.Dot(tmppos-pos);
426 if (min_plane<0)
continue;
427 const unsigned int ihit = tracksInPlanes[min_plane][iterTracksInPlanes[min_plane]];
429 rejectedhsidx.push_back(hitstateidx[ihit]);
430 iterTracksInPlanes[min_plane]++;
433 auto& trackstate = fwdUpdTkState[ihit];
434 pos = trackstate.position();
435 dir = trackstate.momentum();
437 sortedtksidx.push_back(ihit);
438 iterTracksInPlanes[min_plane]++;
441 for (
unsigned int p = 0; p<fwdUpdTkState.size(); ++p) {
442 sortedtksidx.push_back(p);
447 std::vector<unsigned int> itoerase;
451 auto pos0 = fwdUpdTkState[sortedtksidx[0]].position();
453 unsigned int end=sortedtksidx.size()-1;
455 auto dir0 = fwdUpdTkState[sortedtksidx[i]].position()-pos0;
456 auto dir2 = fwdUpdTkState[sortedtksidx[i+1]].position()-fwdUpdTkState[sortedtksidx[i]].position();
459 if (dir2.Dot(dir0)<0.) {
463 }
else pos0 = fwdUpdTkState[sortedtksidx[i]].position();
468 rejectedhsidx.push_back(hitstateidx[sortedtksidx[i]]);
469 sortedtksidx.erase(sortedtksidx.begin()+i);
478 std::vector<HitState>& hitstatev, std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv,
479 std::vector<KFTrackState>& fwdPrdTkState, std::vector<KFTrackState>& fwdUpdTkState,
480 std::vector<unsigned int>& hitstateidx, std::vector<unsigned int>& rejectedhsidx, std::vector<unsigned int>& sortedtksidx,
484 for (
unsigned int p : sortedtksidx) {
485 const auto& trackstate = fwdUpdTkState[p];
486 const auto& hitflags = hitflagsv[hitstateidx[p]];
487 const unsigned int originalPos = (reverseHits ? hitstatev.size()-hitstateidx[p]-1 : hitstateidx[p]);
488 if (
dumpLevel_>2) assert(originalPos>=0 && originalPos<hitstatev.size());
490 const auto& prdtrack = fwdPrdTkState[p];
491 const auto& hitstate = hitstatev[hitstateidx[p]];
492 if (
dumpLevel_>2) assert(hitstate.wireId().Plane == inHits[originalPos]->WireID().Plane);
498 tcbk.
addPoint(trackstate.position(), trackstate.momentum(), inHits[originalPos],
504 for (
int i=0;i<5;i++)
for (
int j=i;j<5;j++) fakeCov55(i,j) =
util::kBogusD;
505 for (
unsigned int rejidx = 0; rejidx<rejectedhsidx.size(); ++rejidx) {
506 const unsigned int originalPos = (reverseHits ? hitstatev.size()-rejectedhsidx[rejidx]-1 : rejectedhsidx[rejidx]);
507 auto& mask = hitflagsv[rejectedhsidx[rejidx]];
511 const auto& hitstate = hitstatev[rejectedhsidx[rejidx]];
512 if (
dumpLevel_>2) assert(hitstate.wireId().Plane == inHits[originalPos]->WireID().Plane);
522 if (
dumpLevel_>1) std::cout <<
"outHits.size()=" << outHits.size() <<
" inHits.size()=" << inHits.size() << std::endl;
523 if (
dumpLevel_>2) assert(outHits.size()==inHits.size());
527 Plane(fwdUpdTkState[sortedtksidx.front()].position(),fwdUpdTkState[sortedtksidx.front()].momentum()));
529 Plane(fwdUpdTkState[sortedtksidx.back()].position(),fwdUpdTkState[sortedtksidx.back()].momentum()));
540 std::cout <<
"outTrack vertex=" << outTrack.
Start()
543 <<
"\nlength=" << outTrack.
Length()
Geometry description of a TPC wireThe wire is a single straight segment on a wire plane...
bool setupInputStates(const std::vector< art::Ptr< recob::Hit > > &hits, const std::vector< recob::TrajectoryPointFlags > &flags, const KFTrackState &trackState, bool &reverseHits, std::vector< HitState > &hitstatev, std::vector< recob::TrajectoryPointFlags::Mask_t > &hitflagsv) const
Setup vectors of HitState and Masks to be used during the fit.
static constexpr Flag_t Merged
The hit might have contribution from particles other than this.
const TrackState & trackState() const
Get the (const reference to the) TrackState.
static constexpr Flag_t Suspicious
The point reconstruction is somehow questionable.
Flags_t const & Flags() const
Returns all flags.
static constexpr Flag_t NoPoint
The trajectory point is not defined.
recob::tracking::Point_t Point_t
recob::tracking::Vector_t Vector_t
static constexpr Mask_t DefaultFlagsMask()
Flags used in default construction.
const SMatrixSym55 & StartCovariance() const
Access to covariance matrices.
Declaration of signal hit object.
recob::tracking::SMatrixSym55 SMatrixSym55
bool HasValidPoint(size_t i) const
Various functions related to the presence and the number of (valid) points.
bool fitTrack(const recob::TrackTrajectory &traj, int tkID, const SMatrixSym55 &covVtx, const SMatrixSym55 &covEnd, const std::vector< art::Ptr< recob::Hit > > &hits, const double pval, const int pdgid, const bool flipDirection, recob::Track &outTrack, std::vector< art::Ptr< recob::Hit > > &outHits, trkmkr::OptionalOutputs &optionals) const
Fit track starting from TrackTrajectory.
double distanceToPlane(bool &success, const Point_t &origpos, const Vector_t &origdir, const Plane &target) const
Distance of a TrackState (Point and Vector) to a Plane, along the TrackState direction.
double MomentumAtPoint(unsigned int p) const
const detinfo::DetectorProperties * detprop
void setCovariance(const SMatrixSym55 &trackStateCov)
This function calls the homonymous function of the stored TrackState.
const Point_t & position() const
This function calls the homonymous function of the stored TrackState.
void addPoint(const Point_t &point, const Vector_t &vect, art::Ptr< Hit > hit, const PointFlags_t &flag, double chi2)
Add a single point; different version of the functions are provided using const references or rvalue ...
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Vector_t VertexDirection() const
Returns the direction of the trajectory at the first point.
Track finalizeTrack(const tracking::SMatrixSym55 &covStart, const tracking::SMatrixSym55 &covEnd)
Get the finalized recob::Track; needs the start and end covariance matrices.
Extension of a TrackState to perform KalmanFilter calculations.
Vector_t StartDirection() const
Access to track direction at different points.
unsigned int MaxPlanes() const
Returns the largest number of planes among all TPCs in this detector.
double Length(size_t p=0) const
Access to various track properties.
bool rejectHitsNegativeGOF_
recob::tracking::SVector5 SVector5
Point_t const & Start() const
Access to track position at different points.
const SMatrixSym55 & covariance() const
This function calls the homonymous function of the stored TrackState.
A trajectory in space reconstructed from hits.
Object storing per-hit information from a track fit.
virtual double GetXTicksCoefficient(int t, int c) const =0
Struct holding point-by-point elements used in OptionalOutputs.
TrackState rotateToPlane(bool &success, const TrackState &origin, const Plane &target) const
Rotation of a TrackState to a Plane (zero distance propagation)
Plane makePlane(recob::tracking::Point_t const &pos, recob::tracking::Vector_t const &dir)
helper function to construct a recob::tracking::Plane from a Point_t and a Vector_t; the point is on ...
const TrackStatePropagator * propagator
bool updateWithHitState(const HitState &hitstate)
Update the TrackState given a HitState (they need to be on the same plane)
art::ServiceHandle< geo::Geometry > geom
static constexpr Flag_t HitIgnored
Hit was not included for the computation of the trajectory.
Point_t const & Vertex() const
Returns the position of the first valid point of the trajectory [cm].
Detector simulation of raw signals on wires.
Class defining a plane for tracking. It provides various functionalities to convert track parameters ...
static constexpr Flag_t Rejected
The hit is extraneous to this track.
static constexpr Flag_t ExcludedFromFit
Vector_t EndDirection() const
Returns the direction of the trajectory at the last point.
KFTrackState setupInitialTrackState(const Point_t &position, const Vector_t &direction, SMatrixSym55 &trackStateCov, const double pval, const int pdgid) const
Return track state from intial position, direction, and covariance.
virtual double ConvertTicksToX(double ticks, int p, int t, int c) const =0
size_t NextValidPoint(size_t index) const
Various functions related to the presence and the number of (valid) points.
bool sortOutputHitsMinLength_
Point_t const & End() const
Returns the position of the last valid point of the trajectory [cm].
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
void sortOutput(std::vector< HitState > &hitstatev, std::vector< KFTrackState > &fwdUpdTkState, std::vector< unsigned int > &hitstateidx, std::vector< unsigned int > &rejectedhsidx, std::vector< unsigned int > &sortedtksidx, bool applySkipClean=true) const
Sort the output states.
Helper class to aid the creation of a recob::Track, keeping data vectors in sync. ...
std::ostream & dump(std::ostream &out=std::cout) const
Printout information.
TrackState propagateToPlane(bool &success, const TrackState &origin, const Plane &target, bool dodedx, bool domcs, PropDirection dir=FORWARD) const
Main function for propagation of a TrackState to a Plane.
static constexpr Flag_t DetectorIssue
The hit is associated to a problematic channel.
bool isTrackFitInfosInit()
check initialization of the output vector of TrackFitHitInfos
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
static constexpr Flag_t DeltaRay
The hit might have contribution from a δ ray.
constexpr double kBogusD
obviously bogus double value
bool doFitWork(KFTrackState &trackState, std::vector< HitState > &hitstatev, std::vector< recob::TrajectoryPointFlags::Mask_t > &hitflagsv, std::vector< KFTrackState > &fwdPrdTkState, std::vector< KFTrackState > &fwdUpdTkState, std::vector< unsigned int > &hitstateidx, std::vector< unsigned int > &rejectedhsidx, std::vector< unsigned int > &sortedtksidx, bool applySkipClean=true) const
Function where the core of the fit is performed.
std::ostream & dump(std::ostream &out=std::cout) const
Printout information.
void setTrackFitHitInfo(recob::TrackFitHitInfo &&aTrackFitHitInfo)
set the recob::TrackFitHitInfo unique_ptr
const Plane & plane() const
plane where the parameters are defined
recob::tracking::Plane Plane
static constexpr Flag_t Shared
The hit is known to be associated also to another trajectory.
Struct holding optional TrackMaker outputs.
Set of flags pertaining a point of the track.
Track from a non-cascading particle.A recob::Track consists of a recob::TrackTrajectory, plus additional members relevant for a "fitted" track:
bool fillResult(const std::vector< art::Ptr< recob::Hit > > &inHits, const int tkID, const int pdgid, const bool reverseHits, std::vector< HitState > &hitstatev, std::vector< recob::TrajectoryPointFlags::Mask_t > &hitflagsv, std::vector< KFTrackState > &fwdPrdTkState, std::vector< KFTrackState > &fwdUpdTkState, std::vector< unsigned int > &hitstateidx, std::vector< unsigned int > &rejectedhsidx, std::vector< unsigned int > &sortedtksidx, recob::Track &outTrack, std::vector< art::Ptr< recob::Hit > > &outHits, trkmkr::OptionalOutputs &optionals) const
Fill the output objects.
WireGeo const & WireIDToWireGeo(geo::WireID const &wireid) const
Returns the specified wire.