All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TrackKalmanFitter.h
Go to the documentation of this file.
1 #ifndef TRACKKALMANFITTER_H
2 #define TRACKKALMANFITTER_H
3 
4 #include "art/Framework/Services/Registry/ServiceHandle.h"
5 #include "canvas/Persistency/Common/Ptr.h"
6 #include "fhiclcpp/types/Atom.h"
7 #include "fhiclcpp/types/Comment.h"
8 #include "fhiclcpp/types/Name.h"
9 #include "fhiclcpp/types/Table.h"
10 
15 
16 namespace detinfo {
17  class DetectorPropertiesData;
18 }
19 
20 namespace recob {
21  class Track;
22  class TrackTrajectory;
23 }
24 
25 namespace trkmkr {
26  struct OptionalOutputs;
27 }
28 
29 #include <limits>
30 #include <vector>
31 
32 namespace trkf {
33 
34  class TrackStatePropagator;
35 
36  /**
37  * @file larreco/RecoAlg/TrackKalmanFitter.h
38  * @class trkf::TrackKalmanFitter
39  *
40  * @brief Fit tracks using Kalman Filter fit+smooth.
41  *
42  * This algorithm fits tracks using a Kalman Filter forward fit followed by a backward smoothing. The resulting track will feature covariance matrices at start and end positions.
43  * Fundamental components of the fit are trkf::KFTrackState and trkf::TrackStatePropagator.
44  *
45  * Inputs are: recob::TrackTrajectory, initial covariance, associated hits, momentum estimate, particle id hypothesis. Alternatively, instead of a TrackTrajectory the fit can be input with initial position and direction, and vector of recob::TrajectoryPointFlags.
46  *
47  * Outputs are: resulting recob::Track, associated hits, and trkmkr::OptionalOutputs.
48  *
49  * For configuration options see TrackKalmanFitter#Config
50  *
51  * @author G. Cerati (FNAL, MicroBooNE)
52  * @date 2017
53  * @version 1.0
54  */
55 
57 
58  public:
59  struct Config {
60  using Name = fhicl::Name;
61  using Comment = fhicl::Comment;
62  fhicl::Atom<bool> useRMS{Name("useRMSError"),
63  Comment("Flag to replace the default hit error "
64  "recob::Hit::SigmaPeakTime() with recob::Hit::RMS()."),
65  true};
66  fhicl::Atom<bool> sortHitsByPlane{
67  Name("sortHitsByPlane"),
68  Comment("Flag to sort hits along the forward fit. The hit order in each plane is preserved "
69  "(unless sortHitsByWire is true), the next hit to process in 3D is chosen as the "
70  "one with shorter 3D propagation distance among the next hit in all planes."),
71  true};
72  fhicl::Atom<bool> sortHitsByWire{
73  Name("sortHitsByWire"),
74  Comment("Set to true if, instead of keeping the hit sorting in each plane from the pattern "
75  "recognition stage, the hits need to be sorted by wire number. Ignored if "
76  "sortHitsByPlane = false."),
77  false};
78  fhicl::Atom<bool> sortOutputHitsMinLength{
79  Name("sortOutputHitsMinLength"),
80  Comment("Flag to decide whether the hits are sorted before creating the output track in "
81  "order to avoid tracks with huge length."),
82  true};
83  fhicl::Atom<bool> skipNegProp{
84  Name("skipNegProp"),
85  Comment(
86  "Flag to decide whether, during the forward fit, the hits corresponding to a negative "
87  "propagation distance should be dropped. Also, if sortOutputHitsMinLength is true, "
88  "during sorting hits at a negative distance with respect to the previous are rejected."),
89  true};
90  fhicl::Atom<bool> cleanZigzag{
91  Name("cleanZigzag"),
92  Comment("Flag to decide whether hits with a zigzag pattern should be iteratively removed. "
93  "Zigzag identified as negative dot product of segments connecting a point to the "
94  "points before and after it."),
95  false};
96  fhicl::Atom<bool> rejectHighMultHits{
97  Name("rejectHighMultHits"),
98  Comment("Flag to rejects hits with recob::Hit::Multiplicity()>1."),
99  false};
100  fhicl::Atom<bool> rejectHitsNegativeGOF{
101  Name("rejectHitsNegativeGOF"),
102  Comment("Flag to rejects hits with recob::Hit::GoodnessOfFit<0."),
103  true};
104  fhicl::Atom<float> hitErr2ScaleFact{Name("hitErr2ScaleFact"),
105  Comment("Scale the hit error squared by this factor."),
106  1.0};
107  fhicl::Atom<bool> tryNoSkipWhenFails{
108  Name("tryNoSkipWhenFails"),
109  Comment("In case skipNegProp is true and the track fit fails, make a second attempt to fit "
110  "the track with skipNegProp=false in order to attempt to avoid losing efficiency."),
111  true};
112  fhicl::Atom<bool> tryBothDirs{
113  Name("tryBothDirs"),
114  Comment("Try fit in both with default and reversed direction, choose the track with "
115  "highest score=CountValidPoints/(Length*Chi2PerNdof)."),
116  false};
117  fhicl::Atom<bool> pickBestHitOnWire{
118  Name("pickBestHitOnWire"),
119  Comment("If there is >1 consecutive hit on the same wire, choose the one with best chi2 "
120  "and exclude the others."),
121  false};
122  fhicl::Atom<float> maxResidue{
123  Name("maxResidue"),
124  Comment("Reject hits with residue > maxResidue [cm]. If negative, it is set to "
125  "std::numeric_limits<float>::max()."),
126  -1.};
127  fhicl::Atom<float> maxResidueFirstHit{
128  Name("maxResidueFirstHit"),
129  Comment("Reject firt hit if has residue > maxResidueFirstHit [cm]. If negative, it is set "
130  "to std::numeric_limits<float>::max()."),
131  -1.};
132  fhicl::Atom<float> maxChi2{Name("maxChi2"),
133  Comment("Reject hits with chi2 > maxChi2. If negative, it is set "
134  "to std::numeric_limits<float>::max()."),
135  -1.};
136  fhicl::Atom<float> maxDist{
137  Name("maxDist"),
138  Comment("Reject hits with propagation distance > maxDist [cm]. If negative, it is set to "
139  "std::numeric_limits<float>::max()."),
140  -1.};
141  fhicl::Atom<float> negDistTolerance{
142  Name("negDistTolerance"),
143  Comment("Tolerance for negative propagation distance to avoid hit rejection (so this is "
144  "expected to be a small negative number)."),
145  0.};
146  fhicl::Atom<int> dumpLevel{
147  Name("dumpLevel"),
148  Comment("0 for no debug printouts, 1 for moderate, 2 for maximum."),
149  0};
150  };
151  using Parameters = fhicl::Table<Config>;
152 
153  /// Constructor from TrackStatePropagator and values of configuration parameters
155  bool useRMS,
156  bool sortHitsByPlane,
157  bool sortHitsByWire,
158  bool sortOutputHitsMinLength,
159  bool skipNegProp,
160  bool cleanZigzag,
161  bool rejectHighMultHits,
162  bool rejectHitsNegativeGOF,
163  float hitErr2ScaleFact,
164  bool tryNoSkipWhenFails,
165  bool tryBothDirs,
166  bool pickBestHitOnWire,
167  float maxResidue,
168  float maxResidueFirstHit,
169  float maxChi2,
170  float maxDist,
171  float negDistTolerance,
172  int dumpLevel)
173  {
174  propagator = prop;
175  useRMS_ = useRMS;
176  sortHitsByPlane_ = sortHitsByPlane;
178  sortOutputHitsMinLength_ = sortOutputHitsMinLength;
179  skipNegProp_ = skipNegProp;
180  cleanZigzag_ = cleanZigzag;
181  rejectHighMultHits_ = rejectHighMultHits;
182  rejectHitsNegativeGOF_ = rejectHitsNegativeGOF;
183  hitErr2ScaleFact_ = hitErr2ScaleFact;
184  tryNoSkipWhenFails_ = tryNoSkipWhenFails;
185  tryBothDirs_ = tryBothDirs;
186  pickBestHitOnWire_ = pickBestHitOnWire;
187  maxResidue_ = (maxResidue > 0 ? maxResidue : std::numeric_limits<float>::max());
189  (maxResidueFirstHit > 0 ? maxResidueFirstHit : std::numeric_limits<float>::max());
190  maxChi2_ = (maxChi2 > 0 ? maxChi2 : std::numeric_limits<float>::max());
191  maxDist_ = (maxDist > 0 ? maxDist : std::numeric_limits<float>::max());
192  negDistTolerance_ = negDistTolerance;
193  dumpLevel_ = dumpLevel;
194  }
195 
196  /// Constructor from TrackStatePropagator and Parameters table
197  explicit TrackKalmanFitter(const TrackStatePropagator* prop, Parameters const& p)
198  : TrackKalmanFitter(prop,
199  p().useRMS(),
200  p().sortHitsByPlane(),
201  p().sortHitsByWire(),
202  p().sortOutputHitsMinLength(),
203  p().skipNegProp(),
204  p().cleanZigzag(),
205  p().rejectHighMultHits(),
206  p().rejectHitsNegativeGOF(),
207  p().hitErr2ScaleFact(),
208  p().tryNoSkipWhenFails(),
209  p().tryBothDirs(),
210  p().pickBestHitOnWire(),
211  p().maxResidue(),
212  p().maxResidueFirstHit(),
213  p().maxChi2(),
214  p().maxDist(),
215  p().negDistTolerance(),
216  p().dumpLevel())
217  {}
218 
219  /// Fit track starting from TrackTrajectory
221  const recob::TrackTrajectory& traj,
222  int tkID,
223  const SMatrixSym55& covVtx,
224  const SMatrixSym55& covEnd,
225  const std::vector<art::Ptr<recob::Hit>>& hits,
226  const double pval,
227  const int pdgid,
228  const bool flipDirection,
229  recob::Track& outTrack,
230  std::vector<art::Ptr<recob::Hit>>& outHits,
231  trkmkr::OptionalOutputs& optionals) const;
232 
233  /// Fit track starting from intial position, direction, and flags
235  const Point_t& position,
236  const Vector_t& direction,
237  SMatrixSym55& trackStateCov,
238  const std::vector<art::Ptr<recob::Hit>>& hits,
239  const std::vector<recob::TrajectoryPointFlags>& flags,
240  const int tkID,
241  const double pval,
242  const int pdgid,
243  recob::Track& outTrack,
244  std::vector<art::Ptr<recob::Hit>>& outHits,
245  trkmkr::OptionalOutputs& optionals) const;
246 
247  /// Function where the core of the fit is performed
248  bool doFitWork(KFTrackState& trackState,
250  std::vector<HitState>& hitstatev,
251  std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv,
252  std::vector<KFTrackState>& fwdPrdTkState,
253  std::vector<KFTrackState>& fwdUpdTkState,
254  std::vector<unsigned int>& hitstateidx,
255  std::vector<unsigned int>& rejectedhsidx,
256  std::vector<unsigned int>& sortedtksidx,
257  bool applySkipClean = true) const;
258 
259  private:
260  /// Return track state from intial position, direction, and covariance
262  const Vector_t& direction,
263  SMatrixSym55& trackStateCov,
264  const double pval,
265  const int pdgid) const;
266 
267  /// Setup vectors of HitState and Masks to be used during the fit
269  const std::vector<art::Ptr<recob::Hit>>& hits,
270  const std::vector<recob::TrajectoryPointFlags>& flags,
271  const KFTrackState& trackState,
272  std::vector<HitState>& hitstatev,
273  std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv) const;
274 
275  /// Sort the output states
276  void sortOutput(std::vector<HitState>& hitstatev,
277  std::vector<KFTrackState>& fwdUpdTkState,
278  std::vector<unsigned int>& hitstateidx,
279  std::vector<unsigned int>& rejectedhsidx,
280  std::vector<unsigned int>& sortedtksidx,
281  std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv,
282  bool applySkipClean = true) const;
283 
284  /// Fill the output objects
285  bool fillResult(const std::vector<art::Ptr<recob::Hit>>& inHits,
286  const int tkID,
287  const int pdgid,
288  std::vector<HitState>& hitstatev,
289  std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv,
290  std::vector<KFTrackState>& fwdPrdTkState,
291  std::vector<KFTrackState>& fwdUpdTkState,
292  std::vector<unsigned int>& hitstateidx,
293  std::vector<unsigned int>& rejectedhsidx,
294  std::vector<unsigned int>& sortedtksidx,
295  recob::Track& outTrack,
296  std::vector<art::Ptr<recob::Hit>>& outHits,
297  trkmkr::OptionalOutputs& optionals) const;
298 
299  art::ServiceHandle<geo::Geometry const> geom;
301  bool useRMS_;
313  float maxResidue_;
315  float maxChi2_;
316  float maxDist_;
319  };
320 
321 }
322 
323 #endif
Fit tracks using Kalman Filter fit+smooth.
bool doFitWork(KFTrackState &trackState, detinfo::DetectorPropertiesData const &detProp, 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.
recob::tracking::Point_t Point_t
Definition: TrackState.h:18
recob::tracking::Vector_t Vector_t
Definition: TrackState.h:19
Declaration of signal hit object.
fhicl::Atom< bool > sortHitsByPlane
pdgs p
Definition: selectors.fcl:22
recob::tracking::SMatrixSym55 SMatrixSym55
Definition: TrackState.h:15
TrackKalmanFitter(const TrackStatePropagator *prop, bool useRMS, bool sortHitsByPlane, bool sortHitsByWire, bool sortOutputHitsMinLength, bool skipNegProp, bool cleanZigzag, bool rejectHighMultHits, bool rejectHitsNegativeGOF, float hitErr2ScaleFact, bool tryNoSkipWhenFails, bool tryBothDirs, bool pickBestHitOnWire, float maxResidue, float maxResidueFirstHit, float maxChi2, float maxDist, float negDistTolerance, int dumpLevel)
Constructor from TrackStatePropagator and values of configuration parameters.
bool fitTrack(detinfo::DetectorPropertiesData const &detProp, 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.
Class for propagation of a trkf::TrackState to a recob::tracking::Plane.
art::ServiceHandle< geo::Geometry const > geom
bool setupInputStates(detinfo::DetectorPropertiesData const &detProp, const std::vector< art::Ptr< recob::Hit >> &hits, const std::vector< recob::TrajectoryPointFlags > &flags, const KFTrackState &trackState, std::vector< HitState > &hitstatev, std::vector< recob::TrajectoryPointFlags::Mask_t > &hitflagsv) const
Setup vectors of HitState and Masks to be used during the fit.
fhicl::Atom< bool > pickBestHitOnWire
Definition: Data.h:7
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, std::vector< recob::TrajectoryPointFlags::Mask_t > &hitflagsv, bool applySkipClean=true) const
Sort the output states.
bool sortHitsByWire(art::Ptr< recob::Hit > a, art::Ptr< recob::Hit > b)
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
fhicl::Atom< float > negDistTolerance
fhicl::Table< Config > Parameters
fhicl::Atom< float > maxResidueFirstHit
Extension of a TrackState to perform KalmanFilter calculations.
Definition: KFTrackState.h:21
bool fillResult(const std::vector< art::Ptr< recob::Hit >> &inHits, const int tkID, const int pdgid, 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.
A trajectory in space reconstructed from hits.
fhicl::Atom< bool > tryNoSkipWhenFails
BEGIN_PROLOG vertical distance to the surface Name
const TrackStatePropagator * propagator
Set of flags pertaining a point of the track.
fhicl::Atom< bool > sortHitsByWire
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.
TrackKalmanFitter(const TrackStatePropagator *prop, Parameters const &p)
Constructor from TrackStatePropagator and Parameters table.
fhicl::Atom< bool > rejectHitsNegativeGOF
fhicl::Atom< bool > sortOutputHitsMinLength
Struct holding optional TrackMaker outputs.
Definition: TrackMaker.h:125
art framework interface to geometry description
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:
fhicl::Atom< bool > rejectHighMultHits
fhicl::Atom< float > hitErr2ScaleFact