LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
TrackKalmanFitter.h
Go to the documentation of this file.
1 #ifndef TRACKKALMANFITTER_H
2 #define TRACKKALMANFITTER_H
3 
6 #include "fhiclcpp/types/Atom.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 
57 
58  public:
59  struct Config {
60  using Name = fhicl::Name;
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};
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.};
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  };
152 
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;
177  sortHitsByWire_ = sortHitsByWire;
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());
188  maxResidueFirstHit_ =
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 
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 
220  bool fitTrack(detinfo::DetectorPropertiesData const& detProp,
221  const recob::TrackTrajectory& traj,
222  int tkID,
223  const SMatrixSym55& covVtx,
224  const SMatrixSym55& covEnd,
226  const double pval,
227  const int pdgid,
228  const bool flipDirection,
229  recob::Track& outTrack,
231  trkmkr::OptionalOutputs& optionals) const;
232 
234  bool fitTrack(detinfo::DetectorPropertiesData const& detProp,
235  const Point_t& position,
236  const Vector_t& direction,
237  SMatrixSym55& trackStateCov,
239  const std::vector<recob::TrajectoryPointFlags>& flags,
240  const int tkID,
241  const double pval,
242  const int pdgid,
243  recob::Track& outTrack,
245  trkmkr::OptionalOutputs& optionals) const;
246 
248  bool doFitWork(KFTrackState& trackState,
249  detinfo::DetectorPropertiesData const& detProp,
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:
261  KFTrackState setupInitialTrackState(const Point_t& position,
262  const Vector_t& direction,
263  SMatrixSym55& trackStateCov,
264  const double pval,
265  const int pdgid) const;
266 
268  bool setupInputStates(detinfo::DetectorPropertiesData const& detProp,
270  const std::vector<recob::TrajectoryPointFlags>& flags,
271  std::vector<HitState>& hitstatev,
272  std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv) const;
273 
275  void sortOutput(std::vector<HitState>& hitstatev,
276  std::vector<KFTrackState>& fwdUpdTkState,
277  std::vector<unsigned int>& hitstateidx,
278  std::vector<unsigned int>& rejectedhsidx,
279  std::vector<unsigned int>& sortedtksidx,
280  std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv,
281  bool applySkipClean = true) const;
282 
284  bool fillResult(const std::vector<art::Ptr<recob::Hit>>& inHits,
285  const int tkID,
286  const int pdgid,
287  std::vector<HitState>& hitstatev,
288  std::vector<recob::TrajectoryPointFlags::Mask_t>& hitflagsv,
289  std::vector<KFTrackState>& fwdPrdTkState,
290  std::vector<KFTrackState>& fwdUpdTkState,
291  std::vector<unsigned int>& hitstateidx,
292  std::vector<unsigned int>& rejectedhsidx,
293  std::vector<unsigned int>& sortedtksidx,
294  recob::Track& outTrack,
296  trkmkr::OptionalOutputs& optionals) const;
297 
300  bool useRMS_;
312  float maxResidue_;
314  float maxChi2_;
315  float maxDist_;
318  };
319 
320 }
321 
322 #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
Declaration of signal hit object.
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.
Class for propagation of a trkf::TrackState to a recob::tracking::Plane.
art::ServiceHandle< geo::Geometry const > geom
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:289
Extension of a TrackState to perform KalmanFilter calculations.
Definition: KFTrackState.h:21
void hits()
Definition: readHits.C:15
A trajectory in space reconstructed from hits.
const TrackStatePropagator * propagator
General LArSoft Utilities.
Set of flags pertaining a point of the track.
TrackKalmanFitter(const TrackStatePropagator *prop, Parameters const &p)
Constructor from TrackStatePropagator and Parameters table.
Definition: Track.hh:42
Struct holding optional TrackMaker outputs.
Definition: TrackMaker.h:108
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:49