Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:11:02

0001 #include "L1Trigger/CSCTriggerPrimitives/interface/CSCUpgradeCathodeLCTProcessor.h"
0002 
0003 #include <iomanip>
0004 
0005 CSCUpgradeCathodeLCTProcessor::CSCUpgradeCathodeLCTProcessor(unsigned endcap,
0006                                                              unsigned station,
0007                                                              unsigned sector,
0008                                                              unsigned subsector,
0009                                                              unsigned chamber,
0010                                                              const edm::ParameterSet& conf)
0011     : CSCCathodeLCTProcessor(endcap, station, sector, subsector, chamber, conf) {
0012   if (!runPhase2_)
0013     edm::LogError("CSCUpgradeCathodeLCTProcessor|ConfigError")
0014         << "+++ Upgrade CSCUpgradeCathodeLCTProcessor constructed while runPhase2_ is not set! +++\n";
0015 
0016   // use of localized dead-time zones
0017   use_dead_time_zoning_ = clctParams_.getParameter<bool>("useDeadTimeZoning");
0018   clct_state_machine_zone_ = clctParams_.getParameter<unsigned int>("clctStateMachineZone");
0019 
0020   // how far away may trigger happen from pretrigger
0021   pretrig_trig_zone_ = clctParams_.getParameter<unsigned int>("clctPretriggerTriggerZone");
0022 }
0023 
0024 // --------------------------------------------------------------------------
0025 // The code below is for Phase2 studies of the CLCT algorithm
0026 // --------------------------------------------------------------------------
0027 
0028 // Phase2 version, add the feature of localized dead time zone for pretrigger
0029 bool CSCUpgradeCathodeLCTProcessor::preTrigger(const int start_bx, int& first_bx) {
0030   if (runPhase2_ and !use_dead_time_zoning_) {
0031     return CSCCathodeLCTProcessor::preTrigger(start_bx, first_bx);
0032   }
0033 
0034   if (infoV > 1)
0035     LogTrace("CSCUpgradeCathodeLCTProcessor")
0036         << "....................PreTrigger, Phase2 version with localized dead time zone...........................";
0037 
0038   int nPreTriggers = 0;
0039 
0040   bool pre_trig = false;
0041 
0042   // Now do a loop over bx times to see (if/when) track goes over threshold
0043   for (unsigned int bx_time = start_bx; bx_time < fifo_tbins; bx_time++) {
0044     // For any given bunch-crossing, start at the lowest keystrip and look for
0045     // the number of separate layers in the pattern for that keystrip that have
0046     // pulses at that bunch-crossing time.  Do the same for the next keystrip,
0047     // etc.  Then do the entire process again for the next bunch-crossing, etc
0048     // until you find a pre-trigger.
0049     std::map<int, std::map<int, CSCCLCTDigi::ComparatorContainer> > hits_in_patterns;
0050     hits_in_patterns.clear();
0051 
0052     bool hits_in_time = patternFinding(bx_time, hits_in_patterns);
0053     if (hits_in_time) {
0054       for (int hstrip = stagger[CSCConstants::KEY_CLCT_LAYER - 1]; hstrip < numHalfStrips_; hstrip++) {
0055         if (infoV > 1) {
0056           if (nhits[hstrip] > 0) {
0057             LogTrace("CSCUpgradeCathodeLCTProcessor")
0058                 << " bx = " << std::setw(2) << bx_time << " --->"
0059                 << " halfstrip = " << std::setw(3) << hstrip << " best pid = " << std::setw(2) << best_pid[hstrip]
0060                 << " nhits = " << nhits[hstrip];
0061           }
0062         }
0063         // note that ispretrig_ is initialized in findLCT function
0064         if (nhits[hstrip] >= nplanes_hit_pretrig && best_pid[hstrip] >= pid_thresh_pretrig &&
0065             !busyMap_[hstrip][bx_time]) {
0066           pre_trig = true;
0067           ispretrig_[hstrip] = true;
0068 
0069           // write each pre-trigger to output
0070           nPreTriggers++;
0071           thePreTriggerDigis.push_back(constructPreCLCT(bx_time, hstrip, nPreTriggers));
0072         }
0073         // busy zone, keep pretriggering, ignore this
0074         else if (nhits[hstrip] >= nplanes_hit_pretrig && best_pid[hstrip] >= pid_thresh_pretrig) {
0075           ispretrig_[hstrip] = true;
0076           if (infoV > 1)
0077             LogTrace("CSCUpgradeCathodeLCTProcessor")
0078                 << " halfstrip " << std::setw(3) << hstrip << " in dead zone and is pretriggerred";
0079         }
0080         // no pretrigger on this halfstrip, release dead zone
0081         else if (nhits[hstrip] < nplanes_hit_pretrig || best_pid[hstrip] < pid_thresh_pretrig) {
0082           ispretrig_[hstrip] = false;
0083         }
0084       }  // find all pretriggers
0085 
0086       // update dead zone
0087       markBusyZone(bx_time);
0088 
0089       if (pre_trig) {
0090         first_bx = bx_time;  // bx at time of pretrigger
0091         return true;
0092       }
0093     } else {
0094       // no pattern found, remove all dead zone
0095       clearPreTriggers();
0096     }
0097   }  // end loop over bx times
0098 
0099   if (infoV > 1)
0100     LogTrace("CSCUpgradeCathodeLCTProcessor") << "no pretrigger, returning \n";
0101   first_bx = fifo_tbins;
0102   return false;
0103 }  // preTrigger -- Phase2 version.
0104 
0105 // Phase2 version.
0106 std::vector<CSCCLCTDigi> CSCUpgradeCathodeLCTProcessor::findLCTs(
0107     const std::vector<int> halfstrip[CSCConstants::NUM_LAYERS][CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER]) {
0108   // run the original algorithm in case we do not use dead time zoning
0109   if (runPhase2_ and !use_dead_time_zoning_) {
0110     return CSCCathodeLCTProcessor::findLCTs(halfstrip);
0111   }
0112 
0113   std::vector<CSCCLCTDigi> lctList;
0114 
0115   // initialize the ispretrig_ before doing pretriggering
0116   clearPreTriggers();
0117 
0118   if (infoV > 1)
0119     dumpDigis(halfstrip);
0120 
0121   // keeps dead-time zones around key halfstrips of triggered CLCTs
0122   for (int i = 0; i < CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER; i++) {
0123     for (int j = 0; j < CSCConstants::MAX_CLCT_TBINS; j++) {
0124       busyMap_[i][j] = false;
0125     }
0126   }
0127 
0128   // Fire half-strip one-shots for hit_persist bx's (4 bx's by default).
0129   pulseExtension(halfstrip);
0130 
0131   unsigned int start_bx = start_bx_shift;
0132   // Stop drift_delay bx's short of fifo_tbins since at later bx's we will
0133   // not have a full set of hits to start pattern search anyway.
0134   unsigned int stop_bx = fifo_tbins - drift_delay;
0135 
0136   // Allow for more than one pass over the hits in the time window.
0137   // Do search in every BX
0138   while (start_bx < stop_bx) {
0139     // temp CLCT objects
0140     CSCCLCTDigi tempBestCLCT;
0141     CSCCLCTDigi tempSecondCLCT;
0142 
0143     // All half-strip pattern envelopes are evaluated simultaneously, on every clock cycle.
0144     int first_bx = 999;
0145 
0146     // Check for a pre-trigger. If so, find the first BX when the pre-trigger occurred
0147     bool pre_trig = CSCUpgradeCathodeLCTProcessor::preTrigger(start_bx, first_bx);
0148 
0149     // If any of half-strip envelopes has enough layers hit in it, TMB
0150     // will pre-trigger.
0151     if (pre_trig) {
0152       if (infoV > 1)
0153         LogTrace("CSCUpgradeCathodeLCTProcessor")
0154             << "..... pretrigger at bx = " << first_bx << "; waiting drift delay .....";
0155 
0156       // TMB latches LCTs drift_delay clocks after pretrigger.
0157       int latch_bx = first_bx + drift_delay;
0158 
0159       // temporary container to keep track of the hits in the CLCT
0160       std::map<int, std::map<int, CSCCLCTDigi::ComparatorContainer> > hits_in_patterns;
0161       hits_in_patterns.clear();
0162 
0163       // We check if there is at least one key half strip for which at least
0164       // one pattern id has at least the minimum number of hits
0165       bool hits_in_time = patternFinding(latch_bx, hits_in_patterns);
0166       if (infoV > 1) {
0167         if (hits_in_time) {
0168           for (int hstrip = stagger[CSCConstants::KEY_CLCT_LAYER - 1]; hstrip < numHalfStrips_; hstrip++) {
0169             if (nhits[hstrip] > 0) {
0170               LogTrace("CSCUpgradeCathodeLCTProcessor")
0171                   << " bx = " << std::setw(2) << latch_bx << " --->"
0172                   << " halfstrip = " << std::setw(3) << hstrip << " best pid = " << std::setw(2) << best_pid[hstrip]
0173                   << " nhits = " << nhits[hstrip];
0174             }
0175           }
0176         }
0177       }
0178 
0179       // Quality for sorting.
0180       int quality[CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER];
0181       int best_halfstrip[CSCConstants::MAX_CLCTS_PER_PROCESSOR], best_quality[CSCConstants::MAX_CLCTS_PER_PROCESSOR];
0182       for (int ilct = 0; ilct < CSCConstants::MAX_CLCTS_PER_PROCESSOR; ilct++) {
0183         best_halfstrip[ilct] = -1;
0184         best_quality[ilct] = 0;
0185       }
0186 
0187       bool pretrig_zone[CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER];
0188 
0189       // Calculate quality from pattern id and number of hits, and
0190       // simultaneously select best-quality CLCT.
0191       if (hits_in_time) {
0192         // first, mark half-strip zones around pretriggers
0193         // that happened at the current first_bx
0194         markPreTriggerZone(pretrig_zone);
0195 
0196         for (int hstrip = stagger[CSCConstants::KEY_CLCT_LAYER - 1]; hstrip < numHalfStrips_; hstrip++) {
0197           /* The bend-direction bit pid[0] is ignored (left and right bends have equal quality).
0198            This works both for the Run-2 patterns
0199            - PID 2,3: 2 & 14 == 2, 3 & 14 == 2
0200            - PID 4,5: 4 & 14 == 4, 3 & 14 == 4
0201            - PID 6,7: 6 & 14 == 6, 3 & 14 == 6
0202            - PID 8,9: 8 & 14 == 8, 3 & 14 == 8
0203            - PID 10: 10 & 14 == 10
0204            It also works for the Run-3 patterns:
0205            - PID 0,1: 0 & 14 == 0, 1 & 14 == 0
0206            - PID 2,3: 2 & 14 == 2, 3 & 14 == 2
0207            - PID  4:  4 & 14 == 4
0208           */
0209           quality[hstrip] = (best_pid[hstrip] & 14) | (nhits[hstrip] << 5);
0210           // do not consider halfstrips:
0211           // - out of pretrigger-trigger zones
0212           // - in busy zones from previous trigger
0213           if (quality[hstrip] > best_quality[0] && pretrig_zone[hstrip] && !busyMap_[hstrip][first_bx]) {
0214             best_halfstrip[0] = hstrip;
0215             best_quality[0] = quality[hstrip];
0216             // temporary alias
0217             const int best_hs(best_halfstrip[0]);
0218             const int best_pat(best_pid[best_hs]);
0219             // construct a CLCT if the trigger condition has been met
0220             if (best_hs >= 0 && nhits[best_hs] >= nplanes_hit_pattern) {
0221               // overwrite the current best CLCT
0222               tempBestCLCT = constructCLCT(first_bx, best_hs, hits_in_patterns[best_hs][best_pat]);
0223             }
0224           }
0225         }
0226       }
0227 
0228       // If 1st best CLCT is found, look for the 2nd best.
0229       if (best_halfstrip[0] >= 0) {
0230         // Get the half-strip of the best CLCT in this BX that was put into the list.
0231         // You do need to re-add the any stagger, because the busy keys are based on
0232         // the pulse array which takes into account strip stagger!!!
0233         const unsigned halfStripBestCLCT(tempBestCLCT.getKeyStrip() + stagger[CSCConstants::KEY_CLCT_LAYER - 1]);
0234 
0235         // Mark keys near best CLCT as busy by setting their quality to
0236         // zero, and repeat the search.
0237         //markBusyKeys(best_halfstrip[0], best_pid[best_halfstrip[0]], quality);
0238         markBusyKeys(halfStripBestCLCT, best_pid[halfStripBestCLCT], quality);
0239 
0240         for (int hstrip = stagger[CSCConstants::KEY_CLCT_LAYER - 1]; hstrip < numHalfStrips_; hstrip++) {
0241           // we don't have to recalculate the quality for each half-strip
0242           if (quality[hstrip] > best_quality[1] && pretrig_zone[hstrip] && !busyMap_[hstrip][first_bx]) {
0243             best_halfstrip[1] = hstrip;
0244             best_quality[1] = quality[hstrip];
0245             // temporary alias
0246             const int best_hs(best_halfstrip[1]);
0247             const int best_pat(best_pid[best_hs]);
0248             // construct a CLCT if the trigger condition has been met
0249             if (best_hs >= 0 && nhits[best_hs] >= nplanes_hit_pattern) {
0250               // overwrite the current second best CLCT
0251               tempSecondCLCT = constructCLCT(first_bx, best_hs, hits_in_patterns[best_hs][best_pat]);
0252             }
0253           }
0254         }
0255 
0256         // Sort bestCLCT and secondALCT by quality
0257         // if qualities are the same, sort by run-2 or run-3 pattern
0258         // if qualities and patterns are the same, sort by half strip number
0259         bool changeOrder = false;
0260 
0261         unsigned qualityBest = 0, qualitySecond = 0;
0262         unsigned patternBest = 0, patternSecond = 0;
0263         unsigned halfStripBest = 0, halfStripSecond = 0;
0264 
0265         if (tempBestCLCT.isValid() and tempSecondCLCT.isValid()) {
0266           qualityBest = tempBestCLCT.getQuality();
0267           qualitySecond = tempSecondCLCT.getQuality();
0268           if (!run3_) {
0269             patternBest = tempBestCLCT.getPattern();
0270             patternSecond = tempSecondCLCT.getPattern();
0271           } else {
0272             patternBest = tempBestCLCT.getRun3Pattern();
0273             patternSecond = tempSecondCLCT.getRun3Pattern();
0274           }
0275           halfStripBest = tempBestCLCT.getKeyStrip();
0276           halfStripSecond = tempSecondCLCT.getKeyStrip();
0277 
0278           if (qualitySecond > qualityBest)
0279             changeOrder = true;
0280           else if ((qualitySecond == qualityBest) and (int(patternSecond / 2) > int(patternBest / 2)))
0281             changeOrder = true;
0282           else if ((qualitySecond == qualityBest) and (int(patternSecond / 2) == int(patternBest / 2)) and
0283                    (halfStripSecond < halfStripBest))
0284             changeOrder = true;
0285         }
0286 
0287         CSCCLCTDigi tempCLCT;
0288         if (changeOrder) {
0289           tempCLCT = tempBestCLCT;
0290           tempBestCLCT = tempSecondCLCT;
0291           tempSecondCLCT = tempCLCT;
0292         }
0293 
0294         // add the CLCTs to the collection
0295         if (tempBestCLCT.isValid()) {
0296           lctList.push_back(tempBestCLCT);
0297         }
0298         if (tempSecondCLCT.isValid()) {
0299           lctList.push_back(tempSecondCLCT);
0300         }
0301       }  //find CLCT, end of best_halfstrip[0] >= 0
0302     }    //pre_trig
0303     // The pattern finder runs continuously, so another pre-trigger
0304     // could occur already at the next bx.
0305     start_bx = first_bx + 1;
0306   }
0307   return lctList;
0308 }  // findLCTs -- Phase2 version.
0309 
0310 void CSCUpgradeCathodeLCTProcessor::markPreTriggerZone(
0311     bool pretrig_zone[CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER]) const {
0312   // first reset the pretrigger zone (no pretriggers anywhere in this BX
0313   for (int hstrip = 0; hstrip < CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER; hstrip++) {
0314     pretrig_zone[hstrip] = false;
0315   }
0316   // then set the pretrigger zone according to the ispretrig_ array
0317   for (int hstrip = 0; hstrip < CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER; hstrip++) {
0318     if (ispretrig_[hstrip]) {
0319       int min_hs = hstrip - pretrig_trig_zone_;
0320       int max_hs = hstrip + pretrig_trig_zone_;
0321       // set the minimum strip
0322       if (min_hs < 0)
0323         min_hs = 0;
0324       // set the maximum strip
0325       if (max_hs > CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER - 1)
0326         max_hs = CSCConstants::MAX_NUM_HALF_STRIPS_RUN2_TRIGGER - 1;
0327       // mark the pre-trigger zone
0328       for (int hs = min_hs; hs <= max_hs; hs++)
0329         pretrig_zone[hs] = true;
0330       if (infoV > 1)
0331         LogTrace("CSCUpgradeCathodeLCTProcessor")
0332             << " marked pretrigger halfstrip zone [" << min_hs << "," << max_hs << "]";
0333     }
0334   }
0335 }
0336 
0337 void CSCUpgradeCathodeLCTProcessor::markBusyZone(const int bx) {
0338   for (int hstrip = stagger[CSCConstants::KEY_CLCT_LAYER - 1]; hstrip < numHalfStrips_; hstrip++) {
0339     // check if this halfstrip has a pretrigger
0340     if (ispretrig_[hstrip]) {
0341       // only fixed localized dead time zone is implemented in firmware
0342       int min_hstrip = hstrip - clct_state_machine_zone_;
0343       int max_hstrip = hstrip + clct_state_machine_zone_;
0344       // set the minimum strip
0345       if (min_hstrip < stagger[CSCConstants::KEY_CLCT_LAYER - 1])
0346         min_hstrip = stagger[CSCConstants::KEY_CLCT_LAYER - 1];
0347       // set the maximum strip
0348       if (max_hstrip >= numHalfStrips_)
0349         max_hstrip = numHalfStrips_ - 1;
0350       // mask the busy half-strips for 1 BX after the pretrigger
0351       for (int hs = min_hstrip; hs <= max_hstrip; hs++)
0352         busyMap_[hs][bx + 1] = true;
0353       if (infoV > 1)
0354         LogTrace("CSCUpgradeCathodeLCTProcessor")
0355             << " marked zone around pretriggerred halfstrip " << hstrip << " as dead zone for pretriggering at bx"
0356             << bx + 1 << " halfstrip: [" << min_hstrip << "," << max_hstrip << "]";
0357     }
0358   }
0359 }