Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-07 22:33:37

0001 #include "L1Trigger/TrackFindingTracklet/interface/TrackletCalculatorBase.h"
0002 #include "L1Trigger/TrackFindingTracklet/interface/Tracklet.h"
0003 #include "L1Trigger/TrackFindingTracklet/interface/Stub.h"
0004 #include "L1Trigger/TrackFindingTracklet/interface/Globals.h"
0005 #include "L1Trigger/TrackFindingTracklet/interface/HistBase.h"
0006 #include "L1Trigger/TrackFindingTracklet/interface/IMATH_TrackletCalculator.h"
0007 #include "L1Trigger/TrackFindingTracklet/interface/IMATH_TrackletCalculatorDisk.h"
0008 #include "L1Trigger/TrackFindingTracklet/interface/IMATH_TrackletCalculatorOverlap.h"
0009 
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include "FWCore/Utilities/interface/Exception.h"
0012 #include "DataFormats/Math/interface/deltaPhi.h"
0013 
0014 using namespace std;
0015 using namespace trklet;
0016 
0017 TrackletCalculatorBase::TrackletCalculatorBase(string name, Settings const& settings, Globals* global)
0018     : ProcessBase(name, settings, global) {}
0019 
0020 void TrackletCalculatorBase::exacttracklet(double r1,
0021                                            double z1,
0022                                            double phi1,
0023                                            double r2,
0024                                            double z2,
0025                                            double phi2,
0026                                            double,
0027                                            double& rinv,
0028                                            double& phi0,
0029                                            double& t,
0030                                            double& z0,
0031                                            double phiproj[N_LAYER - 2],
0032                                            double zproj[N_LAYER - 2],
0033                                            double phider[N_LAYER - 2],
0034                                            double zder[N_LAYER - 2],
0035                                            double phiprojdisk[N_DISK],
0036                                            double rprojdisk[N_DISK],
0037                                            double phiderdisk[N_DISK],
0038                                            double rderdisk[N_DISK]) {
0039   double deltaphi = reco::reduceRange(phi1 - phi2);
0040 
0041   double dist = sqrt(r2 * r2 + r1 * r1 - 2 * r1 * r2 * cos(deltaphi));
0042 
0043   rinv = 2 * sin(deltaphi) / dist;
0044 
0045   double phi1tmp = phi1 - phimin_;
0046 
0047   phi0 = reco::reduceRange(phi1tmp + asin(0.5 * r1 * rinv));
0048 
0049   double rhopsi1 = 2 * asin(0.5 * r1 * rinv) / rinv;
0050   double rhopsi2 = 2 * asin(0.5 * r2 * rinv) / rinv;
0051 
0052   t = (z1 - z2) / (rhopsi1 - rhopsi2);
0053 
0054   z0 = z1 - t * rhopsi1;
0055 
0056   for (unsigned int i = 0; i < N_LAYER - 2; i++) {
0057     exactproj(settings_.rmean(settings_.projlayers(iSeed_, i) - 1),
0058               rinv,
0059               phi0,
0060               t,
0061               z0,
0062               phiproj[i],
0063               zproj[i],
0064               phider[i],
0065               zder[i]);
0066   }
0067 
0068   for (unsigned int i = 0; i < N_DISK; i++) {
0069     exactprojdisk(settings_.zmean(i), rinv, phi0, t, z0, phiprojdisk[i], rprojdisk[i], phiderdisk[i], rderdisk[i]);
0070   }
0071 }
0072 
0073 void TrackletCalculatorBase::exacttrackletdisk(double r1,
0074                                                double z1,
0075                                                double phi1,
0076                                                double r2,
0077                                                double z2,
0078                                                double phi2,
0079                                                double,
0080                                                double& rinv,
0081                                                double& phi0,
0082                                                double& t,
0083                                                double& z0,
0084                                                double phiprojLayer[N_PSLAYER],  //=3 (project to PS barrel layers only)
0085                                                double zprojLayer[N_PSLAYER],
0086                                                double phiderLayer[N_PSLAYER],
0087                                                double zderLayer[N_PSLAYER],
0088                                                double phiproj[N_DISK - 2],  //=3 (max project to 3 other disks)
0089                                                double rproj[N_DISK - 2],
0090                                                double phider[N_DISK - 2],
0091                                                double rder[N_DISK - 2]) {
0092   double deltaphi = reco::reduceRange(phi1 - phi2);
0093 
0094   double dist = sqrt(r2 * r2 + r1 * r1 - 2 * r1 * r2 * cos(deltaphi));
0095 
0096   rinv = 2 * sin(deltaphi) / dist;
0097 
0098   double phi1tmp = phi1 - phimin_;
0099 
0100   phi0 = reco::reduceRange(phi1tmp + asin(0.5 * r1 * rinv));
0101 
0102   double rhopsi1 = 2 * asin(0.5 * r1 * rinv) / rinv;
0103   double rhopsi2 = 2 * asin(0.5 * r2 * rinv) / rinv;
0104 
0105   t = (z1 - z2) / (rhopsi1 - rhopsi2);
0106 
0107   z0 = z1 - t * rhopsi1;
0108 
0109   for (unsigned int i = 0; i < N_DISK - 2; i++) {
0110     exactprojdisk(settings_.zmean(settings_.projdisks(iSeed_, i) - 1),
0111                   rinv,
0112                   phi0,
0113                   t,
0114                   z0,
0115                   phiproj[i],
0116                   rproj[i],
0117                   phider[i],
0118                   rder[i]);
0119   }
0120 
0121   for (unsigned int i = 0; i < N_DISK - 2; i++) {
0122     exactproj(settings_.rmean(i), rinv, phi0, t, z0, phiprojLayer[i], zprojLayer[i], phiderLayer[i], zderLayer[i]);
0123   }
0124 }
0125 
0126 void TrackletCalculatorBase::exacttrackletOverlap(double r1,
0127                                                   double z1,
0128                                                   double phi1,
0129                                                   double r2,
0130                                                   double z2,
0131                                                   double phi2,
0132                                                   double,
0133                                                   double& rinv,
0134                                                   double& phi0,
0135                                                   double& t,
0136                                                   double& z0,
0137                                                   double phiprojLayer[N_PSLAYER],
0138                                                   double zprojLayer[N_PSLAYER],
0139                                                   double phiderLayer[N_PSLAYER],
0140                                                   double zderLayer[N_PSLAYER],
0141                                                   double phiproj[N_DISK - 2],
0142                                                   double rproj[N_DISK - 2],
0143                                                   double phider[N_DISK - 2],
0144                                                   double rder[N_DISK - 2]) {
0145   double deltaphi = reco::reduceRange(phi1 - phi2);
0146 
0147   double dist = sqrt(r2 * r2 + r1 * r1 - 2 * r1 * r2 * cos(deltaphi));
0148 
0149   rinv = 2 * sin(deltaphi) / dist;
0150 
0151   if (r1 > r2)
0152     rinv = -rinv;
0153 
0154   double phi1tmp = phi1 - phimin_;
0155 
0156   phi0 = reco::reduceRange(phi1tmp + asin(0.5 * r1 * rinv));
0157 
0158   double rhopsi1 = 2 * asin(0.5 * r1 * rinv) / rinv;
0159   double rhopsi2 = 2 * asin(0.5 * r2 * rinv) / rinv;
0160 
0161   t = (z1 - z2) / (rhopsi1 - rhopsi2);
0162 
0163   z0 = z1 - t * rhopsi1;
0164 
0165   for (int i = 0; i < 4; i++) {
0166     exactprojdisk(settings_.zmean(i + 1), rinv, phi0, t, z0, phiproj[i], rproj[i], phider[i], rder[i]);
0167   }
0168 
0169   for (int i = 0; i < 1; i++) {
0170     exactproj(settings_.rmean(i), rinv, phi0, t, z0, phiprojLayer[i], zprojLayer[i], phiderLayer[i], zderLayer[i]);
0171   }
0172 }
0173 
0174 void TrackletCalculatorBase::exactproj(double rproj,
0175                                        double rinv,
0176                                        double phi0,
0177                                        double t,
0178                                        double z0,
0179                                        double& phiproj,
0180                                        double& zproj,
0181                                        double& phider,
0182                                        double& zder) {
0183   phiproj = phi0 - asin(0.5 * rproj * rinv);
0184   zproj = z0 + (2 * t / rinv) * asin(0.5 * rproj * rinv);
0185 
0186   phider = -0.5 * rinv / sqrt(1 - pow(0.5 * rproj * rinv, 2));
0187   zder = t / sqrt(1 - pow(0.5 * rproj * rinv, 2));
0188 }
0189 
0190 void TrackletCalculatorBase::exactprojdisk(double zproj,
0191                                            double rinv,
0192                                            double phi0,
0193                                            double t,
0194                                            double z0,
0195                                            double& phiproj,
0196                                            double& rproj,
0197                                            double& phider,
0198                                            double& rder) {
0199   if (t < 0)
0200     zproj = -zproj;
0201 
0202   double tmp = rinv * (zproj - z0) / (2.0 * t);
0203   rproj = (2.0 / rinv) * sin(tmp);
0204   phiproj = phi0 - tmp;
0205 
0206   phider = -rinv / (2 * t);
0207   rder = cos(tmp) / t;
0208 }
0209 
0210 void TrackletCalculatorBase::addDiskProj(Tracklet* tracklet, int disk) {
0211   disk = std::abs(disk);
0212 
0213   FPGAWord fpgar = tracklet->proj(N_LAYER + disk - 1).fpgarzproj();
0214 
0215   if (fpgar.value() * settings_.krprojshiftdisk() < settings_.rmindiskvm())
0216     return;
0217   if (fpgar.value() * settings_.krprojshiftdisk() > settings_.rmaxdisk())
0218     return;
0219 
0220   FPGAWord fpgaphi = tracklet->proj(N_LAYER + disk - 1).fpgaphiproj();
0221 
0222   int iphivmRaw = fpgaphi.value() >> (fpgaphi.nbits() - 5);
0223 
0224   int iphi = iphivmRaw / (32 / settings_.nallstubs(disk + N_LAYER - 1));
0225 
0226   addProjectionDisk(disk, iphi, trackletprojdisks_[disk - 1][iphi], tracklet);
0227 }
0228 
0229 bool TrackletCalculatorBase::addLayerProj(Tracklet* tracklet, int layer) {
0230   assert(layer > 0);
0231 
0232   FPGAWord fpgaz = tracklet->proj(layer - 1).fpgarzproj();
0233   FPGAWord fpgaphi = tracklet->proj(layer - 1).fpgaphiproj();
0234 
0235   if (fpgaphi.atExtreme())
0236     edm::LogProblem("Tracklet") << "at extreme! " << fpgaphi.value();
0237 
0238   assert(!fpgaphi.atExtreme());
0239 
0240   if (fpgaz.atExtreme())
0241     return false;
0242 
0243   if (std::abs(fpgaz.value() * settings_.kz()) > settings_.zlength())
0244     return false;
0245 
0246   int iphivmRaw = fpgaphi.value() >> (fpgaphi.nbits() - 5);
0247   int iphi = iphivmRaw / (32 / settings_.nallstubs(layer - 1));
0248 
0249   addProjection(layer, iphi, trackletprojlayers_[layer - 1][iphi], tracklet);
0250 
0251   return true;
0252 }
0253 
0254 void TrackletCalculatorBase::addProjection(int layer,
0255                                            int iphi,
0256                                            TrackletProjectionsMemory* trackletprojs,
0257                                            Tracklet* tracklet) {
0258   if (trackletprojs == nullptr) {
0259     if (settings_.warnNoMem()) {
0260       edm::LogVerbatim("Tracklet") << "No projection memory exists in " << getName() << " for layer = " << layer
0261                                    << " iphi = " << iphi + 1;
0262     }
0263     return;
0264   }
0265   assert(trackletprojs != nullptr);
0266   trackletprojs->addProj(tracklet);
0267 }
0268 
0269 void TrackletCalculatorBase::addProjectionDisk(int disk,
0270                                                int iphi,
0271                                                TrackletProjectionsMemory* trackletprojs,
0272                                                Tracklet* tracklet) {
0273   if (iSeed_ == Seed::L3L4 && abs(disk) == 4)
0274     return;  //L3L4 projections to D3 are not used. Should be in configuration
0275   if (trackletprojs == nullptr) {
0276     if (iSeed_ == Seed::L3L4 && abs(disk) == 3)
0277       return;  //L3L4 projections to D3 are not used.
0278     if (settings_.warnNoMem()) {
0279       edm::LogVerbatim("Tracklet") << "No projection memory exists in " << getName() << " for disk = " << abs(disk)
0280                                    << " iphi = " << iphi + 1;
0281     }
0282     return;
0283   }
0284   assert(trackletprojs != nullptr);
0285   trackletprojs->addProj(tracklet);
0286 }
0287 
0288 bool TrackletCalculatorBase::goodTrackPars(bool goodrinv, bool goodz0) {
0289   bool success = true;
0290   if (!goodrinv) {
0291     if (settings_.debugTracklet()) {
0292       edm::LogVerbatim("Tracklet") << getName() << " TrackletCalculatorBase irinv too large";
0293     }
0294     success = false;
0295   }
0296   if (!goodz0) {
0297     if (settings_.debugTracklet()) {
0298       edm::LogVerbatim("Tracklet") << getName() << " TrackletCalculatorBase z0 cut to large";
0299     }
0300     success = false;
0301   }
0302   return success;
0303 }
0304 
0305 bool TrackletCalculatorBase::inSector(int iphi0, int irinv, double phi0approx, double rinvapprox) {
0306   double phicritapprox = phi0approx - asin(0.5 * settings_.rcrit() * rinvapprox);
0307 
0308   int ifactor = 0.5 * settings_.rcrit() * settings_.krinvpars() / settings_.kphi0pars() * (1 << 8);
0309   int iphicrit = iphi0 - (irinv >> 8) * ifactor;
0310 
0311   int iphicritmincut = settings_.phicritminmc() / globals_->ITC_L1L2()->phi0_final.K();
0312   int iphicritmaxcut = settings_.phicritmaxmc() / globals_->ITC_L1L2()->phi0_final.K();
0313 
0314   bool keepapprox = (phicritapprox > settings_.phicritminmc()) && (phicritapprox < settings_.phicritmaxmc()),
0315        keep = (iphicrit > iphicritmincut) && (iphicrit < iphicritmaxcut);
0316   if (settings_.debugTracklet())
0317     if (keepapprox && !keep)
0318       edm::LogVerbatim("Tracklet") << getName()
0319                                    << " Tracklet kept with exact phicrit cut but not approximate, phicritapprox: "
0320                                    << phicritapprox;
0321   if (settings_.usephicritapprox()) {
0322     return keepapprox;
0323   } else {
0324     return keep;
0325   }
0326 
0327   return true;
0328 }
0329 
0330 bool TrackletCalculatorBase::barrelSeeding(const Stub* innerFPGAStub,
0331                                            const L1TStub* innerStub,
0332                                            const Stub* outerFPGAStub,
0333                                            const L1TStub* outerStub) {
0334   if (settings_.debugTracklet()) {
0335     edm::LogVerbatim("Tracklet") << "TrackletCalculatorBase " << getName()
0336                                  << " trying stub pair in layer (inner outer): " << innerFPGAStub->layer().value()
0337                                  << " " << outerFPGAStub->layer().value();
0338   }
0339 
0340   assert(outerFPGAStub->layerdisk() < N_LAYER);
0341   assert(layerdisk1_ == (unsigned int)innerFPGAStub->layer().value());
0342   assert(layerdisk1_ < N_LAYER && layerdisk2_ < N_LAYER);
0343 
0344   double r1 = innerStub->r();
0345   double z1 = innerStub->z();
0346   double phi1 = innerStub->phi();
0347 
0348   double r2 = outerStub->r();
0349   double z2 = outerStub->z();
0350   double phi2 = outerStub->phi();
0351 
0352   double rinv, phi0, t, z0;
0353 
0354   double phiproj[N_LAYER - 2], zproj[N_LAYER - 2], phider[N_LAYER - 2], zder[N_LAYER - 2];
0355   double phiprojdisk[N_DISK], rprojdisk[N_DISK], phiderdisk[N_DISK], rderdisk[N_DISK];
0356 
0357   exacttracklet(r1,
0358                 z1,
0359                 phi1,
0360                 r2,
0361                 z2,
0362                 phi2,
0363                 outerStub->sigmaz(),
0364                 rinv,
0365                 phi0,
0366                 t,
0367                 z0,
0368                 phiproj,
0369                 zproj,
0370                 phider,
0371                 zder,
0372                 phiprojdisk,
0373                 rprojdisk,
0374                 phiderdisk,
0375                 rderdisk);
0376 
0377   if (settings_.useapprox()) {
0378     phi1 = innerFPGAStub->phiapprox(phimin_, phimax_);
0379     z1 = innerFPGAStub->zapprox();
0380     r1 = innerFPGAStub->rapprox();
0381 
0382     phi2 = outerFPGAStub->phiapprox(phimin_, phimax_);
0383     z2 = outerFPGAStub->zapprox();
0384     r2 = outerFPGAStub->rapprox();
0385   }
0386 
0387   double rinvapprox, phi0approx, tapprox, z0approx;
0388   double phiprojapprox[N_LAYER - 2], zprojapprox[N_LAYER - 2];
0389   double phiprojdiskapprox[N_DISK], rprojdiskapprox[N_DISK];
0390 
0391   IMATH_TrackletCalculator* ITC;
0392   if (iSeed_ == 0)
0393     ITC = globals_->ITC_L1L2();
0394   else if (iSeed_ == 1)
0395     ITC = globals_->ITC_L2L3();
0396   else if (iSeed_ == 2)
0397     ITC = globals_->ITC_L3L4();
0398   else
0399     ITC = globals_->ITC_L5L6();
0400 
0401   ITC->r1.set_fval(r1 - settings_.rmean(layerdisk1_));
0402   ITC->r2.set_fval(r2 - settings_.rmean(layerdisk2_));
0403   ITC->z1.set_fval(z1);
0404   ITC->z2.set_fval(z2);
0405   double sphi1 = angle0to2pi::make0To2pi(phi1 - phimin_);
0406   double sphi2 = angle0to2pi::make0To2pi(phi2 - phimin_);
0407 
0408   ITC->phi1.set_fval(sphi1);
0409   ITC->phi2.set_fval(sphi2);
0410 
0411   ITC->rproj0.set_fval(settings_.rmean(settings_.projlayers(iSeed_, 0) - 1));
0412   ITC->rproj1.set_fval(settings_.rmean(settings_.projlayers(iSeed_, 1) - 1));
0413   ITC->rproj2.set_fval(settings_.rmean(settings_.projlayers(iSeed_, 2) - 1));
0414   ITC->rproj3.set_fval(settings_.rmean(settings_.projlayers(iSeed_, 3) - 1));
0415 
0416   ITC->zproj0.set_fval(t > 0 ? settings_.zmean(0) : -settings_.zmean(0));
0417   ITC->zproj1.set_fval(t > 0 ? settings_.zmean(1) : -settings_.zmean(1));
0418   ITC->zproj2.set_fval(t > 0 ? settings_.zmean(2) : -settings_.zmean(2));
0419   ITC->zproj3.set_fval(t > 0 ? settings_.zmean(3) : -settings_.zmean(3));
0420   ITC->zproj4.set_fval(t > 0 ? settings_.zmean(4) : -settings_.zmean(4));
0421 
0422   ITC->rinv_final.calculate();
0423   ITC->phi0_final.calculate();
0424   ITC->t_final.calculate();
0425   ITC->z0_final.calculate();
0426 
0427   ITC->phiL_0_final.calculate();
0428   ITC->phiL_1_final.calculate();
0429   ITC->phiL_2_final.calculate();
0430   ITC->phiL_3_final.calculate();
0431 
0432   ITC->zL_0_final.calculate();
0433   ITC->zL_1_final.calculate();
0434   ITC->zL_2_final.calculate();
0435   ITC->zL_3_final.calculate();
0436 
0437   ITC->phiD_0_final.calculate();
0438   ITC->phiD_1_final.calculate();
0439   ITC->phiD_2_final.calculate();
0440   ITC->phiD_3_final.calculate();
0441   ITC->phiD_4_final.calculate();
0442 
0443   ITC->rD_0_final.calculate();
0444   ITC->rD_1_final.calculate();
0445   ITC->rD_2_final.calculate();
0446   ITC->rD_3_final.calculate();
0447   ITC->rD_4_final.calculate();
0448 
0449   ITC->der_phiL_final.calculate();
0450   ITC->der_zL_final.calculate();
0451   ITC->der_phiD_final.calculate();
0452   ITC->der_rD_final.calculate();
0453 
0454   //store the approximate results
0455   rinvapprox = ITC->rinv_final.fval();
0456   phi0approx = ITC->phi0_final.fval();
0457   tapprox = ITC->t_final.fval();
0458   z0approx = ITC->z0_final.fval();
0459 
0460   phiprojapprox[0] = ITC->phiL_0_final.fval();
0461   phiprojapprox[1] = ITC->phiL_1_final.fval();
0462   phiprojapprox[2] = ITC->phiL_2_final.fval();
0463   phiprojapprox[3] = ITC->phiL_3_final.fval();
0464 
0465   zprojapprox[0] = ITC->zL_0_final.fval();
0466   zprojapprox[1] = ITC->zL_1_final.fval();
0467   zprojapprox[2] = ITC->zL_2_final.fval();
0468   zprojapprox[3] = ITC->zL_3_final.fval();
0469 
0470   phiprojdiskapprox[0] = ITC->phiD_0_final.fval();
0471   phiprojdiskapprox[1] = ITC->phiD_1_final.fval();
0472   phiprojdiskapprox[2] = ITC->phiD_2_final.fval();
0473   phiprojdiskapprox[3] = ITC->phiD_3_final.fval();
0474   phiprojdiskapprox[4] = ITC->phiD_4_final.fval();
0475 
0476   rprojdiskapprox[0] = ITC->rD_0_final.fval();
0477   rprojdiskapprox[1] = ITC->rD_1_final.fval();
0478   rprojdiskapprox[2] = ITC->rD_2_final.fval();
0479   rprojdiskapprox[3] = ITC->rD_3_final.fval();
0480   rprojdiskapprox[4] = ITC->rD_4_final.fval();
0481 
0482   //now binary
0483 
0484   int irinv, iphi0, it, iz0;
0485   Projection projs[N_LAYER + N_DISK];
0486 
0487   int iphiproj[N_LAYER - 2], izproj[N_LAYER - 2];
0488   int iphiprojdisk[N_DISK], irprojdisk[N_DISK];
0489 
0490   int ir1 = innerFPGAStub->r().value();
0491   int iphi1 = innerFPGAStub->phi().value();
0492   int iz1 = innerFPGAStub->z().value();
0493 
0494   int ir2 = outerFPGAStub->r().value();
0495   int iphi2 = outerFPGAStub->phi().value();
0496   int iz2 = outerFPGAStub->z().value();
0497 
0498   iphi1 <<= (settings_.nphibitsstub(5) - settings_.nphibitsstub(layerdisk1_));
0499   iphi2 <<= (settings_.nphibitsstub(5) - settings_.nphibitsstub(layerdisk2_));
0500   ir1 <<= (8 - settings_.nrbitsstub(layerdisk1_));
0501   ir2 <<= (8 - settings_.nrbitsstub(layerdisk2_));
0502 
0503   iz1 <<= (settings_.nzbitsstub(0) - settings_.nzbitsstub(layerdisk1_));
0504   iz2 <<= (settings_.nzbitsstub(0) - settings_.nzbitsstub(layerdisk2_));
0505 
0506   ITC->r1.set_ival(ir1);
0507   ITC->r2.set_ival(ir2);
0508   ITC->z1.set_ival(iz1);
0509   ITC->z2.set_ival(iz2);
0510   ITC->phi1.set_ival(iphi1);
0511   ITC->phi2.set_ival(iphi2);
0512 
0513   ITC->rinv_final.calculate();
0514   ITC->phi0_final.calculate();
0515   ITC->t_final.calculate();
0516   ITC->z0_final.calculate();
0517 
0518   ITC->phiL_0_final.calculate();
0519   ITC->phiL_1_final.calculate();
0520   ITC->phiL_2_final.calculate();
0521   ITC->phiL_3_final.calculate();
0522 
0523   ITC->zL_0_final.calculate();
0524   ITC->zL_1_final.calculate();
0525   ITC->zL_2_final.calculate();
0526   ITC->zL_3_final.calculate();
0527 
0528   ITC->phiD_0_final.calculate();
0529   ITC->phiD_1_final.calculate();
0530   ITC->phiD_2_final.calculate();
0531   ITC->phiD_3_final.calculate();
0532   ITC->phiD_4_final.calculate();
0533 
0534   ITC->rD_0_final.calculate();
0535   ITC->rD_1_final.calculate();
0536   ITC->rD_2_final.calculate();
0537   ITC->rD_3_final.calculate();
0538   ITC->rD_4_final.calculate();
0539 
0540   ITC->der_phiL_final.calculate();
0541   ITC->der_zL_final.calculate();
0542   ITC->der_phiD_final.calculate();
0543   ITC->der_rD_final.calculate();
0544 
0545   //store the binary results
0546   irinv = ITC->rinv_final.ival();
0547   iphi0 = ITC->phi0_final.ival();
0548   it = ITC->t_final.ival();
0549   iz0 = ITC->z0_final.ival();
0550 
0551   iphiproj[0] = ITC->phiL_0_final.ival();
0552   iphiproj[1] = ITC->phiL_1_final.ival();
0553   iphiproj[2] = ITC->phiL_2_final.ival();
0554   iphiproj[3] = ITC->phiL_3_final.ival();
0555 
0556   izproj[0] = ITC->zL_0_final.ival();
0557   izproj[1] = ITC->zL_1_final.ival();
0558   izproj[2] = ITC->zL_2_final.ival();
0559   izproj[3] = ITC->zL_3_final.ival();
0560 
0561   if (!goodTrackPars(ITC->rinv_final.local_passes(), ITC->z0_final.local_passes())) {
0562     if (settings_.debugTracklet()) {
0563       edm::LogVerbatim("Tracklet") << getName() << " Failed rinv or z0 cut";
0564     }
0565     return false;
0566   }
0567 
0568   if (!inSector(iphi0, irinv, phi0approx, rinvapprox)) {
0569     if (settings_.debugTracklet()) {
0570       edm::LogVerbatim("Tracklet") << getName() << " Failed in sector check";
0571     }
0572     return false;
0573   }
0574 
0575   for (unsigned int i = 0; i < N_LAYER - 2; ++i) {
0576     //reject projection if z is out of range
0577     if (izproj[i] < -(1 << (settings_.nzbitsstub(0) - 1)))
0578       continue;
0579     if (izproj[i] >= (1 << (settings_.nzbitsstub(0) - 1)))
0580       continue;
0581 
0582     //reject projection if phi is out of range
0583     if (iphiproj[i] >= (1 << settings_.nphibitsstub(5)) - 1)
0584       continue;
0585     if (iphiproj[i] <= 0)
0586       continue;
0587 
0588     //Adjust bits for r and z projection depending on layer
0589     if (settings_.projlayers(iSeed_, i) <= 3) {  //TODO clean up logic
0590       iphiproj[i] >>= (settings_.nphibitsstub(5) - settings_.nphibitsstub(settings_.projlayers(iSeed_, i) - 1));
0591     } else {
0592       izproj[i] >>= (settings_.nzbitsstub(0) - settings_.nzbitsstub(5));
0593     }
0594 
0595     projs[settings_.projlayers(iSeed_, i) - 1].init(settings_,
0596                                                     settings_.projlayers(iSeed_, i) - 1,
0597                                                     iphiproj[i],
0598                                                     izproj[i],
0599                                                     ITC->der_phiL_final.ival(),
0600                                                     ITC->der_zL_final.ival(),
0601                                                     phiproj[i],
0602                                                     zproj[i],
0603                                                     phider[i],
0604                                                     zder[i],
0605                                                     phiprojapprox[i],
0606                                                     zprojapprox[i],
0607                                                     ITC->der_phiL_final.fval(),
0608                                                     ITC->der_zL_final.fval(),
0609                                                     !(iSeed_ == 2 || iSeed_ == 3));
0610   }
0611 
0612   iphiprojdisk[0] = ITC->phiD_0_final.ival();
0613   iphiprojdisk[1] = ITC->phiD_1_final.ival();
0614   iphiprojdisk[2] = ITC->phiD_2_final.ival();
0615   iphiprojdisk[3] = ITC->phiD_3_final.ival();
0616   iphiprojdisk[4] = ITC->phiD_4_final.ival();
0617 
0618   irprojdisk[0] = ITC->rD_0_final.ival();
0619   irprojdisk[1] = ITC->rD_1_final.ival();
0620   irprojdisk[2] = ITC->rD_2_final.ival();
0621   irprojdisk[3] = ITC->rD_3_final.ival();
0622   irprojdisk[4] = ITC->rD_4_final.ival();
0623 
0624   if (std::abs(it * ITC->t_final.K()) > 1.0) {
0625     for (unsigned int i = 0; i < N_DISK; ++i) {
0626       if (iphiprojdisk[i] <= 0)
0627         continue;
0628       if (iphiprojdisk[i] >= (1 << settings_.nphibitsstub(0)) - 1)
0629         continue;
0630 
0631       if (irprojdisk[i] < settings_.rmindisk() / ITC->rD_0_final.K() ||
0632           irprojdisk[i] > settings_.rmaxdisk() / ITC->rD_0_final.K())
0633         continue;
0634 
0635       projs[i + N_LAYER].init(settings_,
0636                               i + N_LAYER,
0637                               iphiprojdisk[i],
0638                               irprojdisk[i],
0639                               ITC->der_phiD_final.ival(),
0640                               ITC->der_rD_final.ival(),
0641                               phiprojdisk[i],
0642                               rprojdisk[i],
0643                               phiderdisk[i],
0644                               rderdisk[i],
0645                               phiprojdiskapprox[i],
0646                               rprojdiskapprox[i],
0647                               ITC->der_phiD_final.fval(),
0648                               ITC->der_rD_final.fval(),
0649                               !(iSeed_ == 2 || iSeed_ == 3));
0650     }
0651   }
0652 
0653   if (settings_.writeMonitorData("TPars")) {
0654     globals_->ofstream("trackletpars.txt")
0655         << "Trackpars " << layerdisk1_ + 1 << "   " << rinv << " " << rinvapprox << " " << ITC->rinv_final.fval()
0656         << "   " << phi0 << " " << phi0approx << " " << ITC->phi0_final.fval() << "   " << t << " " << tapprox << " "
0657         << ITC->t_final.fval() << "   " << z0 << " " << z0approx << " " << ITC->z0_final.fval() << endl;
0658   }
0659 
0660   Tracklet* tracklet = new Tracklet(settings_,
0661                                     iSeed_,
0662                                     innerFPGAStub,
0663                                     nullptr,
0664                                     outerFPGAStub,
0665                                     rinv,
0666                                     phi0,
0667                                     0.0,
0668                                     z0,
0669                                     t,
0670                                     rinvapprox,
0671                                     phi0approx,
0672                                     0.0,
0673                                     z0approx,
0674                                     tapprox,
0675                                     irinv,
0676                                     iphi0,
0677                                     0,
0678                                     iz0,
0679                                     it,
0680                                     projs,
0681                                     false);
0682 
0683   if (settings_.debugTracklet()) {
0684     edm::LogVerbatim("Tracklet") << "TrackletCalculator " << getName() << " Found tracklet for seed = " << iSeed_ << " "
0685                                  << iSector_ << " phi0 = " << phi0;
0686   }
0687 
0688   tracklet->setTrackletIndex(trackletpars_->nTracklets());
0689   tracklet->setTCIndex(TCIndex_);
0690 
0691   if (settings_.writeMonitorData("Seeds")) {
0692     ofstream fout("seeds.txt", ofstream::app);
0693     fout << __FILE__ << ":" << __LINE__ << " " << name_ << "_" << iSector_ << " " << tracklet->getISeed() << endl;
0694     fout.close();
0695   }
0696   trackletpars_->addTracklet(tracklet);
0697 
0698   if (settings_.bookHistos()) {
0699     HistBase* hists = globals_->histograms();
0700     int tp = tracklet->tpseed();
0701     hists->fillTrackletParams(settings_,
0702                               globals_,
0703                               iSeed_,
0704                               iSector_,
0705                               rinvapprox,
0706                               irinv * ITC->rinv_final.K(),
0707                               phi0approx,
0708                               iphi0 * ITC->phi0_final.K(),
0709                               asinh(tapprox),
0710                               asinh(it * ITC->t_final.K()),
0711                               z0approx,
0712                               iz0 * ITC->z0_final.K(),
0713                               tp);
0714   }
0715 
0716   bool addL3 = false;
0717   bool addL4 = false;
0718   bool addL5 = false;
0719   bool addL6 = false;
0720   for (unsigned int j = 0; j < N_LAYER - 2; j++) {
0721     int lproj = settings_.projlayers(iSeed_, j);
0722     bool added = false;
0723     if (tracklet->validProj(lproj - 1)) {
0724       added = addLayerProj(tracklet, lproj);
0725       if (added && lproj == 3)
0726         addL3 = true;
0727       if (added && lproj == 4)
0728         addL4 = true;
0729       if (added && lproj == 5)
0730         addL5 = true;
0731       if (added && lproj == 6)
0732         addL6 = true;
0733     }
0734   }
0735 
0736   for (unsigned int j = 0; j < N_DISK - 1; j++) {  //no projections to 5th disk!!
0737     int disk = j + 1;
0738     if (disk == 4 && addL3)
0739       continue;
0740     if (disk == 3 && addL4)
0741       continue;
0742     if (disk == 2 && addL5)
0743       continue;
0744     if (disk == 1 && addL6)
0745       continue;
0746     if (it < 0)
0747       disk = -disk;
0748     if (tracklet->validProj(N_LAYER + abs(disk) - 1)) {
0749       addDiskProj(tracklet, disk);
0750     }
0751   }
0752 
0753   return true;
0754 }
0755 
0756 bool TrackletCalculatorBase::diskSeeding(const Stub* innerFPGAStub,
0757                                          const L1TStub* innerStub,
0758                                          const Stub* outerFPGAStub,
0759                                          const L1TStub* outerStub) {
0760   if (settings_.debugTracklet()) {
0761     edm::LogVerbatim("Tracklet") << "TrackletCalculator::execute calculate disk seeds";
0762   }
0763 
0764   int sign = 1;
0765   if (innerFPGAStub->disk().value() < 0)
0766     sign = -1;
0767 
0768   int disk = innerFPGAStub->disk().value();
0769   assert(abs(disk) == 1 || abs(disk) == 3);
0770 
0771   assert(innerStub->isPSmodule());
0772   assert(outerStub->isPSmodule());
0773 
0774   double r1 = innerStub->r();
0775   double z1 = innerStub->z();
0776   double phi1 = innerStub->phi();
0777 
0778   double r2 = outerStub->r();
0779   double z2 = outerStub->z();
0780   double phi2 = outerStub->phi();
0781 
0782   if (r2 < r1 + 2.0) {
0783     return false;  //Protection... Should be handled cleaner to avoid problem with floating point calculation
0784   }
0785 
0786   double rinv, phi0, t, z0;
0787 
0788   double phiproj[N_PSLAYER], zproj[N_PSLAYER], phider[N_PSLAYER], zder[N_PSLAYER];
0789   double phiprojdisk[N_DISK - 2], rprojdisk[N_DISK - 2], phiderdisk[N_DISK - 2], rderdisk[N_DISK - 2];
0790 
0791   exacttrackletdisk(r1,
0792                     z1,
0793                     phi1,
0794                     r2,
0795                     z2,
0796                     phi2,
0797                     outerStub->sigmaz(),
0798                     rinv,
0799                     phi0,
0800                     t,
0801                     z0,
0802                     phiproj,
0803                     zproj,
0804                     phider,
0805                     zder,
0806                     phiprojdisk,
0807                     rprojdisk,
0808                     phiderdisk,
0809                     rderdisk);
0810 
0811   //Truncates floating point positions to integer representation precision
0812   if (settings_.useapprox()) {
0813     phi1 = innerFPGAStub->phiapprox(phimin_, phimax_);
0814     z1 = innerFPGAStub->zapprox();
0815     r1 = innerFPGAStub->rapprox();
0816 
0817     phi2 = outerFPGAStub->phiapprox(phimin_, phimax_);
0818     z2 = outerFPGAStub->zapprox();
0819     r2 = outerFPGAStub->rapprox();
0820   }
0821 
0822   double rinvapprox, phi0approx, tapprox, z0approx;
0823   double phiprojapprox[N_PSLAYER], zprojapprox[N_PSLAYER];
0824   double phiprojdiskapprox[N_DISK - 2], rprojdiskapprox[N_DISK - 2];
0825 
0826   IMATH_TrackletCalculatorDisk* ITC;
0827   if (disk == 1)
0828     ITC = globals_->ITC_F1F2();
0829   else if (disk == 3)
0830     ITC = globals_->ITC_F3F4();
0831   else if (disk == -1)
0832     ITC = globals_->ITC_B1B2();
0833   else
0834     ITC = globals_->ITC_B3B4();
0835 
0836   ITC->r1.set_fval(r1);
0837   ITC->r2.set_fval(r2);
0838   int signt = t > 0 ? 1 : -1;
0839   ITC->z1.set_fval(z1 - signt * settings_.zmean(layerdisk1_ - N_LAYER));
0840   ITC->z2.set_fval(z2 - signt * settings_.zmean(layerdisk2_ - N_LAYER));
0841   double sphi1 = angle0to2pi::make0To2pi(phi1 - phimin_);
0842   double sphi2 = angle0to2pi::make0To2pi(phi2 - phimin_);
0843   ITC->phi1.set_fval(sphi1);
0844   ITC->phi2.set_fval(sphi2);
0845 
0846   ITC->rproj0.set_fval(settings_.rmean(0));
0847   ITC->rproj1.set_fval(settings_.rmean(1));
0848   ITC->rproj2.set_fval(settings_.rmean(2));
0849 
0850   ITC->zproj0.set_fval(signt * settings_.zmean(settings_.projdisks(iSeed_, 0) - 1));
0851   ITC->zproj1.set_fval(signt * settings_.zmean(settings_.projdisks(iSeed_, 1) - 1));
0852   ITC->zproj2.set_fval(signt * settings_.zmean(settings_.projdisks(iSeed_, 2) - 1));
0853 
0854   ITC->rinv_final.calculate();
0855   ITC->phi0_final.calculate();
0856   ITC->t_final.calculate();
0857   ITC->z0_final.calculate();
0858 
0859   ITC->phiL_0_final.calculate();
0860   ITC->phiL_1_final.calculate();
0861   ITC->phiL_2_final.calculate();
0862 
0863   ITC->zL_0_final.calculate();
0864   ITC->zL_1_final.calculate();
0865   ITC->zL_2_final.calculate();
0866 
0867   ITC->phiD_0_final.calculate();
0868   ITC->phiD_1_final.calculate();
0869   ITC->phiD_2_final.calculate();
0870 
0871   ITC->rD_0_final.calculate();
0872   ITC->rD_1_final.calculate();
0873   ITC->rD_2_final.calculate();
0874 
0875   ITC->der_phiL_final.calculate();
0876   ITC->der_zL_final.calculate();
0877   ITC->der_phiD_final.calculate();
0878   ITC->der_rD_final.calculate();
0879 
0880   //store the approximate results
0881   rinvapprox = ITC->rinv_final.fval();
0882   phi0approx = ITC->phi0_final.fval();
0883   tapprox = ITC->t_final.fval();
0884   z0approx = ITC->z0_final.fval();
0885 
0886   phiprojapprox[0] = ITC->phiL_0_final.fval();
0887   phiprojapprox[1] = ITC->phiL_1_final.fval();
0888   phiprojapprox[2] = ITC->phiL_2_final.fval();
0889 
0890   zprojapprox[0] = ITC->zL_0_final.fval();
0891   zprojapprox[1] = ITC->zL_1_final.fval();
0892   zprojapprox[2] = ITC->zL_2_final.fval();
0893 
0894   phiprojdiskapprox[0] = ITC->phiD_0_final.fval();
0895   phiprojdiskapprox[1] = ITC->phiD_1_final.fval();
0896   phiprojdiskapprox[2] = ITC->phiD_2_final.fval();
0897 
0898   rprojdiskapprox[0] = ITC->rD_0_final.fval();
0899   rprojdiskapprox[1] = ITC->rD_1_final.fval();
0900   rprojdiskapprox[2] = ITC->rD_2_final.fval();
0901 
0902   //now binary
0903 
0904   int irinv, iphi0, it, iz0;
0905   int iphiproj[N_PSLAYER], izproj[N_PSLAYER];
0906 
0907   int iphiprojdisk[N_DISK - 2], irprojdisk[N_DISK - 2];
0908 
0909   int ir1 = innerFPGAStub->r().value();
0910   int iphi1 = innerFPGAStub->phi().value();
0911   int iz1 = innerFPGAStub->z().value();
0912 
0913   int ir2 = outerFPGAStub->r().value();
0914   int iphi2 = outerFPGAStub->phi().value();
0915   int iz2 = outerFPGAStub->z().value();
0916 
0917   //To get same precission as for layers.
0918   iphi1 <<= (settings_.nphibitsstub(5) - settings_.nphibitsstub(0));
0919   iphi2 <<= (settings_.nphibitsstub(5) - settings_.nphibitsstub(0));
0920 
0921   ITC->r1.set_ival(ir1);
0922   ITC->r2.set_ival(ir2);
0923   ITC->z1.set_ival(iz1);
0924   ITC->z2.set_ival(iz2);
0925   ITC->phi1.set_ival(iphi1);
0926   ITC->phi2.set_ival(iphi2);
0927 
0928   ITC->rinv_final.calculate();
0929   ITC->phi0_final.calculate();
0930   ITC->t_final.calculate();
0931   ITC->z0_final.calculate();
0932 
0933   ITC->phiL_0_final.calculate();
0934   ITC->phiL_1_final.calculate();
0935   ITC->phiL_2_final.calculate();
0936 
0937   ITC->zL_0_final.calculate();
0938   ITC->zL_1_final.calculate();
0939   ITC->zL_2_final.calculate();
0940 
0941   ITC->phiD_0_final.calculate();
0942   ITC->phiD_1_final.calculate();
0943   ITC->phiD_2_final.calculate();
0944 
0945   ITC->rD_0_final.calculate();
0946   ITC->rD_1_final.calculate();
0947   ITC->rD_2_final.calculate();
0948 
0949   ITC->der_phiL_final.calculate();
0950   ITC->der_zL_final.calculate();
0951   ITC->der_phiD_final.calculate();
0952   ITC->der_rD_final.calculate();
0953 
0954   //store the binary results
0955   irinv = ITC->rinv_final.ival();
0956   iphi0 = ITC->phi0_final.ival();
0957   it = ITC->t_final.ival();
0958   iz0 = ITC->z0_final.ival();
0959 
0960   iphiproj[0] = ITC->phiL_0_final.ival();
0961   iphiproj[1] = ITC->phiL_1_final.ival();
0962   iphiproj[2] = ITC->phiL_2_final.ival();
0963 
0964   izproj[0] = ITC->zL_0_final.ival();
0965   izproj[1] = ITC->zL_1_final.ival();
0966   izproj[2] = ITC->zL_2_final.ival();
0967 
0968   if (!goodTrackPars(ITC->rinv_final.local_passes(), ITC->z0_final.local_passes()))
0969     return false;
0970 
0971   if (!inSector(iphi0, irinv, phi0approx, rinvapprox))
0972     return false;
0973 
0974   Projection projs[N_LAYER + N_DISK];
0975 
0976   for (unsigned int i = 0; i < N_DISK - 2; ++i) {
0977     //Check is outside z range
0978     if (izproj[i] < -(1 << (settings_.nzbitsstub(0) - 1)))
0979       continue;
0980     if (izproj[i] >= (1 << (settings_.nzbitsstub(0) - 1)))
0981       continue;
0982 
0983     //Check if outside phi range
0984     if (iphiproj[i] >= (1 << settings_.nphibitsstub(5)) - 1)
0985       continue;
0986     if (iphiproj[i] <= 0)
0987       continue;
0988 
0989     //shift bits - allways in PS modules for disk seeding
0990     iphiproj[i] >>= (settings_.nphibitsstub(5) - settings_.nphibitsstub(0));
0991 
0992     projs[i].init(settings_,
0993                   i,
0994                   iphiproj[i],
0995                   izproj[i],
0996                   ITC->der_phiL_final.ival(),
0997                   ITC->der_zL_final.ival(),
0998                   phiproj[i],
0999                   zproj[i],
1000                   phider[i],
1001                   zder[i],
1002                   phiprojapprox[i],
1003                   zprojapprox[i],
1004                   ITC->der_phiL_final.fval(),
1005                   ITC->der_zL_final.fval(),
1006                   true);
1007   }
1008 
1009   iphiprojdisk[0] = ITC->phiD_0_final.ival();
1010   iphiprojdisk[1] = ITC->phiD_1_final.ival();
1011   iphiprojdisk[2] = ITC->phiD_2_final.ival();
1012 
1013   irprojdisk[0] = ITC->rD_0_final.ival();
1014   irprojdisk[1] = ITC->rD_1_final.ival();
1015   irprojdisk[2] = ITC->rD_2_final.ival();
1016 
1017   for (unsigned int i = 0; i < N_DISK - 2; ++i) {
1018     //check that phi projection in range
1019     if (iphiprojdisk[i] <= 0)
1020       continue;
1021     if (iphiprojdisk[i] >= (1 << settings_.nphibitsstub(0)) - 1)
1022       continue;
1023 
1024     //check that r projection in range
1025     if (irprojdisk[i] <= 0 || irprojdisk[i] > settings_.rmaxdisk() / ITC->rD_0_final.K())
1026       continue;
1027 
1028     projs[settings_.projdisks(iSeed_, i) + N_LAYER - 1].init(settings_,
1029                                                              settings_.projdisks(iSeed_, i) + N_LAYER - 1,
1030                                                              iphiprojdisk[i],
1031                                                              irprojdisk[i],
1032                                                              ITC->der_phiD_final.ival(),
1033                                                              ITC->der_rD_final.ival(),
1034                                                              phiprojdisk[i],
1035                                                              rprojdisk[i],
1036                                                              phiderdisk[i],
1037                                                              rderdisk[i],
1038                                                              phiprojdiskapprox[i],
1039                                                              rprojdiskapprox[i],
1040                                                              ITC->der_phiD_final.fval(),
1041                                                              ITC->der_rD_final.fval(),
1042                                                              true);
1043   }
1044 
1045   if (settings_.writeMonitorData("TPars")) {
1046     globals_->ofstream("trackletparsdisk.txt")
1047         << "Trackpars         " << layerdisk1_ - 5 << "   " << rinv << " " << rinvapprox << " "
1048         << ITC->rinv_final.fval() << "   " << phi0 << " " << phi0approx << " " << ITC->phi0_final.fval() << "   " << t
1049         << " " << tapprox << " " << ITC->t_final.fval() << "   " << z0 << " " << z0approx << " " << ITC->z0_final.fval()
1050         << endl;
1051   }
1052 
1053   Tracklet* tracklet = new Tracklet(settings_,
1054                                     iSeed_,
1055                                     innerFPGAStub,
1056                                     nullptr,
1057                                     outerFPGAStub,
1058                                     rinv,
1059                                     phi0,
1060                                     0.0,
1061                                     z0,
1062                                     t,
1063                                     rinvapprox,
1064                                     phi0approx,
1065                                     0.0,
1066                                     z0approx,
1067                                     tapprox,
1068                                     irinv,
1069                                     iphi0,
1070                                     0,
1071                                     iz0,
1072                                     it,
1073                                     projs,
1074                                     true);
1075 
1076   if (settings_.debugTracklet()) {
1077     edm::LogVerbatim("Tracklet") << "Found tracklet for disk seed = " << iSeed_ << " " << tracklet << " " << iSector_;
1078   }
1079 
1080   tracklet->setTrackletIndex(trackletpars_->nTracklets());
1081   tracklet->setTCIndex(TCIndex_);
1082 
1083   if (settings_.writeMonitorData("Seeds")) {
1084     ofstream fout("seeds.txt", ofstream::app);
1085     fout << __FILE__ << ":" << __LINE__ << " " << name_ << "_" << iSector_ << " " << tracklet->getISeed() << endl;
1086     fout.close();
1087   }
1088   trackletpars_->addTracklet(tracklet);
1089 
1090   if (tracklet->validProj(0)) {
1091     addLayerProj(tracklet, 1);
1092   }
1093 
1094   if (tracklet->validProj(1)) {
1095     addLayerProj(tracklet, 2);
1096   }
1097 
1098   for (unsigned int j = 0; j < N_DISK - 2; j++) {
1099     if (tracklet->validProj(N_LAYER + settings_.projdisks(iSeed_, j) - 1)) {
1100       addDiskProj(tracklet, sign * settings_.projdisks(iSeed_, j));
1101     }
1102   }
1103 
1104   return true;
1105 }
1106 
1107 bool TrackletCalculatorBase::overlapSeeding(const Stub* innerFPGAStub,
1108                                             const L1TStub* innerStub,
1109                                             const Stub* outerFPGAStub,
1110                                             const L1TStub* outerStub) {
1111   //Deal with overlap stubs here
1112   assert(outerFPGAStub->layerdisk() < N_LAYER);
1113 
1114   assert(innerFPGAStub->layerdisk() >= N_LAYER);
1115 
1116   int disk = innerFPGAStub->disk().value();
1117 
1118   if (settings_.debugTracklet()) {
1119     edm::LogVerbatim("Tracklet") << "trying to make overlap tracklet for seed = " << iSeed_ << " " << getName();
1120   }
1121 
1122   double r1 = innerStub->r();
1123   double z1 = innerStub->z();
1124   double phi1 = innerStub->phi();
1125 
1126   double r2 = outerStub->r();
1127   double z2 = outerStub->z();
1128   double phi2 = outerStub->phi();
1129 
1130   //Protection for wrong radii. Could be handled cleaner to avoid problem with floating point calculation and with overflows in the integer calculation.
1131   if (r1 < r2 + 1.5) {
1132     return false;
1133   }
1134 
1135   double rinv, phi0, t, z0;
1136 
1137   double phiproj[N_PSLAYER], zproj[N_PSLAYER], phider[N_PSLAYER], zder[N_PSLAYER];
1138   double phiprojdisk[N_DISK - 1], rprojdisk[N_DISK - 1], phiderdisk[N_DISK - 1], rderdisk[N_DISK - 1];
1139 
1140   exacttrackletOverlap(r1,
1141                        z1,
1142                        phi1,
1143                        r2,
1144                        z2,
1145                        phi2,
1146                        outerStub->sigmaz(),
1147                        rinv,
1148                        phi0,
1149                        t,
1150                        z0,
1151                        phiproj,
1152                        zproj,
1153                        phider,
1154                        zder,
1155                        phiprojdisk,
1156                        rprojdisk,
1157                        phiderdisk,
1158                        rderdisk);
1159 
1160   //Truncates floating point positions to integer representation precision
1161   if (settings_.useapprox()) {
1162     phi1 = innerFPGAStub->phiapprox(phimin_, phimax_);
1163     z1 = innerFPGAStub->zapprox();
1164     r1 = innerFPGAStub->rapprox();
1165 
1166     phi2 = outerFPGAStub->phiapprox(phimin_, phimax_);
1167     z2 = outerFPGAStub->zapprox();
1168     r2 = outerFPGAStub->rapprox();
1169   }
1170 
1171   double rinvapprox, phi0approx, tapprox, z0approx;
1172   double phiprojapprox[N_PSLAYER], zprojapprox[N_PSLAYER];
1173   double phiprojdiskapprox[N_DISK - 1], rprojdiskapprox[N_DISK - 1];
1174 
1175   IMATH_TrackletCalculatorOverlap* ITC;
1176   int ll = outerFPGAStub->layer().value() + 1;
1177   if (ll == 1 && disk == 1)
1178     ITC = globals_->ITC_L1F1();
1179   else if (ll == 2 && disk == 1)
1180     ITC = globals_->ITC_L2F1();
1181   else if (ll == 1 && disk == -1)
1182     ITC = globals_->ITC_L1B1();
1183   else if (ll == 2 && disk == -1)
1184     ITC = globals_->ITC_L2B1();
1185   else
1186     throw cms::Exception("LogicError") << __FILE__ << " " << __LINE__ << " Invalid seeding!";
1187 
1188   ITC->r1.set_fval(r2 - settings_.rmean(ll - 1));
1189   ITC->r2.set_fval(r1);
1190   int signt = t > 0 ? 1 : -1;
1191   ITC->z1.set_fval(z2);
1192   ITC->z2.set_fval(z1 - signt * settings_.zmean(layerdisk2_ - N_LAYER));
1193   double sphi1 = angle0to2pi::make0To2pi(phi1 - phimin_);
1194   double sphi2 = angle0to2pi::make0To2pi(phi2 - phimin_);
1195   ITC->phi1.set_fval(sphi2);
1196   ITC->phi2.set_fval(sphi1);
1197 
1198   ITC->rproj0.set_fval(settings_.rmean(0));
1199   ITC->rproj1.set_fval(settings_.rmean(1));
1200   ITC->rproj2.set_fval(settings_.rmean(2));
1201 
1202   ITC->zproj0.set_fval(signt * settings_.zmean(1));
1203   ITC->zproj1.set_fval(signt * settings_.zmean(2));
1204   ITC->zproj2.set_fval(signt * settings_.zmean(3));
1205   ITC->zproj3.set_fval(signt * settings_.zmean(4));
1206 
1207   ITC->rinv_final.calculate();
1208   ITC->phi0_final.calculate();
1209   ITC->t_final.calculate();
1210   ITC->z0_final.calculate();
1211 
1212   ITC->phiL_0_final.calculate();
1213   ITC->phiL_1_final.calculate();
1214   ITC->phiL_2_final.calculate();
1215 
1216   ITC->zL_0_final.calculate();
1217   ITC->zL_1_final.calculate();
1218   ITC->zL_2_final.calculate();
1219 
1220   ITC->phiD_0_final.calculate();
1221   ITC->phiD_1_final.calculate();
1222   ITC->phiD_2_final.calculate();
1223   ITC->phiD_3_final.calculate();
1224 
1225   ITC->rD_0_final.calculate();
1226   ITC->rD_1_final.calculate();
1227   ITC->rD_2_final.calculate();
1228   ITC->rD_3_final.calculate();
1229 
1230   ITC->der_phiL_final.calculate();
1231   ITC->der_zL_final.calculate();
1232   ITC->der_phiD_final.calculate();
1233   ITC->der_rD_final.calculate();
1234 
1235   //store the approximate results
1236   rinvapprox = ITC->rinv_final.fval();
1237   phi0approx = ITC->phi0_final.fval();
1238   tapprox = ITC->t_final.fval();
1239   z0approx = ITC->z0_final.fval();
1240 
1241   phiprojapprox[0] = ITC->phiL_0_final.fval();
1242   phiprojapprox[1] = ITC->phiL_1_final.fval();
1243   phiprojapprox[2] = ITC->phiL_2_final.fval();
1244 
1245   zprojapprox[0] = ITC->zL_0_final.fval();
1246   zprojapprox[1] = ITC->zL_1_final.fval();
1247   zprojapprox[2] = ITC->zL_2_final.fval();
1248 
1249   phiprojdiskapprox[0] = ITC->phiD_0_final.fval();
1250   phiprojdiskapprox[1] = ITC->phiD_1_final.fval();
1251   phiprojdiskapprox[2] = ITC->phiD_2_final.fval();
1252   phiprojdiskapprox[3] = ITC->phiD_3_final.fval();
1253 
1254   rprojdiskapprox[0] = ITC->rD_0_final.fval();
1255   rprojdiskapprox[1] = ITC->rD_1_final.fval();
1256   rprojdiskapprox[2] = ITC->rD_2_final.fval();
1257   rprojdiskapprox[3] = ITC->rD_3_final.fval();
1258 
1259   //now binary
1260 
1261   int irinv, iphi0, it, iz0;
1262   int iphiproj[N_LAYER - 2], izproj[N_LAYER - 2];
1263   int iphiprojdisk[N_DISK], irprojdisk[N_DISK];
1264 
1265   int ir2 = innerFPGAStub->r().value();
1266   int iphi2 = innerFPGAStub->phi().value();
1267   int iz2 = innerFPGAStub->z().value();
1268 
1269   int ir1 = outerFPGAStub->r().value();
1270   int iphi1 = outerFPGAStub->phi().value();
1271   int iz1 = outerFPGAStub->z().value();
1272 
1273   //To get global precission
1274   ir1 <<= (8 - settings_.nrbitsstub(ll - 1));
1275   iphi1 <<= (settings_.nphibitsstub(5) - settings_.nphibitsstub(0));
1276   iphi2 <<= (settings_.nphibitsstub(5) - settings_.nphibitsstub(0));
1277 
1278   ITC->r1.set_ival(ir1);
1279   ITC->r2.set_ival(ir2);
1280   ITC->z1.set_ival(iz1);
1281   ITC->z2.set_ival(iz2);
1282   ITC->phi1.set_ival(iphi1);
1283   ITC->phi2.set_ival(iphi2);
1284 
1285   ITC->rinv_final.calculate();
1286   ITC->phi0_final.calculate();
1287   ITC->t_final.calculate();
1288   ITC->z0_final.calculate();
1289 
1290   ITC->phiL_0_final.calculate();
1291   ITC->phiL_1_final.calculate();
1292   ITC->phiL_2_final.calculate();
1293 
1294   ITC->zL_0_final.calculate();
1295   ITC->zL_1_final.calculate();
1296   ITC->zL_2_final.calculate();
1297 
1298   ITC->phiD_0_final.calculate();
1299   ITC->phiD_1_final.calculate();
1300   ITC->phiD_2_final.calculate();
1301   ITC->phiD_3_final.calculate();
1302 
1303   ITC->rD_0_final.calculate();
1304   ITC->rD_1_final.calculate();
1305   ITC->rD_2_final.calculate();
1306   ITC->rD_3_final.calculate();
1307 
1308   ITC->der_phiL_final.calculate();
1309   ITC->der_zL_final.calculate();
1310   ITC->der_phiD_final.calculate();
1311   ITC->der_rD_final.calculate();
1312 
1313   //store the binary results
1314   irinv = ITC->rinv_final.ival();
1315   iphi0 = ITC->phi0_final.ival();
1316   it = ITC->t_final.ival();
1317   iz0 = ITC->z0_final.ival();
1318 
1319   iphiproj[0] = ITC->phiL_0_final.ival();
1320   iphiproj[1] = ITC->phiL_1_final.ival();
1321   iphiproj[2] = ITC->phiL_2_final.ival();
1322 
1323   izproj[0] = ITC->zL_0_final.ival();
1324   izproj[1] = ITC->zL_1_final.ival();
1325   izproj[2] = ITC->zL_2_final.ival();
1326 
1327   iphiprojdisk[0] = ITC->phiD_0_final.ival();
1328   iphiprojdisk[1] = ITC->phiD_1_final.ival();
1329   iphiprojdisk[2] = ITC->phiD_2_final.ival();
1330   iphiprojdisk[3] = ITC->phiD_3_final.ival();
1331 
1332   irprojdisk[0] = ITC->rD_0_final.ival();
1333   irprojdisk[1] = ITC->rD_1_final.ival();
1334   irprojdisk[2] = ITC->rD_2_final.ival();
1335   irprojdisk[3] = ITC->rD_3_final.ival();
1336 
1337   if (!goodTrackPars(ITC->rinv_final.local_passes(), ITC->z0_final.local_passes()))
1338     return false;
1339 
1340   if (!inSector(iphi0, irinv, phi0approx, rinvapprox))
1341     return false;
1342 
1343   Projection projs[N_LAYER + N_DISK];
1344 
1345   for (unsigned int i = 0; i < N_DISK - 2; ++i) {
1346     //check that zproj is in range
1347     if (izproj[i] < -(1 << (settings_.nzbitsstub(0) - 1)))
1348       continue;
1349     if (izproj[i] >= (1 << (settings_.nzbitsstub(0) - 1)))
1350       continue;
1351 
1352     //check that phiproj is in range
1353     if (iphiproj[i] >= (1 << settings_.nphibitsstub(5)) - 1)
1354       continue;
1355     if (iphiproj[i] <= 0)
1356       continue;
1357 
1358     //adjust bits for PS modules (no 2S modules in overlap seeds)
1359     iphiproj[i] >>= (settings_.nphibitsstub(5) - settings_.nphibitsstub(0));
1360 
1361     projs[i].init(settings_,
1362                   i,
1363                   iphiproj[i],
1364                   izproj[i],
1365                   ITC->der_phiL_final.ival(),
1366                   ITC->der_zL_final.ival(),
1367                   phiproj[i],
1368                   zproj[i],
1369                   phider[i],
1370                   zder[i],
1371                   phiprojapprox[i],
1372                   zprojapprox[i],
1373                   ITC->der_phiL_final.fval(),
1374                   ITC->der_zL_final.fval(),
1375                   true);
1376   }
1377 
1378   for (int i = 0; i < 4; ++i) {
1379     //check that phi projection in range
1380     if (iphiprojdisk[i] <= 0)
1381       continue;
1382     if (iphiprojdisk[i] >= (1 << settings_.nphibitsstub(0)) - 1)
1383       continue;
1384 
1385     //check that r projection in range
1386     if (irprojdisk[i] <= 0 || irprojdisk[i] > settings_.rmaxdisk() / ITC->rD_0_final.K())
1387       continue;
1388 
1389     projs[N_LAYER + i + 1].init(settings_,
1390                                 N_LAYER + i + 1,
1391                                 iphiprojdisk[i],
1392                                 irprojdisk[i],
1393                                 ITC->der_phiD_final.ival(),
1394                                 ITC->der_rD_final.ival(),
1395                                 phiprojdisk[i],
1396                                 rprojdisk[i],
1397                                 phiderdisk[i],
1398                                 rderdisk[i],
1399                                 phiprojdiskapprox[i],
1400                                 rprojdiskapprox[i],
1401                                 ITC->der_phiD_final.fval(),
1402                                 ITC->der_rD_final.fval(),
1403                                 true);
1404   }
1405 
1406   if (settings_.writeMonitorData("TPars")) {
1407     globals_->ofstream("trackletparsoverlap.txt")
1408         << "Trackpars " << layerdisk1_ - 5 << "   " << rinv << " " << irinv << " " << ITC->rinv_final.fval() << "   "
1409         << phi0 << " " << iphi0 << " " << ITC->phi0_final.fval() << "   " << t << " " << it << " "
1410         << ITC->t_final.fval() << "   " << z0 << " " << iz0 << " " << ITC->z0_final.fval() << endl;
1411   }
1412 
1413   Tracklet* tracklet = new Tracklet(settings_,
1414                                     iSeed_,
1415                                     innerFPGAStub,
1416                                     nullptr,
1417                                     outerFPGAStub,
1418                                     rinv,
1419                                     phi0,
1420                                     0.0,
1421                                     z0,
1422                                     t,
1423                                     rinvapprox,
1424                                     phi0approx,
1425                                     0.0,
1426                                     z0approx,
1427                                     tapprox,
1428                                     irinv,
1429                                     iphi0,
1430                                     0,
1431                                     iz0,
1432                                     it,
1433                                     projs,
1434                                     false,
1435                                     true);
1436 
1437   if (settings_.debugTracklet()) {
1438     edm::LogVerbatim("Tracklet") << "Found tracklet in overlap seed = " << iSeed_ << " " << tracklet << " " << iSector_;
1439   }
1440 
1441   tracklet->setTrackletIndex(trackletpars_->nTracklets());
1442   tracklet->setTCIndex(TCIndex_);
1443 
1444   if (settings_.writeMonitorData("Seeds")) {
1445     ofstream fout("seeds.txt", ofstream::app);
1446     fout << __FILE__ << ":" << __LINE__ << " " << name_ << "_" << iSector_ << " " << tracklet->getISeed() << endl;
1447     fout.close();
1448   }
1449   trackletpars_->addTracklet(tracklet);
1450 
1451   int layer = outerFPGAStub->layer().value() + 1;
1452 
1453   if (layer == 2) {
1454     if (tracklet->validProj(0)) {
1455       addLayerProj(tracklet, 1);
1456     }
1457   }
1458 
1459   for (unsigned int disk = 2; disk < 6; disk++) {
1460     if (layer == 2 && disk == 5)
1461       continue;
1462     if (tracklet->validProj(N_LAYER + disk - 1)) {
1463       addDiskProj(tracklet, disk);
1464     }
1465   }
1466 
1467   return true;
1468 }