Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-15 23:40:47

0001 #include "L1Trigger/TrackFindingTracklet/interface/TrackletProcessorDisplaced.h"
0002 #include "L1Trigger/TrackFindingTracklet/interface/Settings.h"
0003 #include "L1Trigger/TrackFindingTracklet/interface/Globals.h"
0004 #include "L1Trigger/TrackFindingTracklet/interface/AllStubsMemory.h"
0005 #include "L1Trigger/TrackFindingTracklet/interface/AllInnerStubsMemory.h"
0006 #include "L1Trigger/TrackFindingTracklet/interface/StubPairsMemory.h"
0007 #include "L1Trigger/TrackFindingTracklet/interface/Tracklet.h"
0008 #include "L1Trigger/TrackFindingTracklet/interface/Util.h"
0009 #include "L1Trigger/TrackFindingTracklet/interface/IMATH_TrackletCalculator.h"
0010 
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 #include "FWCore/Utilities/interface/Exception.h"
0013 #include "DataFormats/Math/interface/deltaPhi.h"
0014 
0015 #include <utility>
0016 #include <tuple>
0017 
0018 using namespace std;
0019 using namespace trklet;
0020 
0021 // TrackletProcessorDisplaced
0022 //
0023 // This module takes in collections of stubs within a phi region and a
0024 // displaced seed name and tries to create that displaced seed out of the stubs
0025 //
0026 // Update: Claire Savard, Oct. 2024
0027 
0028 TrackletProcessorDisplaced::TrackletProcessorDisplaced(string name, Settings const& settings, Globals* globals)
0029     : TrackletCalculatorDisplaced(name, settings, globals), innerTable_(settings), innerThirdTable_(settings) {
0030   innerallstubs_.clear();
0031   middleallstubs_.clear();
0032   outerallstubs_.clear();
0033   innervmstubs_.clear();
0034   outervmstubs_.clear();
0035 
0036   // set layer/disk types based on input seed name
0037   initLayerDisksandISeedDisp(layerdisk1_, layerdisk2_, layerdisk3_, iSeed_);
0038 
0039   // get projection tables
0040   unsigned int region = name.back() - 'A';
0041   innerTable_.initVMRTable(
0042       layerdisk1_, TrackletLUT::VMRTableType::inner, region, false);  //projection to next layer/disk
0043   innerThirdTable_.initVMRTable(
0044       layerdisk1_, TrackletLUT::VMRTableType::innerthird, region, false);  //projection to third layer/disk
0045 
0046   nbitszfinebintable_ = settings_.vmrlutzbits(layerdisk1_);
0047   nbitsrfinebintable_ = settings_.vmrlutrbits(layerdisk1_);
0048 
0049   for (unsigned int ilayer = 0; ilayer < N_LAYER; ilayer++) {
0050     vector<TrackletProjectionsMemory*> tmp(settings_.nallstubs(ilayer), nullptr);
0051     trackletprojlayers_.push_back(tmp);
0052   }
0053 
0054   for (unsigned int idisk = 0; idisk < N_DISK; idisk++) {
0055     vector<TrackletProjectionsMemory*> tmp(settings_.nallstubs(idisk + N_LAYER), nullptr);
0056     trackletprojdisks_.push_back(tmp);
0057   }
0058 
0059   // set TC index
0060   iTC_ = region;
0061   constexpr int TCIndexMin = 128;
0062   constexpr int TCIndexMax = 191;
0063   TCIndex_ = (iSeed_ << 4) + iTC_;
0064   assert(TCIndex_ >= TCIndexMin && TCIndex_ < TCIndexMax);
0065 }
0066 
0067 void TrackletProcessorDisplaced::addOutputProjection(TrackletProjectionsMemory*& outputProj, MemoryBase* memory) {
0068   outputProj = dynamic_cast<TrackletProjectionsMemory*>(memory);
0069   assert(outputProj != nullptr);
0070 }
0071 
0072 void TrackletProcessorDisplaced::addOutput(MemoryBase* memory, string output) {
0073   if (settings_.writetrace()) {
0074     edm::LogVerbatim("Tracklet") << "In " << name_ << " adding output to " << memory->getName() << " to output "
0075                                  << output;
0076   }
0077 
0078   if (output == "trackpar") {
0079     auto* tmp = dynamic_cast<TrackletParametersMemory*>(memory);
0080     assert(tmp != nullptr);
0081     trackletpars_ = tmp;
0082     return;
0083   }
0084 
0085   if (output.substr(0, 7) == "projout") {
0086     //output is on the form 'projoutL2PHIC' or 'projoutD3PHIB'
0087     auto* tmp = dynamic_cast<TrackletProjectionsMemory*>(memory);
0088     assert(tmp != nullptr);
0089 
0090     constexpr unsigned layerdiskPosInprojout = 8;
0091     constexpr unsigned phiPosInprojout = 12;
0092 
0093     unsigned int layerdisk = output[layerdiskPosInprojout] - '1';  //layer or disk counting from 0
0094     unsigned int phiregion = output[phiPosInprojout] - 'A';        //phiregion counting from 0
0095 
0096     if (output[7] == 'L') {
0097       assert(layerdisk < N_LAYER);
0098       assert(phiregion < trackletprojlayers_[layerdisk].size());
0099       //check that phiregion not already initialized
0100       assert(trackletprojlayers_[layerdisk][phiregion] == nullptr);
0101       trackletprojlayers_[layerdisk][phiregion] = tmp;
0102       return;
0103     }
0104 
0105     if (output[7] == 'D') {
0106       assert(layerdisk < N_DISK);
0107       assert(phiregion < trackletprojdisks_[layerdisk].size());
0108       //check that phiregion not already initialized
0109       assert(trackletprojdisks_[layerdisk][phiregion] == nullptr);
0110       trackletprojdisks_[layerdisk][phiregion] = tmp;
0111       return;
0112     }
0113   }
0114 
0115   throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find output : " << output;
0116 }
0117 
0118 void TrackletProcessorDisplaced::addInput(MemoryBase* memory, string input) {
0119   if (settings_.writetrace()) {
0120     edm::LogVerbatim("Tracklet") << "In " << name_ << " adding input from " << memory->getName() << " to input "
0121                                  << input;
0122   }
0123 
0124   if (input == "thirdallstubin") {
0125     auto* tmp = dynamic_cast<AllStubsMemory*>(memory);
0126     assert(tmp != nullptr);
0127     innerallstubs_.push_back(tmp);
0128     return;
0129   }
0130   if (input == "firstallstubin") {
0131     auto* tmp = dynamic_cast<AllStubsMemory*>(memory);
0132     assert(tmp != nullptr);
0133     middleallstubs_.push_back(tmp);
0134     return;
0135   }
0136   if (input == "secondallstubin") {
0137     auto* tmp = dynamic_cast<AllStubsMemory*>(memory);
0138     assert(tmp != nullptr);
0139     outerallstubs_.push_back(tmp);
0140     return;
0141   }
0142   if (input == "thirdvmstubin") {
0143     auto* tmp = dynamic_cast<VMStubsTEMemory*>(memory);
0144     assert(tmp != nullptr);
0145     innervmstubs_.push_back(tmp);
0146     return;
0147   }
0148   if (input == "secondvmstubin") {
0149     auto* tmp = dynamic_cast<VMStubsTEMemory*>(memory);
0150     assert(tmp != nullptr);
0151     outervmstubs_.push_back(tmp);
0152     return;
0153   }
0154 
0155   throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find input : " << input;
0156 }
0157 
0158 void TrackletProcessorDisplaced::execute(unsigned int iSector, double phimin, double phimax) {
0159   unsigned int countall = 0;
0160   unsigned int countsel = 0;
0161 
0162   phimin_ = phimin;
0163   phimax_ = phimax;
0164   iSector_ = iSector;
0165 
0166   // loop over the middle stubs in the potential seed
0167   for (unsigned int midmem = 0; midmem < middleallstubs_.size(); midmem++) {
0168     for (unsigned int i = 0; i < middleallstubs_[midmem]->nStubs(); i++) {
0169       const Stub* midallstub = middleallstubs_[midmem]->getStub(i);
0170 
0171       if (settings_.debugTracklet()) {
0172         edm::LogVerbatim("Tracklet") << "In " << getName() << " have middle stub";
0173       }
0174 
0175       // get r/z index of the middle stub
0176       int indexz = (((1 << (midallstub->z().nbits() - 1)) + midallstub->z().value()) >>
0177                     (midallstub->z().nbits() - nbitszfinebintable_));
0178       int indexr = -1;
0179       bool negdisk = (midallstub->disk().value() < 0);  // check if disk in negative z region
0180       if (layerdisk1_ >= LayerDisk::D1) {               // if a disk
0181         if (negdisk)
0182           indexz = (1 << nbitszfinebintable_) - indexz;
0183         indexr = midallstub->r().value();
0184         if (midallstub->isPSmodule()) {
0185           indexr = midallstub->r().value() >> (midallstub->r().nbits() - nbitsrfinebintable_);
0186         }
0187       } else {  // else a layer
0188         indexr = (((1 << (midallstub->r().nbits() - 1)) + midallstub->r().value()) >>
0189                   (midallstub->r().nbits() - nbitsrfinebintable_));
0190       }
0191 
0192       assert(indexz >= 0);
0193       assert(indexr >= 0);
0194       assert(indexz < (1 << nbitszfinebintable_));
0195       assert(indexr < (1 << nbitsrfinebintable_));
0196 
0197       // create lookupbits that define projections from middle stub
0198       unsigned int lutwidth = settings_.lutwidthtabextended(0, iSeed_);
0199       int lutval = -1;
0200       const auto& lutshift = innerTable_.nbits();
0201       lutval = innerTable_.lookup((indexz << nbitsrfinebintable_) + indexr);
0202       int lutval2 = innerThirdTable_.lookup((indexz << nbitsrfinebintable_) + indexr);
0203       if (lutval != -1 && lutval2 != -1)
0204         lutval += (lutval2 << lutshift);
0205       if (lutval == -1)
0206         continue;
0207       FPGAWord lookupbits(lutval, lutwidth, true, __LINE__, __FILE__);
0208 
0209       // get r/z bins for projection into outer layer/disk
0210       int nbitsrzbin = N_RZBITS;
0211       if (iSeed_ == Seed::D1D2L2)
0212         nbitsrzbin--;
0213       int rzbinfirst = lookupbits.bits(0, NFINERZBITS);
0214       int next = lookupbits.bits(NFINERZBITS, 1);
0215       int rzdiffmax = lookupbits.bits(NFINERZBITS + 1 + nbitsrzbin, NFINERZBITS);
0216 
0217       int start = lookupbits.bits(NFINERZBITS + 1, nbitsrzbin);  // first rz bin projection
0218       if (iSeed_ == Seed::D1D2L2 && negdisk)                     // if projecting into disk
0219         start += (1 << nbitsrzbin);
0220       int last = start + next;  // last rz bin projection
0221 
0222       if (settings_.debugTracklet()) {
0223         edm::LogVerbatim("Tracklet") << "Will look in r/z bins for outer stub " << start << " to " << last << endl;
0224       }
0225 
0226       // loop over outer stubs that the middle stub can project to
0227       for (int ibin = start; ibin <= last; ibin++) {
0228         for (unsigned int outmem = 0; outmem < outervmstubs_.size(); outmem++) {
0229           for (unsigned int j = 0; j < outervmstubs_[outmem]->nVMStubsBinned(ibin); j++) {
0230             if (settings_.debugTracklet())
0231               edm::LogVerbatim("Tracklet") << "In " << getName() << " have outer stub" << endl;
0232 
0233             const VMStubTE& outvmstub = outervmstubs_[outmem]->getVMStubTEBinned(ibin, j);
0234 
0235             // check if r/z of outer stub is within projection range
0236             int rzbin = (outvmstub.vmbits().value() & (settings_.NLONGVMBINS() - 1));
0237             if (start != ibin)
0238               rzbin += 8;
0239             if (rzbin < rzbinfirst || rzbin - rzbinfirst > rzdiffmax) {
0240               if (settings_.debugTracklet()) {
0241                 edm::LogVerbatim("Tracklet") << "Outer stub rejected because of wrong r/z bin";
0242               }
0243               continue;
0244             }
0245 
0246             // get r/z bins for projection into third layer/disk
0247             int nbitsrzbin_ = N_RZBITS;
0248             int next_ = lookupbits.bits(lutshift + NFINERZBITS, 1);
0249 
0250             int start_ = lookupbits.bits(lutshift + NFINERZBITS + 1, nbitsrzbin_);  // first rz bin projection
0251             if (iSeed_ == Seed::D1D2L2 && negdisk)  // if projecting from disk into layer
0252               start_ = settings_.NLONGVMBINS() - 1 - start_ - next_;
0253             int last_ = start_ + next_;  // last rz bin projection
0254 
0255             if (settings_.debugTracklet()) {
0256               edm::LogVerbatim("Tracklet")
0257                   << "Will look in rz bins for inner stub " << start_ << " to " << last_ << endl;
0258             }
0259 
0260             // loop over inner stubs that the middle stub can project to
0261             for (int ibin_ = start_; ibin_ <= last_; ibin_++) {
0262               for (unsigned int inmem = 0; inmem < innervmstubs_.size(); inmem++) {
0263                 for (unsigned int k = 0; k < innervmstubs_[inmem]->nVMStubsBinned(ibin_); k++) {
0264                   if (settings_.debugTracklet())
0265                     edm::LogVerbatim("Tracklet") << "In " << getName() << " have inner stub" << endl;
0266 
0267                   const VMStubTE& invmstub = innervmstubs_[inmem]->getVMStubTEBinned(ibin_, k);
0268 
0269                   countall++;
0270 
0271                   const Stub* innerFPGAStub = invmstub.stub();
0272                   const Stub* middleFPGAStub = midallstub;
0273                   const Stub* outerFPGAStub = outvmstub.stub();
0274 
0275                   const L1TStub* innerStub = innerFPGAStub->l1tstub();
0276                   const L1TStub* middleStub = middleFPGAStub->l1tstub();
0277                   const L1TStub* outerStub = outerFPGAStub->l1tstub();
0278 
0279                   if (settings_.debugTracklet()) {
0280                     edm::LogVerbatim("Tracklet")
0281                         << "triplet seeding\n"
0282                         << innerFPGAStub->strbare() << middleFPGAStub->strbare() << outerFPGAStub->strbare()
0283                         << innerStub->stubword() << middleStub->stubword() << outerStub->stubword()
0284                         << innerFPGAStub->layerdisk() << middleFPGAStub->layerdisk() << outerFPGAStub->layerdisk();
0285                     edm::LogVerbatim("Tracklet")
0286                         << "TrackletCalculatorDisplaced execute " << getName() << "[" << iSector_ << "]";
0287                   }
0288 
0289                   // check if the seed made from the 3 stubs is valid
0290                   bool accept = false;
0291                   if (iSeed_ == Seed::L2L3L4 || iSeed_ == Seed::L4L5L6)
0292                     accept = LLLSeeding(innerFPGAStub, innerStub, middleFPGAStub, middleStub, outerFPGAStub, outerStub);
0293                   else if (iSeed_ == Seed::L2L3D1)
0294                     accept = LLDSeeding(innerFPGAStub, innerStub, middleFPGAStub, middleStub, outerFPGAStub, outerStub);
0295                   else if (iSeed_ == Seed::D1D2L2)
0296                     accept = DDLSeeding(innerFPGAStub, innerStub, middleFPGAStub, middleStub, outerFPGAStub, outerStub);
0297 
0298                   if (accept)
0299                     countsel++;
0300 
0301                   if (settings_.debugTracklet()) {
0302                     edm::LogVerbatim("Tracklet") << "TrackletCalculatorDisplaced execute done";
0303                   }
0304                   if (countall >= settings_.maxStep("TPD"))
0305                     break;
0306                 }
0307                 if (countall >= settings_.maxStep("TPD"))
0308                   break;
0309               }
0310               if (countall >= settings_.maxStep("TPD"))
0311                 break;
0312             }
0313             if (countall >= settings_.maxStep("TPD"))
0314               break;
0315           }
0316           if (countall >= settings_.maxStep("TPD"))
0317             break;
0318         }
0319         if (countall >= settings_.maxStep("TPD"))
0320           break;
0321       }
0322       if (countall >= settings_.maxStep("TPD"))
0323         break;
0324     }
0325     if (countall >= settings_.maxStep("TPD"))
0326       break;
0327   }
0328 
0329   if (settings_.writeMonitorData("TPD")) {
0330     globals_->ofstream("trackletprocessordisplaced.txt") << getName() << " " << countall << " " << countsel << endl;
0331   }
0332 }