CMS 3D CMS Logo

TrackletLUT.cc
Go to the documentation of this file.
6 
7 #include <filesystem>
8 
9 using namespace std;
10 using namespace trklet;
11 
12 TrackletLUT::TrackletLUT(const Settings& settings) : settings_(settings) {}
13 
14 void TrackletLUT::initmatchcut(unsigned int layerdisk, MatchType type, unsigned int region) {
15  char cregion = 'A' + region;
16 
17  for (unsigned int iSeed = 0; iSeed < 12; iSeed++) {
18  if (type == barrelphi) {
19  table_.push_back(settings_.rphimatchcut(iSeed, layerdisk) / (settings_.kphi1() * settings_.rmean(layerdisk)));
20  }
21  if (type == barrelz) {
22  table_.push_back(settings_.zmatchcut(iSeed, layerdisk) / settings_.kz());
23  }
24  if (type == diskPSphi) {
25  table_.push_back(settings_.rphicutPS(iSeed, layerdisk - N_LAYER) / (settings_.kphi() * settings_.kr()));
26  }
27  if (type == disk2Sphi) {
28  table_.push_back(settings_.rphicut2S(iSeed, layerdisk - N_LAYER) / (settings_.kphi() * settings_.kr()));
29  }
30  if (type == disk2Sr) {
31  table_.push_back(settings_.rcut2S(iSeed, layerdisk - N_LAYER) / settings_.krprojshiftdisk());
32  }
33  if (type == diskPSr) {
34  table_.push_back(settings_.rcutPS(iSeed, layerdisk - N_LAYER) / settings_.krprojshiftdisk());
35  }
36  }
37 
38  name_ = settings_.combined() ? "MP_" : "MC_";
39 
40  if (type == barrelphi) {
41  name_ += TrackletConfigBuilder::LayerName(layerdisk) + "PHI" + cregion + "_phicut.tab";
42  }
43  if (type == barrelz) {
44  name_ += TrackletConfigBuilder::LayerName(layerdisk) + "PHI" + cregion + "_zcut.tab";
45  }
46  if (type == diskPSphi) {
47  name_ += TrackletConfigBuilder::LayerName(layerdisk) + "PHI" + cregion + "_PSphicut.tab";
48  }
49  if (type == disk2Sphi) {
50  name_ += TrackletConfigBuilder::LayerName(layerdisk) + "PHI" + cregion + "_2Sphicut.tab";
51  }
52  if (type == disk2Sr) {
53  name_ += TrackletConfigBuilder::LayerName(layerdisk) + "PHI" + cregion + "_2Srcut.tab";
54  }
55  if (type == diskPSr) {
56  name_ += TrackletConfigBuilder::LayerName(layerdisk) + "PHI" + cregion + "_PSrcut.tab";
57  }
58 
59  positive_ = false;
60 
61  writeTable();
62 }
63 
64 void TrackletLUT::initTPlut(bool fillInner,
65  unsigned int iSeed,
66  unsigned int layerdisk1,
67  unsigned int layerdisk2,
68  unsigned int nbitsfinephidiff,
69  unsigned int iTP) {
70  //number of fine phi bins in sector
71  int nfinephibins = settings_.nallstubs(layerdisk2) * settings_.nvmte(1, iSeed) * (1 << settings_.nfinephi(1, iSeed));
72  double dfinephi = settings_.dphisectorHG() / nfinephibins;
73 
74  int outerrbits = 3;
75 
76  if (iSeed == Seed::L1L2 || iSeed == Seed::L2L3 || iSeed == Seed::L3L4 || iSeed == Seed::L5L6) {
77  outerrbits = 0;
78  }
79 
80  int outerrbins = (1 << outerrbits);
81 
82  double dphi[2];
83  double router[2];
84 
85  unsigned int nbendbitsinner = 3;
86  unsigned int nbendbitsouter = 3;
87  if (iSeed == Seed::L3L4) {
88  nbendbitsouter = 4;
89  } else if (iSeed == Seed::L5L6) {
90  nbendbitsinner = 4;
91  nbendbitsouter = 4;
92  }
93 
94  int nbinsfinephidiff = (1 << nbitsfinephidiff);
95 
96  for (int iphibin = 0; iphibin < nbinsfinephidiff; iphibin++) {
97  int iphidiff = iphibin;
98  if (iphibin >= nbinsfinephidiff / 2) {
99  iphidiff = iphibin - nbinsfinephidiff;
100  }
101  //min and max dphi
102  dphi[0] = (iphidiff - 1.5) * dfinephi;
103  dphi[1] = (iphidiff + 1.5) * dfinephi;
104  for (int irouterbin = 0; irouterbin < outerrbins; irouterbin++) {
105  if (iSeed == Seed::D1D2 || iSeed == Seed::D3D4 || iSeed == Seed::L1D1 || iSeed == Seed::L2D1) {
106  router[0] =
107  settings_.rmindiskvm() + irouterbin * (settings_.rmaxdiskvm() - settings_.rmindiskvm()) / outerrbins;
108  router[1] =
109  settings_.rmindiskvm() + (irouterbin + 1) * (settings_.rmaxdiskvm() - settings_.rmindiskvm()) / outerrbins;
110  } else {
111  router[0] = settings_.rmean(layerdisk2);
112  router[1] = settings_.rmean(layerdisk2);
113  }
114 
115  double bendinnermin = 20.0;
116  double bendinnermax = -20.0;
117  double bendoutermin = 20.0;
118  double bendoutermax = -20.0;
119  double rinvmin = 1.0;
120  for (int i2 = 0; i2 < 2; i2++) {
121  for (int i3 = 0; i3 < 2; i3++) {
122  double rinner = 0.0;
123  if (iSeed == Seed::D1D2 || iSeed == Seed::D3D4) {
124  rinner = router[i3] * settings_.zmean(layerdisk1 - N_LAYER) / settings_.zmean(layerdisk2 - N_LAYER);
125  } else {
126  rinner = settings_.rmean(layerdisk1);
127  }
128  double rinv1 = rinv(0.0, -dphi[i2], rinner, router[i3]);
129  double pitchinner = (rinner < settings_.rcrit()) ? settings_.stripPitch(true) : settings_.stripPitch(false);
130  double pitchouter =
131  (router[i3] < settings_.rcrit()) ? settings_.stripPitch(true) : settings_.stripPitch(false);
132  double abendinner = bendstrip(rinner, rinv1, pitchinner);
133  double abendouter = bendstrip(router[i3], rinv1, pitchouter);
134  if (abendinner < bendinnermin)
135  bendinnermin = abendinner;
136  if (abendinner > bendinnermax)
137  bendinnermax = abendinner;
138  if (abendouter < bendoutermin)
139  bendoutermin = abendouter;
140  if (abendouter > bendoutermax)
141  bendoutermax = abendouter;
142  if (std::abs(rinv1) < rinvmin) {
143  rinvmin = std::abs(rinv1);
144  }
145  }
146  }
147 
148  bool passptcut = rinvmin < settings_.rinvcutte();
149 
150  if (fillInner) {
151  for (int ibend = 0; ibend < (1 << nbendbitsinner); ibend++) {
152  double bend = settings_.benddecode(ibend, layerdisk1, nbendbitsinner == 3);
153 
154  bool passinner = bend <= bendinnermax + settings_.bendcutte(ibend, layerdisk1, nbendbitsinner == 3) &&
155  bend >= bendinnermin - settings_.bendcutte(ibend, layerdisk1, nbendbitsinner == 3);
156  table_.push_back(passinner && passptcut);
157  }
158  } else {
159  for (int ibend = 0; ibend < (1 << nbendbitsouter); ibend++) {
160  double bend = settings_.benddecode(ibend, layerdisk2, nbendbitsouter == 3);
161 
162  bool passouter = bend <= bendoutermax + settings_.bendcutte(ibend, layerdisk2, nbendbitsouter == 3) &&
163  bend >= bendoutermin - settings_.bendcutte(ibend, layerdisk2, nbendbitsouter == 3);
164  table_.push_back(passouter && passptcut);
165  }
166  }
167  }
168  }
169 
170  positive_ = false;
171  char cTP = 'A' + iTP;
172 
173  name_ = "TP_" + TrackletConfigBuilder::LayerName(layerdisk1) + TrackletConfigBuilder::LayerName(layerdisk2) + cTP;
174 
175  if (fillInner) {
176  name_ += "_stubptinnercut.tab";
177  } else {
178  name_ += "_stubptoutercut.tab";
179  }
180 
181  writeTable();
182 }
183 
184 void TrackletLUT::initTPregionlut(unsigned int iSeed,
185  unsigned int layerdisk1,
186  unsigned int layerdisk2,
187  unsigned int iAllStub,
188  unsigned int nbitsfinephidiff,
189  unsigned int nbitsfinephi,
190  const TrackletLUT& tplutinner,
191  unsigned int iTP) {
192  int nirbits = 0;
193  if (iSeed == Seed::D1D2 || iSeed == Seed::D3D4 || iSeed == Seed::L1D1 || iSeed == Seed::L2D1) {
194  nirbits = 3;
195  }
196 
197  unsigned int nbendbitsinner = 3;
198 
199  if (iSeed == Seed::L5L6) {
200  nbendbitsinner = 4;
201  }
202 
203  for (int innerfinephi = 0; innerfinephi < (1 << nbitsfinephi); innerfinephi++) {
204  for (int innerbend = 0; innerbend < (1 << nbendbitsinner); innerbend++) {
205  for (int ir = 0; ir < (1 << nirbits); ir++) {
206  unsigned int usereg = 0;
207  for (unsigned int ireg = 0; ireg < settings_.nvmte(1, iSeed); ireg++) {
208  bool match = false;
209  for (int ifinephiouter = 0; ifinephiouter < (1 << settings_.nfinephi(1, iSeed)); ifinephiouter++) {
210  int outerfinephi = iAllStub * (1 << (nbitsfinephi - settings_.nbitsallstubs(layerdisk2))) +
211  ireg * (1 << settings_.nfinephi(1, iSeed)) + ifinephiouter;
212  int idphi = outerfinephi - innerfinephi;
213  bool inrange = (idphi < (1 << (nbitsfinephidiff - 1))) && (idphi >= -(1 << (nbitsfinephidiff - 1)));
214  if (idphi < 0)
215  idphi = idphi + (1 << nbitsfinephidiff);
216  int idphi1 = idphi;
217  if (iSeed >= 4)
218  idphi1 = (idphi << 3) + ir;
219  int ptinnerindexnew = l1t::bitShift(idphi1, nbendbitsinner) + innerbend;
220  match = match || (inrange && tplutinner.lookup(ptinnerindexnew));
221  }
222  if (match) {
223  usereg = usereg | (1 << ireg);
224  }
225  }
226 
227  table_.push_back(usereg);
228  }
229  }
230  }
231 
232  positive_ = false;
233  char cTP = 'A' + iTP;
234 
235  name_ = "TP_" + TrackletConfigBuilder::LayerName(layerdisk1) + TrackletConfigBuilder::LayerName(layerdisk2) + cTP +
236  "_usereg.tab";
237 
238  writeTable();
239 }
240 
241 void TrackletLUT::initteptlut(bool fillInner,
242  bool fillTEMem,
243  unsigned int iSeed,
244  unsigned int layerdisk1,
245  unsigned int layerdisk2,
246  unsigned int innerphibits,
247  unsigned int outerphibits,
248  double innerphimin,
249  double innerphimax,
250  double outerphimin,
251  double outerphimax,
252  const std::string& innermem,
253  const std::string& outermem) {
254  int outerrbits = 0;
255  if (iSeed == Seed::D1D2 || iSeed == Seed::D3D4 || iSeed == Seed::L1D1 || iSeed == Seed::L2D1) {
256  outerrbits = 3;
257  }
258 
259  int outerrbins = (1 << outerrbits);
260  int innerphibins = (1 << innerphibits);
261  int outerphibins = (1 << outerphibits);
262 
263  double phiinner[2];
264  double phiouter[2];
265  double router[2];
266 
267  unsigned int nbendbitsinner = 3;
268  unsigned int nbendbitsouter = 3;
269  if (iSeed == Seed::L3L4) {
270  nbendbitsouter = 4;
271  }
272  if (iSeed == Seed::L5L6) {
273  nbendbitsinner = 4;
274  nbendbitsouter = 4;
275  }
276 
277  if (fillTEMem) {
278  if (fillInner) {
279  table_.resize((1 << nbendbitsinner), false);
280  } else {
281  table_.resize((1 << nbendbitsouter), false);
282  }
283  }
284 
285  for (int iphiinnerbin = 0; iphiinnerbin < innerphibins; iphiinnerbin++) {
286  phiinner[0] = innerphimin + iphiinnerbin * (innerphimax - innerphimin) / innerphibins;
287  phiinner[1] = innerphimin + (iphiinnerbin + 1) * (innerphimax - innerphimin) / innerphibins;
288  for (int iphiouterbin = 0; iphiouterbin < outerphibins; iphiouterbin++) {
289  phiouter[0] = outerphimin + iphiouterbin * (outerphimax - outerphimin) / outerphibins;
290  phiouter[1] = outerphimin + (iphiouterbin + 1) * (outerphimax - outerphimin) / outerphibins;
291  for (int irouterbin = 0; irouterbin < outerrbins; irouterbin++) {
292  if (iSeed == Seed::D1D2 || iSeed == Seed::D3D4 || iSeed == Seed::L1D1 || iSeed == Seed::L2D1) {
293  router[0] =
294  settings_.rmindiskvm() + irouterbin * (settings_.rmaxdiskvm() - settings_.rmindiskvm()) / outerrbins;
295  router[1] = settings_.rmindiskvm() +
296  (irouterbin + 1) * (settings_.rmaxdiskvm() - settings_.rmindiskvm()) / outerrbins;
297  } else {
298  router[0] = settings_.rmean(layerdisk2);
299  router[1] = settings_.rmean(layerdisk2);
300  }
301 
302  double bendinnermin = 20.0;
303  double bendinnermax = -20.0;
304  double bendoutermin = 20.0;
305  double bendoutermax = -20.0;
306  double rinvmin = 1.0;
307  for (int i1 = 0; i1 < 2; i1++) {
308  for (int i2 = 0; i2 < 2; i2++) {
309  for (int i3 = 0; i3 < 2; i3++) {
310  double rinner = 0.0;
311  if (iSeed == Seed::D1D2 || iSeed == Seed::D3D4) {
312  rinner = router[i3] * settings_.zmean(layerdisk1 - N_LAYER) / settings_.zmean(layerdisk2 - N_LAYER);
313  } else {
314  rinner = settings_.rmean(layerdisk1);
315  }
316  double rinv1 = -rinv(phiinner[i1], phiouter[i2], rinner, router[i3]);
317  double pitchinner =
318  (rinner < settings_.rcrit()) ? settings_.stripPitch(true) : settings_.stripPitch(false);
319  double pitchouter =
320  (router[i3] < settings_.rcrit()) ? settings_.stripPitch(true) : settings_.stripPitch(false);
321  double abendinner = bendstrip(rinner, rinv1, pitchinner);
322  double abendouter = bendstrip(router[i3], rinv1, pitchouter);
323  if (abendinner < bendinnermin)
324  bendinnermin = abendinner;
325  if (abendinner > bendinnermax)
326  bendinnermax = abendinner;
327  if (abendouter < bendoutermin)
328  bendoutermin = abendouter;
329  if (abendouter > bendoutermax)
330  bendoutermax = abendouter;
331  if (std::abs(rinv1) < rinvmin) {
332  rinvmin = std::abs(rinv1);
333  }
334  }
335  }
336  }
337 
338  bool passptcut = rinvmin < settings_.rinvcutte();
339 
340  if (fillInner) {
341  for (int ibend = 0; ibend < (1 << nbendbitsinner); ibend++) {
342  double bend = settings_.benddecode(ibend, layerdisk1, nbendbitsinner == 3);
343 
344  bool passinner = bend > bendinnermin - settings_.bendcutte(ibend, layerdisk1, nbendbitsinner == 3) &&
345  bend < bendinnermax + settings_.bendcutte(ibend, layerdisk1, nbendbitsinner == 3);
346 
347  if (fillTEMem) {
348  if (passinner) {
349  table_[ibend] = 1;
350  }
351  } else {
352  table_.push_back(passinner && passptcut);
353  }
354  }
355  } else {
356  for (int ibend = 0; ibend < (1 << nbendbitsouter); ibend++) {
357  double bend = settings_.benddecode(ibend, layerdisk2, nbendbitsouter == 3);
358 
359  bool passouter = bend > bendoutermin - settings_.bendcutte(ibend, layerdisk2, nbendbitsouter == 3) &&
360  bend < bendoutermax + settings_.bendcutte(ibend, layerdisk2, nbendbitsouter == 3);
361  if (fillTEMem) {
362  if (passouter) {
363  table_[ibend] = 1;
364  }
365  } else {
366  table_.push_back(passouter && passptcut);
367  }
368  }
369  }
370  }
371  }
372  }
373 
374  positive_ = false;
375 
376  if (fillTEMem) {
377  if (fillInner) {
378  name_ = "VMSTE_" + innermem + "_vmbendcut.tab";
379  } else {
380  name_ = "VMSTE_" + outermem + "_vmbendcut.tab";
381  }
382  } else {
383  name_ = "TE_" + innermem.substr(0, innermem.size() - 2) + "_" + outermem.substr(0, outermem.size() - 2);
384  if (fillInner) {
385  name_ += "_stubptinnercut.tab";
386  } else {
387  name_ += "_stubptoutercut.tab";
388  }
389  }
390 
391  writeTable();
392 }
393 
394 void TrackletLUT::initProjectionBend(double k_phider,
395  unsigned int idisk,
396  unsigned int nrbits,
397  unsigned int nphiderbits) {
398  unsigned int nsignbins = 2;
399  unsigned int nrbins = 1 << (nrbits);
400  unsigned int nphiderbins = 1 << (nphiderbits);
401 
402  for (unsigned int isignbin = 0; isignbin < nsignbins; isignbin++) {
403  for (unsigned int irbin = 0; irbin < nrbins; irbin++) {
404  int ir = irbin;
405  if (ir > (1 << (nrbits - 1)))
406  ir -= (1 << nrbits);
407  ir = l1t::bitShift(ir, (settings_.nrbitsstub(N_LAYER) - nrbits));
408  for (unsigned int iphiderbin = 0; iphiderbin < nphiderbins; iphiderbin++) {
409  int iphider = iphiderbin;
410  if (iphider > (1 << (nphiderbits - 1)))
411  iphider -= (1 << nphiderbits);
412  iphider = l1t::bitShift(iphider, (settings_.nbitsphiprojderL123() - nphiderbits));
413 
414  double rproj = ir * settings_.krprojshiftdisk();
415  double phider = iphider * k_phider;
416  double t = settings_.zmean(idisk) / rproj;
417 
418  if (isignbin)
419  t = -t;
420 
421  double rinv = -phider * (2.0 * t);
422 
423  double stripPitch = (rproj < settings_.rcrit()) ? settings_.stripPitch(true) : settings_.stripPitch(false);
424  double bendproj = bendstrip(rproj, rinv, stripPitch);
425 
426  int ibendproj = 2.0 * bendproj + 15.5;
427  if (ibendproj < 0)
428  ibendproj = 0;
429  if (ibendproj > 31)
430  ibendproj = 31;
431 
432  table_.push_back(ibendproj);
433  }
434  }
435  }
436 
437  positive_ = false;
438  name_ = settings_.combined() ? "MP_" : "PR_";
439  name_ += "ProjectionBend_" + TrackletConfigBuilder::LayerName(N_LAYER + idisk) + ".tab";
440 
441  writeTable();
442 }
443 
444 void TrackletLUT::initBendMatch(unsigned int layerdisk) {
445  unsigned int nrinv = NRINVBITS;
446  double rinvhalf = 0.5 * ((1 << nrinv) - 1);
447 
448  bool barrel = layerdisk < N_LAYER;
449  bool isPSmodule = layerdisk < N_PSLAYER;
450  double stripPitch = settings_.stripPitch(isPSmodule);
451 
452  if (barrel) {
453  unsigned int nbits = isPSmodule ? N_BENDBITS_PS : N_BENDBITS_2S;
454 
455  for (unsigned int irinv = 0; irinv < (1u << nrinv); irinv++) {
456  double rinv = (irinv - rinvhalf) * (1 << (settings_.nbitsrinv() - nrinv)) * settings_.krinvpars();
457 
458  double projbend = bendstrip(settings_.rmean(layerdisk), rinv, stripPitch);
459  for (unsigned int ibend = 0; ibend < (1u << nbits); ibend++) {
460  double stubbend = settings_.benddecode(ibend, layerdisk, isPSmodule);
461  bool pass = std::abs(stubbend - projbend) < settings_.bendcutme(ibend, layerdisk, isPSmodule);
462  table_.push_back(pass);
463  }
464  }
465  } else {
466  for (unsigned int iprojbend = 0; iprojbend < (1u << nrinv); iprojbend++) {
467  double projbend = 0.5 * (iprojbend - rinvhalf);
468  for (unsigned int ibend = 0; ibend < (1 << N_BENDBITS_2S); ibend++) {
469  double stubbend = settings_.benddecode(ibend, layerdisk, false);
470  bool pass = std::abs(stubbend - projbend) < settings_.bendcutme(ibend, layerdisk, false);
471  table_.push_back(pass);
472  }
473  }
474  for (unsigned int iprojbend = 0; iprojbend < (1u << nrinv); iprojbend++) {
475  double projbend = 0.5 * (iprojbend - rinvhalf);
476  for (unsigned int ibend = 0; ibend < (1 << N_BENDBITS_PS); ibend++) {
477  double stubbend = settings_.benddecode(ibend, layerdisk, true);
478  bool pass = std::abs(stubbend - projbend) < settings_.bendcutme(ibend, layerdisk, true);
479  table_.push_back(pass);
480  }
481  }
482  }
483 
484  positive_ = false;
485 
486  name_ = "METable_" + TrackletConfigBuilder::LayerName(layerdisk) + ".tab";
487 
488  writeTable();
489 }
490 
491 void TrackletLUT::initVMRTable(unsigned int layerdisk, VMRTableType type, int region) {
492  unsigned int zbits = settings_.vmrlutzbits(layerdisk);
493  unsigned int rbits = settings_.vmrlutrbits(layerdisk);
494 
495  unsigned int rbins = (1 << rbits);
496  unsigned int zbins = (1 << zbits);
497 
498  double zmin, zmax, rmin, rmax;
499 
500  if (layerdisk < N_LAYER) {
501  zmin = -settings_.zlength();
502  zmax = settings_.zlength();
503  rmin = settings_.rmean(layerdisk) - settings_.drmax();
504  rmax = settings_.rmean(layerdisk) + settings_.drmax();
505  } else {
506  rmin = 0;
507  rmax = settings_.rmaxdisk();
508  zmin = settings_.zmean(layerdisk - N_LAYER) - settings_.dzmax();
509  zmax = settings_.zmean(layerdisk - N_LAYER) + settings_.dzmax();
510  }
511 
512  double dr = (rmax - rmin) / rbins;
513  double dz = (zmax - zmin) / zbins;
514 
516 
517  for (unsigned int izbin = 0; izbin < zbins; izbin++) {
518  for (unsigned int irbin = 0; irbin < rbins; irbin++) {
519  double r = rmin + (irbin + 0.5) * dr;
520  double z = zmin + (izbin + 0.5) * dz;
521 
522  if (settings_.combined()) {
523  int iznew = izbin - (1 << (zbits - 1));
524  if (iznew < 0)
525  iznew += (1 << zbits);
526  assert(iznew >= 0);
527  assert(iznew < (1 << zbits));
528  z = zmin + (iznew + 0.5) * dz;
529  if (layerdisk < N_LAYER) {
530  int irnew = irbin - (1 << (rbits - 1));
531  if (irnew < 0)
532  irnew += (1 << rbits);
533  assert(irnew >= 0);
534  assert(irnew < (1 << rbits));
535  r = rmin + (irnew + 0.5) * dr;
536  }
537  }
538 
539  if (layerdisk >= N_LAYER && irbin < 10) //special case for the tabulated radii in 2S disks
540  r = (layerdisk < N_LAYER + 2) ? settings_.rDSSinner(irbin) : settings_.rDSSouter(irbin);
541 
542  int bin;
543  if (layerdisk < N_LAYER) {
544  double zproj = z * settings_.rmean(layerdisk) / r;
545  bin = NBINS * (zproj + settings_.zlength()) / (2 * settings_.zlength());
546  } else {
547  double rproj = r * settings_.zmean(layerdisk - N_LAYER) / z;
549  }
550  if (bin < 0)
551  bin = 0;
552  if (bin >= NBINS)
553  bin = NBINS - 1;
554 
555  if (type == VMRTableType::me) {
556  table_.push_back(bin);
557  }
558 
559  if (type == VMRTableType::disk) {
560  if (layerdisk >= N_LAYER) {
561  double rproj = r * settings_.zmean(layerdisk - N_LAYER) / z;
562  bin = 0.5 * NBINS * (rproj - settings_.rmindiskvm()) / (settings_.rmaxdiskvm() - settings_.rmindiskvm());
563  //bin value of zero indicates that stub is out of range
564  if (bin < 0)
565  bin = 0;
566  if (bin >= NBINS / 2)
567  bin = 0;
568  table_.push_back(bin);
569  }
570  }
571 
572  if (type == VMRTableType::inner) {
573  if (layerdisk == LayerDisk::L1 || layerdisk == LayerDisk::L3 || layerdisk == LayerDisk::L5 ||
574  layerdisk == LayerDisk::D1 || layerdisk == LayerDisk::D3) {
575  table_.push_back(getVMRLookup(layerdisk + 1, z, r, dz, dr));
576  }
577  if (layerdisk == LayerDisk::L2) {
578  table_.push_back(getVMRLookup(layerdisk + 1, z, r, dz, dr, Seed::L2L3));
579  }
580  }
581 
582  if (type == VMRTableType::inneroverlap) {
583  if (layerdisk == LayerDisk::L1 || layerdisk == LayerDisk::L2) {
584  table_.push_back(getVMRLookup(6, z, r, dz, dr, layerdisk + 6));
585  }
586  }
587 
588  if (type == VMRTableType::innerthird) {
589  if (layerdisk == LayerDisk::L2) { //projection from L2 to D1 for L2L3D1 seeding
590  table_.push_back(getVMRLookup(LayerDisk::D1, z, r, dz, dr, Seed::L2L3D1));
591  }
592 
593  if (layerdisk == LayerDisk::L5) { //projection from L5 to L4 for L5L6L4 seeding
594  table_.push_back(getVMRLookup(LayerDisk::L4, z, r, dz, dr));
595  }
596 
597  if (layerdisk == LayerDisk::L3) { //projection from L3 to L5 for L3L4L2 seeding
598  table_.push_back(getVMRLookup(LayerDisk::L2, z, r, dz, dr));
599  }
600 
601  if (layerdisk == LayerDisk::D1) { //projection from D1 to L2 for D1D2L2 seeding
602  table_.push_back(getVMRLookup(LayerDisk::L2, z, r, dz, dr));
603  }
604  }
605  }
606  }
607 
608  if (settings_.combined()) {
609  if (type == VMRTableType::me) {
610  positive_ = false;
611  name_ = "VMRME_" + TrackletConfigBuilder::LayerName(layerdisk) + ".tab";
612  }
613  if (type == VMRTableType::disk) {
614  positive_ = false;
615  name_ = "VMRTE_" + TrackletConfigBuilder::LayerName(layerdisk) + ".tab";
616  }
617  if (type == VMRTableType::inner) {
618  positive_ = true;
619  nbits_ = 10;
620  name_ = "TP_" + TrackletConfigBuilder::LayerName(layerdisk) + TrackletConfigBuilder::LayerName(layerdisk + 1) +
621  ".tab";
622  }
623 
624  if (type == VMRTableType::inneroverlap) {
625  positive_ = true;
626  nbits_ = 10;
628  }
629 
630  } else {
631  if (type == VMRTableType::me) {
632  //This if a hack where the same memory is used in both ME and TE modules
633  if (layerdisk == 1 || layerdisk == 2 || layerdisk == 3 || layerdisk == 4) {
634  positive_ = false;
635  name_ = "VMTableOuter" + TrackletConfigBuilder::LayerName(layerdisk) + ".tab";
636  writeTable();
637  }
638 
639  assert(region >= 0);
640  char cregion = 'A' + region;
641  name_ = "VMR_" + TrackletConfigBuilder::LayerName(layerdisk) + "PHI" + cregion + "_finebin.tab";
642  positive_ = false;
643  }
644 
645  if (type == VMRTableType::inner) {
646  positive_ = false;
647  name_ = "VMTableInner" + TrackletConfigBuilder::LayerName(layerdisk) +
648  TrackletConfigBuilder::LayerName(layerdisk + 1) + ".tab";
649  }
650 
651  if (type == VMRTableType::inneroverlap) {
652  positive_ = false;
654  ".tab";
655  }
656 
657  if (type == VMRTableType::disk) {
658  positive_ = false;
659  name_ = "VMTableOuter" + TrackletConfigBuilder::LayerName(layerdisk) + ".tab";
660  }
661  }
662 
663  writeTable();
664 }
665 
666 int TrackletLUT::getVMRLookup(unsigned int layerdisk, double z, double r, double dz, double dr, int iseed) const {
667  double z0cut = settings_.z0cut();
668 
669  if (layerdisk < N_LAYER) {
670  if (iseed == Seed::L2L3 && std::abs(z) < 52.0)
671  return -1;
672 
673  double rmean = settings_.rmean(layerdisk);
674 
675  double rratio1 = rmean / (r + 0.5 * dr);
676  double rratio2 = rmean / (r - 0.5 * dr);
677 
678  double z1 = (z - 0.5 * dz) * rratio1 + z0cut * (rratio1 - 1.0);
679  double z2 = (z + 0.5 * dz) * rratio1 + z0cut * (rratio1 - 1.0);
680  double z3 = (z - 0.5 * dz) * rratio2 + z0cut * (rratio2 - 1.0);
681  double z4 = (z + 0.5 * dz) * rratio2 + z0cut * (rratio2 - 1.0);
682  double z5 = (z - 0.5 * dz) * rratio1 - z0cut * (rratio1 - 1.0);
683  double z6 = (z + 0.5 * dz) * rratio1 - z0cut * (rratio1 - 1.0);
684  double z7 = (z - 0.5 * dz) * rratio2 - z0cut * (rratio2 - 1.0);
685  double z8 = (z + 0.5 * dz) * rratio2 - z0cut * (rratio2 - 1.0);
686 
687  double zmin = std::min({z1, z2, z3, z4, z5, z6, z7, z8});
688  double zmax = std::max({z1, z2, z3, z4, z5, z6, z7, z8});
689 
691 
692  int zbin1 = NBINS * (zmin + settings_.zlength()) / (2 * settings_.zlength());
693  int zbin2 = NBINS * (zmax + settings_.zlength()) / (2 * settings_.zlength());
694 
695  if (zbin1 >= NBINS)
696  return -1;
697  if (zbin2 < 0)
698  return -1;
699 
700  if (zbin2 >= NBINS)
701  zbin2 = NBINS - 1;
702  if (zbin1 < 0)
703  zbin1 = 0;
704 
705  // This is a 10 bit word:
706  // xxx|yyy|z|rrr
707  // xxx is the delta z window
708  // yyy is the z bin
709  // z is flag to look in next bin
710  // rrr first fine z bin
711  // NOTE : this encoding is not efficient z is one if xxx+rrr is greater than 8
712  // and xxx is only 1,2, or 3
713  // should also reject xxx=0 as this means projection is outside range
714 
715  int value = zbin1 / 8;
716  value *= 2;
717  if (zbin2 / 8 - zbin1 / 8 > 0)
718  value += 1;
719  value *= 8;
720  value += (zbin1 & 7);
721  assert(value / 8 < 15);
722  int deltaz = zbin2 - zbin1;
723  if (deltaz > 7) {
724  deltaz = 7;
725  }
726  assert(deltaz < 8);
727  value += (deltaz << 7);
728 
729  return value;
730 
731  } else {
732  if (std::abs(z) < 2.0 * z0cut)
733  return -1;
734 
735  double zmean = settings_.zmean(layerdisk - N_LAYER);
736  if (z < 0.0)
737  zmean = -zmean;
738 
739  double r1 = (r + 0.5 * dr) * (zmean + z0cut) / (z + 0.5 * dz + z0cut);
740  double r2 = (r - 0.5 * dr) * (zmean - z0cut) / (z + 0.5 * dz - z0cut);
741  double r3 = (r + 0.5 * dr) * (zmean + z0cut) / (z - 0.5 * dz + z0cut);
742  double r4 = (r - 0.5 * dr) * (zmean - z0cut) / (z - 0.5 * dz - z0cut);
743  double r5 = (r + 0.5 * dr) * (zmean - z0cut) / (z + 0.5 * dz - z0cut);
744  double r6 = (r - 0.5 * dr) * (zmean + z0cut) / (z + 0.5 * dz + z0cut);
745  double r7 = (r + 0.5 * dr) * (zmean - z0cut) / (z - 0.5 * dz - z0cut);
746  double r8 = (r - 0.5 * dr) * (zmean + z0cut) / (z - 0.5 * dz + z0cut);
747 
748  double rmin = std::min({r1, r2, r3, r4, r5, r6, r7, r8});
749  double rmax = std::max({r1, r2, r3, r4, r5, r6, r7, r8});
750 
752 
753  double rmindisk = settings_.rmindiskvm();
754  double rmaxdisk = settings_.rmaxdiskvm();
755 
756  if (iseed == Seed::L1D1)
757  rmaxdisk = settings_.rmaxdiskl1overlapvm();
758  if (iseed == Seed::L2D1)
759  rmindisk = settings_.rmindiskl2overlapvm();
760  if (iseed == Seed::L2L3D1)
761  rmaxdisk = settings_.rmaxdisk();
762 
763  if (rmin > rmaxdisk)
764  return -1;
765  if (rmax > rmaxdisk)
766  rmax = rmaxdisk;
767 
768  if (rmax < rmindisk)
769  return -1;
770  if (rmin < rmindisk)
771  rmin = rmindisk;
772 
773  int rbin1 = NBINS * (rmin - settings_.rmindiskvm()) / (settings_.rmaxdiskvm() - settings_.rmindiskvm());
774  int rbin2 = NBINS * (rmax - settings_.rmindiskvm()) / (settings_.rmaxdiskvm() - settings_.rmindiskvm());
775 
776  if (iseed == Seed::L2L3D1) {
777  constexpr double rminspec = 40.0;
778  rbin1 = NBINS * (rmin - rminspec) / (settings_.rmaxdisk() - rminspec);
779  rbin2 = NBINS * (rmax - rminspec) / (settings_.rmaxdisk() - rminspec);
780  }
781 
782  if (rbin2 >= NBINS)
783  rbin2 = NBINS - 1;
784  if (rbin1 < 0)
785  rbin1 = 0;
786 
787  // This is a 9 bit word:
788  // xxx|yy|z|rrr
789  // xxx is the delta r window
790  // yy is the r bin yy is three bits for overlaps
791  // z is flag to look in next bin
792  // rrr fine r bin
793  // NOTE : this encoding is not efficient z is one if xxx+rrr is greater than 8
794  // and xxx is only 1,2, or 3
795  // should also reject xxx=0 as this means projection is outside range
796 
797  bool overlap = iseed == Seed::L1D1 || iseed == Seed::L2D1 || iseed == Seed::L2L3D1;
798 
799  int value = rbin1 / 8;
800  if (overlap) {
801  if (z < 0.0)
802  value += 4;
803  }
804  value *= 2;
805  if (rbin2 / 8 - rbin1 / 8 > 0)
806  value += 1;
807  value *= 8;
808  value += (rbin1 & 7);
809  assert(value / 8 < 15);
810  int deltar = rbin2 - rbin1;
811  if (deltar > 7)
812  deltar = 7;
813  if (overlap) {
814  value += (deltar << 7);
815  } else {
816  value += (deltar << 6);
817  }
818 
819  return value;
820  }
821 }
822 
823 void TrackletLUT::initPhiCorrTable(unsigned int layerdisk, unsigned int rbits) {
824  bool psmodule = layerdisk < N_PSLAYER;
825 
826  unsigned int bendbits = psmodule ? N_BENDBITS_PS : N_BENDBITS_2S;
827 
828  unsigned int rbins = (1 << rbits);
829 
830  double rmean = settings_.rmean(layerdisk);
831  double drmax = settings_.drmax();
832 
833  double dr = 2.0 * drmax / rbins;
834 
835  unsigned int bendbins = (1 << bendbits);
836 
837  for (unsigned int ibend = 0; ibend < bendbins; ibend++) {
838  for (unsigned int irbin = 0; irbin < rbins; irbin++) {
839  int value = getphiCorrValue(layerdisk, ibend, irbin, rmean, dr, drmax);
840  table_.push_back(value);
841  }
842  }
843 
844  name_ = "VMPhiCorrL" + std::to_string(layerdisk + 1) + ".tab";
845  nbits_ = 14;
846  positive_ = false;
847 
848  writeTable();
849 }
850 
852  unsigned int layerdisk, unsigned int ibend, unsigned int irbin, double rmean, double dr, double drmax) const {
853  bool psmodule = layerdisk < N_PSLAYER;
854 
855  double bend = -settings_.benddecode(ibend, layerdisk, psmodule);
856 
857  //for the rbin - calculate the distance to the nominal layer radius
858  double Delta = (irbin + 0.5) * dr - drmax;
859 
860  //calculate the phi correction - this is a somewhat approximate formula
861  double dphi = (Delta / 0.18) * bend * settings_.stripPitch(psmodule) / rmean;
862 
863  double kphi = psmodule ? settings_.kphi() : settings_.kphi1();
864 
865  int idphi = dphi / kphi;
866 
867  return idphi;
868 }
869 
870 // Write LUT table.
872  if (!settings_.writeTable()) {
873  return;
874  }
875 
876  if (name_.empty()) {
877  return;
878  }
879 
880  ofstream out = openfile(settings_.tablePath(), name_, __FILE__, __LINE__);
881 
882  out << "{" << endl;
883  for (unsigned int i = 0; i < table_.size(); i++) {
884  if (i != 0) {
885  out << "," << endl;
886  }
887 
888  int itable = table_[i];
889  if (positive_) {
890  if (table_[i] < 0) {
891  itable = (1 << nbits_) - 1;
892  }
893  }
894 
895  out << itable;
896  }
897  out << endl << "};" << endl;
898  out.close();
899 }
900 
901 int TrackletLUT::lookup(unsigned int index) const {
902  assert(index < table_.size());
903  return table_[index];
904 }
double rphicutPS(unsigned int iSeed, unsigned int idisk) const
Definition: Settings.h:159
unsigned int vmrlutrbits(unsigned int layerdisk) const
Definition: Settings.h:179
void initBendMatch(unsigned int layerdisk)
Definition: TrackletLUT.cc:444
double kz() const
Definition: Settings.h:302
double zmatchcut(unsigned int iSeed, unsigned int ilayer) const
Definition: Settings.h:158
unsigned int nrbitsstub(unsigned int layerdisk) const
Definition: Settings.h:84
bool writeTable() const
Definition: Settings.h:189
double kphi1() const
Definition: Settings.h:299
void initmatchcut(unsigned int layerdisk, MatchType type, unsigned int region)
Definition: TrackletLUT.cc:14
double rDSSinner(unsigned int iBin) const
Definition: Settings.h:171
double benddecode(int ibend, int layerdisk, bool isPSmodule) const
Definition: Settings.h:402
constexpr unsigned int NRINVBITS
Definition: Settings.h:32
bool combined() const
Definition: Settings.h:250
void initProjectionBend(double k_phider, unsigned int idisk, unsigned int nrbits, unsigned int nphiderbits)
Definition: TrackletLUT.cc:394
void initteptlut(bool fillInner, bool fillTEMem, unsigned int iSeed, unsigned int layerdisk1, unsigned int layerdisk2, unsigned int innerphibits, unsigned int outerphibits, double innerphimin, double innerphimax, double outerphimin, double outerphimax, const std::string &innermem, const std::string &outermem)
Definition: TrackletLUT.cc:241
double dphisectorHG() const
Definition: Settings.h:281
const Settings & settings_
Definition: TrackletLUT.h:85
static std::string LayerName(unsigned int ilayer)
double zlength() const
Definition: Settings.h:124
constexpr unsigned int N_BENDBITS_2S
Definition: Settings.h:30
std::string to_string(const V &value)
Definition: OMSAccess.h:71
int lookup(unsigned int index) const
Definition: TrackletLUT.cc:901
double rmindiskvm() const
Definition: Settings.h:315
double rphimatchcut(unsigned int iSeed, unsigned int ilayer) const
Definition: Settings.h:157
double krinvpars() const
Definition: Settings.h:384
double dzmax() const
Definition: Settings.h:129
assert(be >=bs)
constexpr unsigned int N_BENDBITS_PS
Definition: Settings.h:29
void initPhiCorrTable(unsigned int layerdisk, unsigned int rbits)
Definition: TrackletLUT.cc:823
unsigned int NLONGVMBINS() const
Definition: Settings.h:329
std::string tablePath() const
Definition: Settings.h:193
double rmindiskl2overlapvm() const
Definition: Settings.h:319
unsigned int nbitsallstubs(unsigned int layerdisk) const
Definition: Settings.h:106
std::string name_
Definition: TrackletLUT.h:87
double rmean(unsigned int iLayer) const
Definition: Settings.h:164
double rDSSouter(unsigned int iBin) const
Definition: Settings.h:174
Divides< A, C > D1
Definition: Factorize.h:136
int nbitsrinv() const
Definition: Settings.h:334
double stripPitch(bool isPSmodule) const
Definition: Settings.h:260
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double bendcutme(int ibend, int layerdisk, bool isPSmodule) const
Definition: Settings.h:428
Definition: value.py:1
double bendcutte(int ibend, int layerdisk, bool isPSmodule) const
Definition: Settings.h:426
double zmean(unsigned int iDisk) const
Definition: Settings.h:167
unsigned int nvmte(unsigned int inner, unsigned int iSeed) const
Definition: Settings.h:101
unsigned int nallstubs(unsigned int layerdisk) const
Definition: Settings.h:107
double rcut2S(unsigned int iSeed, unsigned int idisk) const
Definition: Settings.h:162
constexpr unsigned int N_PSLAYER
Definition: Settings.h:23
double kr() const
Definition: Settings.h:304
double drmax() const
Definition: Settings.h:128
double z0cut() const
Definition: Settings.h:324
int iseed
Definition: AMPTWrapper.h:134
double rmaxdisk() const
Definition: Settings.h:125
double rphicut2S(unsigned int iSeed, unsigned int idisk) const
Definition: Settings.h:161
Definition: deltar.py:1
double rcutPS(unsigned int iSeed, unsigned int idisk) const
Definition: Settings.h:160
double rinv(double phi1, double phi2, double r1, double r2)
Definition: Util.h:49
void initTPlut(bool fillInner, unsigned int iSeed, unsigned int layerdisk1, unsigned int layerdisk2, unsigned int nbitsfinephidiff, unsigned int iTP)
Definition: TrackletLUT.cc:64
unsigned int nbits_
Definition: TrackletLUT.h:91
int nfinephi(unsigned int inner, unsigned int iSeed) const
Definition: Settings.h:133
unsigned int nbitsphiprojderL123() const
Definition: Settings.h:87
void initTPregionlut(unsigned int iSeed, unsigned int layerdisk1, unsigned int layerdisk2, unsigned int iAllStub, unsigned int nbitsfinephidiff, unsigned int nbitsfinephi, const TrackletLUT &tplutinner, unsigned int iTP)
Definition: TrackletLUT.cc:184
double rmaxdiskvm() const
Definition: Settings.h:316
int getVMRLookup(unsigned int layerdisk, double z, double r, double dz, double dr, int iseed=-1) const
Definition: TrackletLUT.cc:666
std::pair< typename Association::data_type::first_type, double > match(Reference key, Association association, bool bestMatchByMaxValue)
Generic matching function.
Definition: Utils.h:10
double rinvcutte() const
Definition: Settings.h:313
int getphiCorrValue(unsigned int layerdisk, unsigned int ibend, unsigned int irbin, double rmean, double dr, double drmax) const
Definition: TrackletLUT.cc:851
std::vector< int > table_
Definition: TrackletLUT.h:89
double krprojshiftdisk() const
Definition: Settings.h:400
double kphi() const
Definition: Settings.h:298
double bendstrip(double r, double rinv, double stripPitch)
Definition: Util.h:42
void initVMRTable(unsigned int layerdisk, VMRTableType type, int region=-1)
Definition: TrackletLUT.cc:491
int bitShift(int num, int bits)
Definition: BitShift.h:6
const int NBINS
void writeTable() const
Definition: TrackletLUT.cc:871
std::ofstream openfile(const std::string &dir, const std::string &fname, const char *file, int line)
Definition: Util.h:139
unsigned int vmrlutzbits(unsigned int layerdisk) const
Definition: Settings.h:178
constexpr int N_LAYER
Definition: Settings.h:21
double rmaxdiskl1overlapvm() const
Definition: Settings.h:318
double rcrit() const
Definition: Settings.h:288