CMS 3D CMS Logo

TrackletCalculatorDisplaced.cc
Go to the documentation of this file.
8 
12 
13 using namespace std;
14 using namespace trklet;
15 
16 TrackletCalculatorDisplaced::TrackletCalculatorDisplaced(string name, Settings const& settings, Globals* global)
17  : ProcessBase(name, settings, global) {
18  for (unsigned int ilayer = 0; ilayer < N_LAYER; ilayer++) {
19  vector<TrackletProjectionsMemory*> tmp(settings.nallstubs(ilayer), nullptr);
20  trackletprojlayers_.push_back(tmp);
21  }
22 
23  for (unsigned int idisk = 0; idisk < N_DISK; idisk++) {
24  vector<TrackletProjectionsMemory*> tmp(settings.nallstubs(idisk + N_LAYER), nullptr);
25  trackletprojdisks_.push_back(tmp);
26  }
27 
28  layer_ = 0;
29  disk_ = 0;
30 
31  string name1 = name.substr(1); //this is to correct for "TCD" having one more letter then "TC"
32  if (name1[3] == 'L')
33  layer_ = name1[4] - '0';
34  if (name1[3] == 'D')
35  disk_ = name1[4] - '0';
36 
37  // set TC index
38  iSeed_ = 0;
39 
40  int iTC = name1[9] - 'A';
41 
42  if (name1.substr(3, 6) == "L3L4L2")
43  iSeed_ = 8;
44  else if (name1.substr(3, 6) == "L5L6L4")
45  iSeed_ = 9;
46  else if (name1.substr(3, 6) == "L2L3D1")
47  iSeed_ = 10;
48  else if (name1.substr(3, 6) == "D1D2L2")
49  iSeed_ = 11;
50 
51  assert(iSeed_ != 0);
52 
53  TCIndex_ = (iSeed_ << 4) + iTC;
54  assert(TCIndex_ >= 128 && TCIndex_ < 191);
55 
56  assert((layer_ != 0) || (disk_ != 0));
57 
58  toR_.clear();
59  toZ_.clear();
60 
61  if (iSeed_ == 8 || iSeed_ == 9) {
62  if (layer_ == 3) {
63  rzmeanInv_[0] = 1.0 / settings_.rmean(2 - 1);
64  rzmeanInv_[1] = 1.0 / settings_.rmean(3 - 1);
65  rzmeanInv_[2] = 1.0 / settings_.rmean(4 - 1);
66 
67  rproj_[0] = settings_.rmean(0);
68  rproj_[1] = settings_.rmean(4);
69  rproj_[2] = settings_.rmean(5);
70  lproj_[0] = 1;
71  lproj_[1] = 5;
72  lproj_[2] = 6;
73 
74  dproj_[0] = 1;
75  dproj_[1] = 2;
76  dproj_[2] = 0;
77  toZ_.push_back(settings_.zmean(0));
78  toZ_.push_back(settings_.zmean(1));
79  }
80  if (layer_ == 5) {
81  rzmeanInv_[0] = 1.0 / settings_.rmean(4 - 1);
82  rzmeanInv_[1] = 1.0 / settings_.rmean(5 - 1);
83  rzmeanInv_[2] = 1.0 / settings_.rmean(6 - 1);
84 
85  rproj_[0] = settings_.rmean(0);
86  rproj_[1] = settings_.rmean(1);
87  rproj_[2] = settings_.rmean(2);
88  lproj_[0] = 1;
89  lproj_[1] = 2;
90  lproj_[2] = 3;
91 
92  dproj_[0] = 0;
93  dproj_[1] = 0;
94  dproj_[2] = 0;
95  }
96  for (unsigned int i = 0; i < N_LAYER - 3; ++i)
97  toR_.push_back(rproj_[i]);
98  }
99 
100  if (iSeed_ == 10 || iSeed_ == 11) {
101  if (layer_ == 2) {
102  rzmeanInv_[0] = 1.0 / settings_.rmean(2 - 1);
103  rzmeanInv_[1] = 1.0 / settings_.rmean(3 - 1);
104  rzmeanInv_[2] = 1.0 / settings_.zmean(1 - 1);
105 
106  rproj_[0] = settings_.rmean(0);
107  lproj_[0] = 1;
108  lproj_[1] = -1;
109  lproj_[2] = -1;
110 
111  zproj_[0] = settings_.zmean(1);
112  zproj_[1] = settings_.zmean(2);
113  zproj_[2] = settings_.zmean(3);
114  dproj_[0] = 2;
115  dproj_[1] = 3;
116  dproj_[2] = 4;
117  }
118  if (disk_ == 1) {
119  rzmeanInv_[0] = 1.0 / settings_.rmean(2 - 1);
120  rzmeanInv_[1] = 1.0 / settings_.zmean(1 - 1);
121  rzmeanInv_[2] = 1.0 / settings_.zmean(2 - 1);
122 
123  rproj_[0] = settings_.rmean(0);
124  lproj_[0] = 1;
125  lproj_[1] = -1;
126  lproj_[2] = -1;
127 
128  zproj_[0] = settings_.zmean(2);
129  zproj_[1] = settings_.zmean(3);
130  zproj_[2] = settings_.zmean(4);
131  dproj_[0] = 3;
132  dproj_[1] = 4;
133  dproj_[2] = 5;
134  }
135  toR_.push_back(settings_.rmean(0));
136  for (unsigned int i = 0; i < N_DISK - 2; ++i)
137  toZ_.push_back(zproj_[i]);
138  }
139 }
140 
142  outputProj = dynamic_cast<TrackletProjectionsMemory*>(memory);
143  assert(outputProj != nullptr);
144 }
145 
147  if (settings_.writetrace()) {
148  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding output to " << memory->getName() << " to output "
149  << output;
150  }
151 
152  if (output == "trackpar") {
153  auto* tmp = dynamic_cast<TrackletParametersMemory*>(memory);
154  assert(tmp != nullptr);
155  trackletpars_ = tmp;
156  return;
157  }
158 
159  if (output.substr(0, 7) == "projout") {
160  //output is on the form 'projoutL2PHIC' or 'projoutD3PHIB'
161  auto* tmp = dynamic_cast<TrackletProjectionsMemory*>(memory);
162  assert(tmp != nullptr);
163 
164  unsigned int layerdisk = output[8] - '1'; //layer or disk counting from 0
165  unsigned int phiregion = output[12] - 'A'; //phiregion counting from 0
166 
167  if (output[7] == 'L') {
168  assert(layerdisk < N_LAYER);
169  assert(phiregion < trackletprojlayers_[layerdisk].size());
170  //check that phiregion not already initialized
171  assert(trackletprojlayers_[layerdisk][phiregion] == nullptr);
172  trackletprojlayers_[layerdisk][phiregion] = tmp;
173  return;
174  }
175 
176  if (output[7] == 'D') {
177  assert(layerdisk < N_DISK);
178  assert(phiregion < trackletprojdisks_[layerdisk].size());
179  //check that phiregion not already initialized
180  assert(trackletprojdisks_[layerdisk][phiregion] == nullptr);
181  trackletprojdisks_[layerdisk][phiregion] = tmp;
182  return;
183  }
184  }
185 
186  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find output : " << output;
187 }
188 
190  if (settings_.writetrace()) {
191  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding input from " << memory->getName() << " to input "
192  << input;
193  }
194 
195  if (input == "thirdallstubin") {
196  auto* tmp = dynamic_cast<AllStubsMemory*>(memory);
197  assert(tmp != nullptr);
198  innerallstubs_.push_back(tmp);
199  return;
200  }
201  if (input == "firstallstubin") {
202  auto* tmp = dynamic_cast<AllStubsMemory*>(memory);
203  assert(tmp != nullptr);
204  middleallstubs_.push_back(tmp);
205  return;
206  }
207  if (input == "secondallstubin") {
208  auto* tmp = dynamic_cast<AllStubsMemory*>(memory);
209  assert(tmp != nullptr);
210  outerallstubs_.push_back(tmp);
211  return;
212  }
213  if (input.find("stubtriplet") == 0) {
214  auto* tmp = dynamic_cast<StubTripletsMemory*>(memory);
215  assert(tmp != nullptr);
216  stubtriplets_.push_back(tmp);
217  return;
218  }
219  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find input : " << input;
220 }
221 
222 void TrackletCalculatorDisplaced::execute(unsigned int iSector, double phimin, double phimax) {
223  unsigned int countall = 0;
224  unsigned int countsel = 0;
225 
226  phimin_ = phimin;
227  phimax_ = phimax;
228  iSector_ = iSector;
229 
230  for (auto& stubtriplet : stubtriplets_) {
232  edm::LogVerbatim("Tracklet") << "Will break on too many tracklets in " << getName();
233  break;
234  }
235  for (unsigned int i = 0; i < stubtriplet->nStubTriplets(); i++) {
236  countall++;
237 
238  const Stub* innerFPGAStub = stubtriplet->getFPGAStub1(i);
239  const L1TStub* innerStub = innerFPGAStub->l1tstub();
240 
241  const Stub* middleFPGAStub = stubtriplet->getFPGAStub2(i);
242  const L1TStub* middleStub = middleFPGAStub->l1tstub();
243 
244  const Stub* outerFPGAStub = stubtriplet->getFPGAStub3(i);
245  const L1TStub* outerStub = outerFPGAStub->l1tstub();
246 
247  if (settings_.debugTracklet())
248  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced execute " << getName() << "[" << iSector_ << "]";
249 
250  if (innerFPGAStub->layerdisk() < N_LAYER && middleFPGAStub->layerdisk() < N_LAYER &&
251  outerFPGAStub->layerdisk() < N_LAYER) {
252  //barrel+barrel seeding
253  bool accept = LLLSeeding(innerFPGAStub, innerStub, middleFPGAStub, middleStub, outerFPGAStub, outerStub);
254  if (accept)
255  countsel++;
256  } else if (innerFPGAStub->layerdisk() >= N_LAYER && middleFPGAStub->layerdisk() >= N_LAYER &&
257  outerFPGAStub->layerdisk() >= N_LAYER) {
258  throw cms::Exception("LogicError") << __FILE__ << " " << __LINE__ << " Invalid seeding!";
259  } else {
260  //layer+disk seeding
261  if (innerFPGAStub->layerdisk() < N_LAYER && middleFPGAStub->layerdisk() >= N_LAYER &&
262  outerFPGAStub->layerdisk() >= N_LAYER) { //D1D2L2
263  bool accept = DDLSeeding(innerFPGAStub, innerStub, middleFPGAStub, middleStub, outerFPGAStub, outerStub);
264  if (accept)
265  countsel++;
266  } else if (innerFPGAStub->layerdisk() >= N_LAYER && middleFPGAStub->layerdisk() < N_LAYER &&
267  outerFPGAStub->layerdisk() < N_LAYER) { //L2L3D1
268  bool accept = LLDSeeding(innerFPGAStub, innerStub, middleFPGAStub, middleStub, outerFPGAStub, outerStub);
269  if (accept)
270  countsel++;
271  } else {
272  throw cms::Exception("LogicError") << __FILE__ << " " << __LINE__ << " Invalid seeding!";
273  }
274  }
275 
277  edm::LogVerbatim("Tracklet") << "Will break on number of tracklets in " << getName();
278  break;
279  }
280 
281  if (countall >= settings_.maxStep("TC")) {
282  if (settings_.debugTracklet())
283  edm::LogVerbatim("Tracklet") << "Will break on MAXTC 1";
284  break;
285  }
286  if (settings_.debugTracklet())
287  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced execute done";
288  }
289  if (countall >= settings_.maxStep("TC")) {
290  if (settings_.debugTracklet())
291  edm::LogVerbatim("Tracklet") << "Will break on MAXTC 2";
292  break;
293  }
294  }
295 
296  if (settings_.writeMonitorData("TPD")) {
297  globals_->ofstream("trackletcalculatordisplaced.txt") << getName() << " " << countall << " " << countsel << endl;
298  }
299 }
300 
302  disk = std::abs(disk);
303  FPGAWord fpgar = tracklet->proj(N_LAYER + disk - 1).fpgarzproj();
304 
305  if (fpgar.value() * settings_.krprojshiftdisk() < settings_.rmindiskvm())
306  return;
307  if (fpgar.value() * settings_.krprojshiftdisk() > settings_.rmaxdisk())
308  return;
309 
310  FPGAWord fpgaphi = tracklet->proj(N_LAYER + disk - 1).fpgaphiproj();
311 
312  int iphivmRaw = fpgaphi.value() >> (fpgaphi.nbits() - 5);
313  int iphi = iphivmRaw / (32 / settings_.nallstubs(disk + N_LAYER - 1));
314 
315  addProjectionDisk(disk, iphi, trackletprojdisks_[disk - 1][iphi], tracklet);
316 }
317 
319  assert(layer > 0);
320 
321  FPGAWord fpgaz = tracklet->proj(layer - 1).fpgarzproj();
322  FPGAWord fpgaphi = tracklet->proj(layer - 1).fpgaphiproj();
323 
324  if (fpgaz.atExtreme())
325  return false;
326 
327  if (std::abs(fpgaz.value() * settings_.kz()) > settings_.zlength())
328  return false;
329 
330  int iphivmRaw = fpgaphi.value() >> (fpgaphi.nbits() - 5);
331  int iphi = iphivmRaw / (32 / settings_.nallstubs(layer - 1));
332 
333  addProjection(layer, iphi, trackletprojlayers_[layer - 1][iphi], tracklet);
334 
335  return true;
336 }
337 
339  int iphi,
340  TrackletProjectionsMemory* trackletprojs,
341  Tracklet* tracklet) {
342  if (trackletprojs == nullptr) {
343  if (settings_.warnNoMem()) {
344  edm::LogVerbatim("Tracklet") << "No projection memory exists in " << getName() << " for layer = " << layer
345  << " iphi = " << iphi + 1;
346  }
347  return;
348  }
349  assert(trackletprojs != nullptr);
350  trackletprojs->addProj(tracklet);
351 }
352 
354  int iphi,
355  TrackletProjectionsMemory* trackletprojs,
356  Tracklet* tracklet) {
357  if (trackletprojs == nullptr) {
358  if (layer_ == 3 && abs(disk) == 3)
359  return; //L3L4 projections to D3 are not used.
360  if (settings_.warnNoMem()) {
361  edm::LogVerbatim("Tracklet") << "No projection memory exists in " << getName() << " for disk = " << abs(disk)
362  << " iphi = " << iphi + 1;
363  }
364  return;
365  }
366  assert(trackletprojs != nullptr);
367  if (settings_.debugTracklet())
368  edm::LogVerbatim("Tracklet") << getName() << " adding projection to " << trackletprojs->getName();
369  trackletprojs->addProj(tracklet);
370 }
371 
373  const L1TStub* innerStub,
374  const Stub* middleFPGAStub,
375  const L1TStub* middleStub,
376  const Stub* outerFPGAStub,
377  const L1TStub* outerStub) {
378  if (settings_.debugTracklet())
379  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced " << getName() << " " << layer_
380  << " trying stub triplet in layer (L L L): " << innerFPGAStub->layer().value() << " "
381  << middleFPGAStub->layer().value() << " " << outerFPGAStub->layer().value();
382 
383  assert(outerFPGAStub->layerdisk() < N_LAYER);
384 
385  double r1 = innerStub->r();
386  double z1 = innerStub->z();
387  double phi1 = innerStub->phi();
388 
389  double r2 = middleStub->r();
390  double z2 = middleStub->z();
391  double phi2 = middleStub->phi();
392 
393  double r3 = outerStub->r();
394  double z3 = outerStub->z();
395  double phi3 = outerStub->phi();
396 
397  int take3 = 0;
398  if (layer_ == 5)
399  take3 = 1;
400  unsigned ndisks = 0;
401 
402  double rinv, phi0, d0, t, z0;
403 
404  Projection projs[N_LAYER + N_DISK];
405 
406  double phiproj[N_LAYER - 2], zproj[N_LAYER - 2], phider[N_LAYER - 2], zder[N_LAYER - 2];
407  double phiprojdisk[N_DISK], rprojdisk[N_DISK], phiderdisk[N_DISK], rderdisk[N_DISK];
408 
409  exacttracklet(r1,
410  z1,
411  phi1,
412  r2,
413  z2,
414  phi2,
415  r3,
416  z3,
417  phi3,
418  take3,
419  rinv,
420  phi0,
421  d0,
422  t,
423  z0,
424  phiproj,
425  zproj,
426  phiprojdisk,
427  rprojdisk,
428  phider,
429  zder,
430  phiderdisk,
431  rderdisk);
432  if (settings_.debugTracklet())
433  edm::LogVerbatim("Tracklet") << __LINE__ << ":" << __FILE__ << "LLL Exact values " << innerFPGAStub->isBarrel()
434  << middleFPGAStub->isBarrel() << outerFPGAStub->isBarrel() << " " << phi1 << ", " << z1
435  << ", " << r1 << ", " << phi2 << ", " << z2 << ", " << r2 << ", " << phi3 << ", " << z3
436  << ", " << r3 << endl;
437 
438  if (settings_.useapprox()) {
439  phi1 = innerFPGAStub->phiapprox(phimin_, phimax_);
440  z1 = innerFPGAStub->zapprox();
441  r1 = innerFPGAStub->rapprox();
442 
443  phi2 = middleFPGAStub->phiapprox(phimin_, phimax_);
444  z2 = middleFPGAStub->zapprox();
445  r2 = middleFPGAStub->rapprox();
446 
447  phi3 = outerFPGAStub->phiapprox(phimin_, phimax_);
448  z3 = outerFPGAStub->zapprox();
449  r3 = outerFPGAStub->rapprox();
450  }
451 
452  if (settings_.debugTracklet())
453  edm::LogVerbatim("Tracklet") << __LINE__ << ":" << __FILE__ << "LLL Approx values " << innerFPGAStub->isBarrel()
454  << middleFPGAStub->isBarrel() << outerFPGAStub->isBarrel() << " " << phi1 << ", " << z1
455  << ", " << r1 << ", " << phi2 << ", " << z2 << ", " << r2 << ", " << phi3 << ", " << z3
456  << ", " << r3 << endl;
457 
458  double rinvapprox, phi0approx, d0approx, tapprox, z0approx;
459  double phiprojapprox[N_LAYER - 2], zprojapprox[N_LAYER - 2], phiderapprox[N_LAYER - 2], zderapprox[N_LAYER - 2];
460  double phiprojdiskapprox[N_DISK], rprojdiskapprox[N_DISK];
461  double phiderdiskapprox[N_DISK], rderdiskapprox[N_DISK];
462 
463  //TODO: implement the actual integer calculation
464  if (settings_.useapprox()) {
465  approxtracklet(r1,
466  z1,
467  phi1,
468  r2,
469  z2,
470  phi2,
471  r3,
472  z3,
473  phi3,
474  take3,
475  ndisks,
476  rinvapprox,
477  phi0approx,
478  d0approx,
479  tapprox,
480  z0approx,
481  phiprojapprox,
482  zprojapprox,
483  phiderapprox,
484  zderapprox,
485  phiprojdiskapprox,
486  rprojdiskapprox,
487  phiderdiskapprox,
488  rderdiskapprox);
489  } else {
490  rinvapprox = rinv;
491  phi0approx = phi0;
492  d0approx = d0;
493  tapprox = t;
494  z0approx = z0;
495 
496  for (unsigned int i = 0; i < toR_.size(); ++i) {
497  phiprojapprox[i] = phiproj[i];
498  zprojapprox[i] = zproj[i];
499  phiderapprox[i] = phider[i];
500  zderapprox[i] = zder[i];
501  }
502 
503  for (unsigned int i = 0; i < toZ_.size(); ++i) {
504  phiprojdiskapprox[i] = phiprojdisk[i];
505  rprojdiskapprox[i] = rprojdisk[i];
506  phiderdiskapprox[i] = phiderdisk[i];
507  rderdiskapprox[i] = rderdisk[i];
508  }
509  }
510 
511  //store the approcximate results
512 
513  if (settings_.debugTracklet()) {
514  edm::LogVerbatim("Tracklet") << "rinvapprox: " << rinvapprox << " rinv: " << rinv << endl;
515  edm::LogVerbatim("Tracklet") << "phi0approx: " << phi0approx << " phi0: " << phi0 << endl;
516  edm::LogVerbatim("Tracklet") << "d0approx: " << d0approx << " d0: " << d0 << endl;
517  edm::LogVerbatim("Tracklet") << "tapprox: " << tapprox << " t: " << t << endl;
518  edm::LogVerbatim("Tracklet") << "z0approx: " << z0approx << " z0: " << z0 << endl;
519  }
520 
521  for (unsigned int i = 0; i < toR_.size(); ++i) {
522  if (settings_.debugTracklet()) {
523  edm::LogVerbatim("Tracklet") << "phiprojapprox[" << i << "]: " << phiprojapprox[i] << " phiproj[" << i
524  << "]: " << phiproj[i] << endl;
525  edm::LogVerbatim("Tracklet") << "zprojapprox[" << i << "]: " << zprojapprox[i] << " zproj[" << i
526  << "]: " << zproj[i] << endl;
527  edm::LogVerbatim("Tracklet") << "phiderapprox[" << i << "]: " << phiderapprox[i] << " phider[" << i
528  << "]: " << phider[i] << endl;
529  edm::LogVerbatim("Tracklet") << "zderapprox[" << i << "]: " << zderapprox[i] << " zder[" << i << "]: " << zder[i]
530  << endl;
531  }
532  }
533 
534  for (unsigned int i = 0; i < toZ_.size(); ++i) {
535  if (settings_.debugTracklet()) {
536  edm::LogVerbatim("Tracklet") << "phiprojdiskapprox[" << i << "]: " << phiprojdiskapprox[i] << " phiprojdisk[" << i
537  << "]: " << phiprojdisk[i] << endl;
538  edm::LogVerbatim("Tracklet") << "rprojdiskapprox[" << i << "]: " << rprojdiskapprox[i] << " rprojdisk[" << i
539  << "]: " << rprojdisk[i] << endl;
540  edm::LogVerbatim("Tracklet") << "phiderdiskapprox[" << i << "]: " << phiderdiskapprox[i] << " phiderdisk[" << i
541  << "]: " << phiderdisk[i] << endl;
542  edm::LogVerbatim("Tracklet") << "rderdiskapprox[" << i << "]: " << rderdiskapprox[i] << " rderdisk[" << i
543  << "]: " << rderdisk[i] << endl;
544  }
545  }
546 
547  //now binary
548  double krinv = settings_.kphi1() / settings_.kr() * pow(2, settings_.rinv_shift()),
549  kphi0 = settings_.kphi1() * pow(2, settings_.phi0_shift()),
550  kt = settings_.kz() / settings_.kr() * pow(2, settings_.t_shift()),
551  kz0 = settings_.kz() * pow(2, settings_.z0_shift()),
552  kphiproj = settings_.kphi1() * pow(2, settings_.SS_phiL_shift()),
553  kphider = settings_.kphi1() / settings_.kr() * pow(2, settings_.SS_phiderL_shift()),
554  kzproj = settings_.kz() * pow(2, settings_.PS_zL_shift()),
555  kzder = settings_.kz() / settings_.kr() * pow(2, settings_.PS_zderL_shift()),
556  kphiprojdisk = settings_.kphi1() * pow(2, settings_.SS_phiD_shift()),
557  kphiderdisk = settings_.kphi1() / settings_.kr() * pow(2, settings_.SS_phiderD_shift()),
558  krprojdisk = settings_.kr() * pow(2, settings_.PS_rD_shift()),
559  krderdisk = settings_.kr() / settings_.kz() * pow(2, settings_.PS_rderD_shift());
560 
561  int irinv, iphi0, id0, it, iz0;
562  int iphiproj[N_LAYER - 2], izproj[N_LAYER - 2], iphider[N_LAYER - 2], izder[N_LAYER - 2];
563  int iphiprojdisk[N_DISK], irprojdisk[N_DISK], iphiderdisk[N_DISK], irderdisk[N_DISK];
564 
565  //store the binary results
566  irinv = rinvapprox / krinv;
567  iphi0 = phi0approx / kphi0;
568  id0 = d0approx / settings_.kd0();
569  it = tapprox / kt;
570  iz0 = z0approx / kz0;
571 
572  bool success = true;
573  if (std::abs(rinvapprox) > settings_.rinvcut()) {
574  if (settings_.debugTracklet())
575  edm::LogVerbatim("Tracklet") << "TrackletCalculator::LLL Seeding irinv too large: " << rinvapprox << "(" << irinv
576  << ")";
577  success = false;
578  }
579  if (std::abs(z0approx) > settings_.disp_z0cut()) {
580  if (settings_.debugTracklet())
581  edm::LogVerbatim("Tracklet") << "Failed tracklet z0 cut " << z0approx << " in layer " << layer_;
582  success = false;
583  }
584  if (std::abs(d0approx) > settings_.maxd0()) {
585  if (settings_.debugTracklet())
586  edm::LogVerbatim("Tracklet") << "Failed tracklet approx d0 cut " << d0approx;
587  success = false;
588  }
589  if (std::abs(d0) > settings_.maxd0()) {
590  if (settings_.debugTracklet())
591  edm::LogVerbatim("Tracklet") << "Failed tracklet exact d0 cut " << d0;
592  success = false;
593  }
594 
595  if (!success) {
596  return false;
597  }
598 
599  double phicritapprox = phi0approx - asin((0.5 * settings_.rcrit() * rinvapprox) + (d0approx / settings_.rcrit()));
600  int phicrit = iphi0 - 2 * irinv - 2 * id0;
601 
602  int iphicritmincut = settings_.phicritminmc() / globals_->ITC_L1L2()->phi0_final.K();
603  int iphicritmaxcut = settings_.phicritmaxmc() / globals_->ITC_L1L2()->phi0_final.K();
604 
605  bool keepapprox = (phicritapprox > settings_.phicritminmc()) && (phicritapprox < settings_.phicritmaxmc()),
606  keep = (phicrit > iphicritmincut) && (phicrit < iphicritmaxcut);
607 
608  if (settings_.debugTracklet())
609  if (keep && !keepapprox)
610  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced::LLLSeeding tracklet kept with exact phicrit cut "
611  "but not approximate, phicritapprox: "
612  << phicritapprox;
613  if (settings_.usephicritapprox()) {
614  if (!keepapprox) {
615  return false;
616  }
617  } else {
618  if (!keep) {
619  return false;
620  }
621  }
622 
623  for (unsigned int i = 0; i < toR_.size(); ++i) {
624  iphiproj[i] = phiprojapprox[i] / kphiproj;
625  izproj[i] = zprojapprox[i] / kzproj;
626 
627  iphider[i] = phiderapprox[i] / kphider;
628  izder[i] = zderapprox[i] / kzder;
629 
630  //check that z projection is in range
631  if (izproj[i] < -(1 << (settings_.nzbitsstub(0) - 1)))
632  continue;
633  if (izproj[i] >= (1 << (settings_.nzbitsstub(0) - 1)))
634  continue;
635 
636  //check that phi projection is in range
637  if (iphiproj[i] >= (1 << settings_.nphibitsstub(N_LAYER - 1)) - 1)
638  continue;
639  if (iphiproj[i] <= 0)
640  continue;
641 
642  //adjust number of bits for phi and z projection
643  if (rproj_[i] < settings_.rPS2S()) {
644  iphiproj[i] >>= (settings_.nphibitsstub(N_LAYER - 1) - settings_.nphibitsstub(0));
645  if (iphiproj[i] >= (1 << settings_.nphibitsstub(0)) - 1)
646  iphiproj[i] = (1 << settings_.nphibitsstub(0)) - 2; //-2 not to hit atExtreme
647  } else {
648  izproj[i] >>= (settings_.nzbitsstub(0) - settings_.nzbitsstub(N_LAYER - 1));
649  }
650 
651  if (rproj_[i] < settings_.rPS2S()) {
652  if (iphider[i] < -(1 << (settings_.nbitsphiprojderL123() - 1))) {
653  iphider[i] = -(1 << (settings_.nbitsphiprojderL123() - 1));
654  }
655  if (iphider[i] >= (1 << (settings_.nbitsphiprojderL123() - 1))) {
656  iphider[i] = (1 << (settings_.nbitsphiprojderL123() - 1)) - 1;
657  }
658  } else {
659  if (iphider[i] < -(1 << (settings_.nbitsphiprojderL456() - 1))) {
660  iphider[i] = -(1 << (settings_.nbitsphiprojderL456() - 1));
661  }
662  if (iphider[i] >= (1 << (settings_.nbitsphiprojderL456() - 1))) {
663  iphider[i] = (1 << (settings_.nbitsphiprojderL456() - 1)) - 1;
664  }
665  }
666 
667  projs[lproj_[i] - 1].init(settings_,
668  lproj_[i] - 1,
669  iphiproj[i],
670  izproj[i],
671  iphider[i],
672  izder[i],
673  phiproj[i],
674  zproj[i],
675  phider[i],
676  zder[i],
677  phiprojapprox[i],
678  zprojapprox[i],
679  phiderapprox[i],
680  zderapprox[i],
681  false);
682  }
683 
684  if (std::abs(it * kt) > 1.0) {
685  for (unsigned int i = 0; i < toZ_.size(); ++i) {
686  iphiprojdisk[i] = phiprojdiskapprox[i] / kphiprojdisk;
687  irprojdisk[i] = rprojdiskapprox[i] / krprojdisk;
688 
689  iphiderdisk[i] = phiderdiskapprox[i] / kphiderdisk;
690  irderdisk[i] = rderdiskapprox[i] / krderdisk;
691 
692  //check phi projection in range
693  if (iphiprojdisk[i] <= 0)
694  continue;
695  if (iphiprojdisk[i] >= (1 << settings_.nphibitsstub(0)) - 1)
696  continue;
697 
698  //check r projection in range
699  if (rprojdiskapprox[i] < settings_.rmindisk() || rprojdiskapprox[i] >= settings_.rmaxdisk())
700  continue;
701 
702  projs[N_LAYER + i].init(settings_,
703  N_LAYER + i,
704  iphiprojdisk[i],
705  irprojdisk[i],
706  iphiderdisk[i],
707  irderdisk[i],
708  phiprojdisk[i],
709  rprojdisk[i],
710  phiderdisk[i],
711  rderdisk[i],
712  phiprojdiskapprox[i],
713  rprojdiskapprox[i],
714  phiderdisk[i],
715  rderdisk[i],
716  false);
717  }
718  }
719 
720  if (settings_.writeMonitorData("TrackletPars")) {
721  globals_->ofstream("trackletpars.txt")
722  << layer_ << " , " << rinv << " , " << rinvapprox << " , " << phi0 << " , " << phi0approx << " , " << t << " , "
723  << tapprox << " , " << z0 << " , " << z0approx << " , " << d0 << " , " << d0approx << endl;
724  }
725 
726  Tracklet* tracklet = new Tracklet(settings_,
727  iSeed_,
728  innerFPGAStub,
729  middleFPGAStub,
730  outerFPGAStub,
731  rinv,
732  phi0,
733  d0,
734  z0,
735  t,
736  rinvapprox,
737  phi0approx,
738  d0approx,
739  z0approx,
740  tapprox,
741  irinv,
742  iphi0,
743  id0,
744  iz0,
745  it,
746  projs,
747  false);
748 
749  if (settings_.debugTracklet())
750  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced " << getName()
751  << " Found LLL tracklet in sector = " << iSector_ << " phi0 = " << phi0;
752 
754  tracklet->setTCIndex(TCIndex_);
755 
756  if (settings_.writeMonitorData("Seeds")) {
757  ofstream fout("seeds.txt", ofstream::app);
758  fout << __FILE__ << ":" << __LINE__ << " " << name_ << "_" << iSector_ << " " << tracklet->getISeed() << endl;
759  fout.close();
760  }
761  trackletpars_->addTracklet(tracklet);
762 
763  bool addL5 = false;
764  bool addL6 = false;
765  for (unsigned int j = 0; j < toR_.size(); j++) {
766  bool added = false;
767 
768  if (settings_.debugTracklet())
769  edm::LogVerbatim("Tracklet") << "adding layer projection " << j << "/" << toR_.size() << " " << lproj_[j];
770  if (tracklet->validProj(lproj_[j] - 1)) {
771  added = addLayerProj(tracklet, lproj_[j]);
772  if (added && lproj_[j] == 5)
773  addL5 = true;
774  if (added && lproj_[j] == 6)
775  addL6 = true;
776  }
777  }
778 
779  for (unsigned int j = 0; j < toZ_.size(); j++) {
780  int disk = dproj_[j];
781  if (disk == 0)
782  continue;
783  if (disk == 2 && addL5)
784  continue;
785  if (disk == 1 && addL6)
786  continue;
787  if (it < 0)
788  disk = -disk;
789  if (settings_.debugTracklet())
790  edm::LogVerbatim("Tracklet") << "adding disk projection " << j << "/" << toZ_.size() << " " << disk;
791  if (tracklet->validProj(N_LAYER + abs(disk) - 1)) {
792  addDiskProj(tracklet, disk);
793  }
794  }
795 
796  return true;
797 }
798 
800  const L1TStub* innerStub,
801  const Stub* middleFPGAStub,
802  const L1TStub* middleStub,
803  const Stub* outerFPGAStub,
804  const L1TStub* outerStub) {
805  if (settings_.debugTracklet())
806  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced " << getName() << " " << layer_
807  << " trying stub triplet in (L2 D1 D2): " << innerFPGAStub->layer().value() << " "
808  << middleFPGAStub->disk().value() << " " << outerFPGAStub->disk().value();
809 
810  int take3 = 1; //D1D2L2
811  unsigned ndisks = 2;
812 
813  double r1 = innerStub->r();
814  double z1 = innerStub->z();
815  double phi1 = innerStub->phi();
816 
817  double r2 = middleStub->r();
818  double z2 = middleStub->z();
819  double phi2 = middleStub->phi();
820 
821  double r3 = outerStub->r();
822  double z3 = outerStub->z();
823  double phi3 = outerStub->phi();
824 
825  double rinv, phi0, d0, t, z0;
826 
827  double phiproj[N_LAYER - 2], zproj[N_LAYER - 2], phider[N_LAYER - 2], zder[N_LAYER - 2];
828  double phiprojdisk[N_DISK], rprojdisk[N_DISK], phiderdisk[N_DISK], rderdisk[N_DISK];
829 
830  exacttracklet(r1,
831  z1,
832  phi1,
833  r2,
834  z2,
835  phi2,
836  r3,
837  z3,
838  phi3,
839  take3,
840  rinv,
841  phi0,
842  d0,
843  t,
844  z0,
845  phiproj,
846  zproj,
847  phiprojdisk,
848  rprojdisk,
849  phider,
850  zder,
851  phiderdisk,
852  rderdisk);
853 
854  if (settings_.debugTracklet())
855  edm::LogVerbatim("Tracklet") << __LINE__ << ":" << __FILE__ << " DLL Exact values " << innerFPGAStub->isBarrel()
856  << middleFPGAStub->isBarrel() << outerFPGAStub->isBarrel() << " " << phi1 << ", " << z1
857  << ", " << r1 << ", " << phi2 << ", " << z2 << ", " << r2 << ", " << phi3 << ", " << z3
858  << ", " << r3 << endl;
859 
860  if (settings_.useapprox()) {
861  phi1 = innerFPGAStub->phiapprox(phimin_, phimax_);
862  z1 = innerFPGAStub->zapprox();
863  r1 = innerFPGAStub->rapprox();
864 
865  phi2 = middleFPGAStub->phiapprox(phimin_, phimax_);
866  z2 = middleFPGAStub->zapprox();
867  r2 = middleFPGAStub->rapprox();
868 
869  phi3 = outerFPGAStub->phiapprox(phimin_, phimax_);
870  z3 = outerFPGAStub->zapprox();
871  r3 = outerFPGAStub->rapprox();
872  }
873 
874  if (settings_.debugTracklet())
875  edm::LogVerbatim("Tracklet") << __LINE__ << ":" << __FILE__ << "DLL Approx values " << innerFPGAStub->isBarrel()
876  << middleFPGAStub->isBarrel() << outerFPGAStub->isBarrel() << " " << phi1 << ", " << z1
877  << ", " << r1 << ", " << phi2 << ", " << z2 << ", " << r2 << ", " << phi3 << ", " << z3
878  << ", " << r3 << endl;
879 
880  double rinvapprox, phi0approx, d0approx, tapprox, z0approx;
881  double phiprojapprox[N_LAYER - 2], zprojapprox[N_LAYER - 2], phiderapprox[N_LAYER - 2], zderapprox[N_LAYER - 2];
882  double phiprojdiskapprox[N_DISK], rprojdiskapprox[N_DISK];
883  double phiderdiskapprox[N_DISK], rderdiskapprox[N_DISK];
884 
885  //TODO: implement the actual integer calculation
886  if (settings_.useapprox()) {
887  approxtracklet(r1,
888  z1,
889  phi1,
890  r2,
891  z2,
892  phi2,
893  r3,
894  z3,
895  phi3,
896  take3,
897  ndisks,
898  rinvapprox,
899  phi0approx,
900  d0approx,
901  tapprox,
902  z0approx,
903  phiprojapprox,
904  zprojapprox,
905  phiderapprox,
906  zderapprox,
907  phiprojdiskapprox,
908  rprojdiskapprox,
909  phiderdiskapprox,
910  rderdiskapprox);
911  } else {
912  rinvapprox = rinv;
913  phi0approx = phi0;
914  d0approx = d0;
915  tapprox = t;
916  z0approx = z0;
917 
918  for (unsigned int i = 0; i < toR_.size(); ++i) {
919  phiprojapprox[i] = phiproj[i];
920  zprojapprox[i] = zproj[i];
921  phiderapprox[i] = phider[i];
922  zderapprox[i] = zder[i];
923  }
924 
925  for (unsigned int i = 0; i < toZ_.size(); ++i) {
926  phiprojdiskapprox[i] = phiprojdisk[i];
927  rprojdiskapprox[i] = rprojdisk[i];
928  phiderdiskapprox[i] = phiderdisk[i];
929  rderdiskapprox[i] = rderdisk[i];
930  }
931  }
932 
933  //store the approcximate results
934  if (settings_.debugTracklet()) {
935  edm::LogVerbatim("Tracklet") << "rinvapprox: " << rinvapprox << " rinv: " << rinv << endl;
936  edm::LogVerbatim("Tracklet") << "phi0approx: " << phi0approx << " phi0: " << phi0 << endl;
937  edm::LogVerbatim("Tracklet") << "d0approx: " << d0approx << " d0: " << d0 << endl;
938  edm::LogVerbatim("Tracklet") << "tapprox: " << tapprox << " t: " << t << endl;
939  edm::LogVerbatim("Tracklet") << "z0approx: " << z0approx << " z0: " << z0 << endl;
940  }
941 
942  for (unsigned int i = 0; i < toR_.size(); ++i) {
943  if (settings_.debugTracklet()) {
944  edm::LogVerbatim("Tracklet") << "phiprojapprox[" << i << "]: " << phiprojapprox[i] << " phiproj[" << i
945  << "]: " << phiproj[i] << endl;
946  edm::LogVerbatim("Tracklet") << "zprojapprox[" << i << "]: " << zprojapprox[i] << " zproj[" << i
947  << "]: " << zproj[i] << endl;
948  edm::LogVerbatim("Tracklet") << "phiderapprox[" << i << "]: " << phiderapprox[i] << " phider[" << i
949  << "]: " << phider[i] << endl;
950  edm::LogVerbatim("Tracklet") << "zderapprox[" << i << "]: " << zderapprox[i] << " zder[" << i << "]: " << zder[i]
951  << endl;
952  }
953  }
954 
955  for (unsigned int i = 0; i < toZ_.size(); ++i) {
956  if (settings_.debugTracklet()) {
957  edm::LogVerbatim("Tracklet") << "phiprojdiskapprox[" << i << "]: " << phiprojdiskapprox[i] << " phiprojdisk[" << i
958  << "]: " << phiprojdisk[i] << endl;
959  edm::LogVerbatim("Tracklet") << "rprojdiskapprox[" << i << "]: " << rprojdiskapprox[i] << " rprojdisk[" << i
960  << "]: " << rprojdisk[i] << endl;
961  edm::LogVerbatim("Tracklet") << "phiderdiskapprox[" << i << "]: " << phiderdiskapprox[i] << " phiderdisk[" << i
962  << "]: " << phiderdisk[i] << endl;
963  edm::LogVerbatim("Tracklet") << "rderdiskapprox[" << i << "]: " << rderdiskapprox[i] << " rderdisk[" << i
964  << "]: " << rderdisk[i] << endl;
965  }
966  }
967 
968  //now binary
969  double krinv = settings_.kphi1() / settings_.kr() * pow(2, settings_.rinv_shift()),
970  kphi0 = settings_.kphi1() * pow(2, settings_.phi0_shift()),
971  kt = settings_.kz() / settings_.kr() * pow(2, settings_.t_shift()),
972  kz0 = settings_.kz() * pow(2, settings_.z0_shift()),
973  kphiproj = settings_.kphi1() * pow(2, settings_.SS_phiL_shift()),
974  kphider = settings_.kphi1() / settings_.kr() * pow(2, settings_.SS_phiderL_shift()),
975  kzproj = settings_.kz() * pow(2, settings_.PS_zL_shift()),
976  kzder = settings_.kz() / settings_.kr() * pow(2, settings_.PS_zderL_shift()),
977  kphiprojdisk = settings_.kphi1() * pow(2, settings_.SS_phiD_shift()),
978  kphiderdisk = settings_.kphi1() / settings_.kr() * pow(2, settings_.SS_phiderD_shift()),
979  krprojdisk = settings_.kr() * pow(2, settings_.PS_rD_shift()),
980  krderdisk = settings_.kr() / settings_.kz() * pow(2, settings_.PS_rderD_shift());
981 
982  int irinv, iphi0, id0, it, iz0;
983  int iphiproj[N_LAYER - 2], izproj[N_LAYER - 2], iphider[N_LAYER - 2], izder[N_LAYER - 2];
984  int iphiprojdisk[N_DISK], irprojdisk[N_DISK], iphiderdisk[N_DISK], irderdisk[N_DISK];
985 
986  //store the binary results
987  irinv = rinvapprox / krinv;
988  iphi0 = phi0approx / kphi0;
989  id0 = d0approx / settings_.kd0();
990  it = tapprox / kt;
991  iz0 = z0approx / kz0;
992 
993  bool success = true;
994  if (std::abs(rinvapprox) > settings_.rinvcut()) {
995  if (settings_.debugTracklet())
996  edm::LogVerbatim("Tracklet") << "TrackletCalculator::DDL Seeding irinv too large: " << rinvapprox << "(" << irinv
997  << ")";
998  success = false;
999  }
1000  if (std::abs(z0approx) > settings_.disp_z0cut()) {
1001  if (settings_.debugTracklet())
1002  edm::LogVerbatim("Tracklet") << "Failed tracklet z0 cut " << z0approx;
1003  success = false;
1004  }
1005  if (std::abs(d0approx) > settings_.maxd0()) {
1006  if (settings_.debugTracklet())
1007  edm::LogVerbatim("Tracklet") << "Failed tracklet approx d0 cut " << d0approx;
1008  success = false;
1009  }
1010  if (std::abs(d0) > settings_.maxd0()) {
1011  if (settings_.debugTracklet())
1012  edm::LogVerbatim("Tracklet") << "Failed tracklet exact d0 cut " << d0;
1013  success = false;
1014  }
1015 
1016  if (!success)
1017  return false;
1018 
1019  double phicritapprox = phi0approx - asin((0.5 * settings_.rcrit() * rinvapprox) + (d0approx / settings_.rcrit()));
1020  int phicrit = iphi0 - 2 * irinv - 2 * id0;
1021 
1022  int iphicritmincut = settings_.phicritminmc() / globals_->ITC_L1L2()->phi0_final.K();
1023  int iphicritmaxcut = settings_.phicritmaxmc() / globals_->ITC_L1L2()->phi0_final.K();
1024 
1025  bool keepapprox = (phicritapprox > settings_.phicritminmc()) && (phicritapprox < settings_.phicritmaxmc()),
1026  keep = (phicrit > iphicritmincut) && (phicrit < iphicritmaxcut);
1027 
1028  if (settings_.debugTracklet())
1029  if (keep && !keepapprox)
1030  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced::DDLSeeding tracklet kept with exact phicrit cut "
1031  "but not approximate, phicritapprox: "
1032  << phicritapprox;
1033  if (settings_.usephicritapprox()) {
1034  if (!keepapprox)
1035  return false;
1036  } else {
1037  if (!keep)
1038  return false;
1039  }
1040 
1041  Projection projs[N_LAYER + N_DISK];
1042 
1043  for (unsigned int i = 0; i < toR_.size(); ++i) {
1044  iphiproj[i] = phiprojapprox[i] / kphiproj;
1045  izproj[i] = zprojapprox[i] / kzproj;
1046 
1047  iphider[i] = phiderapprox[i] / kphider;
1048  izder[i] = zderapprox[i] / kzder;
1049 
1050  //check that z projection in range
1051  if (izproj[i] < -(1 << (settings_.nzbitsstub(0) - 1)))
1052  continue;
1053  if (izproj[i] >= (1 << (settings_.nzbitsstub(0) - 1)))
1054  continue;
1055 
1056  //check that phi projection in range
1057  if (iphiproj[i] >= (1 << settings_.nphibitsstub(5)) - 1)
1058  continue;
1059  if (iphiproj[i] <= 0)
1060  continue;
1061 
1062  if (rproj_[i] < settings_.rPS2S()) {
1063  iphiproj[i] >>= (settings_.nphibitsstub(5) - settings_.nphibitsstub(0));
1064  } else {
1065  izproj[i] >>= (settings_.nzbitsstub(0) - settings_.nzbitsstub(5));
1066  }
1067 
1068  if (rproj_[i] < settings_.rPS2S()) {
1069  if (iphider[i] < -(1 << (settings_.nbitsphiprojderL123() - 1)))
1070  iphider[i] = -(1 << (settings_.nbitsphiprojderL123() - 1));
1071  if (iphider[i] >= (1 << (settings_.nbitsphiprojderL123() - 1)))
1072  iphider[i] = (1 << (settings_.nbitsphiprojderL123() - 1)) - 1;
1073  } else {
1074  if (iphider[i] < -(1 << (settings_.nbitsphiprojderL456() - 1)))
1075  iphider[i] = -(1 << (settings_.nbitsphiprojderL456() - 1));
1076  if (iphider[i] >= (1 << (settings_.nbitsphiprojderL456() - 1)))
1077  iphider[i] = (1 << (settings_.nbitsphiprojderL456() - 1)) - 1;
1078  }
1079 
1080  projs[lproj_[i] - 1].init(settings_,
1081  lproj_[i] - 1,
1082  iphiproj[i],
1083  izproj[i],
1084  iphider[i],
1085  izder[i],
1086  phiproj[i],
1087  zproj[i],
1088  phider[i],
1089  zder[i],
1090  phiprojapprox[i],
1091  zprojapprox[i],
1092  phiderapprox[i],
1093  zderapprox[i],
1094  false);
1095  }
1096 
1097  if (std::abs(it * kt) > 1.0) {
1098  for (unsigned int i = 0; i < toZ_.size(); ++i) {
1099  iphiprojdisk[i] = phiprojdiskapprox[i] / kphiprojdisk;
1100  irprojdisk[i] = rprojdiskapprox[i] / krprojdisk;
1101 
1102  iphiderdisk[i] = phiderdiskapprox[i] / kphiderdisk;
1103  irderdisk[i] = rderdiskapprox[i] / krderdisk;
1104 
1105  if (iphiprojdisk[i] <= 0)
1106  continue;
1107  if (iphiprojdisk[i] >= (1 << settings_.nphibitsstub(0)) - 1)
1108  continue;
1109 
1110  if (irprojdisk[i] < settings_.rmindisk() / krprojdisk || irprojdisk[i] >= settings_.rmaxdisk() / krprojdisk)
1111  continue;
1112 
1113  projs[N_LAYER + i + 2].init(settings_,
1114  N_LAYER + i + 2,
1115  iphiprojdisk[i],
1116  irprojdisk[i],
1117  iphiderdisk[i],
1118  irderdisk[i],
1119  phiprojdisk[i],
1120  rprojdisk[i],
1121  phiderdisk[i],
1122  rderdisk[i],
1123  phiprojdiskapprox[i],
1124  rprojdiskapprox[i],
1125  phiderdisk[i],
1126  rderdisk[i],
1127  false);
1128  }
1129  }
1130 
1131  if (settings_.writeMonitorData("TrackletPars")) {
1132  globals_->ofstream("trackletpars.txt")
1133  << layer_ << " , " << rinv << " , " << rinvapprox << " , " << phi0 << " , " << phi0approx << " , " << t << " , "
1134  << tapprox << " , " << z0 << " , " << z0approx << " , " << d0 << " , " << d0approx << endl;
1135  }
1136 
1137  Tracklet* tracklet = new Tracklet(settings_,
1138  iSeed_,
1139  innerFPGAStub,
1140  middleFPGAStub,
1141  outerFPGAStub,
1142  rinv,
1143  phi0,
1144  d0,
1145  z0,
1146  t,
1147  rinvapprox,
1148  phi0approx,
1149  d0approx,
1150  z0approx,
1151  tapprox,
1152  irinv,
1153  iphi0,
1154  id0,
1155  iz0,
1156  it,
1157  projs,
1158  true);
1159 
1160  if (settings_.debugTracklet())
1161  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced " << getName()
1162  << " Found DDL tracklet in sector = " << iSector_ << " phi0 = " << phi0;
1163 
1165  tracklet->setTCIndex(TCIndex_);
1166 
1167  if (settings_.writeMonitorData("Seeds")) {
1168  ofstream fout("seeds.txt", ofstream::app);
1169  fout << __FILE__ << ":" << __LINE__ << " " << name_ << "_" << iSector_ << " " << tracklet->getISeed() << endl;
1170  fout.close();
1171  }
1172  trackletpars_->addTracklet(tracklet);
1173 
1174  for (unsigned int j = 0; j < toR_.size(); j++) {
1175  if (settings_.debugTracklet())
1176  edm::LogVerbatim("Tracklet") << "adding layer projection " << j << "/" << toR_.size() << " " << lproj_[j] << " "
1177  << tracklet->validProj(lproj_[j] - 1);
1178  if (tracklet->validProj(lproj_[j] - 1)) {
1179  addLayerProj(tracklet, lproj_[j]);
1180  }
1181  }
1182 
1183  for (unsigned int j = 0; j < toZ_.size(); j++) {
1184  int disk = dproj_[j];
1185  if (disk == 0)
1186  continue;
1187  if (it < 0)
1188  disk = -disk;
1189  if (settings_.debugTracklet())
1190  edm::LogVerbatim("Tracklet") << "adding disk projection " << j << "/" << toZ_.size() << " " << disk << " "
1191  << tracklet->validProj(N_LAYER + abs(disk) - 1);
1192  if (tracklet->validProj(N_LAYER + abs(disk) - 1)) {
1193  addDiskProj(tracklet, disk);
1194  }
1195  }
1196 
1197  return true;
1198 }
1199 
1201  const L1TStub* innerStub,
1202  const Stub* middleFPGAStub,
1203  const L1TStub* middleStub,
1204  const Stub* outerFPGAStub,
1205  const L1TStub* outerStub) {
1206  if (settings_.debugTracklet())
1207  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced " << getName() << " " << layer_
1208  << " trying stub triplet in (L2L3D1): " << middleFPGAStub->layer().value() << " "
1209  << outerFPGAStub->layer().value() << " " << innerFPGAStub->disk().value();
1210 
1211  int take3 = 0; //L2L3D1
1212  unsigned ndisks = 1;
1213 
1214  double r3 = innerStub->r();
1215  double z3 = innerStub->z();
1216  double phi3 = innerStub->phi();
1217 
1218  double r1 = middleStub->r();
1219  double z1 = middleStub->z();
1220  double phi1 = middleStub->phi();
1221 
1222  double r2 = outerStub->r();
1223  double z2 = outerStub->z();
1224  double phi2 = outerStub->phi();
1225 
1226  double rinv, phi0, d0, t, z0;
1227 
1228  double phiproj[N_LAYER - 2], zproj[N_LAYER - 2], phider[N_LAYER - 2], zder[N_LAYER - 2];
1229  double phiprojdisk[N_DISK], rprojdisk[N_DISK], phiderdisk[N_DISK], rderdisk[N_DISK];
1230 
1231  exacttracklet(r1,
1232  z1,
1233  phi1,
1234  r2,
1235  z2,
1236  phi2,
1237  r3,
1238  z3,
1239  phi3,
1240  take3,
1241  rinv,
1242  phi0,
1243  d0,
1244  t,
1245  z0,
1246  phiproj,
1247  zproj,
1248  phiprojdisk,
1249  rprojdisk,
1250  phider,
1251  zder,
1252  phiderdisk,
1253  rderdisk);
1254 
1255  if (settings_.debugTracklet())
1256  edm::LogVerbatim("Tracklet") << __LINE__ << ":" << __FILE__ << "LLD Exact values " << innerFPGAStub->isBarrel()
1257  << middleFPGAStub->isBarrel() << outerFPGAStub->isBarrel() << " " << phi3 << ", " << z3
1258  << ", " << r3 << ", " << phi1 << ", " << z1 << ", " << r1 << ", " << phi2 << ", " << z2
1259  << ", " << r2 << endl;
1260 
1261  if (settings_.useapprox()) {
1262  phi3 = innerFPGAStub->phiapprox(phimin_, phimax_);
1263  z3 = innerFPGAStub->zapprox();
1264  r3 = innerFPGAStub->rapprox();
1265 
1266  phi1 = middleFPGAStub->phiapprox(phimin_, phimax_);
1267  z1 = middleFPGAStub->zapprox();
1268  r1 = middleFPGAStub->rapprox();
1269 
1270  phi2 = outerFPGAStub->phiapprox(phimin_, phimax_);
1271  z2 = outerFPGAStub->zapprox();
1272  r2 = outerFPGAStub->rapprox();
1273  }
1274 
1275  if (settings_.debugTracklet())
1276  edm::LogVerbatim("Tracklet") << __LINE__ << ":" << __FILE__ << "LLD approx values " << innerFPGAStub->isBarrel()
1277  << middleFPGAStub->isBarrel() << outerFPGAStub->isBarrel() << " " << phi3 << ", " << z3
1278  << ", " << r3 << ", " << phi1 << ", " << z1 << ", " << r1 << ", " << phi2 << ", " << z2
1279  << ", " << r2 << endl;
1280 
1281  double rinvapprox, phi0approx, d0approx, tapprox, z0approx;
1282  double phiprojapprox[N_LAYER - 2], zprojapprox[N_LAYER - 2], phiderapprox[N_LAYER - 2], zderapprox[N_LAYER - 2];
1283  double phiprojdiskapprox[N_DISK], rprojdiskapprox[N_DISK];
1284  double phiderdiskapprox[N_DISK], rderdiskapprox[N_DISK];
1285 
1286  //TODO: implement the actual integer calculation
1287  if (settings_.useapprox()) {
1288  approxtracklet(r1,
1289  z1,
1290  phi1,
1291  r2,
1292  z2,
1293  phi2,
1294  r3,
1295  z3,
1296  phi3,
1297  take3,
1298  ndisks,
1299  rinvapprox,
1300  phi0approx,
1301  d0approx,
1302  tapprox,
1303  z0approx,
1304  phiprojapprox,
1305  zprojapprox,
1306  phiderapprox,
1307  zderapprox,
1308  phiprojdiskapprox,
1309  rprojdiskapprox,
1310  phiderdiskapprox,
1311  rderdiskapprox);
1312  } else {
1313  rinvapprox = rinv;
1314  phi0approx = phi0;
1315  d0approx = d0;
1316  tapprox = t;
1317  z0approx = z0;
1318 
1319  for (unsigned int i = 0; i < toR_.size(); ++i) {
1320  phiprojapprox[i] = phiproj[i];
1321  zprojapprox[i] = zproj[i];
1322  phiderapprox[i] = phider[i];
1323  zderapprox[i] = zder[i];
1324  }
1325 
1326  for (unsigned int i = 0; i < toZ_.size(); ++i) {
1327  phiprojdiskapprox[i] = phiprojdisk[i];
1328  rprojdiskapprox[i] = rprojdisk[i];
1329  phiderdiskapprox[i] = phiderdisk[i];
1330  rderdiskapprox[i] = rderdisk[i];
1331  }
1332  }
1333 
1334  //store the approcximate results
1335  if (settings_.debugTracklet()) {
1336  edm::LogVerbatim("Tracklet") << "rinvapprox: " << rinvapprox << " rinv: " << rinv << endl;
1337  edm::LogVerbatim("Tracklet") << "phi0approx: " << phi0approx << " phi0: " << phi0 << endl;
1338  edm::LogVerbatim("Tracklet") << "d0approx: " << d0approx << " d0: " << d0 << endl;
1339  edm::LogVerbatim("Tracklet") << "tapprox: " << tapprox << " t: " << t << endl;
1340  edm::LogVerbatim("Tracklet") << "z0approx: " << z0approx << " z0: " << z0 << endl;
1341  }
1342 
1343  for (unsigned int i = 0; i < toR_.size(); ++i) {
1344  if (settings_.debugTracklet()) {
1345  edm::LogVerbatim("Tracklet") << "phiprojapprox[" << i << "]: " << phiprojapprox[i] << " phiproj[" << i
1346  << "]: " << phiproj[i] << endl;
1347  edm::LogVerbatim("Tracklet") << "zprojapprox[" << i << "]: " << zprojapprox[i] << " zproj[" << i
1348  << "]: " << zproj[i] << endl;
1349  edm::LogVerbatim("Tracklet") << "phiderapprox[" << i << "]: " << phiderapprox[i] << " phider[" << i
1350  << "]: " << phider[i] << endl;
1351  edm::LogVerbatim("Tracklet") << "zderapprox[" << i << "]: " << zderapprox[i] << " zder[" << i << "]: " << zder[i]
1352  << endl;
1353  }
1354  }
1355 
1356  for (unsigned int i = 0; i < toZ_.size(); ++i) {
1357  if (settings_.debugTracklet()) {
1358  edm::LogVerbatim("Tracklet") << "phiprojdiskapprox[" << i << "]: " << phiprojdiskapprox[i] << " phiprojdisk[" << i
1359  << "]: " << phiprojdisk[i] << endl;
1360  edm::LogVerbatim("Tracklet") << "rprojdiskapprox[" << i << "]: " << rprojdiskapprox[i] << " rprojdisk[" << i
1361  << "]: " << rprojdisk[i] << endl;
1362  edm::LogVerbatim("Tracklet") << "phiderdiskapprox[" << i << "]: " << phiderdiskapprox[i] << " phiderdisk[" << i
1363  << "]: " << phiderdisk[i] << endl;
1364  edm::LogVerbatim("Tracklet") << "rderdiskapprox[" << i << "]: " << rderdiskapprox[i] << " rderdisk[" << i
1365  << "]: " << rderdisk[i] << endl;
1366  }
1367  }
1368 
1369  //now binary
1370  double krinv = settings_.kphi1() / settings_.kr() * pow(2, settings_.rinv_shift()),
1371  kphi0 = settings_.kphi1() * pow(2, settings_.phi0_shift()),
1372  kt = settings_.kz() / settings_.kr() * pow(2, settings_.t_shift()),
1373  kz0 = settings_.kz() * pow(2, settings_.z0_shift()),
1374  kphiproj = settings_.kphi1() * pow(2, settings_.SS_phiL_shift()),
1375  kphider = settings_.kphi1() / settings_.kr() * pow(2, settings_.SS_phiderL_shift()),
1376  kzproj = settings_.kz() * pow(2, settings_.PS_zL_shift()),
1377  kzder = settings_.kz() / settings_.kr() * pow(2, settings_.PS_zderL_shift()),
1378  kphiprojdisk = settings_.kphi1() * pow(2, settings_.SS_phiD_shift()),
1379  kphiderdisk = settings_.kphi1() / settings_.kr() * pow(2, settings_.SS_phiderD_shift()),
1380  krprojdisk = settings_.kr() * pow(2, settings_.PS_rD_shift()),
1381  krderdisk = settings_.kr() / settings_.kz() * pow(2, settings_.PS_rderD_shift());
1382 
1383  int irinv, iphi0, id0, it, iz0;
1384  int iphiproj[N_LAYER - 2], izproj[N_LAYER - 2], iphider[N_LAYER - 2], izder[N_LAYER - 2];
1385  int iphiprojdisk[N_DISK], irprojdisk[N_DISK], iphiderdisk[N_DISK], irderdisk[N_DISK];
1386 
1387  //store the binary results
1388  irinv = rinvapprox / krinv;
1389  iphi0 = phi0approx / kphi0;
1390  id0 = d0approx / settings_.kd0();
1391  it = tapprox / kt;
1392  iz0 = z0approx / kz0;
1393 
1394  bool success = true;
1395  if (std::abs(rinvapprox) > settings_.rinvcut()) {
1396  if (settings_.debugTracklet())
1397  edm::LogVerbatim("Tracklet") << "TrackletCalculator:: LLD Seeding irinv too large: " << rinvapprox << "(" << irinv
1398  << ")";
1399  success = false;
1400  }
1401  if (std::abs(z0approx) > settings_.disp_z0cut()) {
1402  if (settings_.debugTracklet())
1403  edm::LogVerbatim("Tracklet") << "Failed tracklet z0 cut " << z0approx;
1404  success = false;
1405  }
1406  if (std::abs(d0approx) > settings_.maxd0()) {
1407  if (settings_.debugTracklet())
1408  edm::LogVerbatim("Tracklet") << "Failed tracklet approx d0 cut " << d0approx;
1409  success = false;
1410  }
1411  if (std::abs(d0) > settings_.maxd0()) {
1412  if (settings_.debugTracklet())
1413  edm::LogVerbatim("Tracklet") << "Failed tracklet exact d0 cut " << d0;
1414  success = false;
1415  }
1416 
1417  if (!success)
1418  return false;
1419 
1420  double phicritapprox = phi0approx - asin((0.5 * settings_.rcrit() * rinvapprox) + (d0approx / settings_.rcrit()));
1421  int phicrit = iphi0 - 2 * irinv - 2 * id0;
1422 
1423  int iphicritmincut = settings_.phicritminmc() / globals_->ITC_L1L2()->phi0_final.K();
1424  int iphicritmaxcut = settings_.phicritmaxmc() / globals_->ITC_L1L2()->phi0_final.K();
1425 
1426  bool keepapprox = (phicritapprox > settings_.phicritminmc()) && (phicritapprox < settings_.phicritmaxmc()),
1427  keep = (phicrit > iphicritmincut) && (phicrit < iphicritmaxcut);
1428 
1429  if (settings_.debugTracklet())
1430  if (keep && !keepapprox)
1431  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced::LLDSeeding tracklet kept with exact phicrit cut "
1432  "but not approximate, phicritapprox: "
1433  << phicritapprox;
1434  if (settings_.usephicritapprox()) {
1435  if (!keepapprox)
1436  return false;
1437  } else {
1438  if (!keep)
1439  return false;
1440  }
1441 
1442  Projection projs[N_LAYER + N_DISK];
1443 
1444  for (unsigned int i = 0; i < toR_.size(); ++i) {
1445  iphiproj[i] = phiprojapprox[i] / kphiproj;
1446  izproj[i] = zprojapprox[i] / kzproj;
1447 
1448  iphider[i] = phiderapprox[i] / kphider;
1449  izder[i] = zderapprox[i] / kzder;
1450 
1451  if (izproj[i] < -(1 << (settings_.nzbitsstub(0) - 1)))
1452  continue;
1453  if (izproj[i] >= (1 << (settings_.nzbitsstub(0) - 1)))
1454  continue;
1455 
1456  //this is left from the original....
1457  if (iphiproj[i] >= (1 << settings_.nphibitsstub(5)) - 1)
1458  continue;
1459  if (iphiproj[i] <= 0)
1460  continue;
1461 
1462  if (rproj_[i] < settings_.rPS2S()) {
1463  iphiproj[i] >>= (settings_.nphibitsstub(5) - settings_.nphibitsstub(0));
1464  } else {
1465  izproj[i] >>= (settings_.nzbitsstub(0) - settings_.nzbitsstub(5));
1466  }
1467 
1468  if (rproj_[i] < settings_.rPS2S()) {
1469  if (iphider[i] < -(1 << (settings_.nbitsphiprojderL123() - 1)))
1470  iphider[i] = -(1 << (settings_.nbitsphiprojderL123() - 1));
1471  if (iphider[i] >= (1 << (settings_.nbitsphiprojderL123() - 1)))
1472  iphider[i] = (1 << (settings_.nbitsphiprojderL123() - 1)) - 1;
1473  } else {
1474  if (iphider[i] < -(1 << (settings_.nbitsphiprojderL456() - 1)))
1475  iphider[i] = -(1 << (settings_.nbitsphiprojderL456() - 1));
1476  if (iphider[i] >= (1 << (settings_.nbitsphiprojderL456() - 1)))
1477  iphider[i] = (1 << (settings_.nbitsphiprojderL456() - 1)) - 1;
1478  }
1479 
1480  projs[lproj_[i] - 1].init(settings_,
1481  lproj_[i] - 1,
1482  iphiproj[i],
1483  izproj[i],
1484  iphider[i],
1485  izder[i],
1486  phiproj[i],
1487  zproj[i],
1488  phider[i],
1489  zder[i],
1490  phiprojapprox[i],
1491  zprojapprox[i],
1492  phiderapprox[i],
1493  zderapprox[i],
1494  false);
1495  }
1496 
1497  if (std::abs(it * kt) > 1.0) {
1498  for (unsigned int i = 0; i < toZ_.size(); ++i) {
1499  iphiprojdisk[i] = phiprojdiskapprox[i] / kphiprojdisk;
1500  irprojdisk[i] = rprojdiskapprox[i] / krprojdisk;
1501 
1502  iphiderdisk[i] = phiderdiskapprox[i] / kphiderdisk;
1503  irderdisk[i] = rderdiskapprox[i] / krderdisk;
1504 
1505  //Check phi range of projection
1506  if (iphiprojdisk[i] <= 0)
1507  continue;
1508  if (iphiprojdisk[i] >= (1 << settings_.nphibitsstub(0)) - 1)
1509  continue;
1510 
1511  //Check r range of projection
1512  if (irprojdisk[i] < settings_.rmindisk() / krprojdisk || irprojdisk[i] >= settings_.rmaxdisk() / krprojdisk)
1513  continue;
1514 
1515  projs[N_LAYER + i + 1].init(settings_,
1516  N_LAYER + i + 1,
1517  iphiprojdisk[i],
1518  irprojdisk[i],
1519  iphiderdisk[i],
1520  irderdisk[i],
1521  phiprojdisk[i],
1522  rprojdisk[i],
1523  phiderdisk[i],
1524  rderdisk[i],
1525  phiprojdiskapprox[i],
1526  rprojdiskapprox[i],
1527  phiderdisk[i],
1528  rderdisk[i],
1529  false);
1530  }
1531  }
1532 
1533  if (settings_.writeMonitorData("TrackletPars")) {
1534  globals_->ofstream("trackletpars.txt")
1535  << layer_ << " , " << rinv << " , " << rinvapprox << " , " << phi0 << " , " << phi0approx << " , " << t << " , "
1536  << tapprox << " , " << z0 << " , " << z0approx << " , " << d0 << " , " << d0approx << endl;
1537  }
1538 
1539  Tracklet* tracklet = new Tracklet(settings_,
1540  iSeed_,
1541  innerFPGAStub,
1542  middleFPGAStub,
1543  outerFPGAStub,
1544  rinv,
1545  phi0,
1546  d0,
1547  z0,
1548  t,
1549  rinvapprox,
1550  phi0approx,
1551  d0approx,
1552  z0approx,
1553  tapprox,
1554  irinv,
1555  iphi0,
1556  id0,
1557  iz0,
1558  it,
1559  projs,
1560  false);
1561 
1562  if (settings_.debugTracklet())
1563  edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced " << getName()
1564  << " Found LLD tracklet in sector = " << iSector_ << " phi0 = " << phi0;
1565 
1567  tracklet->setTCIndex(TCIndex_);
1568 
1569  if (settings_.writeMonitorData("Seeds")) {
1570  ofstream fout("seeds.txt", ofstream::app);
1571  fout << __FILE__ << ":" << __LINE__ << " " << name_ << "_" << iSector_ << " " << tracklet->getISeed() << endl;
1572  fout.close();
1573  }
1574  trackletpars_->addTracklet(tracklet);
1575 
1576  for (unsigned int j = 0; j < toR_.size(); j++) {
1577  if (settings_.debugTracklet())
1578  edm::LogVerbatim("Tracklet") << "adding layer projection " << j << "/" << toR_.size() << " " << lproj_[j];
1579  if (tracklet->validProj(lproj_[j] - 1)) {
1580  addLayerProj(tracklet, lproj_[j]);
1581  }
1582  }
1583 
1584  for (unsigned int j = 0; j < toZ_.size(); j++) {
1585  int disk = dproj_[j];
1586  if (disk == 0)
1587  continue;
1588  if (it < 0)
1589  disk = -disk;
1590  if (settings_.debugTracklet())
1591  edm::LogVerbatim("Tracklet") << "adding disk projection " << j << "/" << toZ_.size() << " " << disk;
1592  if (tracklet->validProj(N_LAYER + abs(disk) - 1)) {
1593  addDiskProj(tracklet, disk);
1594  }
1595  }
1596 
1597  return true;
1598 }
1599 
1601  double rinv,
1602  double phi0,
1603  double d0,
1604  double t,
1605  double z0,
1606  double r0,
1607  double& phiproj,
1608  double& zproj,
1609  double& phider,
1610  double& zder) {
1611  double rho = 1 / rinv;
1612  if (rho < 0) {
1613  r0 = -r0;
1614  }
1615  phiproj = phi0 - asin((rproj * rproj + r0 * r0 - rho * rho) / (2 * rproj * r0));
1616  double beta = acos((rho * rho + r0 * r0 - rproj * rproj) / (2 * r0 * rho));
1617  zproj = z0 + t * std::abs(rho * beta);
1618 
1619  //not exact, but close
1620  phider = -0.5 * rinv / sqrt(1 - pow(0.5 * rproj * rinv, 2)) + d0 / (rproj * rproj);
1621  zder = t / sqrt(1 - pow(0.5 * rproj * rinv, 2));
1622 
1623  if (settings_.debugTracklet())
1624  edm::LogVerbatim("Tracklet") << "exact proj layer at " << rproj << " : " << phiproj << " " << zproj;
1625 }
1626 
1628  double rinv,
1629  double,
1630  double, //phi0 and d0 are not used.
1631  double t,
1632  double z0,
1633  double x0,
1634  double y0,
1635  double& phiproj,
1636  double& rproj,
1637  double& phider,
1638  double& rder) {
1639  //protect against t=0
1640  if (std::abs(t) < 0.1)
1641  t = 0.1;
1642  if (t < 0)
1643  zproj = -zproj;
1644  double rho = std::abs(1 / rinv);
1645  double beta = (zproj - z0) / (t * rho);
1646  double phiV = atan2(-y0, -x0);
1647  double c = rinv > 0 ? -1 : 1;
1648 
1649  double x = x0 + rho * cos(phiV + c * beta);
1650  double y = y0 + rho * sin(phiV + c * beta);
1651 
1652  phiproj = atan2(y, x);
1653 
1654  phiproj = reco::reduceRange(phiproj - phimin_);
1655 
1656  rproj = sqrt(x * x + y * y);
1657 
1658  phider = c / t / (x * x + y * y) * (rho + x0 * cos(phiV + c * beta) + y0 * sin(phiV + c * beta));
1659  rder = c / t / rproj * (y0 * cos(phiV + c * beta) - x0 * sin(phiV + c * beta));
1660 
1661  if (settings_.debugTracklet())
1662  edm::LogVerbatim("Tracklet") << "exact proj disk at" << zproj << " : " << phiproj << " " << rproj;
1663 }
1664 
1666  double z1,
1667  double phi1,
1668  double r2,
1669  double z2,
1670  double phi2,
1671  double r3,
1672  double z3,
1673  double phi3,
1674  int take3,
1675  double& rinv,
1676  double& phi0,
1677  double& d0,
1678  double& t,
1679  double& z0,
1680  double phiproj[N_LAYER - 2],
1681  double zproj[N_LAYER - 2],
1682  double phiprojdisk[N_DISK],
1683  double rprojdisk[N_DISK],
1684  double phider[N_LAYER - 2],
1685  double zder[N_LAYER - 2],
1686  double phiderdisk[N_DISK],
1687  double rderdisk[N_DISK]) {
1688  //two lines perpendicular to the 1->2 and 2->3
1689  double x1 = r1 * cos(phi1);
1690  double x2 = r2 * cos(phi2);
1691  double x3 = r3 * cos(phi3);
1692 
1693  double y1 = r1 * sin(phi1);
1694  double y2 = r2 * sin(phi2);
1695  double y3 = r3 * sin(phi3);
1696 
1697  double dy21 = y2 - y1;
1698  double dy32 = y3 - y2;
1699 
1700  //Hack to protect against dividing by zero
1701  //code should be rewritten to avoid this
1702  if (dy21 == 0.0)
1703  dy21 = 1e-9;
1704  if (dy32 == 0.0)
1705  dy32 = 1e-9;
1706 
1707  double k1 = -(x2 - x1) / dy21;
1708  double k2 = -(x3 - x2) / dy32;
1709  double b1 = 0.5 * (y2 + y1) - 0.5 * (x1 + x2) * k1;
1710  double b2 = 0.5 * (y3 + y2) - 0.5 * (x2 + x3) * k2;
1711  //their intersection gives the center of the circle
1712  double y0 = (b1 * k2 - b2 * k1) / (k2 - k1);
1713  double x0 = (b1 - b2) / (k2 - k1);
1714  //get the radius three ways:
1715  double R1 = sqrt(pow(x1 - x0, 2) + pow(y1 - y0, 2));
1716  double R2 = sqrt(pow(x2 - x0, 2) + pow(y2 - y0, 2));
1717  double R3 = sqrt(pow(x3 - x0, 2) + pow(y3 - y0, 2));
1718  //check if the same
1719  double eps1 = std::abs(R1 / R2 - 1);
1720  double eps2 = std::abs(R3 / R2 - 1);
1721  if (eps1 > 1e-10 || eps2 > 1e-10)
1722  edm::LogVerbatim("Tracklet") << "&&&&&&&&&&&& bad circle! " << R1 << "\t" << R2 << "\t" << R3;
1723 
1724  if (settings_.debugTracklet())
1725  edm::LogVerbatim("Tracklet") << "phimin_: " << phimin_ << " phimax_: " << phimax_;
1726  //results
1727  rinv = 1. / R1;
1728  phi0 = 0.5 * M_PI + atan2(y0, x0);
1729 
1730  phi0 -= phimin_;
1731 
1732  d0 = -R1 + sqrt(x0 * x0 + y0 * y0);
1733  //sign of rinv:
1734  double dphi = reco::reduceRange(phi3 - atan2(y0, x0));
1735  if (dphi < 0) {
1736  rinv = -rinv;
1737  d0 = -d0;
1738  phi0 = phi0 + M_PI;
1739  }
1740  phi0 = angle0to2pi::make0To2pi(phi0);
1741 
1742  //now in RZ:
1743  //turning angle
1744  double beta1 = reco::reduceRange(atan2(y1 - y0, x1 - x0) - atan2(-y0, -x0));
1745  double beta2 = reco::reduceRange(atan2(y2 - y0, x2 - x0) - atan2(-y0, -x0));
1746  double beta3 = reco::reduceRange(atan2(y3 - y0, x3 - x0) - atan2(-y0, -x0));
1747 
1748  double t12 = (z2 - z1) / std::abs(beta2 - beta1) / R1;
1749  double z12 = (z1 * beta2 - z2 * beta1) / (beta2 - beta1);
1750  double t13 = (z3 - z1) / std::abs(beta3 - beta1) / R1;
1751  double z13 = (z1 * beta3 - z3 * beta1) / (beta3 - beta1);
1752 
1753  if (take3 > 0) {
1754  //take 13 (large lever arm)
1755  t = t13;
1756  z0 = z13;
1757  } else {
1758  //take 12 (pixel layers)
1759  t = t12;
1760  z0 = z12;
1761  }
1762 
1763  for (unsigned int i = 0; i < toR_.size(); i++) {
1764  exactproj(toR_[i], rinv, phi0, d0, t, z0, sqrt(x0 * x0 + y0 * y0), phiproj[i], zproj[i], phider[i], zder[i]);
1765  }
1766 
1767  for (unsigned int i = 0; i < toZ_.size(); i++) {
1768  exactprojdisk(toZ_[i], rinv, phi0, d0, t, z0, x0, y0, phiprojdisk[i], rprojdisk[i], phiderdisk[i], rderdisk[i]);
1769  }
1770 
1771  if (settings_.debugTracklet())
1772  edm::LogVerbatim("Tracklet") << "exact tracklet: " << rinv << " " << phi0 << " " << t << " " << z0 << " " << d0;
1773 }
1774 
1776  double phi0,
1777  double d0,
1778  double t,
1779  double z0,
1780  double halfRinv_0,
1781  double d0_0, // zeroeth order result for higher order terms calculation
1782  double rmean,
1783  double& phiproj,
1784  double& phiprojder,
1785  double& zproj,
1786  double& zprojder) {
1787  if (std::abs(2.0 * halfRinv) > settings_.rinvcut() || std::abs(z0) > settings_.disp_z0cut() ||
1788  std::abs(d0) > settings_.maxd0()) {
1789  phiproj = 0.0;
1790  return;
1791  }
1792  double rmeanInv = 1.0 / rmean;
1793 
1794  phiproj = phi0 + rmean * (-halfRinv + 2.0 * d0_0 * halfRinv_0 * halfRinv_0) +
1795  rmeanInv * (-d0 + halfRinv_0 * d0_0 * d0_0) + sixth * pow(-rmean * halfRinv_0 - rmeanInv * d0_0, 3);
1796  phiprojder = -halfRinv + d0 * rmeanInv * rmeanInv; //removed all high terms
1797 
1798  zproj = z0 + t * rmean - 0.5 * rmeanInv * t * d0_0 * d0_0 - t * rmean * halfRinv * d0 +
1799  sixth * pow(rmean, 3) * t * halfRinv_0 * halfRinv_0;
1800  zprojder = t; // removed all high terms
1801 
1802  phiproj = angle0to2pi::make0To2pi(phiproj);
1803 
1804  if (settings_.debugTracklet())
1805  edm::LogVerbatim("Tracklet") << "approx proj layer at " << rmean << " : " << phiproj << " " << zproj << endl;
1806 }
1807 
1809  double phi0,
1810  double d0,
1811  double t,
1812  double z0,
1813  double halfRinv_0,
1814  double d0_0, // zeroeth order result for higher order terms calculation
1815  double zmean,
1816  double& phiproj,
1817  double& phiprojder,
1818  double& rproj,
1819  double& rprojder) {
1820  if (std::abs(2.0 * halfRinv) > settings_.rinvcut() || std::abs(z0) > settings_.disp_z0cut() ||
1821  std::abs(d0) > settings_.maxd0()) {
1822  phiproj = 0.0;
1823  return;
1824  }
1825 
1826  if (t < 0)
1827  zmean = -zmean;
1828 
1829  double zmeanInv = 1.0 / zmean, rstar = (zmean - z0) / t,
1830  epsilon = 0.5 * zmeanInv * zmeanInv * d0_0 * d0_0 * t * t + halfRinv * d0 -
1831  sixth * rstar * rstar * halfRinv_0 * halfRinv_0;
1832 
1833  rproj = rstar * (1 + epsilon);
1834  rprojder = 1 / t;
1835 
1836  double A = rproj * halfRinv;
1837  double B = -d0 * t * zmeanInv * (1 + z0 * zmeanInv) * (1 - epsilon);
1838  double C = -d0 * halfRinv;
1839  double A_0 = rproj * halfRinv_0;
1840  double B_0 = -d0_0 * t * zmeanInv * (1 + z0 * zmeanInv) * (1 - epsilon);
1841  // double C_0 = -d0_0 * halfRinv_0;
1842 
1843  phiproj = phi0 - A + B * (1 + C - 2 * A_0 * A_0) + sixth * pow(-A_0 + B_0, 3);
1844  phiprojder = -halfRinv / t + d0 * t * zmeanInv * zmeanInv;
1845 
1846  phiproj = angle0to2pi::make0To2pi(phiproj);
1847 
1848  if (settings_.debugTracklet())
1849  edm::LogVerbatim("Tracklet") << "approx proj disk at" << zmean << " : " << phiproj << " " << rproj << endl;
1850 }
1851 
1853  double z1,
1854  double phi1,
1855  double r2,
1856  double z2,
1857  double phi2,
1858  double r3,
1859  double z3,
1860  double phi3,
1861  bool take3,
1862  unsigned ndisks,
1863  double& rinv,
1864  double& phi0,
1865  double& d0,
1866  double& t,
1867  double& z0,
1868  double phiproj[4],
1869  double zproj[4],
1870  double phider[4],
1871  double zder[4],
1872  double phiprojdisk[5],
1873  double rprojdisk[5],
1874  double phiderdisk[5],
1875  double rderdisk[5]) {
1876  double a = 1.0 / ((r1 - r2) * (r1 - r3));
1877  double b = 1.0 / ((r1 - r2) * (r2 - r3));
1878  double c = 1.0 / ((r1 - r3) * (r2 - r3));
1879 
1880  // first iteration in r-phi plane
1881  double halfRinv_0 = -phi1 * r1 * a + phi2 * r2 * b - phi3 * r3 * c;
1882  double d0_0 = r1 * r2 * r3 * (-phi1 * a + phi2 * b - phi3 * c);
1883 
1884  // corrections to phi1, phi2, and phi3
1885  double r = r2, z = z2;
1886  if (take3)
1887  r = r3, z = z3;
1888 
1889  double d0OverR1 = d0_0 * rzmeanInv_[0] * (ndisks > 2 ? std::abs((z - z1) / (r - r1)) : 1.0);
1890  double d0OverR2 = d0_0 * rzmeanInv_[1] * (ndisks > 1 ? std::abs((z - z1) / (r - r1)) : 1.0);
1891  double d0OverR3 = d0_0 * rzmeanInv_[2] * (ndisks > 0 ? std::abs((z - z1) / (r - r1)) : 1.0);
1892 
1893  double d0OverR = d0OverR2;
1894  if (take3)
1895  d0OverR = d0OverR3;
1896 
1897  double c1 = d0_0 * halfRinv_0 * d0OverR1 + 2.0 * d0_0 * halfRinv_0 * r1 * halfRinv_0 +
1898  sixth * pow(-r1 * halfRinv_0 - d0OverR1, 3);
1899  double c2 = d0_0 * halfRinv_0 * d0OverR2 + 2.0 * d0_0 * halfRinv_0 * r2 * halfRinv_0 +
1900  sixth * pow(-r2 * halfRinv_0 - d0OverR2, 3);
1901  double c3 = d0_0 * halfRinv_0 * d0OverR3 + 2.0 * d0_0 * halfRinv_0 * r3 * halfRinv_0 +
1902  sixth * pow(-r3 * halfRinv_0 - d0OverR3, 3);
1903 
1904  double phi1c = phi1 - c1;
1905  double phi2c = phi2 - c2;
1906  double phi3c = phi3 - c3;
1907 
1908  // second iteration in r-phi plane
1909  double halfRinv = -phi1c * r1 * a + phi2c * r2 * b - phi3c * r3 * c;
1910  phi0 = -phi1c * r1 * (r2 + r3) * a + phi2c * r2 * (r1 + r3) * b - phi3c * r3 * (r1 + r2) * c;
1911  d0 = r1 * r2 * r3 * (-phi1c * a + phi2c * b - phi3c * c);
1912 
1913  t = ((z - z1) / (r - r1)) *
1914  (1. + d0 * halfRinv - 0.5 * d0OverR1 * d0OverR - sixth * (r1 * r1 + r2 * r2 + r1 * r2) * halfRinv_0 * halfRinv_0);
1915  z0 = z1 - t * r1 * (1.0 - d0_0 * halfRinv_0 - 0.5 * d0OverR1 * d0OverR1 + sixth * r1 * r1 * halfRinv_0 * halfRinv_0);
1916 
1917  rinv = 2.0 * halfRinv;
1918  phi0 += -phimin_;
1919 
1920  phi0 = angle0to2pi::make0To2pi(phi0);
1921 
1922  for (unsigned int i = 0; i < toR_.size(); i++) {
1923  approxproj(halfRinv,
1924  phi0,
1925  d0,
1926  t,
1927  z0,
1928  halfRinv_0,
1929  d0_0, // added _0 version for high term calculations
1930  toR_.at(i),
1931  phiproj[i],
1932  phider[i],
1933  zproj[i],
1934  zder[i]);
1935  }
1936 
1937  for (unsigned int i = 0; i < toZ_.size(); i++) {
1938  approxprojdisk(halfRinv,
1939  phi0,
1940  d0,
1941  t,
1942  z0,
1943  halfRinv_0,
1944  d0_0, // added _0 version for high term calculations
1945  toZ_.at(i),
1946  phiprojdisk[i],
1947  phiderdisk[i],
1948  rprojdisk[i],
1949  rderdisk[i]);
1950  }
1951 
1952  if (std::abs(rinv) > settings_.rinvcut() || std::abs(z0) > settings_.disp_z0cut() ||
1953  std::abs(d0) > settings_.maxd0()) {
1954  phi0 = 0.0;
1955  return;
1956  }
1957 
1958  if (settings_.debugTracklet())
1959  edm::LogVerbatim("Tracklet") << "TCD approx tracklet: " << rinv << " " << phi0 << " " << t << " " << z0 << " " << d0
1960  << endl;
1961 }
Log< level::Info, true > LogVerbatim
double kz() const
Definition: Settings.h:342
double phi() const
Definition: L1TStub.h:65
int PS_rD_shift() const
Definition: Settings.h:397
int PS_zL_shift() const
Definition: Settings.h:389
double zapprox() const
Definition: Stub.cc:166
std::vector< AllStubsMemory * > middleallstubs_
constexpr int N_DISK
Definition: Settings.h:26
unsigned int maxStep(std::string module) const
Definition: Settings.h:125
int PS_rderD_shift() const
Definition: Settings.h:400
int PS_zderL_shift() const
Definition: Settings.h:392
std::vector< StubTripletsMemory * > stubtriplets_
double rPS2S() const
Definition: Settings.h:362
std::string name_
Definition: ProcessBase.h:38
Definition: APVGainStruct.h:7
double kphi1() const
Definition: Settings.h:339
void exactproj(double rproj, double rinv, double phi0, double d0, double t, double z0, double r0, double &phiproj, double &zproj, double &phider, double &zder)
constexpr T reduceRange(T x)
Definition: deltaPhi.h:18
bool addLayerProj(Tracklet *tracklet, int layer)
double phiapprox(double phimin, double) const
Definition: Stub.cc:185
std::vector< AllStubsMemory * > innerallstubs_
void addInput(MemoryBase *memory, std::string input) override
double disp_z0cut() const
Definition: Settings.h:366
double phicritmaxmc() const
Definition: Settings.h:335
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
double rmindisk() const
Definition: Settings.h:135
bool isBarrel() const
Definition: Stub.h:87
double maxd0() const
Definition: Settings.h:348
double zlength() const
Definition: Settings.h:133
Projection & proj(int layerdisk)
Definition: Tracklet.h:87
Settings const & settings_
Definition: ProcessBase.h:40
void addProjection(int layer, int iphi, TrackletProjectionsMemory *trackletprojs, Tracklet *tracklet)
double z() const
Definition: L1TStub.h:59
Globals * globals_
Definition: ProcessBase.h:41
double rmindiskvm() const
Definition: Settings.h:355
bool writetrace() const
Definition: Settings.h:195
int phi0_shift() const
Definition: Settings.h:381
assert(be >=bs)
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:31
double phicritminmc() const
Definition: Settings.h:334
const FPGAWord & fpgaphiproj() const
Definition: Projection.h:39
static std::string const input
Definition: EdmProvDump.cc:50
const FPGAWord & disk() const
Definition: Stub.h:74
bool warnNoMem() const
Definition: Settings.h:197
void exactprojdisk(double zproj, double rinv, double, double, double t, double z0, double x0, double y0, double &phiproj, double &rproj, double &phider, double &rder)
bool LLDSeeding(const Stub *innerFPGAStub, const L1TStub *innerStub, const Stub *middleFPGAStub, const L1TStub *middleStub, const Stub *outerFPGAStub, const L1TStub *outerStub)
std::vector< std::vector< TrackletProjectionsMemory * > > trackletprojlayers_
void init(Settings const &settings, unsigned int layerdisk, int iphiproj, int irzproj, int iphider, int irzder, double phiproj, double rzproj, double phiprojder, double rzprojder, double phiprojapprox, double rzprojapprox, double phiprojderapprox, double rzprojderapprox, bool isPSseed)
Definition: Projection.cc:11
unsigned int nbitsphiprojderL456() const
Definition: Settings.h:97
void exacttracklet(double r1, double z1, double phi1, double r2, double z2, double phi2, double r3, double z3, double phi3, int take3, double &rinv, double &phi0, double &d0, double &t, double &z0, double phiproj[N_LAYER - 2], double zproj[N_LAYER - 2], double phiprojdisk[N_DISK], double rprojdisk[N_DISK], double phider[N_LAYER - 2], double zder[N_LAYER - 2], double phiderdisk[N_DISK], double rderdisk[N_DISK])
unsigned int nzbitsstub(unsigned int layerdisk) const
Definition: Settings.h:91
unsigned int nphibitsstub(unsigned int layerdisk) const
Definition: Settings.h:92
double rmean(unsigned int iLayer) const
Definition: Settings.h:173
int t_shift() const
Definition: Settings.h:382
void approxprojdisk(double halfRinv, double phi0, double d0, double t, double z0, double halfRinv_0, double d0_0, double zmean, double &phiproj, double &phiprojder, double &rproj, double &rprojder)
T sqrt(T t)
Definition: SSEVec.h:19
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
bool LLLSeeding(const Stub *innerFPGAStub, const L1TStub *innerStub, const Stub *middleFPGAStub, const L1TStub *middleStub, const Stub *outerFPGAStub, const L1TStub *outerStub)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
IMATH_TrackletCalculator * ITC_L1L2()
Definition: Globals.h:48
int value() const
Definition: FPGAWord.h:24
void execute(unsigned int iSector, double phimin, double phimax)
double rinvcut() const
Definition: Settings.h:222
L1TStub * l1tstub()
Definition: Stub.h:83
std::string const & getName() const
Definition: MemoryBase.h:19
std::vector< std::vector< TrackletProjectionsMemory * > > trackletprojdisks_
int z0_shift() const
Definition: Settings.h:383
void approxtracklet(double r1, double z1, double phi1, double r2, double z2, double phi2, double r3, double z3, double phi3, bool take3, unsigned ndisks, double &rinv, double &phi0, double &d0, double &t, double &z0, double phiproj[4], double zproj[4], double phider[4], double zder[4], double phiprojdisk[5], double rprojdisk[5], double phiderdisk[5], double rderdisk[5])
bool writeMonitorData(std::string module) const
Definition: Settings.h:118
unsigned int layerdisk() const
Definition: Stub.cc:193
double rapprox() const
Definition: Stub.cc:152
double zmean(unsigned int iDisk) const
Definition: Settings.h:176
void setTCIndex(int index)
Definition: Tracklet.h:210
#define M_PI
double K() const
Definition: imath.h:247
bias2_t b2[25]
Definition: b2.h:9
int SS_phiD_shift() const
Definition: Settings.h:396
unsigned int nallstubs(unsigned int layerdisk) const
Definition: Settings.h:116
bool debugTracklet() const
Definition: Settings.h:194
int SS_phiderD_shift() const
Definition: Settings.h:399
double kr() const
Definition: Settings.h:344
static constexpr float d0
bool DDLSeeding(const Stub *innerFPGAStub, const L1TStub *innerStub, const Stub *middleFPGAStub, const L1TStub *middleStub, const Stub *outerFPGAStub, const L1TStub *outerStub)
void approxproj(double halfRinv, double phi0, double d0, double t, double z0, double halfRinv_0, double d0_0, double rmean, double &phiproj, double &phiprojder, double &zproj, double &zprojder)
double rmaxdisk() const
Definition: Settings.h:134
int nbits() const
Definition: FPGAWord.h:25
void addDiskProj(Tracklet *tracklet, int disk)
void setTrackletIndex(unsigned int index)
Definition: Tracklet.cc:806
double kd0() const
Definition: Settings.h:351
double rinv(double phi1, double phi2, double r1, double r2)
Definition: Util.h:66
int getISeed() const
Definition: Tracklet.cc:811
double b
Definition: hdecay.h:120
constexpr valType make0To2pi(valType angle)
Definition: deltaPhi.h:67
int SS_phiderL_shift() const
Definition: Settings.h:391
unsigned int nbitsphiprojderL123() const
Definition: Settings.h:96
const FPGAWord & fpgarzproj() const
Definition: Projection.h:44
constexpr double sixth
Definition: Settings.h:45
double a
Definition: hdecay.h:121
double r() const
Definition: L1TStub.h:60
int rinv_shift() const
Definition: Settings.h:380
std::ofstream & ofstream(std::string fname)
Definition: Globals.cc:44
float x
unsigned int ntrackletmax() const
Definition: Settings.h:371
Definition: output.py:1
std::vector< AllStubsMemory * > outerallstubs_
Definition: APVGainStruct.h:7
double krprojshiftdisk() const
Definition: Settings.h:441
void addOutputProjection(TrackletProjectionsMemory *&outputProj, MemoryBase *memory)
const FPGAWord & layer() const
Definition: Stub.h:73
tmp
align.sh
Definition: createJobs.py:716
bool usephicritapprox() const
Definition: Settings.h:248
std::string const & getName() const
Definition: ProcessBase.h:22
int SS_phiL_shift() const
Definition: Settings.h:388
void addProjectionDisk(int disk, int iphi, TrackletProjectionsMemory *trackletprojs, Tracklet *tracklet)
bool useapprox() const
Definition: Settings.h:247
void addOutput(MemoryBase *memory, std::string output) override
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
bool validProj(int layerdisk) const
Definition: Tracklet.h:82
static constexpr float b1
bool atExtreme() const
Definition: FPGAWord.cc:79
constexpr int N_LAYER
Definition: Settings.h:25
double rcrit() const
Definition: Settings.h:327