Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-29 06:08:30

0001 #include "L1Trigger/DTTriggerPhase2/interface/MuonPathAssociator.h"
0002 #include "L1Trigger/DTTriggerPhase2/interface/MuonPathAnalyticAnalyzer.h"
0003 #include "L1Trigger/DTTriggerPhase2/interface/constants.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 
0006 using namespace edm;
0007 using namespace std;
0008 using namespace cmsdt;
0009 
0010 // ============================================================================
0011 // Constructors and destructor
0012 // ============================================================================
0013 MuonPathAssociator::MuonPathAssociator(const ParameterSet &pset,
0014                                        edm::ConsumesCollector &iC,
0015                                        std::shared_ptr<GlobalCoordsObtainer> &globalcoordsobtainer)
0016     : debug_(pset.getUntrackedParameter<bool>("debug")),
0017       clean_chi2_correlation_(pset.getParameter<bool>("clean_chi2_correlation")),
0018       useBX_correlation_(pset.getParameter<bool>("useBX_correlation")),
0019       allow_confirmation_(pset.getParameter<bool>("allow_confirmation")),
0020       dT0_correlate_TP_(pset.getParameter<double>("dT0_correlate_TP")),
0021       dBX_correlate_TP_(pset.getParameter<int>("dBX_correlate_TP")),
0022       dTanPsi_correlate_TP_(pset.getParameter<double>("dTanPsi_correlate_TP")),
0023       minx_match_2digis_(pset.getParameter<double>("minx_match_2digis")),
0024       chi2corTh_(pset.getParameter<double>("chi2corTh")) {
0025   // Obtention of parameters
0026 
0027   if (debug_)
0028     LogDebug("MuonPathAssociator") << "MuonPathAssociator: constructor";
0029 
0030   //shift
0031   int rawId;
0032   shift_filename_ = pset.getParameter<edm::FileInPath>("shift_filename");
0033   std::ifstream ifin3(shift_filename_.fullPath());
0034   double shift;
0035   if (ifin3.fail()) {
0036     throw cms::Exception("Missing Input File")
0037         << "MuonPathAnalyzerPerSL::MuonPathAnalyzerPerSL() -  Cannot find " << shift_filename_.fullPath();
0038   }
0039   while (ifin3.good()) {
0040     ifin3 >> rawId >> shift;
0041     shiftinfo_[rawId] = shift;
0042   }
0043 
0044   dtGeomH_ = iC.esConsumes<DTGeometry, MuonGeometryRecord, edm::Transition::BeginRun>();
0045   globalcoordsobtainer_ = globalcoordsobtainer;
0046 }
0047 
0048 MuonPathAssociator::~MuonPathAssociator() {
0049   if (debug_)
0050     LogDebug("MuonPathAssociator") << "MuonPathAssociator: destructor";
0051 }
0052 
0053 // ============================================================================
0054 // Main methods (initialise, run, finish)
0055 // ============================================================================
0056 void MuonPathAssociator::initialise(const edm::EventSetup &iEventSetup) {
0057   if (debug_)
0058     LogDebug("MuonPathAssociator") << "MuonPathAssociator::initialiase";
0059 
0060   auto geom = iEventSetup.getHandle(dtGeomH_);
0061   dtGeo_ = &(*geom);
0062 }
0063 
0064 void MuonPathAssociator::run(edm::Event &iEvent,
0065                              const edm::EventSetup &iEventSetup,
0066                              edm::Handle<DTDigiCollection> digis,
0067                              std::vector<metaPrimitive> &inMPaths,
0068                              std::vector<metaPrimitive> &outMPaths) {
0069   if (dT0_correlate_TP_)
0070     correlateMPaths(digis, inMPaths, outMPaths);
0071   else {
0072     outMPaths.insert(outMPaths.end(), inMPaths.begin(), inMPaths.end());
0073   }
0074 }
0075 
0076 void MuonPathAssociator::finish() {
0077   if (debug_)
0078     LogDebug("MuonPathAssociator") << "MuonPathAssociator: finish";
0079 };
0080 
0081 void MuonPathAssociator::correlateMPaths(edm::Handle<DTDigiCollection> dtdigis,
0082                                          std::vector<metaPrimitive> &inMPaths,
0083                                          std::vector<metaPrimitive> &outMPaths) {
0084   if (debug_)
0085     LogDebug("MuonPathAssociator") << "starting correlation";
0086 
0087   for (int wh = -2; wh <= 2; wh++) {      //wheel num: -2, -1, 0, +1, +2
0088     for (int st = 1; st <= 4; st++) {     //station num (MB): 1, 2, 3, 4
0089       for (int se = 1; se <= 14; se++) {  //sector number: 1-12, special sectors 13, 14 to account for bigger MB4s
0090         if (se >= 13 && st != 4)
0091           continue;
0092 
0093         DTChamberId ChId(wh, st, se);
0094         DTSuperLayerId sl1Id(wh, st, se, 1);
0095         DTSuperLayerId sl3Id(wh, st, se, 3);
0096 
0097         //filterSL1
0098         std::vector<metaPrimitive> SL1metaPrimitives;
0099         for (const auto &metaprimitiveIt : inMPaths) {
0100           if (metaprimitiveIt.rawId == sl1Id.rawId())
0101             SL1metaPrimitives.push_back(metaprimitiveIt);
0102         }
0103 
0104         //filterSL3
0105         std::vector<metaPrimitive> SL3metaPrimitives;
0106         for (const auto &metaprimitiveIt : inMPaths) {
0107           if (metaprimitiveIt.rawId == sl3Id.rawId())
0108             SL3metaPrimitives.push_back(metaprimitiveIt);
0109         }
0110 
0111         if (SL1metaPrimitives.empty() and SL3metaPrimitives.empty())
0112           continue;
0113 
0114         if (debug_)
0115           LogDebug("MuonPathAssociator") << "correlating " << SL1metaPrimitives.size() << " metaPrim in SL1 and "
0116                                          << SL3metaPrimitives.size() << " in SL3 for " << sl3Id;
0117 
0118         bool at_least_one_correlation = false;
0119         bool at_least_one_SL1_confirmation = false;
0120         bool at_least_one_SL3_confirmation = false;
0121 
0122         bool useFitSL1[SL1metaPrimitives.size()];
0123         for (unsigned int i = 0; i < SL1metaPrimitives.size(); i++)
0124           useFitSL1[i] = false;
0125         bool useFitSL3[SL3metaPrimitives.size()];
0126         for (unsigned int i = 0; i < SL3metaPrimitives.size(); i++)
0127           useFitSL3[i] = false;
0128 
0129         //SL1-SL3
0130         vector<metaPrimitive> chamberMetaPrimitives;
0131         vector<metaPrimitive> confirmedMetaPrimitives;
0132         vector<metaPrimitive> normalMetaPrimitives;
0133         int sl1 = 0;
0134         int sl3 = 0;
0135         for (auto SL1metaPrimitive = SL1metaPrimitives.begin(); SL1metaPrimitive != SL1metaPrimitives.end();
0136              ++SL1metaPrimitive, sl1++, sl3 = -1) {
0137           if (clean_chi2_correlation_)
0138             at_least_one_correlation = false;
0139           for (auto SL3metaPrimitive = SL3metaPrimitives.begin(); SL3metaPrimitive != SL3metaPrimitives.end();
0140                ++SL3metaPrimitive, sl3++) {
0141             if (std::abs(SL1metaPrimitive->tanPhi - SL3metaPrimitive->tanPhi) > dTanPsi_correlate_TP_)
0142               continue;  //TanPsi match, SliceTest only
0143             if (useBX_correlation_) {
0144               if (abs(round(SL1metaPrimitive->t0 / (float)LHC_CLK_FREQ) -
0145                       round(SL3metaPrimitive->t0 / (float)LHC_CLK_FREQ)) > dBX_correlate_TP_)
0146                 continue;  //BX match
0147             } else {
0148               if (std::abs(SL1metaPrimitive->t0 - SL3metaPrimitive->t0) >= dT0_correlate_TP_)
0149                 continue;  //time match
0150             }
0151             long int PosSL1 = (int)round(INCREASED_RES_POS_POW * 10 * SL1metaPrimitive->x);
0152             long int PosSL3 = (int)round(INCREASED_RES_POS_POW * 10 * SL3metaPrimitive->x);
0153             double NewSlope = -999.;
0154 
0155             long int pos = (PosSL3 + PosSL1) / 2;
0156             // FW always rounds down (e.g 29.5 -> 29, -29.5 -> -30). For negative numbers, we don't do the same.
0157             // Let's fix it (this also happens for the slope)
0158             if (((PosSL3 + PosSL1) % 2 != 0) && (pos < 0)) {
0159               pos--;
0160             }
0161 
0162             long int difPos_mm_x4 = PosSL3 - PosSL1;
0163             long int tanPsi_x4096_x128 = (difPos_mm_x4)*VERT_PHI1_PHI3_INV;
0164             long int tanpsi = tanPsi_x4096_x128 / ((long int)pow(2, 5 + INCREASED_RES_POS));
0165             if (tanpsi < 0 && tanPsi_x4096_x128 % ((long int)pow(2, 5 + INCREASED_RES_POS)) != 0)
0166               tanpsi--;
0167             NewSlope = -tanpsi / (double)INCREASED_RES_SLOPE_POW;
0168             double MeanT0 = (SL1metaPrimitive->t0 + SL3metaPrimitive->t0) / 2;
0169             double MeanPos = (PosSL3 + PosSL1) / (2. * INCREASED_RES_POS_POW * 10);
0170 
0171             DTSuperLayerId SLId1(SL1metaPrimitive->rawId);
0172             DTSuperLayerId SLId3(SL3metaPrimitive->rawId);
0173             DTWireId wireId1(SLId1, 2, 1);
0174             DTWireId wireId3(SLId3, 2, 1);
0175 
0176             int shift_sl1 = int(round(shiftinfo_[wireId1.rawId()] * INCREASED_RES_POS_POW * 10));
0177             int shift_sl3 = int(round(shiftinfo_[wireId3.rawId()] * INCREASED_RES_POS_POW * 10));
0178             if (shift_sl1 < shift_sl3) {
0179               pos -= shift_sl1;
0180             } else
0181               pos -= shift_sl3;
0182 
0183             int wi[8], tdc[8], lat[8];
0184             wi[0] = SL1metaPrimitive->wi1;
0185             tdc[0] = SL1metaPrimitive->tdc1;
0186             lat[0] = SL1metaPrimitive->lat1;
0187             wi[1] = SL1metaPrimitive->wi2;
0188             tdc[1] = SL1metaPrimitive->tdc2;
0189             lat[1] = SL1metaPrimitive->lat2;
0190             wi[2] = SL1metaPrimitive->wi3;
0191             tdc[2] = SL1metaPrimitive->tdc3;
0192             lat[2] = SL1metaPrimitive->lat3;
0193             wi[3] = SL1metaPrimitive->wi4;
0194             tdc[3] = SL1metaPrimitive->tdc4;
0195             lat[3] = SL1metaPrimitive->lat4;
0196             wi[4] = SL3metaPrimitive->wi1;
0197             tdc[4] = SL3metaPrimitive->tdc1;
0198             lat[4] = SL3metaPrimitive->lat1;
0199             wi[5] = SL3metaPrimitive->wi2;
0200             tdc[5] = SL3metaPrimitive->tdc2;
0201             lat[5] = SL3metaPrimitive->lat2;
0202             wi[6] = SL3metaPrimitive->wi3;
0203             tdc[6] = SL3metaPrimitive->tdc3;
0204             lat[6] = SL3metaPrimitive->lat3;
0205             wi[7] = SL3metaPrimitive->wi4;
0206             tdc[7] = SL3metaPrimitive->tdc4;
0207             lat[7] = SL3metaPrimitive->lat4;
0208 
0209             long int chi2 = 0;
0210 
0211             long int Z_FACTOR_CORR[8] = {-6, -2, 2, 6, -6, -2, 2, 6};
0212 
0213             for (int i = 0; i < 8; i++) {
0214               int sign = 2 * (i / 4) - 1;
0215               Z_FACTOR_CORR[i] = Z_FACTOR_CORR[i] * CELL_HEIGHT + CH_CENTER_TO_MID_SL_X2 * sign;
0216             }
0217             long int sum_A, sum_B;
0218             for (int i = 0; i < NUM_LAYERS_2SL; i++) {
0219               long int shift, slTime;
0220               if (i / NUM_LAYERS == 0) {  // layers 0 - 3 -> SL1
0221                 shift = shift_sl1;
0222                 slTime = SL1metaPrimitive->t0;
0223               } else {  // layers 4 - 7 -> SL3
0224                 shift = shift_sl3;
0225                 slTime = SL3metaPrimitive->t0;
0226               }
0227               if (wi[i] != -1) {
0228                 long int drift_dist_um_x4 = DRIFT_SPEED_X4 * (((long int)tdc[i]) - slTime);
0229                 long int wireHorizPos_x4 =
0230                     (CELL_LENGTH * wi[i] + ((i + 1) % 2) * CELL_SEMILENGTH) * INCREASED_RES_POS_POW;
0231                 long int pos_mm_x4;
0232 
0233                 if (lat[i] == 0) {
0234                   pos_mm_x4 = wireHorizPos_x4 - (drift_dist_um_x4 >> 10);
0235                 } else {
0236                   pos_mm_x4 = wireHorizPos_x4 + (drift_dist_um_x4 >> 10);
0237                 }
0238                 sum_A = shift + pos_mm_x4 - (long int)round(MeanPos * 10 * INCREASED_RES_POS_POW);
0239                 sum_A = sum_A << (14 - INCREASED_RES_POS);
0240                 sum_B = Z_FACTOR_CORR[i] * (long int)round(-NewSlope * INCREASED_RES_SLOPE_POW);
0241                 chi2 += ((sum_A - sum_B) * (sum_A - sum_B)) >> 2;
0242               }
0243             }
0244 
0245             double newChi2 = (double)(chi2 >> INCREASED_RES_POS_POW) / (1024. * 100.);
0246 
0247             if (newChi2 > chi2corTh_)
0248               continue;
0249 
0250             // Fill the used vectors
0251             useFitSL1[sl1] = true;
0252             useFitSL3[sl3] = true;
0253 
0254             int quality = 0;
0255             if (SL3metaPrimitive->quality == LOWQ and SL1metaPrimitive->quality == LOWQ)
0256               quality = LOWLOWQ;
0257 
0258             if ((SL3metaPrimitive->quality == HIGHQ && SL1metaPrimitive->quality == LOWQ) or
0259                 (SL1metaPrimitive->quality == HIGHQ && SL3metaPrimitive->quality == LOWQ))
0260               quality = HIGHLOWQ;
0261 
0262             if (SL3metaPrimitive->quality == HIGHQ && SL1metaPrimitive->quality == HIGHQ)
0263               quality = HIGHHIGHQ;
0264 
0265             double phi = -999.;
0266             double phiB = -999.;
0267             double phi_cmssw = -999.;
0268             double phiB_cmssw = -999.;
0269             double z = 0;
0270             if (ChId.station() >= 3)
0271               z = Z_SHIFT_MB4;
0272             GlobalPoint jm_x_cmssw_global = dtGeo_->chamber(ChId)->toGlobal(
0273                 LocalPoint(MeanPos, 0., z));  //Jm_x is already extrapolated to the middle of the SL
0274             int thisec = ChId.sector();
0275             if (se == 13)
0276               thisec = 4;
0277             if (se == 14)
0278               thisec = 10;
0279             phi_cmssw = jm_x_cmssw_global.phi() - PHI_CONV * (thisec - 1);
0280             double psi = atan(NewSlope);
0281             phiB_cmssw = hasPosRF(ChId.wheel(), ChId.sector()) ? psi - phi_cmssw : -psi - phi_cmssw;
0282 
0283             auto global_coords = globalcoordsobtainer_->get_global_coordinates(ChId.rawId(), 0, pos, tanpsi);
0284             phi = global_coords[0];
0285             phiB = global_coords[1];
0286 
0287             if (!clean_chi2_correlation_)
0288               outMPaths.emplace_back(ChId.rawId(),
0289                                      MeanT0,
0290                                      MeanPos,
0291                                      NewSlope,
0292                                      phi,
0293                                      phiB,
0294                                      phi_cmssw,
0295                                      phiB_cmssw,
0296                                      newChi2,
0297                                      quality,
0298                                      SL1metaPrimitive->wi1,
0299                                      SL1metaPrimitive->tdc1,
0300                                      SL1metaPrimitive->lat1,
0301                                      SL1metaPrimitive->wi2,
0302                                      SL1metaPrimitive->tdc2,
0303                                      SL1metaPrimitive->lat2,
0304                                      SL1metaPrimitive->wi3,
0305                                      SL1metaPrimitive->tdc3,
0306                                      SL1metaPrimitive->lat3,
0307                                      SL1metaPrimitive->wi4,
0308                                      SL1metaPrimitive->tdc4,
0309                                      SL1metaPrimitive->lat4,
0310                                      SL3metaPrimitive->wi1,
0311                                      SL3metaPrimitive->tdc1,
0312                                      SL3metaPrimitive->lat1,
0313                                      SL3metaPrimitive->wi2,
0314                                      SL3metaPrimitive->tdc2,
0315                                      SL3metaPrimitive->lat2,
0316                                      SL3metaPrimitive->wi3,
0317                                      SL3metaPrimitive->tdc3,
0318                                      SL3metaPrimitive->lat3,
0319                                      SL3metaPrimitive->wi4,
0320                                      SL3metaPrimitive->tdc4,
0321                                      SL3metaPrimitive->lat4);
0322             else
0323               chamberMetaPrimitives.emplace_back(ChId.rawId(),
0324                                                  MeanT0,
0325                                                  MeanPos,
0326                                                  NewSlope,
0327                                                  phi,
0328                                                  phiB,
0329                                                  phi_cmssw,
0330                                                  phiB_cmssw,
0331                                                  newChi2,
0332                                                  quality,
0333                                                  SL1metaPrimitive->wi1,
0334                                                  SL1metaPrimitive->tdc1,
0335                                                  SL1metaPrimitive->lat1,
0336                                                  SL1metaPrimitive->wi2,
0337                                                  SL1metaPrimitive->tdc2,
0338                                                  SL1metaPrimitive->lat2,
0339                                                  SL1metaPrimitive->wi3,
0340                                                  SL1metaPrimitive->tdc3,
0341                                                  SL1metaPrimitive->lat3,
0342                                                  SL1metaPrimitive->wi4,
0343                                                  SL1metaPrimitive->tdc4,
0344                                                  SL1metaPrimitive->lat4,
0345                                                  SL3metaPrimitive->wi1,
0346                                                  SL3metaPrimitive->tdc1,
0347                                                  SL3metaPrimitive->lat1,
0348                                                  SL3metaPrimitive->wi2,
0349                                                  SL3metaPrimitive->tdc2,
0350                                                  SL3metaPrimitive->lat2,
0351                                                  SL3metaPrimitive->wi3,
0352                                                  SL3metaPrimitive->tdc3,
0353                                                  SL3metaPrimitive->lat3,
0354                                                  SL3metaPrimitive->wi4,
0355                                                  SL3metaPrimitive->tdc4,
0356                                                  SL3metaPrimitive->lat4);
0357 
0358             at_least_one_correlation = true;
0359           }
0360 
0361           if (at_least_one_correlation == false &&
0362               allow_confirmation_ == true) {  //no correlation was found, trying with pairs of two digis in the other SL
0363             int matched_digis = 0;
0364             double minx = minx_match_2digis_;
0365             double min2x = minx_match_2digis_;
0366             int best_tdc = -1;
0367             int next_tdc = -1;
0368             int best_wire = -1;
0369             int next_wire = -1;
0370             int best_layer = -1;
0371             int next_layer = -1;
0372             int best_lat = -1;
0373             int next_lat = -1;
0374             int lat = -1;
0375             for (const auto &dtLayerId_It : *dtdigis) {
0376               const DTLayerId dtLId = dtLayerId_It.first;
0377               // creating a new DTSuperLayerId object to compare with the required SL id
0378               const DTSuperLayerId dtSLId(dtLId.wheel(), dtLId.station(), dtLId.sector(), dtLId.superLayer());
0379               if (dtSLId.rawId() != sl3Id.rawId())
0380                 continue;
0381               double l_shift = 0;
0382               if (dtLId.layer() == 4)
0383                 l_shift = X_POS_L4;
0384               else if (dtLId.layer() == 3)
0385                 l_shift = X_POS_L3;
0386               else if (dtLId.layer() == 2)
0387                 l_shift = -1 * X_POS_L3;
0388               else if (dtLId.layer() == 1)
0389                 l_shift = -1 * X_POS_L4;
0390               double x_inSL3 = SL1metaPrimitive->x - SL1metaPrimitive->tanPhi * (VERT_PHI1_PHI3 + l_shift);
0391               for (auto digiIt = (dtLayerId_It.second).first; digiIt != (dtLayerId_It.second).second; ++digiIt) {
0392                 DTWireId wireId(dtLId, (*digiIt).wire());
0393                 if ((*digiIt).time() < SL1metaPrimitive->t0)
0394                   continue;
0395                 double x_wire =
0396                     shiftinfo_[wireId.rawId()] + ((*digiIt).time() - SL1metaPrimitive->t0) * DRIFT_SPEED / 10.;
0397                 double x_wire_left =
0398                     shiftinfo_[wireId.rawId()] - ((*digiIt).time() - SL1metaPrimitive->t0) * DRIFT_SPEED / 10.;
0399                 lat = 1;
0400                 if (std::abs(x_inSL3 - x_wire) > std::abs(x_inSL3 - x_wire_left)) {
0401                   x_wire = x_wire_left;  //choose the closest laterality
0402                   lat = 0;
0403                 }
0404                 if (std::abs(x_inSL3 - x_wire) < minx) {
0405                   // different layer than the stored in best, hit added, matched_digis++;. This approach in somewhat
0406                   // buggy, as we could have stored as best LayerX -> LayerY -> LayerX, and this should
0407                   // count only as 2 hits. However, as we confirm with at least 2 hits, having 2 or more
0408                   // makes no difference
0409                   if (dtLId.layer() != best_layer) {
0410                     minx = std::abs(x_inSL3 - x_wire);
0411                     next_wire = best_wire;
0412                     next_tdc = best_tdc;
0413                     next_layer = best_layer;
0414                     next_lat = best_lat;
0415 
0416                     best_wire = (*digiIt).wire();
0417                     best_tdc = (*digiIt).time();
0418                     best_layer = dtLId.layer();
0419                     best_lat = lat;
0420                     matched_digis++;
0421                   } else if (dtLId.layer() ==
0422                              best_layer) {  // same layer than stored, just substituting the hit, no matched_digis++;
0423                     best_wire = (*digiIt).wire();
0424                     best_tdc = (*digiIt).time();
0425                     best_layer = dtLId.layer();
0426                     best_lat = lat;
0427                   }
0428                 } else if ((std::abs(x_inSL3 - x_wire) >= minx) && (std::abs(x_inSL3 - x_wire) < min2x)) {
0429                   // same layer than the stored in best, no hit added
0430                   if (dtLId.layer() == best_layer)
0431                     continue;
0432                   // different layer than the stored in next, hit added. This approach in somewhat
0433                   // buggy, as we could have stored as next LayerX -> LayerY -> LayerX, and this should
0434                   // count only as 2 hits. However, as we confirm with at least 2 hits, having 2 or more
0435                   // makes no difference
0436                   else if (dtLId.layer() != next_layer)
0437                     matched_digis++;
0438                   // whether the layer is the same for this hit and the stored in next, we substitute
0439                   // the one stored and modify the min distance
0440                   min2x = std::abs(x_inSL3 - x_wire);
0441                   next_wire = (*digiIt).wire();
0442                   next_tdc = (*digiIt).time();
0443                   next_layer = dtLId.layer();
0444                   next_lat = lat;
0445                 }
0446               }
0447             }
0448             if (matched_digis >= 2 and best_layer != -1 and next_layer != -1) {
0449               int new_quality = CHIGHQ;
0450               if (SL1metaPrimitive->quality == LOWQ)
0451                 new_quality = CLOWQ;
0452 
0453               int wi1 = -1;
0454               int tdc1 = -1;
0455               int lat1 = -1;
0456               int wi2 = -1;
0457               int tdc2 = -1;
0458               int lat2 = -1;
0459               int wi3 = -1;
0460               int tdc3 = -1;
0461               int lat3 = -1;
0462               int wi4 = -1;
0463               int tdc4 = -1;
0464               int lat4 = -1;
0465 
0466               if (next_layer == 1) {
0467                 wi1 = next_wire;
0468                 tdc1 = next_tdc;
0469                 lat1 = next_lat;
0470               }
0471               if (next_layer == 2) {
0472                 wi2 = next_wire;
0473                 tdc2 = next_tdc;
0474                 lat2 = next_lat;
0475               }
0476               if (next_layer == 3) {
0477                 wi3 = next_wire;
0478                 tdc3 = next_tdc;
0479                 lat3 = next_lat;
0480               }
0481               if (next_layer == 4) {
0482                 wi4 = next_wire;
0483                 tdc4 = next_tdc;
0484                 lat4 = next_lat;
0485               }
0486 
0487               if (best_layer == 1) {
0488                 wi1 = best_wire;
0489                 tdc1 = best_tdc;
0490                 lat1 = best_lat;
0491               }
0492               if (best_layer == 2) {
0493                 wi2 = best_wire;
0494                 tdc2 = best_tdc;
0495                 lat2 = best_lat;
0496               }
0497               if (best_layer == 3) {
0498                 wi3 = best_wire;
0499                 tdc3 = best_tdc;
0500                 lat3 = best_lat;
0501               }
0502               if (best_layer == 4) {
0503                 wi4 = best_wire;
0504                 tdc4 = best_tdc;
0505                 lat4 = best_lat;
0506               }
0507 
0508               if (!clean_chi2_correlation_)
0509                 outMPaths.emplace_back(metaPrimitive({ChId.rawId(),
0510                                                       SL1metaPrimitive->t0,
0511                                                       SL1metaPrimitive->x,
0512                                                       SL1metaPrimitive->tanPhi,
0513                                                       SL1metaPrimitive->phi,
0514                                                       SL1metaPrimitive->phiB,
0515                                                       SL1metaPrimitive->phi_cmssw,
0516                                                       SL1metaPrimitive->phiB_cmssw,
0517                                                       SL1metaPrimitive->chi2,
0518                                                       new_quality,
0519                                                       SL1metaPrimitive->wi1,
0520                                                       SL1metaPrimitive->tdc1,
0521                                                       SL1metaPrimitive->lat1,
0522                                                       SL1metaPrimitive->wi2,
0523                                                       SL1metaPrimitive->tdc2,
0524                                                       SL1metaPrimitive->lat2,
0525                                                       SL1metaPrimitive->wi3,
0526                                                       SL1metaPrimitive->tdc3,
0527                                                       SL1metaPrimitive->lat3,
0528                                                       SL1metaPrimitive->wi4,
0529                                                       SL1metaPrimitive->tdc4,
0530                                                       SL1metaPrimitive->lat4,
0531                                                       wi1,
0532                                                       tdc1,
0533                                                       lat1,
0534                                                       wi2,
0535                                                       tdc2,
0536                                                       lat2,
0537                                                       wi3,
0538                                                       tdc3,
0539                                                       lat3,
0540                                                       wi4,
0541                                                       tdc4,
0542                                                       lat4,
0543                                                       -1}));
0544               else
0545                 confirmedMetaPrimitives.emplace_back(metaPrimitive({ChId.rawId(),
0546                                                                     SL1metaPrimitive->t0,
0547                                                                     SL1metaPrimitive->x,
0548                                                                     SL1metaPrimitive->tanPhi,
0549                                                                     SL1metaPrimitive->phi,
0550                                                                     SL1metaPrimitive->phiB,
0551                                                                     SL1metaPrimitive->phi_cmssw,
0552                                                                     SL1metaPrimitive->phiB_cmssw,
0553                                                                     SL1metaPrimitive->chi2,
0554                                                                     new_quality,
0555                                                                     SL1metaPrimitive->wi1,
0556                                                                     SL1metaPrimitive->tdc1,
0557                                                                     SL1metaPrimitive->lat1,
0558                                                                     SL1metaPrimitive->wi2,
0559                                                                     SL1metaPrimitive->tdc2,
0560                                                                     SL1metaPrimitive->lat2,
0561                                                                     SL1metaPrimitive->wi3,
0562                                                                     SL1metaPrimitive->tdc3,
0563                                                                     SL1metaPrimitive->lat3,
0564                                                                     SL1metaPrimitive->wi4,
0565                                                                     SL1metaPrimitive->tdc4,
0566                                                                     SL1metaPrimitive->lat4,
0567                                                                     wi1,
0568                                                                     tdc1,
0569                                                                     lat1,
0570                                                                     wi2,
0571                                                                     tdc2,
0572                                                                     lat2,
0573                                                                     wi3,
0574                                                                     tdc3,
0575                                                                     lat3,
0576                                                                     wi4,
0577                                                                     tdc4,
0578                                                                     lat4,
0579                                                                     -1}));
0580               useFitSL1[sl1] = true;
0581               at_least_one_SL1_confirmation = true;
0582             }
0583           }
0584         }
0585 
0586         //finish SL1-SL3
0587 
0588         //SL3-SL1
0589         sl3 = 0;
0590         for (auto SL3metaPrimitive = SL3metaPrimitives.begin(); SL3metaPrimitive != SL3metaPrimitives.end();
0591              ++SL3metaPrimitive, sl3++) {
0592           [[clang::suppress]] if (useFitSL3[sl3])
0593             continue;
0594           if ((at_least_one_correlation == false || clean_chi2_correlation_) &&
0595               allow_confirmation_) {  //no correlation was found, trying with pairs of two digis in the other SL
0596 
0597             int matched_digis = 0;
0598             double minx = minx_match_2digis_;
0599             double min2x = minx_match_2digis_;
0600             int best_tdc = -1;
0601             int next_tdc = -1;
0602             int best_wire = -1;
0603             int next_wire = -1;
0604             int best_layer = -1;
0605             int next_layer = -1;
0606             int best_lat = -1;
0607             int next_lat = -1;
0608             int lat = -1;
0609 
0610             for (const auto &dtLayerId_It : *dtdigis) {
0611               const DTLayerId dtLId = dtLayerId_It.first;
0612               // creating a new DTSuperLayerId object to compare with the required SL id
0613               const DTSuperLayerId dtSLId(dtLId.wheel(), dtLId.station(), dtLId.sector(), dtLId.superLayer());
0614               if (dtSLId.rawId() != sl1Id.rawId())
0615                 continue;
0616               double l_shift = 0;
0617               if (dtLId.layer() == 4)
0618                 l_shift = X_POS_L4;
0619               if (dtLId.layer() == 3)
0620                 l_shift = X_POS_L3;
0621               if (dtLId.layer() == 2)
0622                 l_shift = -1 * X_POS_L3;
0623               if (dtLId.layer() == 1)
0624                 l_shift = -1 * X_POS_L4;
0625               double x_inSL1 = SL3metaPrimitive->x + SL3metaPrimitive->tanPhi * (VERT_PHI1_PHI3 - l_shift);
0626               for (auto digiIt = (dtLayerId_It.second).first; digiIt != (dtLayerId_It.second).second; ++digiIt) {
0627                 DTWireId wireId(dtLId, (*digiIt).wire());
0628                 if ((*digiIt).time() < SL3metaPrimitive->t0)
0629                   continue;
0630                 double x_wire =
0631                     shiftinfo_[wireId.rawId()] + ((*digiIt).time() - SL3metaPrimitive->t0) * DRIFT_SPEED / 10.;
0632                 double x_wire_left =
0633                     shiftinfo_[wireId.rawId()] - ((*digiIt).time() - SL3metaPrimitive->t0) * DRIFT_SPEED / 10.;
0634                 lat = 1;
0635                 if (std::abs(x_inSL1 - x_wire) > std::abs(x_inSL1 - x_wire_left)) {
0636                   x_wire = x_wire_left;  //choose the closest laterality
0637                   lat = 0;
0638                 }
0639                 if (std::abs(x_inSL1 - x_wire) < minx) {
0640                   // different layer than the stored in best, hit added, matched_digis++;. This approach in somewhat
0641                   // buggy, as we could have stored as best LayerX -> LayerY -> LayerX, and this should
0642                   // count only as 2 hits. However, as we confirm with at least 2 hits, having 2 or more
0643                   // makes no difference
0644                   if (dtLId.layer() != best_layer) {
0645                     minx = std::abs(x_inSL1 - x_wire);
0646                     next_wire = best_wire;
0647                     next_tdc = best_tdc;
0648                     next_layer = best_layer;
0649                     next_lat = best_lat;
0650 
0651                     best_wire = (*digiIt).wire();
0652                     best_tdc = (*digiIt).time();
0653                     best_layer = dtLId.layer();
0654                     best_lat = lat;
0655                     matched_digis++;
0656                   } else if (dtLId.layer() ==
0657                              best_layer) {  // same layer than stored, just substituting the hit, no matched_digis++;
0658                     best_wire = (*digiIt).wire();
0659                     best_tdc = (*digiIt).time();
0660                     best_layer = dtLId.layer();
0661                     best_lat = lat;
0662                   }
0663                 } else if ((std::abs(x_inSL1 - x_wire) >= minx) && (std::abs(x_inSL1 - x_wire) < min2x)) {
0664                   // same layer than the stored in best, no hit added
0665                   if (dtLId.layer() == best_layer)
0666                     continue;
0667                   // different layer than the stored in next, hit added. This approach in somewhat
0668                   // buggy, as we could have stored as next LayerX -> LayerY -> LayerX, and this should
0669                   // count only as 2 hits. However, as we confirm with at least 2 hits, having 2 or more
0670                   // makes no difference
0671                   else if (dtLId.layer() != next_layer)
0672                     matched_digis++;
0673                   // whether the layer is the same for this hit and the stored in next, we substitute
0674                   // the one stored and modify the min distance
0675                   min2x = std::abs(x_inSL1 - x_wire);
0676                   next_wire = (*digiIt).wire();
0677                   next_tdc = (*digiIt).time();
0678                   next_layer = dtLId.layer();
0679                   next_lat = lat;
0680                 }
0681               }
0682             }
0683             if (matched_digis >= 2 and best_layer != -1 and next_layer != -1) {
0684               int new_quality = CHIGHQ;
0685               if (SL3metaPrimitive->quality == LOWQ)
0686                 new_quality = CLOWQ;
0687 
0688               int wi1 = -1;
0689               int tdc1 = -1;
0690               int lat1 = -1;
0691               int wi2 = -1;
0692               int tdc2 = -1;
0693               int lat2 = -1;
0694               int wi3 = -1;
0695               int tdc3 = -1;
0696               int lat3 = -1;
0697               int wi4 = -1;
0698               int tdc4 = -1;
0699               int lat4 = -1;
0700 
0701               if (next_layer == 1) {
0702                 wi1 = next_wire;
0703                 tdc1 = next_tdc;
0704                 lat1 = next_lat;
0705               }
0706               if (next_layer == 2) {
0707                 wi2 = next_wire;
0708                 tdc2 = next_tdc;
0709                 lat2 = next_lat;
0710               }
0711               if (next_layer == 3) {
0712                 wi3 = next_wire;
0713                 tdc3 = next_tdc;
0714                 lat3 = next_lat;
0715               }
0716               if (next_layer == 4) {
0717                 wi4 = next_wire;
0718                 tdc4 = next_tdc;
0719                 lat4 = next_lat;
0720               }
0721 
0722               if (best_layer == 1) {
0723                 wi1 = best_wire;
0724                 tdc1 = best_tdc;
0725                 lat1 = best_lat;
0726               }
0727               if (best_layer == 2) {
0728                 wi2 = best_wire;
0729                 tdc2 = best_tdc;
0730                 lat2 = best_lat;
0731               }
0732               if (best_layer == 3) {
0733                 wi3 = best_wire;
0734                 tdc3 = best_tdc;
0735                 lat3 = best_lat;
0736               }
0737               if (best_layer == 4) {
0738                 wi4 = best_wire;
0739                 tdc4 = best_tdc;
0740                 lat4 = best_lat;
0741               }
0742 
0743               if (!clean_chi2_correlation_)
0744                 outMPaths.push_back(metaPrimitive({ChId.rawId(),
0745                                                    SL3metaPrimitive->t0,
0746                                                    SL3metaPrimitive->x,
0747                                                    SL3metaPrimitive->tanPhi,
0748                                                    SL3metaPrimitive->phi,
0749                                                    SL3metaPrimitive->phiB,
0750                                                    SL3metaPrimitive->phi_cmssw,
0751                                                    SL3metaPrimitive->phiB_cmssw,
0752                                                    SL3metaPrimitive->chi2,
0753                                                    new_quality,
0754                                                    wi1,
0755                                                    tdc1,
0756                                                    lat1,
0757                                                    wi2,
0758                                                    tdc2,
0759                                                    lat2,
0760                                                    wi3,
0761                                                    tdc3,
0762                                                    lat3,
0763                                                    wi4,
0764                                                    tdc4,
0765                                                    lat4,
0766                                                    SL3metaPrimitive->wi1,
0767                                                    SL3metaPrimitive->tdc1,
0768                                                    SL3metaPrimitive->lat1,
0769                                                    SL3metaPrimitive->wi2,
0770                                                    SL3metaPrimitive->tdc2,
0771                                                    SL3metaPrimitive->lat2,
0772                                                    SL3metaPrimitive->wi3,
0773                                                    SL3metaPrimitive->tdc3,
0774                                                    SL3metaPrimitive->lat3,
0775                                                    SL3metaPrimitive->wi4,
0776                                                    SL3metaPrimitive->tdc4,
0777                                                    SL3metaPrimitive->lat4,
0778                                                    -1}));
0779               else
0780                 confirmedMetaPrimitives.push_back(metaPrimitive({ChId.rawId(),
0781                                                                  SL3metaPrimitive->t0,
0782                                                                  SL3metaPrimitive->x,
0783                                                                  SL3metaPrimitive->tanPhi,
0784                                                                  SL3metaPrimitive->phi,
0785                                                                  SL3metaPrimitive->phiB,
0786                                                                  SL3metaPrimitive->phi_cmssw,
0787                                                                  SL3metaPrimitive->phiB_cmssw,
0788                                                                  SL3metaPrimitive->chi2,
0789                                                                  new_quality,
0790                                                                  wi1,
0791                                                                  tdc1,
0792                                                                  lat1,
0793                                                                  wi2,
0794                                                                  tdc2,
0795                                                                  lat2,
0796                                                                  wi3,
0797                                                                  tdc3,
0798                                                                  lat3,
0799                                                                  wi4,
0800                                                                  tdc4,
0801                                                                  lat4,
0802                                                                  SL3metaPrimitive->wi1,
0803                                                                  SL3metaPrimitive->tdc1,
0804                                                                  SL3metaPrimitive->lat1,
0805                                                                  SL3metaPrimitive->wi2,
0806                                                                  SL3metaPrimitive->tdc2,
0807                                                                  SL3metaPrimitive->lat2,
0808                                                                  SL3metaPrimitive->wi3,
0809                                                                  SL3metaPrimitive->tdc3,
0810                                                                  SL3metaPrimitive->lat3,
0811                                                                  SL3metaPrimitive->wi4,
0812                                                                  SL3metaPrimitive->tdc4,
0813                                                                  SL3metaPrimitive->lat4,
0814                                                                  -1}));
0815               useFitSL3[sl3] = true;
0816               at_least_one_SL3_confirmation = true;
0817             }
0818           }
0819         }
0820         // Start correlation cleaning
0821         if (clean_chi2_correlation_) {
0822           if (debug_)
0823             LogDebug("MuonPathAssociator") << "Pushing back correlated MPs to the MPs collection";
0824           removeSharingFits(chamberMetaPrimitives, outMPaths);
0825         }
0826         if (clean_chi2_correlation_) {
0827           if (debug_)
0828             LogDebug("MuonPathAssociator") << "Pushing back confirmed MPs to the complete vector";
0829           removeSharingHits(confirmedMetaPrimitives, chamberMetaPrimitives, outMPaths);
0830         }
0831 
0832         //finish SL3-SL1
0833         if (at_least_one_correlation == false || clean_chi2_correlation_) {
0834           if (debug_ && !at_least_one_correlation)
0835             LogDebug("MuonPathAssociator")
0836                 << "correlation we found zero correlations, adding both collections as they are to the outMPaths";
0837           if (debug_)
0838             LogDebug("MuonPathAssociator")
0839                 << "correlation sizes:" << SL1metaPrimitives.size() << " " << SL3metaPrimitives.size();
0840           if (at_least_one_SL1_confirmation == false || clean_chi2_correlation_) {
0841             sl1 = 0;
0842             for (auto SL1metaPrimitive = SL1metaPrimitives.begin(); SL1metaPrimitive != SL1metaPrimitives.end();
0843                  ++SL1metaPrimitive, sl1++) {
0844               [[clang::suppress]] if (useFitSL1[sl1])
0845                 continue;
0846 
0847               DTSuperLayerId SLId(SL1metaPrimitive->rawId);
0848               DTChamberId(SLId.wheel(), SLId.station(), SLId.sector());
0849               metaPrimitive newSL1metaPrimitive = {ChId.rawId(),
0850                                                    SL1metaPrimitive->t0,
0851                                                    SL1metaPrimitive->x,
0852                                                    SL1metaPrimitive->tanPhi,
0853                                                    SL1metaPrimitive->phi,
0854                                                    SL1metaPrimitive->phiB,
0855                                                    SL1metaPrimitive->phi_cmssw,
0856                                                    SL1metaPrimitive->phiB_cmssw,
0857                                                    SL1metaPrimitive->chi2,
0858                                                    SL1metaPrimitive->quality,
0859                                                    SL1metaPrimitive->wi1,
0860                                                    SL1metaPrimitive->tdc1,
0861                                                    SL1metaPrimitive->lat1,
0862                                                    SL1metaPrimitive->wi2,
0863                                                    SL1metaPrimitive->tdc2,
0864                                                    SL1metaPrimitive->lat2,
0865                                                    SL1metaPrimitive->wi3,
0866                                                    SL1metaPrimitive->tdc3,
0867                                                    SL1metaPrimitive->lat3,
0868                                                    SL1metaPrimitive->wi4,
0869                                                    SL1metaPrimitive->tdc4,
0870                                                    SL1metaPrimitive->lat4,
0871                                                    -1,
0872                                                    -1,
0873                                                    -1,
0874                                                    -1,
0875                                                    -1,
0876                                                    -1,
0877                                                    -1,
0878                                                    -1,
0879                                                    -1,
0880                                                    -1,
0881                                                    -1,
0882                                                    -1,
0883                                                    -1};
0884 
0885               bool ok = true;
0886               for (auto &metaPrimitive : chamberMetaPrimitives) {
0887                 if (!isNotAPrimo(newSL1metaPrimitive, metaPrimitive)) {
0888                   ok = false;
0889                   break;
0890                 }
0891               }
0892               if (!ok)
0893                 continue;
0894 
0895               if (!clean_chi2_correlation_)
0896                 outMPaths.push_back(newSL1metaPrimitive);
0897               else
0898                 normalMetaPrimitives.push_back(newSL1metaPrimitive);
0899             }
0900           }
0901           if (at_least_one_SL3_confirmation == false || clean_chi2_correlation_) {
0902             sl3 = 0;
0903             for (auto SL3metaPrimitive = SL3metaPrimitives.begin(); SL3metaPrimitive != SL3metaPrimitives.end();
0904                  ++SL3metaPrimitive, sl3++) {
0905               [[clang::suppress]] if (useFitSL3[sl3])
0906                 continue;
0907               DTSuperLayerId SLId(SL3metaPrimitive->rawId);
0908               DTChamberId(SLId.wheel(), SLId.station(), SLId.sector());
0909               metaPrimitive newSL3metaPrimitive = {ChId.rawId(),
0910                                                    SL3metaPrimitive->t0,
0911                                                    SL3metaPrimitive->x,
0912                                                    SL3metaPrimitive->tanPhi,
0913                                                    SL3metaPrimitive->phi,
0914                                                    SL3metaPrimitive->phiB,
0915                                                    SL3metaPrimitive->phi_cmssw,
0916                                                    SL3metaPrimitive->phiB_cmssw,
0917                                                    SL3metaPrimitive->chi2,
0918                                                    SL3metaPrimitive->quality,
0919                                                    -1,
0920                                                    -1,
0921                                                    -1,
0922                                                    -1,
0923                                                    -1,
0924                                                    -1,
0925                                                    -1,
0926                                                    -1,
0927                                                    -1,
0928                                                    -1,
0929                                                    -1,
0930                                                    -1,
0931                                                    SL3metaPrimitive->wi1,
0932                                                    SL3metaPrimitive->tdc1,
0933                                                    SL3metaPrimitive->lat1,
0934                                                    SL3metaPrimitive->wi2,
0935                                                    SL3metaPrimitive->tdc2,
0936                                                    SL3metaPrimitive->lat2,
0937                                                    SL3metaPrimitive->wi3,
0938                                                    SL3metaPrimitive->tdc3,
0939                                                    SL3metaPrimitive->lat3,
0940                                                    SL3metaPrimitive->wi4,
0941                                                    SL3metaPrimitive->tdc4,
0942                                                    SL3metaPrimitive->lat4,
0943                                                    -1};
0944 
0945               if (!clean_chi2_correlation_)
0946                 outMPaths.push_back(newSL3metaPrimitive);
0947               else
0948                 normalMetaPrimitives.push_back(newSL3metaPrimitive);
0949             }
0950           }
0951         }
0952 
0953         SL1metaPrimitives.clear();
0954         SL1metaPrimitives.erase(SL1metaPrimitives.begin(), SL1metaPrimitives.end());
0955         SL3metaPrimitives.clear();
0956         SL3metaPrimitives.erase(SL3metaPrimitives.begin(), SL3metaPrimitives.end());
0957 
0958         vector<metaPrimitive> auxMetaPrimitives;
0959         if (clean_chi2_correlation_) {
0960           if (debug_)
0961             LogDebug("MuonPathAssociator") << "Pushing back normal MPs to the auxiliar vector";
0962           removeSharingHits(normalMetaPrimitives, confirmedMetaPrimitives, auxMetaPrimitives);
0963         }
0964         if (clean_chi2_correlation_) {
0965           if (debug_)
0966             LogDebug("MuonPathAssociator") << "Pushing back normal MPs to the MPs collection";
0967           removeSharingHits(auxMetaPrimitives, chamberMetaPrimitives, outMPaths);
0968         }
0969       }
0970     }
0971   }
0972 
0973   //eta TP we do not correlate with other superlayer in the same chamber so we forward them all
0974   std::vector<metaPrimitive> SL2metaPrimitives;
0975 
0976   for (int wh = -2; wh <= 2; wh++) {
0977     for (int st = 1; st <= 4; st++) {
0978       for (int se = 1; se <= 14; se++) {
0979         if (se >= 13 && st != 4)
0980           continue;
0981 
0982         DTChamberId ChId(wh, st, se);
0983         DTSuperLayerId sl2Id(wh, st, se, 2);
0984 
0985         //filterSL2 etaTP
0986         for (auto metaprimitiveIt = inMPaths.begin(); metaprimitiveIt != inMPaths.end(); ++metaprimitiveIt)
0987           if (metaprimitiveIt->rawId == sl2Id.rawId()) {
0988             SL2metaPrimitives.push_back(*metaprimitiveIt);
0989             if (debug_)
0990               printmPC(*metaprimitiveIt);
0991             outMPaths.push_back(*metaprimitiveIt);
0992           }
0993       }
0994     }
0995   }
0996 
0997   LogDebug("MuonPathAssociator") << "\t etaTP: added " << SL2metaPrimitives.size() << "to outMPaths" << std::endl;
0998 
0999   SL2metaPrimitives.clear();
1000   SL2metaPrimitives.erase(SL2metaPrimitives.begin(), SL2metaPrimitives.end());
1001 }
1002 
1003 void MuonPathAssociator::removeSharingFits(vector<metaPrimitive> &chamberMPaths, vector<metaPrimitive> &allMPaths) {
1004   bool useFit[chamberMPaths.size()];
1005   for (unsigned int i = 0; i < chamberMPaths.size(); i++) {
1006     useFit[i] = true;
1007   }
1008   for (unsigned int i = 0; i < chamberMPaths.size(); i++) {
1009     if (debug_)
1010       LogDebug("MuonPathAssociator") << "Looking at prim" << i;
1011     [[clang::suppress]] if (!useFit[i])
1012       continue;
1013     for (unsigned int j = i + 1; j < chamberMPaths.size(); j++) {
1014       if (debug_)
1015         LogDebug("MuonPathAssociator") << "Comparing with prim " << j;
1016       [[clang::suppress]] if (!useFit[j])
1017         continue;
1018       metaPrimitive first = chamberMPaths[i];
1019       metaPrimitive second = chamberMPaths[j];
1020       if (shareFit(first, second)) {
1021         if (first.quality > second.quality)
1022           useFit[j] = false;
1023         else if (first.quality < second.quality)
1024           useFit[i] = false;
1025         else {
1026           if (first.chi2 < second.chi2)
1027             useFit[j] = false;
1028           else {
1029             useFit[i] = false;
1030             break;
1031           }
1032         }
1033       }
1034     }
1035     if (useFit[i]) {
1036       if (debug_)
1037         printmPC(chamberMPaths[i]);
1038       allMPaths.push_back(chamberMPaths[i]);
1039     }
1040   }
1041   if (debug_)
1042     LogDebug("MuonPathAssociator") << "---Swapping chamber---";
1043 }
1044 
1045 void MuonPathAssociator::removeSharingHits(std::vector<metaPrimitive> &firstMPaths,
1046                                            std::vector<metaPrimitive> &secondMPaths,
1047                                            std::vector<metaPrimitive> &allMPaths) {
1048   for (auto &firstMP : firstMPaths) {
1049     if (debug_)
1050       LogDebug("MuonPathAssociator") << "----------------------------------";
1051     if (debug_)
1052       LogDebug("MuonPathAssociator") << "Turn for ";
1053     if (debug_)
1054       printmPC(firstMP);
1055     bool ok = true;
1056     for (auto &secondMP : secondMPaths) {
1057       if (debug_)
1058         LogDebug("MuonPathAssociator") << "Comparing with ";
1059       if (debug_)
1060         printmPC(secondMP);
1061       if (!isNotAPrimo(firstMP, secondMP)) {
1062         ok = false;
1063         break;
1064       }
1065     }
1066     if (ok) {
1067       allMPaths.push_back(firstMP);
1068       if (debug_)
1069         printmPC(firstMP);
1070     }
1071     if (debug_)
1072       LogDebug("MuonPathAssociator") << "----------------------------------";
1073   }
1074 }
1075 
1076 bool MuonPathAssociator::shareFit(metaPrimitive first, metaPrimitive second) {
1077   bool lay1 = (first.wi1 == second.wi1) && (first.tdc1 = second.tdc1);
1078   bool lay2 = (first.wi2 == second.wi2) && (first.tdc2 = second.tdc2);
1079   bool lay3 = (first.wi3 == second.wi3) && (first.tdc3 = second.tdc3);
1080   bool lay4 = (first.wi4 == second.wi4) && (first.tdc4 = second.tdc4);
1081   bool lay5 = (first.wi5 == second.wi5) && (first.tdc5 = second.tdc5);
1082   bool lay6 = (first.wi6 == second.wi6) && (first.tdc6 = second.tdc6);
1083   bool lay7 = (first.wi7 == second.wi7) && (first.tdc7 = second.tdc7);
1084   bool lay8 = (first.wi8 == second.wi8) && (first.tdc8 = second.tdc8);
1085 
1086   if (lay1 && lay2 && lay3 && lay4) {
1087     if (lay5 || lay6 || lay7 || lay8)
1088       return true;
1089     else
1090       return false;
1091   } else if (lay5 && lay6 && lay7 && lay8) {
1092     if (lay1 || lay2 || lay3 || lay4)
1093       return true;
1094     else
1095       return false;
1096   } else
1097     return false;
1098 }
1099 
1100 bool MuonPathAssociator::isNotAPrimo(metaPrimitive first, metaPrimitive second) {
1101   int hitsSL1 = (first.wi1 != -1) + (first.wi2 != -1) + (first.wi3 != -1) + (first.wi4 != -1);
1102   int hitsSL3 = (first.wi5 != -1) + (first.wi6 != -1) + (first.wi7 != -1) + (first.wi8 != -1);
1103 
1104   bool lay1 = (first.wi1 == second.wi1) && (first.tdc1 = second.tdc1) && (first.wi1 != -1);
1105   bool lay2 = (first.wi2 == second.wi2) && (first.tdc2 = second.tdc2) && (first.wi2 != -1);
1106   bool lay3 = (first.wi3 == second.wi3) && (first.tdc3 = second.tdc3) && (first.wi3 != -1);
1107   bool lay4 = (first.wi4 == second.wi4) && (first.tdc4 = second.tdc4) && (first.wi4 != -1);
1108   bool lay5 = (first.wi5 == second.wi5) && (first.tdc5 = second.tdc5) && (first.wi5 != -1);
1109   bool lay6 = (first.wi6 == second.wi6) && (first.tdc6 = second.tdc6) && (first.wi6 != -1);
1110   bool lay7 = (first.wi7 == second.wi7) && (first.tdc7 = second.tdc7) && (first.wi7 != -1);
1111   bool lay8 = (first.wi8 == second.wi8) && (first.tdc8 = second.tdc8) && (first.wi8 != -1);
1112 
1113   return (((!lay1 && !lay2 && !lay3 && !lay4) || hitsSL1 < 3) && ((!lay5 && !lay6 && !lay7 && !lay8) || hitsSL3 < 3));
1114 }
1115 
1116 void MuonPathAssociator::printmPC(metaPrimitive mP) {
1117   DTChamberId ChId(mP.rawId);
1118   LogDebug("MuonPathAssociator") << ChId << "\t"
1119                                  << " " << setw(2) << left << mP.wi1 << " " << setw(2) << left << mP.wi2 << " "
1120                                  << setw(2) << left << mP.wi3 << " " << setw(2) << left << mP.wi4 << " " << setw(2)
1121                                  << left << mP.wi5 << " " << setw(2) << left << mP.wi6 << " " << setw(2) << left
1122                                  << mP.wi7 << " " << setw(2) << left << mP.wi8 << " " << setw(5) << left << mP.tdc1
1123                                  << " " << setw(5) << left << mP.tdc2 << " " << setw(5) << left << mP.tdc3 << " "
1124                                  << setw(5) << left << mP.tdc4 << " " << setw(5) << left << mP.tdc5 << " " << setw(5)
1125                                  << left << mP.tdc6 << " " << setw(5) << left << mP.tdc7 << " " << setw(5) << left
1126                                  << mP.tdc8 << " " << setw(2) << left << mP.lat1 << " " << setw(2) << left << mP.lat2
1127                                  << " " << setw(2) << left << mP.lat3 << " " << setw(2) << left << mP.lat4 << " "
1128                                  << setw(2) << left << mP.lat5 << " " << setw(2) << left << mP.lat6 << " " << setw(2)
1129                                  << left << mP.lat7 << " " << setw(2) << left << mP.lat8 << " " << setw(10) << right
1130                                  << mP.x << " " << setw(9) << left << mP.tanPhi << " " << setw(5) << left << mP.t0
1131                                  << " " << setw(13) << left << mP.chi2 << " \n";
1132 }