LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
KalmanFilterFitTrackMaker_tool.cc
Go to the documentation of this file.
3 #include "fhiclcpp/types/Atom.h"
5 
8 
12 
15 
20 
21 namespace trkmkr {
22 
48 
49  public:
50  struct Options {
51  using Name = fhicl::Name;
53  //
55  Name("defaultMomInGeV"),
56  Comment("Default momentum estimate value (all other options are set to "
57  "false, or if the estimate is not available)."),
58  1.0};
60  Name("momFromMCSCollection"),
61  Comment("Flag used to get initial momentum estimate from MCSFitResult "
62  "collection specified by mcsInputTag."),
63  false};
65  Comment("InputTag of MCSFitResult collection.")};
67  Name("momFromCombAndPid"),
68  Comment("Flag used to get initial momentum estimate from either range "
69  "or mcs fit, based on particle id and containement (from "
70  "contInputTag collection)."),
71  false};
73  Name("contInputTag"),
74  Comment("InputTag of CosmicTag collection for containement.")};
76  Name("pidFromCollection"),
77  Comment("Flag used to get initial particle id estimate from ParticleID "
78  "collection specified by pidInputTag."),
79  false};
81  Comment("InputTag of ParticleID collection.")};
83  Name("pidFromLengthCut"),
84  Comment("Particle ID based on length: if shorted than cut is assumed "
85  "to be a proton, if longer a muon; disabled if negative."),
86  -1.};
88  Name("defaultPdgId"),
89  Comment("Default particle id hypothesis (all other options are set to "
90  "false, or if the estimate is not available)."),
91  13};
93  Name("dirFromVec"),
94  Comment("Assume track direction as the one giving positive dot product "
95  "with vector specified by dirVec."),
96  false};
98  Name("dirVec"),
99  Comment("Fhicl sequence defining the vector used when dirFromVec=true. "
100  "It must have 3 elements.")};
102  Name("alwaysInvertDir"),
103  Comment("If true, fit all tracks from end to vertex assuming inverted "
104  "direction."),
105  false};
107  Name("keepInputTrajectoryPoints"),
108  Comment("Option to keep positions and directions from input "
109  "trajectory/track. The fit will provide only covariance matrices, "
110  "chi2, ndof, particle Id and absolute momentum. It may also modify "
111  "the trajectory point flags. In order to avoid inconsistencies, it "
112  "has to be used with the following fitter options all set to false: "
113  "sortHitsByPlane, sortOutputHitsMinLength, skipNegProp."),
114  false};
115  //
116  };
117 
118  struct Config {
119  using Name = fhicl::Name;
124  };
126 
129  : p_(p)
130  , prop{p_().propagator}
131  , kalmanFitter{&prop, p_().fitter}
132  , mcsfitter{p_().mcsfit}
133  , mom_def_{p_().options().defaultMomInGeV()}
134  , momFromMCSColl_{p_().options().momFromMCSCollection()}
135  , momFromCombAndPid_{p_().options().momFromCombAndPid()}
136  , pidFromColl_{p_().options().pidFromCollection()}
137  , mom_len_cut_{p_().options().pidFromLengthCut()}
138  , pid_def_{p_().options().defaultPdgId()}
139  , alwaysFlip_{p_().options().alwaysInvertDir()}
140  , dirFromVec_{p_().options().dirFromVec()}
141  {
142  if (momFromMCSColl_) { mcsInputTag_ = p_().options().mcsInputTag(); }
143  if (momFromCombAndPid_) { contInputTag_ = p_().options().contInputTag(); }
144  if (pidFromColl_) { pidInputTag_ = p_().options().pidInputTag(); }
145  if (dirFromVec_) {
146  auto d = p_().options().dirVec();
147  dirVec = recob::tracking::Vector_t{d[0], d[1], d[2]};
148  }
149  //
150  if (p_().options().keepInputTrajectoryPoints() &&
151  (p_().fitter().sortHitsByPlane() || p_().fitter().sortOutputHitsMinLength() ||
152  p_().fitter().skipNegProp())) {
153  throw cet::exception("KalmanFilterFitTrackMaker")
154  << "Incompatible configuration parameters: keepInputTrajectoryPoints "
155  "needs the following fitter options all set to false: "
156  "sortHitsByPlane, sortOutputHitsMinLength, skipNegProp."
157  << "\n";
158  }
160  throw cet::exception("KalmanFilterFitTrackMaker")
161  << "Incompatible configuration parameters: momFromMCSCollection and "
162  "momFromCombAndPid cannot be both true at the same time."
163  << "\n";
164  }
165  if (pidFromColl_ && mom_len_cut_ > 0) {
166  throw cet::exception("KalmanFilterFitTrackMaker")
167  << "Incompatible configuration parameters: pidFromCollection and "
168  "pidFromLengthCut cannot be respectively true and >0. at the same "
169  "time."
170  << "\n";
171  }
172  if (alwaysFlip_ && dirFromVec_) {
173  throw cet::exception("KalmanFilterFitTrackMaker")
174  << "Incompatible configuration parameters: alwaysInvertDir and "
175  "dirFromVec cannot be both true at the same time."
176  << "\n";
177  }
178  //
179  }
180 
182  void initEvent(const art::Event& e) override
183  {
184  if (momFromMCSColl_)
185  mcs = e.getValidHandle<std::vector<recob::MCSFitResult>>(mcsInputTag_).product();
186  if (momFromCombAndPid_) {
187  cont = e.getValidHandle<std::vector<anab::CosmicTag>>(contInputTag_).product();
188  }
189  if (pidFromColl_) {
190  pid = e.getValidHandle<std::vector<anab::ParticleID>>(pidInputTag_).product();
191  }
192  }
193 
196  const recob::TrackTrajectory& traj,
197  const int tkID,
198  const std::vector<art::Ptr<recob::Hit>>& inHits,
199  const recob::tracking::SMatrixSym55& covVtx,
200  const recob::tracking::SMatrixSym55& covEnd,
201  recob::Track& outTrack,
203  OptionalOutputs& optionals) const;
204 
208  const recob::TrackTrajectory& traj,
209  const int tkID,
210  const std::vector<art::Ptr<recob::Hit>>& inHits,
211  recob::Track& outTrack,
213  OptionalOutputs& optionals) const override
214  {
215  return makeTrackImpl(detProp,
216  traj,
217  tkID,
218  inHits,
221  outTrack,
222  outHits,
223  optionals);
224  }
225 
227  int getParticleID(const recob::TrackTrajectory& traj, const int tkID) const;
229  double getMomentum(const recob::TrackTrajectory& traj,
230  const int pid,
231  const bool isFlip,
232  const int tkID) const;
234  bool isFlipDirection(const recob::TrackTrajectory& traj, const int tkID) const;
235 
239  const std::vector<art::Ptr<recob::Hit>>& inHits,
240  recob::Track& outTrack,
242  OptionalOutputs& optionals) const;
243 
244  private:
249  double mom_def_;
256  double mom_len_cut_;
257  int pid_def_;
261  const std::vector<recob::MCSFitResult>* mcs = nullptr;
262  const std::vector<anab::CosmicTag>* cont = nullptr;
263  const std::vector<anab::ParticleID>* pid = nullptr;
265  };
266 }
267 
269  const detinfo::DetectorPropertiesData& detProp,
270  const recob::TrackTrajectory& traj,
271  const int tkID,
272  const std::vector<art::Ptr<recob::Hit>>& inHits,
273  const recob::tracking::SMatrixSym55& covVtx,
274  const recob::tracking::SMatrixSym55& covEnd,
275  recob::Track& outTrack,
277  OptionalOutputs& optionals) const
278 {
279  const int pid = getParticleID(traj, tkID);
280  const bool flipDirection = isFlipDirection(traj, tkID);
281  const double mom = getMomentum(traj, pid, flipDirection, tkID); // what about mom uncertainty?
282  bool fitok = kalmanFitter.fitTrack(detProp,
283  traj,
284  tkID,
285  covVtx,
286  covEnd,
287  inHits,
288  mom,
289  pid,
290  flipDirection,
291  outTrack,
292  outHits,
293  optionals);
294  if (!fitok) return false;
295  if (p_().options().keepInputTrajectoryPoints()) {
296  restoreInputPoints(traj, inHits, outTrack, outHits, optionals);
297  }
298  return true;
299 }
300 
302  const int pid,
303  const bool isFlip,
304  const int tkID) const
305 {
306  double mom = (mom_def_ > 0 ? mom_def_ : traj.StartMomentum());
307  if (momFromMCSColl_) {
308  double mcsmom = (isFlip ? mcs->at(tkID).bwdMomentum() : mcs->at(tkID).fwdMomentum());
309  // make sure the mcs fit converged
310  if (mcsmom > 0.01 && mcsmom < 7.) mom = mcsmom;
311  }
312  if (momFromCombAndPid_) {
313  bool isContained = cont->at(tkID).CosmicType() == anab::kNotTagged;
314  // for now momentum from range implemented only for muons and protons
315  // so treat pions as muons (~MIPs) and kaons as protons
316  int pidtmp = pid;
317  if (pidtmp == 211 || pidtmp == 13)
318  pidtmp = 13;
319  else
320  pidtmp = 2212;
321  mom = tmc.GetTrackMomentum(traj.Length(), pidtmp);
322  if (isContained == false) {
323  auto mcsresult = mcsfitter.fitMcs(traj, pid);
324  double mcsmom = (isFlip ? mcsresult.bwdMomentum() : mcsresult.fwdMomentum());
325  // make sure the mcs fit converged, also the mcsmom should not be less
326  // than the range!
327  if (mcsmom > 0.01 && mcsmom < 7. && mcsmom > mom) mom = mcsmom;
328  }
329  }
330  return mom;
331 }
332 
334  const int /* tkID */) const
335 {
336  if (pidFromColl_) {
337  return -1; //pid->at(tkID).Pdg();
338  }
339  if (mom_len_cut_ > 0.) { return (traj.Length() < mom_len_cut_ ? 2212 : 13); }
340  return pid_def_;
341 }
342 
344  const int /* tkID */) const
345 {
346  if (alwaysFlip_) { return true; }
347  else if (dirFromVec_) {
348  auto tdir = traj.VertexDirection();
349  if (tdir.Dot(dirVec) < 0.) return true;
350  }
351  return false;
352 }
353 
355  const recob::TrackTrajectory& traj,
356  const std::vector<art::Ptr<recob::Hit>>& inHits,
357  recob::Track& outTrack,
359  OptionalOutputs& optionals) const
360 {
361  if (optionals.isTrackFitInfosInit()) {
362  throw cet::exception("KalmanFilterFitTrackMaker")
363  << "Option keepInputTrajectoryPoints not compatible with "
364  "doTrackFitHitInfo, please set doTrackFitHitInfo to false in the "
365  "track producer.\n";
366  }
367  const auto np = outTrack.NumberTrajectoryPoints();
369  outHits, optionals, outTrack.ID(), outTrack.ParticleId(), traj.HasMomentum());
370  //
371  std::vector<unsigned int> flagsmap(np, -1);
372  for (unsigned int i = 0; i < np; ++i)
373  flagsmap[outTrack.FlagsAtPoint(i).fromHit()] = i;
374  //
375  for (unsigned int p = 0; p < np; ++p) {
376  auto mask = outTrack.FlagsAtPoint(flagsmap[p]).mask();
379  tcbk.addPoint(traj.LocationAtPoint(p),
380  traj.MomentumVectorAtPoint(p),
381  inHits[p],
383  0);
384  }
385  auto covs = outTrack.Covariances();
386  tcbk.setTotChi2(outTrack.Chi2());
387  outTrack = tcbk.finalizeTrack(std::move(covs.first), std::move(covs.second));
388  //
389 }
390 
int getParticleID(const recob::TrackTrajectory &traj, const int tkID) const
set the particle ID hypothesis
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
Fit tracks using Kalman Filter fit+smooth.
bool makeTrack(const detinfo::DetectorPropertiesData &detProp, const recob::TrackTrajectory &traj, const int tkID, const std::vector< art::Ptr< recob::Hit >> &inHits, recob::Track &outTrack, std::vector< art::Ptr< recob::Hit >> &outHits, OptionalOutputs &optionals) const override
static constexpr Flag_t NoPoint
The trajectory point is not defined.
int ParticleId() const
Access to various track properties.
Definition: Track.h:211
KalmanFilterFitTrackMaker(Parameters const &p)
Constructor from Parameters.
recob::tracking::SMatrixSym55 SMatrixSym55
Definition: TrackState.h:15
float Chi2() const
Access to various track properties.
Definition: Track.h:208
size_t NumberTrajectoryPoints() const
Various functions related to the presence and the number of (valid) points.
Definition: Track.h:136
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.
ROOT::Math::SMatrix< Double32_t, 5, 5, ROOT::Math::MatRepSym< Double32_t, 5 >> SMatrixSym55
constexpr Mask_t const & mask() const
Returns the entire set of bits as a bit mask.
std::pair< SMatrixSym55, SMatrixSym55 > Covariances() const
Access to position, momentum or covariance at the start and end of the track.
Definition: Track.h:199
void restoreInputPoints(const recob::TrackTrajectory &traj, const std::vector< art::Ptr< recob::Hit >> &inHits, recob::Track &outTrack, std::vector< art::Ptr< recob::Hit >> &outHits, OptionalOutputs &optionals) const
recob::MCSFitResult fitMcs(const recob::TrackTrajectory &traj) const
Base abstract class for tools used to fit tracks.
Definition: TrackMaker.h:199
constexpr HitIndex_t fromHit() const
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
Vector_t VertexDirection() const
Returns the direction of the trajectory at the first point.
const std::vector< anab::CosmicTag > * cont
const std::vector< recob::MCSFitResult > * mcs
double Length(size_t startAt=0) const
Returns the approximate length of the trajectory.
bool makeTrackImpl(const detinfo::DetectorPropertiesData &detProp, const recob::TrackTrajectory &traj, const int tkID, const std::vector< art::Ptr< recob::Hit >> &inHits, const recob::tracking::SMatrixSym55 &covVtx, const recob::tracking::SMatrixSym55 &covEnd, recob::Track &outTrack, std::vector< art::Ptr< recob::Hit >> &outHits, OptionalOutputs &optionals) const
function that actually calls the fitter
const std::vector< anab::ParticleID > * pid
A trajectory in space reconstructed from hits.
T LocationAtPoint(unsigned int p) const
Position at point p. Use e.g. as:
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< Coord_t >, ROOT::Math::GlobalCoordinateSystemTag > Vector_t
Type for representation of momenta in 3D space. See recob::tracking::Coord_t for more details on the ...
Definition: TrackingTypes.h:31
Float_t d
Definition: plot.C:235
double StartMomentum() const
Concrete implementation of a tool to fit tracks with TrackKalmanFitter.
double getMomentum(const recob::TrackTrajectory &traj, const int pid, const bool isFlip, const int tkID) const
set the initial momentum estimate
void initEvent(const art::Event &e) override
initialize event: get collection of recob::MCSFitResult
int ID() const
Definition: Track.h:244
ValidHandle< PROD > getValidHandle(InputTag const &tag) const
void unset(Flag first, OtherFlags...others)
Unsets all specified flags.
Definition: BitMask.h:754
bool HasMomentum() const
Returns whether information about the momentum is available.
Definition: Trajectory.h:387
Helper class to aid the creation of a recob::Track, keeping data vectors in sync. ...
Class for Maximum Likelihood fit of Multiple Coulomb Scattering angles between segments within a Trac...
PointFlags_t const & FlagsAtPoint(size_t i) const
Access to i-th TrajectoryPoint or its Flags.
Definition: Track.h:152
bool isTrackFitInfosInit()
check initialization of the output vector of TrackFitHitInfos
Definition: TrackMaker.h:147
bool isFlipDirection(const recob::TrackTrajectory &traj, const int tkID) const
decide whether to flip the direction or not
T MomentumVectorAtPoint(unsigned int p) const
Momentum vector at point p. Use e.g. as:
Float_t e
Definition: plot.C:35
Struct holding optional TrackMaker outputs.
Definition: TrackMaker.h:108
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:
Definition: Track.h:49
double GetTrackMomentum(double trkrange, int pdg) const
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33