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