CMS 3D CMS Logo

HTbase.cc
Go to the documentation of this file.
4 
6 
7 #include <unordered_set>
8 
9 using namespace std;
10 
11 namespace tmtt {
12 
13  // Initialization.
14  HTbase::HTbase(
15  const Settings* settings, unsigned int iPhiSec, unsigned int iEtaReg, unsigned int nBinsX, unsigned int nBinsY)
16  : settings_(settings),
17  iPhiSec_(iPhiSec),
18  iEtaReg_(iEtaReg),
19  nBinsX_(nBinsX),
20  nBinsY_(nBinsY),
21  htArray_(nBinsX, nBinsY),
22  optoLinkID_(this->calcOptoLinkID()) {}
23 
24  //=== Termination. Causes HT array to search for tracks etc.
25 
26  void HTbase::end() {
27  // Calculate useful info about each cell in array.
28  for (unsigned int i = 0; i < nBinsX_; i++) {
29  for (unsigned int j = 0; j < nBinsY_; j++) {
30  htArray_(i, j)->end(); // Calls HTcell::end()
31  }
32  }
33 
34  // Produce a list of all track candidates found in this array, each containing all the stubs on each one
35  // and the track helix parameters, plus the associated truth particle (if any).
37 
38  // If requested, kill those tracks in this sector that can't be read out during the time-multiplexed period, because
39  // the HT has associated too many stubs to tracks.
40  if (settings_->busySectorKill()) {
42  }
43  }
44 
45  //=== Number of filtered stubs in each cell summed over all cells in HT array.
46  //=== If a stub appears in multiple cells, it will be counted multiple times.
47  unsigned int HTbase::numStubsInc() const {
48  unsigned int nStubs = 0;
49 
50  // Loop over cells in HT array.
51  for (unsigned int i = 0; i < nBinsX_; i++) {
52  for (unsigned int j = 0; j < nBinsY_; j++) {
53  nStubs += htArray_(i, j)->numStubs(); // Calls HTcell::numStubs()
54  }
55  }
56 
57  return nStubs;
58  }
59 
60  //=== Number of filtered stubs in HT array.
61  //=== If a stub appears in multiple cells, it will be counted only once.
62  unsigned int HTbase::numStubsExc() const {
63  unordered_set<unsigned int> stubIDs; // Each ID stored only once, no matter how often it is added.
64 
65  // Loop over cells in HT array.
66  for (unsigned int i = 0; i < nBinsX_; i++) {
67  for (unsigned int j = 0; j < nBinsY_; j++) {
68  // Loop over stubs in each cells, storing their IDs.
69  const vector<Stub*>& vStubs = htArray_(i, j)->stubs(); // Calls HTcell::stubs()
70  for (const Stub* stub : vStubs) {
71  stubIDs.insert(stub->index());
72  }
73  }
74  }
75 
76  return stubIDs.size();
77  }
78 
79  //=== Get number of filtered stubs assigned to track candidates found in this HT array.
80 
81  unsigned int HTbase::numStubsOnTrackCands2D() const {
82  unsigned int nStubs = 0;
83 
84  // Loop over track candidates
85  for (const L1track2D& trk : trackCands2D_) {
86  nStubs += trk.stubs().size();
87  }
88 
89  return nStubs;
90  }
91 
92  //=== Get all reconstructed tracks that were associated to the given tracking particle.
93  //=== (If the vector is empty, then the tracking particle was not reconstructed in this sector).
94 
95  vector<const L1track2D*> HTbase::assocTrackCands2D(const TP& tp) const {
96  vector<const L1track2D*> assocRecoTrk;
97 
98  // Loop over track candidates, looking for those associated to given TP.
99  for (const L1track2D& trk : trackCands2D_) {
100  if (trk.matchedTP() != nullptr) {
101  if (trk.matchedTP()->index() == tp.index())
102  assocRecoTrk.push_back(&trk);
103  }
104  }
105 
106  return assocRecoTrk;
107  }
108 
109  //=== Disable filters (used for debugging).
110 
112  // Loop over cells in HT array.
113  for (unsigned int i = 0; i < nBinsX_; i++) {
114  for (unsigned int j = 0; j < nBinsY_; j++) {
115  htArray_(i, j)->disableBendFilter();
116  }
117  }
118  }
119 
120  //=== Given a range in one of the coordinates specified by coordRange, calculate the corresponding range of bins. The other arguments specify the axis. And also if some cells nominally associated to stub are to be killed.
121 
122  pair<unsigned int, unsigned int> HTbase::convertCoordRangeToBinRange(pair<float, float> coordRange,
123  unsigned int nBinsAxis,
124  float coordAxisMin,
125  float coordAxisBinSize,
126  unsigned int killSomeHTcells) const {
127  float coordMin = coordRange.first;
128  float coordMax = coordRange.second;
129  float coordAvg = (coordRange.first + coordRange.second) / 2.;
130 
131  int iCoordBinMin, iCoordBinMax;
132 
133  //--- There are various options for doing this.
134  //--- Option killSomeHTcells = 0 is the obvious one.
135  //--- If killSomeHTcells > 0, then some of the cells nominally associated with the stub are killed.
136 
137  if (killSomeHTcells == 0) {
138  // Take the full range of phi bins consistent with the stub.
139  iCoordBinMin = floor((coordMin - coordAxisMin) / coordAxisBinSize);
140  iCoordBinMax = floor((coordMax - coordAxisMin) / coordAxisBinSize);
141  } else if (killSomeHTcells == 1) {
142  // Use the reduced range of bins.
143  // This algorithm, proposed by Ian, should reduce the rate, at the cost of some efficiency.
144  const float fracCut = 0.3;
145  iCoordBinMin = floor((coordMin - coordAxisMin) / coordAxisBinSize);
146  iCoordBinMax = floor((coordMax - coordAxisMin) / coordAxisBinSize);
147  unsigned int nbins = iCoordBinMax - iCoordBinMin + 1;
148  if (nbins >= 2) { // Can't reduce range if already only 1 bin
149  float lower = coordAxisMin + (iCoordBinMin + 1) * coordAxisBinSize; // upper edge of lowest bin
150  float upper = coordAxisMin + (iCoordBinMax)*coordAxisBinSize; // lower edge of highest bin.
151  // Calculate fractional amount of min and max bin that this stub uses.
152  float extraLow = (lower - coordMin) / coordAxisBinSize;
153  float extraUp = (coordMax - upper) / coordAxisBinSize;
154  constexpr float small = 0.001; // allow tolerance on floating point precision.
155  if (min(extraLow, extraUp) < -small || max(extraLow, extraUp) > (1.0 + small))
156  throw cms::Exception("LogicError") << "HTbase: convertCoordRangeToBinRange error";
157  if (extraLow < fracCut && (nbins >= 3 || extraLow < extraUp))
158  iCoordBinMin += 1;
159  if (extraUp < fracCut && (nbins >= 3 || extraUp < extraLow))
160  iCoordBinMax -= 1;
161  }
162  } else if (killSomeHTcells == 2) {
163  // This corresponds to Thomas's firmware implementation, which can't fill more than one HT cell per column.
164  iCoordBinMin = floor((coordAvg - coordAxisMin) / coordAxisBinSize);
165  iCoordBinMax = iCoordBinMin;
166  } else {
167  throw cms::Exception("BadConfig") << "HT: invalid KillSomeHTCells option in cfg";
168  }
169 
170  // Limit range to dimensions of HT array.
171  iCoordBinMin = max(iCoordBinMin, 0);
172  iCoordBinMax = min(iCoordBinMax, int(nBinsAxis) - 1);
173 
174  // If whole range is outside HT array, flag this by setting range to specific values with min > max.
175  if (iCoordBinMin > int(nBinsAxis) - 1 || iCoordBinMax < 0) {
176  iCoordBinMin = int(nBinsAxis) - 1;
177  iCoordBinMax = 0;
178  }
179 
180  return pair<unsigned int, unsigned int>(iCoordBinMin, iCoordBinMax);
181  }
182 
183  //=== Return a list of all track candidates found in this array, giving access to all the stubs on each one
184  //=== and the track helix parameters, plus the associated truth particle (if any).
185 
186  list<L1track2D> HTbase::calcTrackCands2D() const {
187  list<L1track2D> trackCands2D;
188 
189  // Check if the hardware processes rows of the HT array in a specific order when outputting track candidates.
190  // Currently this is by decreasing Pt for r-phi HT and unordered for r-z HT.
191  const vector<unsigned int> iOrder = this->rowOrder(nBinsX_);
192  bool wantOrdering = (not iOrder.empty());
193 
194  // Loop over cells in HT array.
195  for (unsigned int i = 0; i < nBinsX_; i++) {
196  // Access rows in specific order if required.
197  unsigned int iPos = wantOrdering ? iOrder[i] : i;
198 
199  for (unsigned int j = 0; j < nBinsY_; j++) {
200  if (htArray_(iPos, j)->trackCandFound()) { // track candidate found in this cell.
201 
202  // Note if this corresponds to a merged HT cell (e.g. 2x2).
203  const bool merged = htArray_(iPos, j)->mergedCell();
204 
205  // Get stubs on this track candidate.
206  const vector<Stub*>& stubs = htArray_(iPos, j)->stubs();
207 
208  // And note location of cell inside HT array.
209  const pair<unsigned int, unsigned int> cellLocation(iPos, j);
210 
211  // Get (q/Pt, phi0) or (tan_lambda, z0) corresponding to middle of this cell.
212  const pair<float, float> helixParams2D = this->helix2Dconventional(iPos, j);
213 
214  // Create track and store it.
215  trackCands2D.emplace_back(
216  settings_, stubs, cellLocation, helixParams2D, iPhiSec_, iEtaReg_, optoLinkID_, merged);
217  }
218  }
219  }
220 
221  return trackCands2D;
222  }
223 
224 } // namespace tmtt
virtual unsigned int numStubsExc() const
Definition: HTbase.cc:62
virtual std::list< L1track2D > killTracksBusySec(const std::list< L1track2D > &tracks) const =0
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
unsigned int iEtaReg_
Definition: HTbase.h:125
Array2D< std::unique_ptr< HTcell > > htArray_
Definition: HTbase.h:132
Definition: TP.h:23
virtual std::vector< unsigned int > rowOrder(unsigned int numRows) const =0
unsigned int nBinsX_
Definition: HTbase.h:127
const Settings * settings_
Definition: HTbase.h:122
virtual void end()
Definition: HTbase.cc:26
=== This is the base class for the linearised chi-squared track fit algorithms.
Definition: Array2D.h:16
virtual std::list< L1track2D > calcTrackCands2D() const
Definition: HTbase.cc:186
unsigned int optoLinkID_
Definition: HTbase.h:134
virtual const std::list< L1track2D > & trackCands2D() const
Definition: HTbase.h:54
std::list< L1track2D > trackCands2D_
Definition: HTbase.h:139
bool busySectorKill() const
Definition: Settings.h:175
virtual std::pair< float, float > helix2Dconventional(unsigned int i, unsigned int j) const =0
virtual void disableBendFilter()
Definition: HTbase.cc:111
virtual std::vector< const L1track2D * > assocTrackCands2D(const TP &tp) const
Definition: HTbase.cc:95
virtual unsigned int numStubsOnTrackCands2D() const
Definition: HTbase.cc:81
unsigned int iPhiSec_
Definition: HTbase.h:124
virtual unsigned int numStubsInc() const
Definition: HTbase.cc:47
unsigned int nBinsY_
Definition: HTbase.h:128