CMS 3D CMS Logo

L1fittedTrack.h
Go to the documentation of this file.
1 #ifndef L1Trigger_TrackFindingTMTT_L1fittedTrack_h
2 #define L1Trigger_TrackFindingTMTT_L1fittedTrack_h
3 
16 
17 #include <vector>
18 #include <set>
19 #include <utility>
20 #include <string>
21 #include <memory>
22 
23 //=== This represents a fitted L1 track candidate found in 3 dimensions.
24 //=== It gives access to the fitted helix parameters & chi2 etc.
25 //=== It also calculates & gives access to associated truth particle (Tracking Particle) if any.
26 //=== It also gives access to the 3D hough-transform track candidate (L1track3D) on which the fit was run.
27 
28 namespace tmtt {
29 
30  class L1fittedTrack : public L1trackBase {
31  public:
32  // Store a new fitted track, specifying the input Hough transform track, the stubs used for the fit,
33  // bit-encoded hit layer pattern (numbered by increasing distance from origin),
34  // the fitted helix parameters & chi2,
35  // and the number of helix parameters being fitted (=5 if d0 is fitted, or =4 if d0 is not fitted).
36  // And if track fit declared this to be a valid track (enough stubs left on track after fit etc.).
37  L1fittedTrack(const Settings* settings,
38  const L1track3D* l1track3D,
39  const std::vector<Stub*>& stubs,
40  unsigned int hitPattern,
41  float qOverPt,
42  float d0,
43  float phi0,
44  float z0,
45  float tanLambda,
46  float chi2rphi,
47  float chi2rz,
48  unsigned int nHelixParam,
49  bool accepted = true)
50  : L1trackBase(),
51  settings_(settings),
53  stubs_(stubs),
54  stubsConst_(stubs_.begin(), stubs_.end()),
57  d0_(d0),
58  phi0_(phi0),
59  z0_(z0),
62  chi2rz_(chi2rz),
65  d0_bcon_(d0),
69  nSkippedLayers_(0),
70  numUpdateCalls_(0),
71  numIterations_(0),
73  if (l1track3D != nullptr) {
77  } else { // Rejected track
78  iPhiSec_ = 0;
79  iEtaReg_ = 0;
80  optoLinkID_ = 0;
81  }
82  if (settings != nullptr) {
83  // Count tracker layers these stubs are in
85  // Find associated truth particle & calculate info about match.
87  } else { // Rejected track
88  nLayers_ = 0;
89  matchedTP_ = nullptr;
90  }
91  // Set d0 = 0 for 4 param fit, in case fitter didn't do it.
92  if (nHelixParam == 4) {
93  d0_ = 0.;
94  d0_bcon_ = 0.;
95  }
96  if (settings != nullptr && not settings->hybrid()) {
97  //Sector class used to check if fitted track trajectory is in expected sector.
98  secTmp_ = std::make_shared<Sector>(settings, iPhiSec_, iEtaReg_);
99  // HT class used to identify HT cell that corresponds to fitted helix parameters.
100  htRphiTmp_ = std::make_shared<HTrphi>(
101  settings, iPhiSec_, iEtaReg_, secTmp_->etaMin(), secTmp_->etaMax(), secTmp_->phiCentre());
102  this->setConsistentHTcell();
103  } else {
104  consistentCell_ = false;
105  }
106  }
107 
108  // Creates track rejected by fitter.
109  L1fittedTrack() : L1fittedTrack(nullptr, nullptr, noStubs_, 0, 0., 0., 0., 0., 0., 0., 0., 0, false) {}
110 
111  ~L1fittedTrack() override = default;
112 
113  //--- Optionally std::set track helix params & chi2 if beam-spot constraint is used (for 5-parameter fit).
114  void setBeamConstr(float qOverPt_bcon, float phi0_bcon, float chi2rphi_bcon, bool accepted) {
115  done_bcon_ = true;
117  d0_bcon_ = 0.0, phi0_bcon_ = phi0_bcon;
120  }
121 
122  //--- Set/get additional info about fitted track that is specific to individual track fit algorithms (KF, LR, chi2)
123  //--- and is used for debugging/histogramming purposes.
124 
125  void setInfoKF(unsigned int nSkippedLayers, unsigned int numUpdateCalls) {
126  nSkippedLayers_ = nSkippedLayers;
127  numUpdateCalls_ = numUpdateCalls;
128  }
129  void setInfoLR(int numIterations, std::string lostMatchingState, std::unordered_map<std::string, int> stateCalls) {
130  numIterations_ = numIterations;
131  lostMatchingState_ = lostMatchingState;
132  stateCalls_ = stateCalls;
133  }
134  void setInfoCHI2() {}
135 
136  void infoKF(unsigned int& nSkippedLayers, unsigned int& numUpdateCalls) const {
137  nSkippedLayers = nSkippedLayers_;
138  numUpdateCalls = numUpdateCalls_;
139  }
140  void infoLR(int& numIterations,
141  std::string& lostMatchingState,
142  std::unordered_map<std::string, int>& stateCalls) const {
143  numIterations = numIterations_;
144  lostMatchingState = lostMatchingState_;
145  stateCalls = stateCalls_;
146  }
147  void infoCHI2() const {}
148 
149  //--- Convert fitted track to KFTrackletTrack format, for use with HYBRID.
150 
152  KFTrackletTrack trk_(l1track3D(),
153  stubsConst(),
154  hitPattern(),
155  qOverPt(),
156  d0(),
157  phi0(),
158  z0(),
159  tanLambda(),
160  chi2rphi(),
161  chi2rz(),
162  nHelixParam(),
163  iPhiSec(),
164  iEtaReg(),
165  accepted(),
166  done_bcon(),
167  qOverPt_bcon(),
168  d0_bcon(),
169  phi0_bcon(),
170  chi2rphi_bcon());
171  return trk_;
172  }
173 
174  //--- Get the 3D Hough transform track candididate corresponding to the fitted track,
175  //--- Provide direct access to some of the info it contains.
176 
177  // Get track candidate from HT (before fit).
178  const L1track3D* l1track3D() const { return l1track3D_; }
179 
180  // Get stubs on fitted track (can differ from those on HT track if track fit kicked out stubs with bad residuals)
181  const std::vector<const Stub*>& stubsConst() const override { return stubsConst_; }
182  const std::vector<Stub*>& stubs() const override { return stubs_; }
183  // Get number of stubs on fitted track.
184  unsigned int numStubs() const override { return stubs_.size(); }
185  // Get number of tracker layers these stubs are in.
186  unsigned int numLayers() const override { return nLayers_; }
187  // Get number of stubs deleted from track candidate by fitter (because they had large residuals)
188  unsigned int numKilledStubs() const { return l1track3D_->numStubs() - this->numStubs(); }
189  // Get bit-encoded hit pattern (where layer number assigned by increasing distance from origin, according to layers track expected to cross).
190  unsigned int hitPattern() const { return hitPattern_; }
191 
192  // Get Hough transform cell locations in units of bin number, corresponding to the fitted helix parameters of the track.
193  // Always uses the beam-spot constrained helix params if they are available.
194  // (If fitted track is outside HT array, it it put in the closest bin inside it).
195  std::pair<unsigned int, unsigned int> cellLocationFit() const { return htRphiTmp_->cell(this); }
196  // Also get HT cell determined by Hough transform.
197  std::pair<unsigned int, unsigned int> cellLocationHT() const override { return l1track3D_->cellLocationHT(); }
198 
199  //--- Get information about its association (if any) to a truth Tracking Particle.
200  //--- Can differ from that of corresponding HT track, if track fit kicked out stubs with bad residuals.
201 
202  // Get best matching tracking particle (=nullptr if none).
203  const TP* matchedTP() const override { return matchedTP_; }
204  // Get the matched stubs with this Tracking Particle
205  const std::vector<const Stub*>& matchedStubs() const override { return matchedStubs_; }
206  // Get number of matched stubs with this Tracking Particle
207  unsigned int numMatchedStubs() const override { return matchedStubs_.size(); }
208  // Get number of tracker layers with matched stubs with this Tracking Particle
209  unsigned int numMatchedLayers() const override { return nMatchedLayers_; }
210  // Get purity of stubs on track (i.e. fraction matching best Tracking Particle)
211  float purity() const { return numMatchedStubs() / float(numStubs()); }
212  // Get number of stubs matched to correct TP that were deleted from track candidate by fitter.
213  unsigned int numKilledMatchedStubs() const {
214  unsigned int nStubCount = l1track3D_->numMatchedStubs();
215  if (nStubCount > 0) { // Original HT track candidate did match a truth particle
216  const TP* tp = l1track3D_->matchedTP();
217  for (const Stub* s : stubs_) {
218  std::set<const TP*> assTPs = s->assocTPs();
219  if (assTPs.find(tp) != assTPs.end())
220  nStubCount--; // We found a stub matched to original truth particle that survived fit.
221  }
222  }
223  return nStubCount;
224  }
225 
226  //--- Get the fitted track helix parameters.
227 
228  float qOverPt() const override { return qOverPt_; }
229  float charge() const { return (qOverPt_ > 0 ? 1 : -1); }
230  float invPt() const { return std::abs(qOverPt_); }
231  // Protect pt against 1/pt = 0.
232  float pt() const {
233  constexpr float small = 1.0e-6;
234  return 1. / (small + this->invPt());
235  }
236  float d0() const { return d0_; }
237  float phi0() const override { return phi0_; }
238  float z0() const { return z0_; }
239  float tanLambda() const { return tanLambda_; }
240  float theta() const { return atan2(1., tanLambda_); } // Use atan2 to ensure 0 < theta < pi.
241  float eta() const { return -log(tan(0.5 * this->theta())); }
242 
243  //--- Get the fitted helix parameters with beam-spot constraint.
244  //--- If constraint not applied (e.g. 4 param fit) then these are identical to unconstrained values.
245 
246  bool done_bcon() const { return done_bcon_; } // Was beam-spot constraint aplied?
247  float qOverPt_bcon() const { return qOverPt_bcon_; }
248  float charge_bcon() const { return (qOverPt_bcon_ > 0 ? 1 : -1); }
249  float invPt_bcon() const { return std::abs(qOverPt_bcon_); }
250  // Protect pt against 1/pt = 0.
251  float pt_bcon() const {
252  constexpr float small = 1.0e-6;
253  return 1. / (small + this->invPt_bcon());
254  }
255  float phi0_bcon() const { return phi0_bcon_; }
256  float d0_bcon() const { return d0_bcon_; }
257 
258  // Phi and z coordinates at which track crosses "chosenR" values used by r-phi HT and rapidity sectors respectively.
259  // (Optionally with beam-spot constraint applied).
260  float phiAtChosenR(bool beamConstraint) const {
261  if (beamConstraint) {
264  0.);
265  } else {
267  d0_ / (settings_->chosenRofPhi()),
268  0.);
269  }
270  }
271  float zAtChosenR() const {
272  return (z0_ + (settings_->chosenRofZ()) * tanLambda_);
273  } // neglects transverse impact parameter & track curvature.
274 
275  // Get the number of helix parameters being fitted (=5 if d0 is fitted or =4 if d0 is not fitted).
276  float nHelixParam() const { return nHelixParam_; }
277 
278  // Get the fit degrees of freedom, chi2 & chi2/DOF (also in r-phi & r-z planes).
279  unsigned int numDOF() const { return 2 * this->numStubs() - nHelixParam_; }
280  unsigned int numDOFrphi() const { return this->numStubs() - (nHelixParam_ - 2); }
281  unsigned int numDOFrz() const { return this->numStubs() - 2; }
282  float chi2rphi() const { return chi2rphi_; }
283  float chi2rz() const { return chi2rz_; }
284  float chi2() const { return chi2rphi_ + chi2rz_; }
285  float chi2dof() const { return (this->chi2()) / this->numDOF(); }
286 
287  //--- Ditto, but if beam-spot constraint is applied.
288  //--- If constraint not applied (e.g. 4 param fit) then these are identical to unconstrained values.
289  unsigned int numDOF_bcon() const { return (this->numDOF() - 1); }
290  unsigned int numDOFrphi_bcon() const { return (this->numDOFrphi() - 1); }
291  float chi2rphi_bcon() const { return chi2rphi_bcon_; }
292  float chi2_bcon() const { return chi2rphi_bcon_ + chi2rz_; }
293  float chi2dof_bcon() const { return (this->chi2_bcon()) / this->numDOF_bcon(); }
294 
295  //--- Get phi sector and eta region used by track finding code that this track is in.
296  unsigned int iPhiSec() const override { return iPhiSec_; }
297  unsigned int iEtaReg() const override { return iEtaReg_; }
298 
299  //--- Opto-link ID used to send this track from HT to Track Fitter
300  unsigned int optoLinkID() const override { return optoLinkID_; }
301 
302  //--- Get whether the track has been rejected or accepted by the fit
303 
304  bool accepted() const { return accepted_; }
305 
306  //--- Functions to help eliminate duplicate tracks.
307 
308  // Is the fitted track trajectory should lie within the same HT cell in which the track was originally found?
309  bool consistentHTcell() const { return consistentCell_; }
310 
311  // Determine if the fitted track trajectory should lie within the same HT cell in which the track was originally found?
313  // Use helix params with beam-spot constaint if done in case of 5 param fit.
314 
315  std::pair<unsigned int, unsigned int> htCell = this->cellLocationHT();
316  bool consistent = (htCell == this->cellLocationFit());
317 
318  if (l1track3D_->mergedHTcell()) {
319  // If this is a merged cell, check other elements of merged cell.
320  std::pair<unsigned int, unsigned int> htCell10(htCell.first + 1, htCell.second);
321  std::pair<unsigned int, unsigned int> htCell01(htCell.first, htCell.second + 1);
322  std::pair<unsigned int, unsigned int> htCell11(htCell.first + 1, htCell.second + 1);
323  if (htCell10 == this->cellLocationFit())
324  consistent = true;
325  if (htCell01 == this->cellLocationFit())
326  consistent = true;
327  if (htCell11 == this->cellLocationFit())
328  consistent = true;
329  }
330 
331  consistentCell_ = consistent;
332  }
333 
334  // Is the fitted track trajectory within the same (eta,phi) sector of the HT used to find it?
335  bool consistentSector() const {
336  if (settings_->hybrid()) {
337  float phiCentre = 2. * M_PI * iPhiSec() / settings_->numPhiSectors();
338  float sectorHalfWidth = M_PI / settings_->numPhiSectors();
339  bool insidePhi = (std::abs(reco::deltaPhi(this->phiAtChosenR(done_bcon_), phiCentre)) < sectorHalfWidth);
340  return insidePhi;
341  } else {
342  bool insidePhi = (std::abs(reco::deltaPhi(this->phiAtChosenR(done_bcon_), secTmp_->phiCentre())) <
343  secTmp_->sectorHalfWidth());
344  bool insideEta =
345  (this->zAtChosenR() > secTmp_->zAtChosenR_Min() && this->zAtChosenR() < secTmp_->zAtChosenR_Max());
346  return (insidePhi && insideEta);
347  }
348  }
349 
350  // Digitize track and degrade helix parameter resolution according to effect of digitisation.
351  void digitizeTrack(const std::string& fitterName);
352 
353  // Access to detailed info about digitized track. (Gets nullptr if trk not digitized)
354  const DigitalTrack* digitaltrack() const { return digitalTrack_.get(); }
355 
356  private:
357  //--- Configuration parameters
359 
360  //--- The 3D hough-transform track candidate which was fitted.
362 
363  //--- The stubs on the fitted track (can differ from those on HT track if fit kicked off stubs with bad residuals)
364  std::vector<Stub*> stubs_;
365  std::vector<const Stub*> stubsConst_;
366  unsigned int nLayers_;
367 
368  //--- Bit-encoded hit pattern (where layer number assigned by increasing distance from origin, according to layers track expected to cross).
369  unsigned int hitPattern_;
370 
371  //--- The fitted helix parameters and fit chi-squared.
372  float qOverPt_;
373  float d0_;
374  float phi0_;
375  float z0_;
376  float tanLambda_;
377  float chi2rphi_;
378  float chi2rz_;
379 
380  //--- Ditto with beam-spot constraint applied in case of 5-parameter fit, plus boolean to indicate
383  float d0_bcon_;
384  float phi0_bcon_;
386 
387  //--- The number of helix parameters being fitted (=5 if d0 is fitted or =4 if d0 is not fitted).
388  unsigned int nHelixParam_;
389 
390  //--- Phi sector and eta region used track finding code that this track was in.
391  unsigned int iPhiSec_;
392  unsigned int iEtaReg_;
393  //--- Opto-link ID from HT to Track Fitter.
394  unsigned int optoLinkID_;
395 
396  //--- Information about its association (if any) to a truth Tracking Particle.
397  const TP* matchedTP_;
398  std::vector<const Stub*> matchedStubs_;
399  unsigned int nMatchedLayers_;
400 
401  //--- Sector class used to check if fitted track trajectory is in same sector as HT used to find it.
402  std::shared_ptr<Sector> secTmp_; // shared so as to allow copy of L1fittedTrack.
403  //--- r-phi HT class used to determine HT cell location that corresponds to fitted track helix parameters.
404  std::shared_ptr<HTrphi> htRphiTmp_;
405 
406  //--- Info specific to KF fitter.
407  unsigned int nSkippedLayers_;
408  unsigned int numUpdateCalls_;
409  //--- Info specific to LR fitter.
412  std::unordered_map<std::string, int> stateCalls_;
413 
414  std::shared_ptr<DigitalTrack> digitalTrack_; // Class used to digitize track if required.
415 
416  static const std::vector<Stub*> noStubs_; // Empty vector used to initialize rejected tracks.
417 
419 
420  //--- Has the track fit declared this to be a valid track?
421  bool accepted_;
422  };
423 
424 } // namespace tmtt
425 
426 #endif
unsigned int iEtaReg() const override
Definition: L1track3D.h:175
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
double invPtToDphi() const
Definition: Settings.h:397
float zAtChosenR() const
std::unordered_map< std::string, int > stateCalls_
bool done_bcon() const
unsigned int iEtaReg() const override
unsigned int numLayers() const override
~L1fittedTrack() override=default
float invPt_bcon() const
unsigned int numMatchedStubs() const override
Definition: L1track3D.h:190
unsigned int numDOF() const
float d0() const
float purity() const
std::pair< unsigned int, unsigned int > cellLocationHT() const override
unsigned int iPhiSec_
std::shared_ptr< HTrphi > htRphiTmp_
const TP * matchedTP() const override
Definition: L1track3D.h:186
void infoLR(int &numIterations, std::string &lostMatchingState, std::unordered_map< std::string, int > &stateCalls) const
unsigned int numDOFrphi() const
static const std::vector< Stub * > noStubs_
unsigned int nSkippedLayers_
void digitizeTrack(const std::string &fitterName)
const L1track3D * l1track3D() const
void infoCHI2() const
std::pair< unsigned int, unsigned int > cellLocationHT() const override
Definition: L1track3D.h:101
float theta() const
unsigned int numKilledStubs() const
float qOverPt_bcon() const
float tanLambda() const
float chi2dof_bcon() const
float invPt() const
unsigned int numKilledMatchedStubs() const
double chosenRofZ() const
Definition: Settings.h:127
unsigned int iEtaReg_
bool mergedHTcell() const
Definition: L1track3D.h:181
KFTrackletTrack returnKFTrackletTrack()
unsigned int iPhiSec() const override
Definition: L1track3D.h:174
Definition: TP.h:23
std::pair< unsigned int, unsigned int > cellLocationFit() const
float chi2rphi() const
float phi0_bcon() const
unsigned int numUpdateCalls_
std::shared_ptr< Sector > secTmp_
float qOverPt() const override
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
const TP * matchingTP(const Settings *settings, const std::vector< const Stub *> &vstubs, unsigned int &nMatchedLayersBest, std::vector< const Stub *> &matchedStubsBest)
Definition: Utility.cc:63
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float phi0() const override
float chi2() const
float chi2rphi_bcon() const
std::shared_ptr< DigitalTrack > digitalTrack_
unsigned int optoLinkID() const override
std::vector< Stub * > stubs_
const DigitalTrack * digitaltrack() const
double chosenRofPhi() const
Definition: Settings.h:112
#define M_PI
std::string lostMatchingState_
unsigned int numDOF_bcon() const
float chi2_bcon() const
float nHelixParam() const
float charge() const
unsigned int nLayers_
bool consistentSector() const
void setInfoKF(unsigned int nSkippedLayers, unsigned int numUpdateCalls)
const TP * matchedTP() const override
float charge_bcon() const
unsigned int nMatchedLayers_
=== This is the base class for the linearised chi-squared track fit algorithms.
Definition: Array2D.h:16
float chi2dof() const
unsigned int numDOFrphi_bcon() const
void setInfoLR(int numIterations, std::string lostMatchingState, std::unordered_map< std::string, int > stateCalls)
const std::vector< const Stub * > & stubsConst() const override
float phiAtChosenR(bool beamConstraint) const
unsigned int hitPattern_
float pt_bcon() const
float chi2rz() const
void infoKF(unsigned int &nSkippedLayers, unsigned int &numUpdateCalls) const
const std::vector< Stub * > & stubs() const override
unsigned int iPhiSec() const override
float pt() const
const L1track3D * l1track3D_
unsigned int numMatchedStubs() const override
std::vector< const Stub * > matchedStubs_
bool hybrid() const
Definition: Settings.h:409
bool accepted() const
unsigned int optoLinkID() const override
Definition: L1track3D.h:178
unsigned int numStubs() const override
Definition: L1track3D.h:97
unsigned int nHelixParam_
unsigned int optoLinkID_
float eta() const
unsigned int hitPattern() const
const std::vector< const Stub * > & matchedStubs() const override
L1fittedTrack(const Settings *settings, const L1track3D *l1track3D, const std::vector< Stub *> &stubs, unsigned int hitPattern, float qOverPt, float d0, float phi0, float z0, float tanLambda, float chi2rphi, float chi2rz, unsigned int nHelixParam, bool accepted=true)
Definition: L1fittedTrack.h:37
unsigned int countLayers(const Settings *settings, const std::vector< const Stub *> &stubs, bool disableReducedLayerID=false, bool onlyPS=false)
Definition: Utility.cc:25
unsigned int numPhiSectors() const
Definition: Settings.h:110
std::vector< const Stub * > stubsConst_
const Settings * settings_
unsigned int numMatchedLayers() const override
bool consistentHTcell() const
unsigned int numStubs() const override
float d0_bcon() const
float z0() const
void setBeamConstr(float qOverPt_bcon, float phi0_bcon, float chi2rphi_bcon, bool accepted)
unsigned int numDOFrz() const