File indexing completed on 2024-04-06 12:22:04
0001 #include "L1Trigger/TrackFindingTracklet/interface/TrackletEngineDisplaced.h"
0002 #include "L1Trigger/TrackFindingTracklet/interface/Settings.h"
0003 #include "L1Trigger/TrackFindingTracklet/interface/Globals.h"
0004 #include "L1Trigger/TrackFindingTracklet/interface/VMStubsTEMemory.h"
0005 #include "L1Trigger/TrackFindingTracklet/interface/StubPairsMemory.h"
0006 #include "L1Trigger/TrackFindingTracklet/interface/MemoryBase.h"
0007 #include "L1Trigger/TrackFindingTracklet/interface/FPGAWord.h"
0008
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 #include "FWCore/Utilities/interface/Exception.h"
0011
0012 using namespace std;
0013 using namespace trklet;
0014
0015 TrackletEngineDisplaced::TrackletEngineDisplaced(string name, Settings const& settings, Globals* global)
0016 : ProcessBase(name, settings, global) {
0017 stubpairs_.clear();
0018 firstvmstubs_.clear();
0019 secondvmstubs_ = nullptr;
0020 layer1_ = 0;
0021 layer2_ = 0;
0022 disk1_ = 0;
0023 disk2_ = 0;
0024 string name1 = name.substr(1);
0025 if (name1[3] == 'L') {
0026 layer1_ = name1[4] - '0';
0027 }
0028 if (name1[3] == 'D') {
0029 disk1_ = name1[4] - '0';
0030 }
0031 if (name1[11] == 'L') {
0032 layer2_ = name1[12] - '0';
0033 }
0034 if (name1[11] == 'D') {
0035 disk2_ = name1[12] - '0';
0036 }
0037 if (name1[12] == 'L') {
0038 layer2_ = name1[13] - '0';
0039 }
0040 if (name1[12] == 'D') {
0041 disk2_ = name1[13] - '0';
0042 }
0043
0044 iSeed_ = -1;
0045 if (layer1_ == 3 && layer2_ == 4)
0046 iSeed_ = 8;
0047 if (layer1_ == 5 && layer2_ == 6)
0048 iSeed_ = 9;
0049 if (layer1_ == 2 && layer2_ == 3)
0050 iSeed_ = 10;
0051 if (disk1_ == 1 && disk2_ == 2)
0052 iSeed_ = 11;
0053
0054 firstphibits_ = settings_.nfinephi(0, iSeed_);
0055 secondphibits_ = settings_.nfinephi(1, iSeed_);
0056 }
0057
0058 TrackletEngineDisplaced::~TrackletEngineDisplaced() { table_.clear(); }
0059
0060 void TrackletEngineDisplaced::addOutput(MemoryBase* memory, string output) {
0061 if (settings_.writetrace()) {
0062 edm::LogVerbatim("Tracklet") << "In " << name_ << " adding output to " << memory->getName() << " to output "
0063 << output;
0064 }
0065 if (output == "stubpairout") {
0066 StubPairsMemory* tmp = dynamic_cast<StubPairsMemory*>(memory);
0067 assert(tmp != nullptr);
0068 stubpairs_.push_back(tmp);
0069 return;
0070 }
0071 throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find output : " << output;
0072 }
0073
0074 void TrackletEngineDisplaced::addInput(MemoryBase* memory, string input) {
0075 if (settings_.writetrace()) {
0076 edm::LogVerbatim("Tracklet") << "In " << name_ << " adding input from " << memory->getName() << " to input "
0077 << input;
0078 }
0079 if (input == "firstvmstubin") {
0080 VMStubsTEMemory* tmp = dynamic_cast<VMStubsTEMemory*>(memory);
0081 assert(tmp != nullptr);
0082 firstvmstubs_.push_back(tmp);
0083 return;
0084 }
0085 if (input == "secondvmstubin") {
0086 VMStubsTEMemory* tmp = dynamic_cast<VMStubsTEMemory*>(memory);
0087 assert(tmp != nullptr);
0088 secondvmstubs_ = tmp;
0089 return;
0090 }
0091 throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find input : " << input;
0092 }
0093
0094 void TrackletEngineDisplaced::execute() {
0095 if (!settings_.useSeed(iSeed_))
0096 return;
0097
0098 if (table_.empty() && (settings_.enableTripletTables() && !settings_.writeTripletTables()))
0099 readTables();
0100
0101 unsigned int countall = 0;
0102 unsigned int countpass = 0;
0103 unsigned int nInnerStubs = 0;
0104
0105 for (unsigned int iInnerMem = 0; iInnerMem < firstvmstubs_.size();
0106 nInnerStubs += firstvmstubs_.at(iInnerMem)->nVMStubs(), iInnerMem++)
0107 ;
0108
0109 assert(!firstvmstubs_.empty());
0110 assert(secondvmstubs_ != nullptr);
0111
0112 for (auto& iInnerMem : firstvmstubs_) {
0113 assert(iInnerMem->nVMStubs() == iInnerMem->nVMStubs());
0114 for (unsigned int i = 0; i < iInnerMem->nVMStubs(); i++) {
0115 const VMStubTE& firstvmstub = iInnerMem->getVMStubTE(i);
0116 if (settings_.debugTracklet()) {
0117 edm::LogVerbatim("Tracklet") << "In " << getName() << " have first stub";
0118 }
0119
0120 if ((layer1_ == 3 && layer2_ == 4) || (layer1_ == 5 && layer2_ == 6)) {
0121 int lookupbits = firstvmstub.vmbits().value() & 1023;
0122 int zdiffmax = (lookupbits >> 7);
0123 int newbin = (lookupbits & 127);
0124 int bin = newbin / 8;
0125
0126 int zbinfirst = newbin & 7;
0127
0128 int start = (bin >> 1);
0129 int last = start + (bin & 1);
0130
0131 assert(last < 8);
0132
0133 if (settings_.debugTracklet()) {
0134 edm::LogVerbatim("Tracklet") << "Will look in zbins " << start << " to " << last;
0135 }
0136 for (int ibin = start; ibin <= last; ibin++) {
0137 for (unsigned int j = 0; j < secondvmstubs_->nVMStubsBinned(ibin); j++) {
0138 if (settings_.debugTracklet()) {
0139 edm::LogVerbatim("Tracklet") << "In " << getName() << " have second stub(1) " << ibin << " " << j;
0140 }
0141
0142 if (countall >= settings_.maxStep("TE"))
0143 break;
0144 countall++;
0145 const VMStubTE& secondvmstub = secondvmstubs_->getVMStubTEBinned(ibin, j);
0146
0147 int zbin = (secondvmstub.vmbits().value() & 7);
0148 if (start != ibin)
0149 zbin += 8;
0150 if (zbin < zbinfirst || zbin - zbinfirst > zdiffmax) {
0151 if (settings_.debugTracklet()) {
0152 edm::LogVerbatim("Tracklet") << "Stubpair rejected because of wrong zbin";
0153 }
0154 continue;
0155 }
0156
0157 assert(firstphibits_ != -1);
0158 assert(secondphibits_ != -1);
0159
0160 FPGAWord iphifirstbin = firstvmstub.finephi();
0161 FPGAWord iphisecondbin = secondvmstub.finephi();
0162
0163 unsigned int index = (iphifirstbin.value() << secondphibits_) + iphisecondbin.value();
0164
0165 FPGAWord firstbend = firstvmstub.bend();
0166 FPGAWord secondbend = secondvmstub.bend();
0167
0168 index = (index << firstbend.nbits()) + firstbend.value();
0169 index = (index << secondbend.nbits()) + secondbend.value();
0170
0171 if ((settings_.enableTripletTables() && !settings_.writeTripletTables()) &&
0172 (index >= table_.size() || table_.at(index).empty())) {
0173 if (settings_.debugTracklet()) {
0174 edm::LogVerbatim("Tracklet")
0175 << "Stub pair rejected because of stub pt cut bends : "
0176 << settings_.benddecode(firstvmstub.bend().value(), layer1_ - 1, firstvmstub.isPSmodule()) << " "
0177 << settings_.benddecode(secondvmstub.bend().value(), layer2_ - 1, secondvmstub.isPSmodule());
0178 }
0179
0180
0181
0182
0183 }
0184
0185 if (settings_.debugTracklet())
0186 edm::LogVerbatim("Tracklet") << "Adding layer-layer pair in " << getName();
0187 for (unsigned int isp = 0; isp < stubpairs_.size(); ++isp) {
0188 if (!settings_.enableTripletTables() || settings_.writeTripletTables() || table_.at(index).count(isp)) {
0189 if (settings_.writeMonitorData("Seeds")) {
0190 ofstream fout("seeds.txt", ofstream::app);
0191 fout << __FILE__ << ":" << __LINE__ << " " << name_ << " " << iSeed_ << endl;
0192 fout.close();
0193 }
0194 stubpairs_.at(isp)->addStubPair(firstvmstub, secondvmstub, index, getName());
0195 }
0196 }
0197
0198 countpass++;
0199 }
0200 }
0201
0202 } else if (layer1_ == 2 && layer2_ == 3) {
0203 int lookupbits = firstvmstub.vmbits().value() & 1023;
0204 int zdiffmax = (lookupbits >> 7);
0205 int newbin = (lookupbits & 127);
0206 int bin = newbin / 8;
0207
0208 int zbinfirst = newbin & 7;
0209
0210 int start = (bin >> 1);
0211 int last = start + (bin & 1);
0212
0213 assert(last < 8);
0214
0215 if (settings_.debugTracklet()) {
0216 edm::LogVerbatim("Tracklet") << "Will look in zbins " << start << " to " << last;
0217 }
0218 for (int ibin = start; ibin <= last; ibin++) {
0219 for (unsigned int j = 0; j < secondvmstubs_->nVMStubsBinned(ibin); j++) {
0220 if (settings_.debugTracklet()) {
0221 edm::LogVerbatim("Tracklet") << "In " << getName() << " have second stub(2) ";
0222 }
0223
0224 if (countall >= settings_.maxStep("TE"))
0225 break;
0226 countall++;
0227
0228 const VMStubTE& secondvmstub = secondvmstubs_->getVMStubTEBinned(ibin, j);
0229
0230 int zbin = (secondvmstub.vmbits().value() & 7);
0231 if (start != ibin)
0232 zbin += 8;
0233 if (zbin < zbinfirst || zbin - zbinfirst > zdiffmax) {
0234 if (settings_.debugTracklet()) {
0235 edm::LogVerbatim("Tracklet") << "Stubpair rejected because of wrong zbin";
0236 }
0237 continue;
0238 }
0239
0240 assert(firstphibits_ != -1);
0241 assert(secondphibits_ != -1);
0242
0243 FPGAWord iphifirstbin = firstvmstub.finephi();
0244 FPGAWord iphisecondbin = secondvmstub.finephi();
0245
0246 unsigned int index = (iphifirstbin.value() << secondphibits_) + iphisecondbin.value();
0247
0248 FPGAWord firstbend = firstvmstub.bend();
0249 FPGAWord secondbend = secondvmstub.bend();
0250
0251 index = (index << firstbend.nbits()) + firstbend.value();
0252 index = (index << secondbend.nbits()) + secondbend.value();
0253
0254 if ((settings_.enableTripletTables() && !settings_.writeTripletTables()) &&
0255 (index >= table_.size() || table_.at(index).empty())) {
0256 if (settings_.debugTracklet()) {
0257 edm::LogVerbatim("Tracklet")
0258 << "Stub pair rejected because of stub pt cut bends : "
0259 << settings_.benddecode(firstvmstub.bend().value(), layer1_ - 1, firstvmstub.isPSmodule()) << " "
0260 << settings_.benddecode(secondvmstub.bend().value(), layer2_ - 1, secondvmstub.isPSmodule());
0261 }
0262 continue;
0263 }
0264
0265 if (settings_.debugTracklet())
0266 edm::LogVerbatim("Tracklet") << "Adding layer-layer pair in " << getName();
0267 for (unsigned int isp = 0; isp < stubpairs_.size(); ++isp) {
0268 if ((!settings_.enableTripletTables() || settings_.writeTripletTables()) ||
0269 (index < table_.size() && table_.at(index).count(isp))) {
0270 if (settings_.writeMonitorData("Seeds")) {
0271 ofstream fout("seeds.txt", ofstream::app);
0272 fout << __FILE__ << ":" << __LINE__ << " " << name_ << " " << iSeed_ << endl;
0273 fout.close();
0274 }
0275 stubpairs_.at(isp)->addStubPair(firstvmstub, secondvmstub, index, getName());
0276 }
0277 }
0278
0279 countpass++;
0280 }
0281 }
0282
0283 } else if (disk1_ == 1 && disk2_ == 2) {
0284 if (settings_.debugTracklet())
0285 edm::LogVerbatim("Tracklet") << getName() << " Disk-disk pair";
0286
0287 int lookupbits = firstvmstub.vmbits().value() & 511;
0288 bool negdisk = firstvmstub.stub()->disk().value() < 0;
0289 int rdiffmax = (lookupbits >> 6);
0290 int newbin = (lookupbits & 63);
0291 int bin = newbin / 8;
0292
0293 int rbinfirst = newbin & 7;
0294
0295 int start = (bin >> 1);
0296 if (negdisk)
0297 start += 4;
0298 int last = start + (bin & 1);
0299 assert(last < 8);
0300 for (int ibin = start; ibin <= last; ibin++) {
0301 if (settings_.debugTracklet()) {
0302 edm::LogVerbatim("Tracklet") << getName() << " looking for matching stub in " << secondvmstubs_->getName()
0303 << " in bin = " << ibin << " with " << secondvmstubs_->nVMStubsBinned(ibin)
0304 << " stubs";
0305 }
0306 for (unsigned int j = 0; j < secondvmstubs_->nVMStubsBinned(ibin); j++) {
0307 if (countall >= settings_.maxStep("TE"))
0308 break;
0309 countall++;
0310
0311 const VMStubTE& secondvmstub = secondvmstubs_->getVMStubTEBinned(ibin, j);
0312
0313 int rbin = (secondvmstub.vmbits().value() & 7);
0314 if (start != ibin)
0315 rbin += 8;
0316 if (rbin < rbinfirst)
0317 continue;
0318 if (rbin - rbinfirst > rdiffmax)
0319 continue;
0320
0321 unsigned int irsecondbin = secondvmstub.vmbits().value() >> 2;
0322
0323 FPGAWord iphifirstbin = firstvmstub.finephi();
0324 FPGAWord iphisecondbin = secondvmstub.finephi();
0325
0326 unsigned int index = (irsecondbin << (secondphibits_ + firstphibits_)) +
0327 (iphifirstbin.value() << secondphibits_) + iphisecondbin.value();
0328
0329 FPGAWord firstbend = firstvmstub.bend();
0330 FPGAWord secondbend = secondvmstub.bend();
0331
0332 index = (index << firstbend.nbits()) + firstbend.value();
0333 index = (index << secondbend.nbits()) + secondbend.value();
0334
0335 if ((settings_.enableTripletTables() && !settings_.writeTripletTables()) &&
0336 (index >= table_.size() || table_.at(index).empty())) {
0337 if (settings_.debugTracklet()) {
0338 edm::LogVerbatim("Tracklet")
0339 << "Stub pair rejected because of stub pt cut bends : "
0340 << settings_.benddecode(firstvmstub.bend().value(), disk1_ + 5, firstvmstub.isPSmodule()) << " "
0341 << settings_.benddecode(secondvmstub.bend().value(), disk2_ + 5, secondvmstub.isPSmodule());
0342 }
0343 continue;
0344 }
0345
0346 if (settings_.debugTracklet())
0347 edm::LogVerbatim("Tracklet") << "Adding disk-disk pair in " << getName();
0348
0349 for (unsigned int isp = 0; isp < stubpairs_.size(); ++isp) {
0350 if ((!settings_.enableTripletTables() || settings_.writeTripletTables()) ||
0351 (index < table_.size() && table_.at(index).count(isp))) {
0352 if (settings_.writeMonitorData("Seeds")) {
0353 ofstream fout("seeds.txt", ofstream::app);
0354 fout << __FILE__ << ":" << __LINE__ << " " << name_ << " " << iSeed_ << endl;
0355 fout.close();
0356 }
0357 stubpairs_.at(isp)->addStubPair(firstvmstub, secondvmstub, index, getName());
0358 }
0359 }
0360 countpass++;
0361 }
0362 }
0363 }
0364 }
0365 }
0366 if (countall > 5000) {
0367 edm::LogVerbatim("Tracklet") << "In TrackletEngineDisplaced::execute : " << getName() << " " << nInnerStubs << " "
0368 << secondvmstubs_->nVMStubs() << " " << countall << " " << countpass;
0369 for (auto& iInnerMem : firstvmstubs_) {
0370 for (unsigned int i = 0; i < iInnerMem->nVMStubs(); i++) {
0371 const VMStubTE& firstvmstub = iInnerMem->getVMStubTE(i);
0372 edm::LogVerbatim("Tracklet") << "In TrackletEngineDisplaced::execute first stub : "
0373 << firstvmstub.stub()->l1tstub()->r() << " "
0374 << firstvmstub.stub()->l1tstub()->phi() << " "
0375 << firstvmstub.stub()->l1tstub()->r() * firstvmstub.stub()->l1tstub()->phi() << " "
0376 << firstvmstub.stub()->l1tstub()->z();
0377 }
0378 }
0379 for (unsigned int i = 0; i < secondvmstubs_->nVMStubs(); i++) {
0380 const VMStubTE& secondvmstub = secondvmstubs_->getVMStubTE(i);
0381 edm::LogVerbatim("Tracklet") << "In TrackletEngineDisplaced::execute second stub : "
0382 << secondvmstub.stub()->l1tstub()->r() << " "
0383 << secondvmstub.stub()->l1tstub()->phi() << " "
0384 << secondvmstub.stub()->l1tstub()->r() * secondvmstub.stub()->l1tstub()->phi() << " "
0385 << secondvmstub.stub()->l1tstub()->z();
0386 }
0387 }
0388
0389 if (settings_.writeMonitorData("TED")) {
0390 globals_->ofstream("trackletenginedisplaces.txt") << getName() << " " << countall << " " << countpass << endl;
0391 }
0392 }
0393
0394 void TrackletEngineDisplaced::readTables() {
0395 ifstream fin;
0396 string tableName, line, word;
0397
0398 string tablePath = settings_.tableTEDFile();
0399 unsigned int finddir = tablePath.find("table_TED_");
0400 tableName = tablePath.substr(0, finddir) + "table_" + name_ + ".txt";
0401
0402 fin.open(tableName, ifstream::in);
0403 if (!fin) {
0404 throw cms::Exception("BadConfig") << "TripletEngine::readTables, file " << tableName << " not known";
0405 }
0406
0407 while (getline(fin, line)) {
0408 istringstream iss(line);
0409 table_.resize(table_.size() + 1);
0410
0411 while (iss >> word)
0412 table_[table_.size() - 1].insert(memNameToIndex(word));
0413 }
0414 fin.close();
0415 }
0416
0417 short TrackletEngineDisplaced::memNameToIndex(const string& name) {
0418 for (unsigned int isp = 0; isp < stubpairs_.size(); ++isp)
0419 if (stubpairs_.at(isp)->getName() == name)
0420 return isp;
0421 return -1;
0422 }