CMS 3D CMS Logo

HTrphi.cc
Go to the documentation of this file.
8 
11 
12 #include <vector>
13 #include <limits>
14 #include <atomic>
15 #include <sstream>
16 #include <mutex>
17 
18 using namespace std;
19 
20 namespace tmtt {
21 
22  //=== The r-phi Hough Transform array for a single (eta,phi) sector.
23  //===
24  //=== Its axes are (q/Pt, phiTrk), where phiTrk is the phi at which the track crosses a
25  //=== user-configurable radius from the beam-line.
26 
27  //=== Initialise
28 
29  HTrphi::HTrphi(const Settings* settings,
30  unsigned int iPhiSec,
31  unsigned int iEtaReg,
32  float etaMinSector,
33  float etaMaxSector,
34  float phiCentreSector,
35  HTrphi::ErrorMonitor* errMon)
36  : HTbase(settings, iPhiSec, iEtaReg, settings->houghNbinsPt(), settings->houghNbinsPhi()),
37  invPtToDphi_((settings->invPtToDphi())),
38  shape_(static_cast<HTshape>(settings->shape())), // shape of HT cells
39 
40  //--- Specification of HT q/Pt axis.
41  maxAbsQoverPtAxis_(1. / settings->houghMinPt()), // Max. |q/Pt| covered by HT array.
42  nBinsQoverPtAxis_(settings->houghNbinsPt()), // No. of bins in HT array in q/Pt.
43  binSizeQoverPtAxis_(2 * maxAbsQoverPtAxis_ / nBinsQoverPtAxis_),
44 
45  //--- Specification of HT phiTrk axis
46  // (phiTrk corresponds to phi where track crosses radius = chosenRofPhi_).
47  chosenRofPhi_(settings->chosenRofPhi()),
48  phiCentreSector_(phiCentreSector), // Centre of phiTrk sector.
49  maxAbsPhiTrkAxis_(M_PI / float(settings->numPhiSectors())), // Half-width of phiTrk axis in HT array.
50  nBinsPhiTrkAxis_(settings->houghNbinsPhi()), // No. of bins in HT array phiTrk
51  binSizePhiTrkAxis_(2 * maxAbsPhiTrkAxis_ / nBinsPhiTrkAxis_),
52  errMon_(errMon) {
53  // Deal with unusually shaped HT cells.
54  if (shape_ != HTshape::square)
56  if (shape_ == HTshape::hexagon)
58  else if (shape_ == HTshape::diamond)
60 
61  // Optionally merge 2x2 neighbouring cells into a single cell at low Pt, to reduce efficiency loss due to
62  // scattering. (Do this if either of options EnableMerge2x2 or MiniHTstage are enabled.
63  // N.B These two options are never both enabled).
64  enableMerge2x2_ = (settings->enableMerge2x2() || settings->miniHTstage());
65  if (settings->miniHTstage()) {
66  // Mini-HT stage cfg: Merge all bins, irrespective of Pt.
68  } else {
69  // Merged cells cfg: Merge bins below specified Pt threshold.
70  minInvPtToMerge2x2_ = 1. / (settings->maxPtToMerge2x2());
72  enableMerge2x2_ = false;
73  }
74 
75  // Merging 2x2 cells into 1 merged cell is only allowed if HT array dimensions are even.
76  // (This restriction could be removed along q/Pt axis, since there are also unmerged cells there. But this
77  // would require correcting the code after each called to mergedCell() below, since
78  // "if (i%2 == 1) iStore = i - 1" not correct in this case).
79  if (enableMerge2x2_ && (nBinsQoverPtAxis_ % 2 != 0 || nBinsPhiTrkAxis_ % 2 != 0))
80  throw cms::Exception("BadConfig") << "HTrphi: You are not allowed to set EnableMerge2x2 or MiniHTstage = True if "
81  "you have an odd number of bins "
82  "in r-phi HT array "
84 
85  //--- Other options used when filling the HT.
86 
87  // Don't fill all HT cells nominally crossed by line corresponding to stub.
89 
90  // Used to kill excess stubs or tracks that can't be transmitted within time-multiplexed period.
91  nReceivedStubs_ = 0;
92  busyInputSectorKill_ = settings_->busyInputSectorKill(); // Kill excess stubs going fron GP to HT?
93  busySectorKill_ = settings_->busySectorKill(); // Kill excess tracks flowing out of HT?
94  // Max. num. of stubs that can be sent from GP to HT within TM period
96  // Max. num. of stubs that can be sent out of HT within TM period
98  // or individual m bin (=q/Pt) ranges to be output to optical links.
100  // Specifies which m bins should be grouped together by BusySectorMbinRanges. If empty, then they are grouped in order 0,1,2,3,4,5 ...
102  // m bin ranges option disabled if vector empty
105 
106  bool rescaleMbins = false;
108  // Check if the total number of bins specified in cfg option BusySectorMbinRanges corresponds
109  // to the number of m bins (q/Pt) in the HT. If not, determine how much the ranges must be scaled
110  // to make this true.
111  unsigned int nTotalBins = 0;
112  for (unsigned int j = 0; j < busySectorMbinRanges_.size(); j++) {
113  nTotalBins += busySectorMbinRanges_[j];
114  }
115  rescaleMbins = (nTotalBins != nBinsQoverPtAxis_);
116  // No rescaling allowed with MBinOrder option.
117  if (rescaleMbins && busySectorUseMbinOrder_)
118  throw cms::Exception("BadConfig") << "HTrphi: BusySectorUserMbin error";
119  float rescaleFactor = rescaleMbins ? float(nBinsQoverPtAxis_) / float(nTotalBins) : 1.;
120  // Find lower and upper inclusive limits of each m bin range to be sent to a separate optical link.
123  float mBinSum = 0.;
124  for (unsigned int i = 0; i < busySectorMbinRanges_.size(); i++) {
125  busySectorMbinLow_[i] = std::round(mBinSum);
126  busySectorMbinHigh_[i] = std::round(mBinSum + rescaleFactor * busySectorMbinRanges_[i]) - 1;
127  mBinSum += rescaleFactor * busySectorMbinRanges_[i];
128  }
129  }
130  //
131  for (unsigned int i = 0; i < nBinsQoverPtAxis_; i++) {
132  for (unsigned int j = 0; j < nBinsPhiTrkAxis_; j++) {
133  pair<float, float> helix = this->helix2Dconventional(i, j); // Get track params at centre of cell.
134  float qOverPt = helix.first;
135  // Check if this cell is merged with its neighbours (as in low Pt region).
136  bool mergedCell = false;
137  if (enableMerge2x2_ && this->mergedCell(i, j))
138  mergedCell = true;
139  // Initialize each cell in HT array.
140  HTbase::htArray_(i, j) =
141  std::make_unique<HTcell>(settings, iPhiSec, iEtaReg, etaMinSector, etaMaxSector, qOverPt, i, mergedCell);
142  }
143  }
144 
145  std::stringstream text;
146  text << "\n";
147  text << "=== R-PHI HOUGH TRANSFORM AXES RANGES: abs(q/Pt) < " << maxAbsQoverPtAxis_ << " & abs(track-phi) < "
148  << maxAbsPhiTrkAxis_ << " ===\n";
149  text << "=== R-PHI HOUGH TRANSFORM ARRAY SIZE: q/Pt bins = " << nBinsQoverPtAxis_
150  << " & track-phi bins = " << nBinsPhiTrkAxis_ << " ===\n";
151  text << "=== R-PHI HOUGH TRANSFORM BIN SIZE: BIN(q/Pt) = " << binSizeQoverPtAxis_
152  << " & BIN(track-phi) = " << binSizePhiTrkAxis_ << " ===\n\n";
153  if (busySectorKill_ && busySectorUseMbinRanges_ && rescaleMbins) {
154  text << "=== R-PHI HOUGH TRANSFORM WARNING: Rescaled m bin ranges specified by cfg parameter "
155  "BusySectorMbinRanges, as they were inconsistent with total number of m bins in HT.\n";
156  text << "=== Rescaled values for BusySectorMbinRanges =";
157  for (unsigned int i = 0; i < busySectorMbinRanges_.size(); i++) {
158  text << " " << (busySectorMbinHigh_[i] - busySectorMbinLow_[i] + 1);
159  }
160  }
161  text << "\n";
162  static std::once_flag printOnce;
163  std::call_once(
164  printOnce, [](string t) { PrintL1trk() << t; }, text.str());
165 
166  // Note helix parameters at the centre of each HT cell.
167  cellCenters_.clear();
168  for (unsigned int m = 0; m < nBinsQoverPtAxis_; m++) {
169  std::vector<std::pair<float, float> > binCenters;
170  for (unsigned int c = 0; c < nBinsPhiTrkAxis_; c++)
171  binCenters.push_back(this->helix2Dhough(m, c));
172  cellCenters_.push_back(binCenters);
173  }
174  }
175 
176  //=== Add stub to HT array.
177  //=== If eta subsectors are being used within each sector, specify which ones the stub is compatible with.
178 
179  void HTrphi::store(Stub* stub, const vector<bool>& inEtaSubSecs) {
180  // Optionally, only store stubs that can be sent from GP to HT within TM period.
182  nReceivedStubs_++;
183 
184  unsigned int jPhiTrkBinMinLast = 0; // Used for error checking
185  unsigned int jPhiTrkBinMaxLast = 99999;
186 
187  // Loop over q/Pt related bins in HT array.
188  for (unsigned int i = 0; i < nBinsQoverPtAxis_; i++) {
189  if (shape_ == HTshape::square) {
190  //--- This is a traditional HT with square cells.
191 
192  // In this q/Pt bin, find the range of phi bins that this stub is consistent with.
193  pair<unsigned int, unsigned int> jRange = this->iPhiRange(stub, i);
194  unsigned int jPhiTrkBinMin = jRange.first;
195  unsigned int jPhiTrkBinMax = jRange.second;
196 
197  // Store stubs in these cells.
198  for (unsigned int j = jPhiTrkBinMin; j <= jPhiTrkBinMax; j++) {
199  bool canStoreStub = true;
200  unsigned int iStore = i;
201  unsigned int jStore = j;
202 
203  // Optionally merge 2x2 neighbouring cells into a single cell at low Pt, to reduce efficiency loss
204  // due to scattering.
205  if (enableMerge2x2_) {
206  // Check if this cell is merged with its neighbours (as in low Pt region).
207  if (this->mergedCell(i, j)) {
208  // Get location of cell that this cell is merged into (iStore, jStore).
209  // Calculation assumes HT array has even number of bins in both dimensions.
210  if (i % 2 == 1)
211  iStore = i - 1;
212  if (j % 2 == 1)
213  jStore = j - 1;
214  // If this stub was already stored in this merged 2x2 cell, then don't store it again.
215  if (HTbase::htArray_(iStore, jStore)->stubStoredInCell(stub))
216  canStoreStub = false;
217  }
218  }
219 
220  if (canStoreStub)
221  HTbase::htArray_(iStore, jStore)->store(stub, inEtaSubSecs); // Calls HTcell::store()
222  }
223 
224  // Check that limitations of firmware would not prevent stub being stored correctly in this HT column.
225  if (errMon_ != nullptr) {
226  this->countFirmwareErrors(i, jPhiTrkBinMin, jPhiTrkBinMax, jPhiTrkBinMinLast, jPhiTrkBinMaxLast);
227  jPhiTrkBinMinLast = jPhiTrkBinMin;
228  jPhiTrkBinMaxLast = jPhiTrkBinMax;
229  }
230 
231  } else {
232  //--- This is are novel HT with unusual shaped cells.
233 
234  if (shape_ == HTshape::diamond) {
235  //--- This HT has diamond shaped cells.
236 
237  float qOverPtBin = -maxAbsQoverPtAxis_ + i * binSizeQoverPtAxis_;
238  float phiTrk = reco::deltaPhi(stub->phi(), phiCentreSector_) +
239  invPtToDphi_ * qOverPtBin * (stub->r() - chosenRofPhi_) + maxAbsPhiTrkAxis_;
240  if (i % 2 == 0)
241  phiTrk += binSizePhiTrkAxis_ / 2.;
242  unsigned int binCenter = std::floor(phiTrk / binSizePhiTrkAxis_);
243  if (binCenter < nBinsPhiTrkAxis_)
244  HTbase::htArray_(i, binCenter)->store(stub, inEtaSubSecs);
245 
246  } else if (shape_ == HTshape::hexagon) {
247  //--- This HT has hexagonal cells (with two of its sides parallel to the phi axis).
248 
249  float qOverPtBin = -maxAbsQoverPtAxis_ + i * binSizeQoverPtAxis_;
250  float qOverPtBinVar = binSizeQoverPtAxis_;
251  float phiTrk = reco::deltaPhi(stub->phi(), phiCentreSector_) +
252  invPtToDphi_ * qOverPtBin * (stub->r() - chosenRofPhi_) + maxAbsPhiTrkAxis_;
253  float phiTrkVar = invPtToDphi_ * qOverPtBinVar * std::abs(stub->r() - chosenRofPhi_);
254  float phiTrkMin = phiTrk - phiTrkVar;
255  float phiTrkMax = phiTrk + phiTrkVar;
256  if (i % 2 == 0)
257  phiTrk += binSizePhiTrkAxis_ / 6.;
258  else {
259  phiTrk -= binSizePhiTrkAxis_ / 3.;
260  phiTrkMin -= binSizePhiTrkAxis_ / 2.;
261  phiTrkMax -= binSizePhiTrkAxis_ / 2.;
262  }
263  unsigned int iCenter = std::floor(phiTrk / binSizePhiTrkAxis_ * 3.);
264  unsigned int iMin = std::floor(phiTrkMin / binSizePhiTrkAxis_ * 3.);
265  unsigned int iMax = std::floor(phiTrkMax / binSizePhiTrkAxis_ * 3.);
266  std::pair<bool, unsigned int> binCenter;
267  std::pair<bool, unsigned int> binMin;
268  std::pair<bool, unsigned int> binMax;
269  binCenter.second = iCenter / 3;
270  binMin.second = iMin / 3;
271  binMax.second = iMax / 3;
272  binCenter.first = !(iCenter % 3 == 2);
273  binMin.first = (iMin % 3 == 0);
274  binMax.first = (iMax % 3 == 0);
275  if (binCenter.first && binCenter.second < nBinsPhiTrkAxis_)
276  HTbase::htArray_(i, binCenter.second)->store(stub, inEtaSubSecs);
277  else if (binMin.first && binMin.second < nBinsPhiTrkAxis_)
278  HTbase::htArray_(i, binMin.second)->store(stub, inEtaSubSecs);
279  else if (binMax.first && binMax.second < nBinsPhiTrkAxis_)
280  HTbase::htArray_(i, binMax.second)->store(stub, inEtaSubSecs);
281 
282  } else if (shape_ == HTshape::brick) {
283  //--- This HT has square cells with alternate rows shifted horizontally by 0.5*cell_width.
284 
285  float qOverPtBin = -maxAbsQoverPtAxis_ + i * binSizeQoverPtAxis_;
286  float qOverPtBinVar = binSizeQoverPtAxis_;
287  float phiTrk = reco::deltaPhi(stub->phi(), phiCentreSector_) +
288  invPtToDphi_ * qOverPtBin * (stub->r() - chosenRofPhi_) + maxAbsPhiTrkAxis_;
289  float phiTrkVar = invPtToDphi_ * qOverPtBinVar * std::abs(stub->r() - chosenRofPhi_);
290  float phiTrkMin = phiTrk - phiTrkVar;
291  float phiTrkMax = phiTrk + phiTrkVar;
292  unsigned int iMin = std::floor(phiTrkMin / binSizePhiTrkAxis_ * 2.);
293  unsigned int iMax = std::floor(phiTrkMax / binSizePhiTrkAxis_ * 2.);
294  std::pair<bool, unsigned int> binMin;
295  std::pair<bool, unsigned int> binMax;
296  binMin.second = iMin / 2;
297  binMax.second = iMax / 2;
298  binMin.first = (iMin % 2 == i % 2);
299  binMax.first = (iMax % 2 == i % 2);
300  if (binMin.first && binMin.second < nBinsPhiTrkAxis_)
301  HTbase::htArray_(i, binMin.second)->store(stub, inEtaSubSecs);
302  else if (binMax.first && binMax.second < nBinsPhiTrkAxis_)
303  HTbase::htArray_(i, binMax.second)->store(stub, inEtaSubSecs);
304  }
305  }
306  }
307  // Note max. |gradient| that the line corresponding to any stub in any of the r-phi HT arrays could have.
308  // Firmware assumes this should not exceed 1.0;
309  if (errMon_ != nullptr) {
311  }
312  }
313  }
314 
315  //=== Determine the m-bin (q/pt) range the specified track is in. (Used if outputting each m bin range on a different opto-link).
316 
317  unsigned int HTrphi::getMbinRange(const L1track2D& trk) const {
319  unsigned int mBin = trk.cellLocationHT().first;
320  unsigned int mBinOrder;
322  // User wants to group bins in a wierd order.
323  mBinOrder = 99999;
324  for (unsigned int k = 0; k < busySectorMbinOrder_.size(); k++) {
325  if (mBin == busySectorMbinOrder_[k])
326  mBinOrder = k;
327  }
328  if (mBinOrder == 99999)
329  throw cms::Exception("LogicError") << "HTrphi::getMbinRange() mBinOrder calculation wrong.";
330  } else {
331  // User grouping bins in numerical order 0,1,2,3,4,5...
332  mBinOrder = mBin;
333  }
334  for (unsigned int i = 0; i < busySectorMbinRanges_.size(); i++) {
335  if (mBinOrder >= busySectorMbinLow_[i] && mBinOrder <= busySectorMbinHigh_[i])
336  return i;
337  }
338  throw cms::Exception("LogicError") << "HTrphi::getMbinRange() messed up";
339  } else {
340  return 0;
341  }
342  }
343 
344  //=== For a given Q/Pt bin, find the range of phi bins that a given stub is consistent with.
345  //=== Return as a pair (min bin, max bin)
346  //=== If it range lies outside the HT array, then the min bin will be set larger than the max bin.
347 
348  pair<unsigned int, unsigned int> HTrphi::iPhiRange(const Stub* stub, unsigned int iQoverPtBin, bool debug) const {
349  // Note q/Pt value corresponding to centre of this bin.
350  float qOverPtBin = -maxAbsQoverPtAxis_ + (iQoverPtBin + 0.5) * binSizeQoverPtAxis_;
351  // Note change in this q/Pt value needed to reach either edge of the bin.
352  float qOverPtBinVar = 0.5 * binSizeQoverPtAxis_;
353 
354  // Reducing effective bin width can reduce fake rate.
355  //qOverPtVar = 0.4*binSizeQoverPtAxis_;
356 
357  // Calculate range of track-phi that would allow a track in this q/Pt range to pass through the stub.
358  float phiTrk = stub->phi() + invPtToDphi_ * qOverPtBin * (stub->r() - chosenRofPhi_);
359  // The next line does the phiTrk calculation without the usual approximation, but it doesn't
360  // improve performance.
361  //float phiTrk = stub->phi() + asin(invPtToDphi_ * qOverPtBin * stub->r()) - asin(invPtToDphi_ * qOverPtBin * chosenRofPhi_);
362  float phiTrkVar = invPtToDphi_ * qOverPtBinVar * std::abs(stub->r() - chosenRofPhi_);
363  float phiTrkMin = phiTrk - phiTrkVar;
364  float phiTrkMax = phiTrk + phiTrkVar;
365 
366  float deltaPhiMin = reco::deltaPhi(phiTrkMin, phiCentreSector_); // Offset to centre of sector.
367  float deltaPhiMax = reco::deltaPhi(phiTrkMax, phiCentreSector_);
368  pair<float, float> phiTrkRange(deltaPhiMin, deltaPhiMax);
369 
370  // Determine which HT array cell range in track-phi this range "phiTrkRange" corresponds to.
371  pair<unsigned int, unsigned int> iPhiTrkBinRange = this->HTbase::convertCoordRangeToBinRange(
373 
374  return iPhiTrkBinRange;
375  }
376 
377  //=== Check that limitations of firmware would not prevent stub being stored correctly in this HT column.
378 
379  void HTrphi::countFirmwareErrors(unsigned int iQoverPtBin,
380  unsigned int jPhiTrkBinMin,
381  unsigned int jPhiTrkBinMax,
382  unsigned int jPhiTrkBinMinLast,
383  unsigned int jPhiTrkBinMaxLast) {
384  // Only do check if stub is being stored somewhere in this HT column.
385  if (jPhiTrkBinMax >= jPhiTrkBinMin) {
386  //--- Remaining code below checks that firmware could successfully store this stub in this column.
387  // (a) Does cell lie NE, E or SE of cell filled in previous column?
388  bool OK_a = (jPhiTrkBinMin + 1 >= jPhiTrkBinMinLast) && (jPhiTrkBinMax <= jPhiTrkBinMaxLast + 1);
389  // (b) Are no more than 2 cells filled in this column
390  bool OK_b = (jPhiTrkBinMax - jPhiTrkBinMin + 1 <= 2);
391 
392  if (!OK_a)
394  if (!OK_b)
396  errMon_->numErrorsNorm++; // No. of times a stub is added to an HT column.
397  }
398  }
399 
400  //=== Get the values of the track helix params corresponding to middle of a specified HT cell (i,j).
401  //=== The helix parameters returned will be those corresponding to the two axes of the HT array.
402  //=== So they might be (q/pt, phi0) or (q/pt, phi65) etc. depending on the configuration.
403 
404  pair<float, float> HTrphi::helix2Dhough(unsigned int i, unsigned int j) const {
405  unsigned int qOverPtBin = i;
406  unsigned int phiTrkBin = j;
407 
408  // If using merged 2x2 cells in low Pt parts of array, must correct for this.
409  bool merged = false;
410  if (enableMerge2x2_) {
411  // Check if this cell is merged with its neighbours (as in low Pt region).
412  if (this->mergedCell(i, j)) {
413  merged = true;
414  // Get location of cell that this cell is merged into (iStore, jStore).
415  // Calculation assumes HT array has even number of bins in both dimensions.
416  if (i % 2 == 1)
417  qOverPtBin = i - 1;
418  if (j % 2 == 1)
419  phiTrkBin = j - 1;
420  }
421  }
422 
423  float qOverPtBinCenter = .5;
424  float phiTrkBinCenter = .5;
425 
426  if (shape_ != HTshape::square) {
427  qOverPtBinCenter = 0.;
428 
429  float evenPhiPos = 0., oddPhiPos = 0.;
430  if (shape_ == HTshape::hexagon) {
431  evenPhiPos = 1. / 6.;
432  oddPhiPos = 2. / 3.;
433  } else if (shape_ == HTshape::diamond) {
434  evenPhiPos = 0.;
435  oddPhiPos = 0.5;
436  } else if (shape_ == HTshape::brick) {
437  evenPhiPos = 0.25;
438  oddPhiPos = 0.75;
439  }
440  phiTrkBinCenter = (qOverPtBin % 2 == 0) ? evenPhiPos : oddPhiPos;
441  }
442 
443  float qOverPt = -maxAbsQoverPtAxis_ + (qOverPtBin + qOverPtBinCenter) * binSizeQoverPtAxis_;
444  float phiTrk = -maxAbsPhiTrkAxis_ + (phiTrkBin + phiTrkBinCenter) * binSizePhiTrkAxis_;
445 
446  if (merged) {
447  qOverPt += 0.5 * binSizeQoverPtAxis_;
448  phiTrk += 0.5 * binSizePhiTrkAxis_;
449  }
450 
451  // Correct phiTrk to centre of sector, taking care of 2*pi wrapping
452  phiTrk = reco::deltaPhi(phiTrk + phiCentreSector_, 0.);
453  return pair<float, float>(qOverPt, phiTrk);
454  }
455 
456  //=== Get the values of the track helix params corresponding to middle of a specified HT cell (i,j).
457  //=== The helix parameters returned will be always be (q/pt, phi0), irrespective of how the axes
458  //=== of the HT array are defined.
459 
460  pair<float, float> HTrphi::helix2Dconventional(unsigned int i, unsigned int j) const {
461  // Get the helix parameters corresponding to the axes definitions of the HT.
462  pair<float, float> helix2Dht = this->helix2Dhough(i, j);
463  // Convert to the conventionally agreed pair of helix parameters, (q/pt, phi0).
464  float qOverPt = helix2Dht.first; // easy
465  // If HT defined track phi other than at r=0, must correct to get phi0. Allow for 2*pi wrapping of phi.
466  float phi0 = reco::deltaPhi(helix2Dht.second + invPtToDphi_ * chosenRofPhi_ * qOverPt, 0.);
467  return pair<float, float>(qOverPt, phi0);
468  }
469 
470  //=== Which cell in HT array should this TP be in, based on its true trajectory?
471  //=== (If TP is outside HT array, it it put in the closest bin inside it).
472 
473  pair<unsigned int, unsigned int> HTrphi::trueCell(const TP* tp) const {
474  // Get HT axis variables corresponding to this TP.
475  float qOverPt = tp->qOverPt();
476  float phiTrk = tp->trkPhiAtR(chosenRofPhi_);
477  // Measure phi relative to centre of sector.
478  float deltaPhi = reco::deltaPhi(phiTrk, phiCentreSector_);
479  // Convert to bin numbers inside HT array.
480  int iQoverPt = floor((qOverPt - (-maxAbsQoverPtAxis_)) / binSizeQoverPtAxis_);
481  int iPhiTrk = floor((deltaPhi - (-maxAbsPhiTrkAxis_)) / binSizePhiTrkAxis_);
482  // Check if this cell was within the HT array.
483  if (iQoverPt >= 0 && iQoverPt < int(nBinsQoverPtAxis_) && iPhiTrk >= 0 && iPhiTrk < int(nBinsPhiTrkAxis_)) {
484  // Check if this cell is merged with its neighbours (as in low Pt region), and if so return merged cell location.
485  // New: because 2nd stage mini HT may recreate tracks from merged cells with finer cell granularity, one can't predict
486  // if a merged cell was used to create a track merely by looking at its cell location.
487  // So instead ask L1track3D, which knows if it was created from a merged HT cell or not.
488  ;
489  } else {
490  // TP is not in this HT array at all. Flag this by setting "outside" bin index to 0 (Nbins) if outside array below (above).
491  if (iQoverPt < 0)
492  iQoverPt = 0;
493  if (iQoverPt >= int(nBinsQoverPtAxis_))
494  iQoverPt = nBinsQoverPtAxis_ - 1;
495  if (iPhiTrk < 0)
496  iPhiTrk = 0;
497  if (iPhiTrk >= int(nBinsPhiTrkAxis_))
498  iPhiTrk = nBinsPhiTrkAxis_ - 1;
499  }
500  return pair<unsigned int, unsigned int>(iQoverPt, iPhiTrk);
501  }
502 
503  //=== Which cell in HT array should this fitted track be in, based on its fitted trajectory?
504  //=== Always uses beam-spot constrained trajectory if available.
505  //=== (If fitted track is outside HT array, it it put in the closest bin inside it).
506 
507  pair<unsigned int, unsigned int> HTrphi::cell(const L1fittedTrack* fitTrk) const {
508  bool beamConstraint = fitTrk->done_bcon(); // Is beam-spot constraint available? (e.g. 5 param helix fit)
509  // Get HT axis variables corresponding to this fitted track.
510  float qOverPt = beamConstraint ? fitTrk->qOverPt_bcon() : fitTrk->qOverPt();
511  // Convert phi0 to phi at chosen radius used in HT.
512  float phiTrk = fitTrk->phiAtChosenR(beamConstraint);
513  // Measure phi relative to centre of sector.
514  float deltaPhi = reco::deltaPhi(phiTrk, phiCentreSector_);
515  // Convert to bin numbers inside HT array.
516  int iQoverPt = 999999;
517  int iPhiTrk = 999999;
518 
519  if (shape_ == HTshape::square) {
520  //--- This is a traditional HT with square cells.
521 
522  iQoverPt = floor((qOverPt - (-maxAbsQoverPtAxis_)) / binSizeQoverPtAxis_);
523  iPhiTrk = floor((deltaPhi - (-maxAbsPhiTrkAxis_)) / binSizePhiTrkAxis_);
524 
525  // Check if this cell was within the HT array.
526  if (iQoverPt >= 0 && iQoverPt < int(nBinsQoverPtAxis_) && iPhiTrk >= 0 && iPhiTrk < int(nBinsPhiTrkAxis_)) {
527  // Check if this cell is merged with its neighbours (as in low Pt region), and if so return merged cell location.
528  // New: because 2nd stage mini HT may recreate tracks from merged cells with finer cell granularity, one can't predict
529  // if a merged cell was used to create a track merely by looking at its cell location.
530  // So instead ask L1track3D, which knows if it was created from a merged HT cell or not.
531  ;
532  } else {
533  // Fitted track is not in this HT array at all. Flag this by setting "outside" bin index to 0 (Nbins-1) if outside array below (above).
534  if (iQoverPt < 0)
535  iQoverPt = 0;
536  if (iQoverPt >= int(nBinsQoverPtAxis_))
537  iQoverPt = nBinsQoverPtAxis_ - 1;
538  if (iPhiTrk < 0)
539  iPhiTrk = 0;
540  if (iPhiTrk >= int(nBinsPhiTrkAxis_))
541  iPhiTrk = nBinsPhiTrkAxis_ - 1;
542  }
543 
544  } else {
545  //--- This is are novel HT with unusual shaped cells.
546 
548  float d(0);
549  unsigned int m(0);
550  for (const auto& binCenters : cellCenters_) {
551  unsigned int c(0);
552  for (auto cellCenter : binCenters) {
553  d = std::pow((cellCenter.first - qOverPt) / (float)binSizeQoverPtAxis_, 2) +
554  std::pow((cellCenter.second - phiTrk) / (float)binSizePhiTrkAxis_, 2);
555  if (d < minD) {
556  minD = d;
557  iQoverPt = m;
558  iPhiTrk = c;
559  }
560  c++;
561  }
562  m++;
563  }
564  // Fitted track is not in this HT array at all. Flag this by setting "outside" bin index to 0 (Nbins-1) if outside array below (above).
565  if (iQoverPt < 0)
566  iQoverPt = 0;
567  if (iQoverPt >= int(nBinsQoverPtAxis_))
568  iQoverPt = nBinsQoverPtAxis_ - 1;
569  if (iPhiTrk < 0)
570  iPhiTrk = 0;
571  if (iPhiTrk >= int(nBinsPhiTrkAxis_))
572  iPhiTrk = nBinsPhiTrkAxis_ - 1;
573  }
574 
575  return pair<unsigned int, unsigned int>(iQoverPt, iPhiTrk);
576  }
577 
578  //=== Check if specified cell is merged with its 2x2 neighbours into a single cell,
579  //=== as it is in low Pt region.
580 
581  bool HTrphi::mergedCell(unsigned int iQoverPtBin, unsigned int jPhiTrkBin) const {
582  bool merge = false;
583 
584  if (enableMerge2x2_) {
585  unsigned int i = iQoverPtBin;
586  //unsigned int j = jPhiTrkBin;
587 
588  // Calculate number of merged bins on each q/Pt side of array.
589  float fMergeBins = (maxAbsQoverPtAxis_ - minInvPtToMerge2x2_) / (2. * binSizeQoverPtAxis_);
590  // Number of unmerged bins this corresponds to, which must be even, since each merged bin comprises two normal q/pt bins.
591  unsigned int numQoverPtBinsToMerge = 2 * min((unsigned int)(std::round(fMergeBins)), (nBinsQoverPtAxis_ / 4));
592  const float small = 0.001;
593  if (minInvPtToMerge2x2_ < small && (unsigned int)(std::round(2. * fMergeBins)) % 2 == 1)
594  numQoverPtBinsToMerge++;
595  unsigned int iB = (nBinsQoverPtAxis_ - 1) - i; // Count backwards across array.
596  if (min(i, iB) < numQoverPtBinsToMerge)
597  merge = true;
598  }
599 
600  return merge;
601  }
602 
603  //=== Calculate line |gradient| of stubs in HT array, so can check it doesn't exceed 1.
604 
605  float HTrphi::calcLineGradArray(float r) const {
606  float grad = std::abs(invPtToDphi_ * (r - chosenRofPhi_));
607  // Convert it to units of bin width.
609  if (shape_ == HTshape::hexagon)
610  grad *= 3.;
611  else if (shape_ == HTshape::diamond)
612  grad *= 2.;
613  else if (shape_ == HTshape::brick)
614  grad *= 4.;
615  return grad;
616  }
617 
618  //=== If requested, kill those tracks in this sector that can't be read out during the time-multiplexed period, because
619  //=== the HT has associated too many stubs to tracks.
620 
621  list<L1track2D> HTrphi::killTracksBusySec(const list<L1track2D>& tracks) const {
622  list<L1track2D> outTracks;
623 
624  if (busySectorKill_) {
625  unsigned int nStubsOut = 0; // #stubs assigned to tracks in this sector.
626  // #stubs assigned to each m bin range in this sector.
627  vector<unsigned int> nStubsOutInRange(busySectorMbinRanges_.size(), 0);
628 
629  for (const L1track2D& trk : tracks) {
630  bool keep = true;
631  unsigned int nStubs = trk.numStubs(); // #stubs on this track.
632  if (busySectorUseMbinRanges_) { // Are tracks from different m bin ranges output seperately to increase bandwidth?
633  unsigned int mBinRange = this->getMbinRange(trk); // Which m bin range is this track in?
634  nStubsOutInRange[mBinRange] += nStubs;
635  if (nStubsOutInRange[mBinRange] > busySectorNumStubs_)
636  keep = false;
637  } else {
638  nStubsOut += nStubs;
639  if (nStubsOut > busySectorNumStubs_)
640  keep = false;
641  }
642 
643  if (keep)
644  outTracks.push_back(trk);
645  }
646 
647  } else {
648  outTracks = tracks;
649  }
650 
651  return outTracks;
652  }
653 
654  //=== Define the order in which the hardware processes rows of the HT array when it outputs track candidates.
655  //=== Currently corresponds to highest Pt tracks first.
656  //=== If two tracks have the same Pt, the -ve charge one is output before the +ve charge one.
657 
658  vector<unsigned int> HTrphi::rowOrder(unsigned int numRows) const {
659  vector<unsigned int> iOrder;
660 
661  // Logic slightly different depending on whether HT array has even or odd number of rows.
662  const bool oddNumRows = (numRows % 2 == 1);
663 
664  // This selects middle rows first before moving to exterior ones.
665  if (oddNumRows) {
666  unsigned int middleRow = (numRows - 1) / 2;
667  iOrder.push_back(middleRow);
668  for (unsigned int i = 1; i <= (numRows - 1) / 2; i++) {
669  iOrder.push_back(middleRow - i); // -ve charge
670  iOrder.push_back(middleRow + i); // +ve charge
671  }
672  } else {
673  unsigned int startRowPos = numRows / 2;
674  unsigned int startRowNeg = startRowPos - 1;
675  for (unsigned int i = 0; i < numRows / 2; i++) {
676  iOrder.push_back(startRowNeg - i); // -ve charge
677  iOrder.push_back(startRowPos + i); // +ve charge
678  }
679  }
680 
681  return iOrder;
682  }
683 
684 } // namespace tmtt
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
std::vector< unsigned int > busySectorMbinRanges_
Definition: HTrphi.h:149
Definition: merge.py:1
bool miniHTstage() const
Definition: Settings.h:149
float maxAbsPhiTrkAxis_
Definition: HTrphi.h:133
unsigned int killSomeHTCellsRphi() const
Definition: Settings.h:166
float maxAbsQoverPtAxis_
Definition: HTrphi.h:127
bool done_bcon() const
bool enableMerge2x2_
Definition: HTrphi.h:137
float r() const
Definition: Stub.h:108
bool mergedCell(unsigned int iQoverPtBin, unsigned int jPhiTrkBin) const
Definition: HTrphi.cc:581
float chosenRofPhi_
Definition: HTrphi.h:131
float binSizeQoverPtAxis_
Definition: HTrphi.h:129
void store(Stub *stub, const std::vector< bool > &inEtaSubSecs)
Definition: HTrphi.cc:179
virtual std::pair< unsigned int, unsigned int > convertCoordRangeToBinRange(std::pair< float, float > coordRange, unsigned int nBinsAxis, float coordAxisMin, float coordAxisBinSize, unsigned int killSomeHTcells) const
Definition: HTbase.cc:122
std::vector< unsigned int > busySectorMbinLow_
Definition: HTrphi.h:153
std::vector< unsigned int > busySectorMbinOrder_
Definition: HTrphi.h:150
const std::vector< unsigned int > & busySectorMbinRanges() const
Definition: Settings.h:178
constexpr int pow(int x)
Definition: conifer.h:24
unsigned int killSomeHTCellsRphi_
Definition: HTrphi.h:143
unsigned int nBinsPhiTrkAxis_
Definition: HTrphi.h:134
unsigned int busyInputSectorNumStubs_
Definition: HTrphi.h:147
float qOverPt_bcon() const
std::pair< unsigned int, unsigned int > iPhiRange(const Stub *stub, unsigned int iQoverPtBin, bool debug=false) const
Definition: HTrphi.cc:348
std::pair< unsigned int, unsigned int > cellLocationHT() const override
Definition: L1track2D.h:63
std::atomic< float > maxLineGradient
Definition: HTrphi.h:31
float invPtToDphi_
Definition: HTrphi.h:120
Array2D< std::unique_ptr< HTcell > > htArray_
Definition: HTbase.h:132
std::atomic< unsigned int > numErrorsNorm
Definition: HTrphi.h:37
ErrorMonitor * errMon_
Definition: HTrphi.h:161
bool busySectorUseMbinOrder_
Definition: HTrphi.h:152
unsigned int busySectorNumStubs() const
Definition: Settings.h:176
std::atomic< unsigned int > numErrorsTypeA
Definition: HTrphi.h:33
Definition: TP.h:23
std::vector< unsigned int > rowOrder(unsigned int numRows) const override
Definition: HTrphi.cc:658
std::pair< unsigned int, unsigned int > trueCell(const TP *tp) const override
Definition: HTrphi.cc:473
const std::vector< unsigned int > & busySectorMbinOrder() const
Definition: Settings.h:181
std::pair< unsigned int, unsigned int > cell(const L1fittedTrack *fitTrk) const override
Definition: HTrphi.cc:507
double maxPtToMerge2x2() const
Definition: Settings.h:143
int merge(int argc, char *argv[])
Definition: DMRmerge.cc:37
std::pair< float, float > helix2Dhough(unsigned int i, unsigned int j) const override
Definition: HTrphi.cc:404
float qOverPt() const override
const double infinity
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::pair< float, float > helix2Dconventional(unsigned int i, unsigned int j) const override
Definition: HTrphi.cc:460
bool busySectorUseMbinRanges_
Definition: HTrphi.h:151
d
Definition: ztail.py:151
unsigned int busyInputSectorNumStubs() const
Definition: Settings.h:185
const Settings * settings_
Definition: HTbase.h:122
void countFirmwareErrors(unsigned int iQoverPtBin, unsigned int iPhiTrkBinMin, unsigned int iPhiTrkBinMax, unsigned int jPhiTrkBinMinLast, unsigned int jPhiTrkBinMaxLast)
Definition: HTrphi.cc:379
#define M_PI
float minInvPtToMerge2x2_
Definition: HTrphi.h:138
float phi() const
Definition: Stub.h:107
bool busySectorKill_
Definition: HTrphi.h:146
unsigned int busySectorNumStubs_
Definition: HTrphi.h:148
#define debug
Definition: HDRShower.cc:19
=== This is the base class for the linearised chi-squared track fit algorithms.
Definition: Array2D.h:16
bool enableMerge2x2() const
Definition: Settings.h:141
unsigned int nReceivedStubs_
Definition: HTrphi.h:158
float phiAtChosenR(bool beamConstraint) const
float binSizePhiTrkAxis_
Definition: HTrphi.h:135
unsigned int getMbinRange(const L1track2D &trk) const
Definition: HTrphi.cc:317
const float binMin[32]
std::atomic< unsigned int > numErrorsTypeB
Definition: HTrphi.h:35
bool busyInputSectorKill_
Definition: HTrphi.h:145
std::vector< std::vector< std::pair< float, float > > > cellCenters_
Definition: HTrphi.h:123
bool busySectorKill() const
Definition: Settings.h:175
float phiCentreSector_
Definition: HTrphi.h:132
float calcLineGradArray(float r) const
Definition: HTrphi.cc:605
std::list< L1track2D > killTracksBusySec(const std::list< L1track2D > &tracks) const override
Definition: HTrphi.cc:621
bool busyInputSectorKill() const
Definition: Settings.h:184
HTshape shape_
Definition: HTrphi.h:122
unsigned int nBinsQoverPtAxis_
Definition: HTrphi.h:128
std::vector< unsigned int > busySectorMbinHigh_
Definition: HTrphi.h:154