CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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),
52  l1track3D_(l1track3D),
53  stubs_(stubs),
55  hitPattern_(hitPattern),
56  qOverPt_(qOverPt),
57  d0_(d0),
58  phi0_(phi0),
59  z0_(z0),
60  tanLambda_(tanLambda),
61  chi2rphi_(chi2rphi),
62  chi2rz_(chi2rz),
64  qOverPt_bcon_(qOverPt),
65  d0_bcon_(d0),
66  phi0_bcon_(phi0),
67  chi2rphi_bcon_(chi2rphi),
68  nHelixParam_(nHelixParam),
69  nSkippedLayers_(0),
70  numUpdateCalls_(0),
71  numIterations_(0),
73  if (l1track3D != nullptr) {
74  iPhiSec_ = l1track3D->iPhiSec();
75  iEtaReg_ = l1track3D->iEtaReg();
76  optoLinkID_ = l1track3D->optoLinkID();
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  bool insidePhi =
337  (std::abs(reco::deltaPhi(this->phiAtChosenR(done_bcon_), secTmp_->phiCentre())) < secTmp_->sectorHalfWidth());
338  bool insideEta =
339  (this->zAtChosenR() > secTmp_->zAtChosenR_Min() && this->zAtChosenR() < secTmp_->zAtChosenR_Max());
340  return (insidePhi && insideEta);
341  }
342 
343  // Digitize track and degrade helix parameter resolution according to effect of digitisation.
344  void digitizeTrack(const std::string& fitterName);
345 
346  // Access to detailed info about digitized track. (Gets nullptr if trk not digitized)
347  const DigitalTrack* digitaltrack() const { return digitalTrack_.get(); }
348 
349  private:
350  //--- Configuration parameters
352 
353  //--- The 3D hough-transform track candidate which was fitted.
355 
356  //--- The stubs on the fitted track (can differ from those on HT track if fit kicked off stubs with bad residuals)
357  std::vector<Stub*> stubs_;
358  std::vector<const Stub*> stubsConst_;
359  unsigned int nLayers_;
360 
361  //--- Bit-encoded hit pattern (where layer number assigned by increasing distance from origin, according to layers track expected to cross).
362  unsigned int hitPattern_;
363 
364  //--- The fitted helix parameters and fit chi-squared.
365  float qOverPt_;
366  float d0_;
367  float phi0_;
368  float z0_;
369  float tanLambda_;
370  float chi2rphi_;
371  float chi2rz_;
372 
373  //--- Ditto with beam-spot constraint applied in case of 5-parameter fit, plus boolean to indicate
376  float d0_bcon_;
377  float phi0_bcon_;
379 
380  //--- The number of helix parameters being fitted (=5 if d0 is fitted or =4 if d0 is not fitted).
381  unsigned int nHelixParam_;
382 
383  //--- Phi sector and eta region used track finding code that this track was in.
384  unsigned int iPhiSec_;
385  unsigned int iEtaReg_;
386  //--- Opto-link ID from HT to Track Fitter.
387  unsigned int optoLinkID_;
388 
389  //--- Information about its association (if any) to a truth Tracking Particle.
390  const TP* matchedTP_;
391  std::vector<const Stub*> matchedStubs_;
392  unsigned int nMatchedLayers_;
393 
394  //--- Sector class used to check if fitted track trajectory is in same sector as HT used to find it.
395  std::shared_ptr<Sector> secTmp_; // shared so as to allow copy of L1fittedTrack.
396  //--- r-phi HT class used to determine HT cell location that corresponds to fitted track helix parameters.
397  std::shared_ptr<HTrphi> htRphiTmp_;
398 
399  //--- Info specific to KF fitter.
400  unsigned int nSkippedLayers_;
401  unsigned int numUpdateCalls_;
402  //--- Info specific to LR fitter.
405  std::unordered_map<std::string, int> stateCalls_;
406 
407  std::shared_ptr<DigitalTrack> digitalTrack_; // Class used to digitize track if required.
408 
409  static const std::vector<Stub*> noStubs_; // Empty vector used to initialize rejected tracks.
410 
412 
413  //--- Has the track fit declared this to be a valid track?
414  bool accepted_;
415  };
416 
417 } // namespace tmtt
418 
419 #endif
unsigned int iEtaReg() const override
Definition: L1track3D.h:175
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
unsigned int numKilledMatchedStubs() const
std::unordered_map< std::string, int > stateCalls_
unsigned int iEtaReg() const override
unsigned int numLayers() const override
~L1fittedTrack() override=default
static std::vector< std::string > checklist log
unsigned int numMatchedStubs() const override
Definition: L1track3D.h:190
float chi2rphi() const
unsigned int numDOF_bcon() const
bool hybrid() const
Definition: Settings.h:409
std::pair< unsigned int, unsigned int > cellLocationHT() const override
float zAtChosenR() const
unsigned int iPhiSec_
std::shared_ptr< HTrphi > htRphiTmp_
const TP * matchedTP() const override
Definition: L1track3D.h:186
unsigned int numKilledStubs() const
float purity() const
float chi2dof_bcon() const
static const std::vector< Stub * > noStubs_
unsigned int nSkippedLayers_
float chi2rz() const
void digitizeTrack(const std::string &fitterName)
const TP * matchingTP(const Settings *settings, const std::vector< const Stub * > &vstubs, unsigned int &nMatchedLayersBest, std::vector< const Stub * > &matchedStubsBest)
Definition: Utility.cc:63
std::pair< unsigned int, unsigned int > cellLocationHT() const override
Definition: L1track3D.h:101
float chi2rphi_bcon() const
std::pair< unsigned int, unsigned int > cellLocationFit() const
float d0() const
float theta() const
float phi0_bcon() const
unsigned int iEtaReg_
float d0_bcon() const
float eta() const
float invPt_bcon() const
float pt_bcon() const
bool consistentHTcell() const
KFTrackletTrack returnKFTrackletTrack()
void infoCHI2() const
unsigned int iPhiSec() const override
Definition: L1track3D.h:174
Definition: TP.h:23
float phiAtChosenR(bool beamConstraint) const
const L1track3D * l1track3D() const
float qOverPt_bcon() const
float nHelixParam() const
unsigned int numUpdateCalls_
unsigned int numDOFrz() const
float chi2() const
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
std::shared_ptr< Sector > secTmp_
float qOverPt() const override
double chosenRofPhi() const
Definition: Settings.h:112
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float phi0() const override
float charge() const
float invPt() const
unsigned int hitPattern() const
unsigned int numDOFrphi() const
std::shared_ptr< DigitalTrack > digitalTrack_
unsigned int optoLinkID() const override
unsigned int numDOF() const
std::vector< Stub * > stubs_
float tanLambda() const
std::string lostMatchingState_
double invPtToDphi() const
Definition: Settings.h:397
unsigned int nLayers_
float pt() const
void setInfoKF(unsigned int nSkippedLayers, unsigned int numUpdateCalls)
const TP * matchedTP() const override
unsigned int nMatchedLayers_
float chi2_bcon() const
const DigitalTrack * digitaltrack() const
void infoKF(unsigned int &nSkippedLayers, unsigned int &numUpdateCalls) const
float charge_bcon() const
void setInfoLR(int numIterations, std::string lostMatchingState, std::unordered_map< std::string, int > stateCalls)
const std::vector< const Stub * > & stubsConst() const override
unsigned int hitPattern_
bool done_bcon() const
const std::vector< Stub * > & stubs() const override
unsigned int countLayers(const Settings *settings, const std::vector< const Stub * > &stubs, bool disableReducedLayerID=false, bool onlyPS=false)
Definition: Utility.cc:25
unsigned int iPhiSec() const override
float chi2dof() const
const L1track3D * l1track3D_
unsigned int numMatchedStubs() const override
std::vector< const Stub * > matchedStubs_
bool mergedHTcell() const
Definition: L1track3D.h:181
string end
Definition: dataset.py:937
unsigned int optoLinkID() const override
Definition: L1track3D.h:178
unsigned int numStubs() const override
Definition: L1track3D.h:97
double chosenRofZ() const
Definition: Settings.h:127
unsigned int nHelixParam_
unsigned int optoLinkID_
const std::vector< const Stub * > & matchedStubs() const override
void infoLR(int &numIterations, std::string &lostMatchingState, std::unordered_map< std::string, int > &stateCalls) const
unsigned int numDOFrphi_bcon() const
float z0() const
std::vector< const Stub * > stubsConst_
bool consistentSector() const
const Settings * settings_
bool accepted() const
unsigned int numMatchedLayers() const override
unsigned int numStubs() const override
void setBeamConstr(float qOverPt_bcon, float phi0_bcon, float chi2rphi_bcon, bool accepted)