Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-10-14 01:44:11

0001 #include "L1Trigger/TrackFindingTracklet/interface/TrackletEventProcessor.h"
0002 #include "L1Trigger/TrackFindingTracklet/interface/SLHCEvent.h"
0003 #include "L1Trigger/TrackFindingTracklet/interface/Globals.h"
0004 #include "L1Trigger/TrackFindingTracklet/interface/SLHCEvent.h"
0005 #include "L1Trigger/TrackFindingTracklet/interface/Sector.h"
0006 #include "L1Trigger/TrackFindingTracklet/interface/HistBase.h"
0007 #include "L1Trigger/TrackFindingTracklet/interface/Track.h"
0008 #include "L1Trigger/TrackFindingTracklet/interface/TrackletConfigBuilder.h"
0009 #include "L1Trigger/TrackFindingTracklet/interface/IMATH_TrackletCalculator.h"
0010 #include "L1Trigger/TrackFindingTracklet/interface/StubStreamData.h"
0011 
0012 #include "DataFormats/Math/interface/deltaPhi.h"
0013 
0014 #include <iomanip>
0015 #include <filesystem>
0016 
0017 using namespace trklet;
0018 using namespace std;
0019 
0020 TrackletEventProcessor::TrackletEventProcessor() : settings_(nullptr) {}
0021 
0022 TrackletEventProcessor::~TrackletEventProcessor() {
0023   if (settings_ && settings_->bookHistos()) {
0024     histbase_->close();
0025   }
0026 }
0027 
0028 void TrackletEventProcessor::init(Settings const& theSettings, const tt::Setup* setup) {
0029   settings_ = &theSettings;
0030   globals_ = make_unique<Globals>(*settings_);
0031 
0032   //Verify consistency
0033   if (settings_->kphi0pars() != globals_->ITC_L1L2()->phi0_final.K()) {
0034     throw cms::Exception("Inconsistency") << "phi0 conversion parameter inconsistency\n";
0035   }
0036 
0037   if (settings_->krinvpars() != globals_->ITC_L1L2()->rinv_final.K()) {
0038     throw cms::Exception("Inconsistency") << "ring conversion parameter inconsistency\n";
0039   }
0040 
0041   if (settings_->ktpars() != globals_->ITC_L1L2()->t_final.K()) {
0042     throw cms::Exception("Inconsistency") << "t conversion parameter inconsistency\n";
0043   }
0044 
0045   if (settings_->kphider() != globals_->ITC_L1L2()->der_phiL_final.K()) {
0046     throw cms::Exception("Inconsistency")
0047         << "t conversion parameter inconsistency:" << settings_->kphider() / globals_->ITC_L1L2()->der_phiL_final.K()
0048         << "\n";
0049   }
0050 
0051   if (settings_->debugTracklet()) {
0052     edm::LogVerbatim("Tracklet") << "========================================================= \n"
0053                                  << "Conversion factors for global coordinates: \n"
0054                                  << "z    kz            = " << settings_->kz() << "\n"
0055                                  << "r    kr            = " << settings_->kr() << "\n"
0056                                  << "phi  kphi1         = " << settings_->kphi1() << "\n"
0057                                  << "========================================================= \n"
0058                                  << "Conversion factors for track(let) parameters: \n"
0059                                  << "rinv krinvpars     = " << settings_->krinvpars() << "\n"
0060                                  << "phi0 kphi0pars     = " << settings_->kphi0pars() << "\n"
0061                                  << "d0   kd0pars       = " << settings_->kd0pars() << "\n"
0062                                  << "t    ktpars        = " << settings_->ktpars() << "\n"
0063                                  << "z0   kz0pars       = " << settings_->kz0pars() << "\n"
0064                                  << "========================================================= \n"
0065                                  << "phi0bitshift = " << settings_->phi0bitshift() << "\n"
0066                                  << "d0bitshift   = ??? \n"
0067                                  << "=========================================================";
0068   }
0069 
0070   if (settings_->bookHistos()) {
0071     histbase_ = new HistBase;
0072     histbase_->open();
0073     histbase_->bookLayerResidual();
0074     histbase_->bookDiskResidual();
0075     histbase_->bookTrackletParams();
0076     histbase_->bookSeedEff();
0077 
0078     globals_->histograms() = histbase_;
0079   }
0080 
0081   sector_ = make_unique<Sector>(*settings_, globals_.get());
0082 
0083   if (settings_->extended() || settings_->reduced()) {
0084     ifstream inmem(settings_->memoryModulesFile().c_str());
0085     assert(inmem.good());
0086 
0087     ifstream inproc(settings_->processingModulesFile().c_str());
0088     assert(inproc.good());
0089 
0090     ifstream inwire(settings_->wiresFile().c_str());
0091     assert(inwire.good());
0092 
0093     configure(inwire, inmem, inproc);
0094 
0095   } else {
0096     TrackletConfigBuilder config(*settings_, setup);
0097 
0098     //Write configurations to file.
0099     if (settings_->writeConfig()) {
0100       std::ofstream wires = openfile(settings_->tablePath(), "wires.dat", __FILE__, __LINE__);
0101       std::ofstream memorymodules = openfile(settings_->tablePath(), "memorymodules.dat", __FILE__, __LINE__);
0102       std::ofstream processingmodules = openfile(settings_->tablePath(), "processingmodules.dat", __FILE__, __LINE__);
0103 
0104       config.writeAll(wires, memorymodules, processingmodules);
0105     }
0106 
0107     std::stringstream wires;
0108     std::stringstream memorymodules;
0109     std::stringstream processingmodules;
0110 
0111     config.writeAll(wires, memorymodules, processingmodules);
0112     configure(wires, memorymodules, processingmodules);
0113   }
0114 }
0115 
0116 void TrackletEventProcessor::configure(istream& inwire, istream& inmem, istream& inproc) {
0117   // get the memory modules
0118   if (settings_->debugTracklet()) {
0119     edm::LogVerbatim("Tracklet") << "Will read memory modules";
0120   }
0121 
0122   while (inmem.good()) {
0123     string memType, memName, size;
0124     inmem >> memType >> memName >> size;
0125     if (!inmem.good())
0126       continue;
0127     if (settings_->writetrace()) {
0128       edm::LogVerbatim("Tracklet") << "Read memory: " << memType << " " << memName;
0129     }
0130     sector_->addMem(memType, memName);
0131   }
0132 
0133   // get the processing modules
0134   if (settings_->debugTracklet()) {
0135     edm::LogVerbatim("Tracklet") << "Will read processing modules";
0136   }
0137 
0138   while (inproc.good()) {
0139     string procType, procName;
0140     inproc >> procType >> procName;
0141     if (!inproc.good())
0142       continue;
0143     if (settings_->writetrace()) {
0144       edm::LogVerbatim("Tracklet") << "Read process: " << procType << " " << procName;
0145     }
0146     sector_->addProc(procType, procName);
0147   }
0148 
0149   // get the wiring information
0150   if (settings_->debugTracklet()) {
0151     edm::LogVerbatim("Tracklet") << "Will read wiring information";
0152   }
0153 
0154   while (inwire.good()) {
0155     string line;
0156     getline(inwire, line);
0157     if (!inwire.good())
0158       continue;
0159     if (settings_->writetrace()) {
0160       edm::LogVerbatim("Tracklet") << "Line : " << line;
0161     }
0162     stringstream ss(line);
0163     string mem, tmp1, procin, tmp2, procout;
0164     ss >> mem >> tmp1 >> procin;
0165     if (procin == "output=>") {
0166       procin = "";
0167       ss >> procout;
0168     } else {
0169       ss >> tmp2 >> procout;
0170     }
0171 
0172     sector_->addWire(mem, procin, procout);
0173   }
0174 }
0175 
0176 void TrackletEventProcessor::event(SLHCEvent& ev,
0177                                    vector<vector<string>>& streamsTrackRaw,
0178                                    vector<vector<StubStreamData>>& streamsStubRaw) {
0179   globals_->event() = &ev;
0180 
0181   tracks_.clear();
0182 
0183   eventnum_++;
0184   bool first = (eventnum_ == 1);
0185 
0186   for (unsigned int k = 0; k < N_SECTOR; k++) {
0187     sector_->setSector(k);
0188 
0189     cleanTimer_.start();
0190     sector_->clean();
0191     cleanTimer_.stop();
0192 
0193     addStubTimer_.start();
0194 
0195     vector<int> layerstubs(N_LAYER + N_DISK, 0);
0196     vector<int> layerstubssector(N_SECTOR * (N_LAYER + N_DISK), 0);
0197 
0198     for (int j = 0; j < ev.nstubs(); j++) {
0199       const L1TStub& stub = ev.stub(j);
0200       unsigned int isector = stub.region();
0201       if (isector != k) {
0202         continue;
0203       }
0204 
0205       const string& dtc = stub.DTClink();
0206 
0207       layerstubs[stub.layerdisk()]++;
0208       layerstubssector[isector * (N_LAYER + N_DISK) + stub.layerdisk()]++;
0209 
0210       sector_->addStub(stub, dtc);
0211     }
0212 
0213     if (settings_->writeMonitorData("StubsLayerSector")) {
0214       for (unsigned int index = 0; index < layerstubssector.size(); index++) {
0215         int layerdisk = index % (N_LAYER + N_DISK);
0216         int sector = index / (N_LAYER + N_DISK);
0217         globals_->ofstream("stubslayersector.txt")
0218             << layerdisk << " " << sector << " " << layerstubssector[index] << endl;
0219       }
0220     }
0221 
0222     if (settings_->writeMonitorData("StubsLayer")) {
0223       for (unsigned int layerdisk = 0; layerdisk < layerstubs.size(); layerdisk++) {
0224         globals_->ofstream("stubslayer.txt") << layerdisk << " " << layerstubs[layerdisk] << endl;
0225       }
0226     }
0227 
0228     addStubTimer_.stop();
0229 
0230     // ----------------------------------------------------------------------------------------
0231     // Now start the tracklet processing
0232 
0233     // VM router
0234     InputRouterTimer_.start();
0235     sector_->executeIR();
0236     if (settings_->writeMem() && k == settings_->writememsect()) {
0237       sector_->writeDTCStubs(first);
0238       sector_->writeIRStubs(first);
0239     }
0240     InputRouterTimer_.stop();
0241 
0242     VMRouterTimer_.start();
0243     sector_->executeVMR();
0244     if (settings_->writeMem() && k == settings_->writememsect()) {
0245       sector_->writeVMSTE(first);
0246       sector_->writeVMSME(first);
0247       sector_->writeAS(first);
0248       sector_->writeAIS(first);
0249     }
0250     VMRouterTimer_.stop();
0251 
0252     // tracklet engine
0253     TETimer_.start();
0254     sector_->executeTE();
0255     TETimer_.stop();
0256 
0257     // tracklet engine displaced
0258     TEDTimer_.start();
0259     sector_->executeTED();
0260     TEDTimer_.stop();
0261 
0262     // triplet engine
0263     TRETimer_.start();
0264     sector_->executeTRE();
0265     if (settings_->writeMem() && k == settings_->writememsect()) {
0266       sector_->writeST(first);
0267     }
0268     TRETimer_.stop();
0269 
0270     // tracklet processor (alternative implementation to TE+TC)
0271     TPTimer_.start();
0272     sector_->executeTP();
0273     TPTimer_.stop();
0274 
0275     if (settings_->writeMem() && k == settings_->writememsect()) {
0276       sector_->writeSP(first);
0277     }
0278 
0279     // tracklet calculator
0280     TCTimer_.start();
0281     sector_->executeTC();
0282     TCTimer_.stop();
0283 
0284     if (settings_->writeMonitorData("HitEff") || settings_->bookHistos()) {
0285       int nTP = globals_->event()->nsimtracks();
0286       for (int iTP = 0; iTP < nTP; iTP++) {
0287         L1SimTrack simtrk = globals_->event()->simtrack(iTP);
0288         if (simtrk.pt() < 2.0)
0289           continue;
0290         if (std::abs(simtrk.vz()) > 15.0)
0291           continue;
0292         if (hypot(simtrk.vx(), simtrk.vy()) > 0.1)
0293           continue;
0294         bool electron = (abs(simtrk.type()) == 11);
0295         bool muon = (abs(simtrk.type()) == 13);
0296         bool pion = (abs(simtrk.type()) == 211);
0297         bool kaon = (abs(simtrk.type()) == 321);
0298         bool proton = (abs(simtrk.type()) == 2212);
0299         if (!(electron || muon || pion || kaon || proton))
0300           continue;
0301         int nlayers = 0;
0302         int ndisks = 0;
0303         int simtrackid = simtrk.trackid();
0304         unsigned int hitmask = 0;
0305         hitmask = ev.layersHit(simtrackid, nlayers, ndisks);  // FIX CPU use.
0306         if (nlayers + ndisks < 4)
0307           continue;
0308 
0309         if (settings_->writeMonitorData("HitEff")) {
0310           static ofstream outhit("hiteff.txt");
0311           outhit << simtrk.eta() << " " << (hitmask & 1) << " " << (hitmask & 2) << " " << (hitmask & 4) << " "
0312                  << (hitmask & 8) << " " << (hitmask & 16) << " " << (hitmask & 32) << " " << (hitmask & 64) << " "
0313                  << (hitmask & 128) << " " << (hitmask & 256) << " " << (hitmask & 512) << " " << (hitmask & 1024)
0314                  << endl;
0315         }
0316 
0317         std::unordered_set<int> matchseed;
0318         std::unordered_set<int> matchseedtmp = sector_->seedMatch(iTP);
0319         matchseed.insert(matchseedtmp.begin(), matchseedtmp.end());
0320         if (settings_->bookHistos()) {
0321           for (int iseed = 0; iseed < 8; iseed++) {
0322             bool eff = matchseed.find(iseed) != matchseed.end();
0323             globals_->histograms()->fillSeedEff(iseed, simtrk.eta(), eff);
0324           }
0325         }
0326       }
0327     }
0328 
0329     // tracklet calculator displaced
0330     TCDTimer_.start();
0331     sector_->executeTCD();
0332     TCDTimer_.stop();
0333 
0334     // tracklet processor displaced
0335     TPDTimer_.start();
0336     sector_->executeTPD();
0337     TPDTimer_.stop();
0338 
0339     if (settings_->writeMem() && k == settings_->writememsect()) {
0340       sector_->writeTPAR(first);
0341       sector_->writeTPROJ(first);
0342     }
0343 
0344     // projection router
0345     PRTimer_.start();
0346     sector_->executePR();
0347     if (settings_->writeMem() && k == settings_->writememsect()) {
0348       sector_->writeVMPROJ(first);
0349       sector_->writeAP(first);
0350     }
0351     PRTimer_.stop();
0352 
0353     // match engine
0354     METimer_.start();
0355     sector_->executeME();
0356     if (settings_->writeMem() && k == settings_->writememsect()) {
0357       sector_->writeCM(first);
0358     }
0359     METimer_.stop();
0360 
0361     // match calculator
0362     MCTimer_.start();
0363     sector_->executeMC();
0364     MCTimer_.stop();
0365 
0366     // match processor (alternative to ME+MC)
0367     MPTimer_.start();
0368     sector_->executeMP();
0369     MPTimer_.stop();
0370 
0371     if (settings_->writeMem() && k == settings_->writememsect()) {
0372       sector_->writeMC(first);
0373     }
0374 
0375     // fit track
0376     FTTimer_.start();
0377     sector_->executeFT(streamsTrackRaw, streamsStubRaw);
0378     if ((settings_->writeMem() || settings_->writeMonitorData("IFit")) && k == settings_->writememsect()) {
0379       sector_->writeTF(first);
0380     }
0381     FTTimer_.stop();
0382 
0383     // purge duplicate
0384     PDTimer_.start();
0385     sector_->executePD(tracks_);
0386     if (((settings_->writeMem() || settings_->writeMonitorData("IFit")) && k == settings_->writememsect()) ||
0387         settings_->writeMonitorData("CT")) {
0388       sector_->writeCT(first);
0389     }
0390     PDTimer_.stop();
0391   }
0392 }
0393 
0394 void TrackletEventProcessor::printSummary() {
0395   if (settings_->bookHistos()) {
0396     globals_->histograms()->close();
0397   }
0398 
0399   edm::LogVerbatim("Tracklet") << "Process             Times called   Average time (ms)      Total time (s)"
0400                                << "\n"
0401                                << "Cleaning              " << setw(10) << cleanTimer_.ntimes() << setw(20)
0402                                << setprecision(3) << cleanTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0403                                << cleanTimer_.tottime() << "\n"
0404                                << "Add Stubs             " << setw(10) << addStubTimer_.ntimes() << setw(20)
0405                                << setprecision(3) << addStubTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0406                                << addStubTimer_.tottime() << "\n"
0407                                << "InputRouter           " << setw(10) << InputRouterTimer_.ntimes() << setw(20)
0408                                << setprecision(3) << InputRouterTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0409                                << InputRouterTimer_.tottime() << "\n"
0410                                << "VMRouter              " << setw(10) << VMRouterTimer_.ntimes() << setw(20)
0411                                << setprecision(3) << VMRouterTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0412                                << VMRouterTimer_.tottime();
0413   if (settings_->combined()) {
0414     edm::LogVerbatim("Tracklet") << "TrackletProcessor     " << setw(10) << TPTimer_.ntimes() << setw(20)
0415                                  << setprecision(3) << TPTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0416                                  << TPTimer_.tottime() << "\n"
0417                                  << "MatchProcessor        " << setw(10) << MPTimer_.ntimes() << setw(20)
0418                                  << setprecision(3) << MPTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0419                                  << MPTimer_.tottime();
0420   } else {
0421     edm::LogVerbatim("Tracklet") << "TrackletEngine        " << setw(10) << TETimer_.ntimes() << setw(20)
0422                                  << setprecision(3) << TETimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0423                                  << TETimer_.tottime();
0424     if (settings_->extended()) {
0425       edm::LogVerbatim("Tracklet") << "TrackletEngineDisplaced" << setw(10) << TEDTimer_.ntimes() << setw(20)
0426                                    << setprecision(3) << TEDTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0427                                    << TEDTimer_.tottime() << "\n"
0428                                    << "TripletEngine         " << setw(10) << TRETimer_.ntimes() << setw(20)
0429                                    << setprecision(3) << TRETimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0430                                    << TRETimer_.tottime() << "\n"
0431                                    << "TrackletCalculatorDisplaced" << setw(10) << TCDTimer_.ntimes() << setw(20)
0432                                    << setprecision(3) << TCDTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0433                                    << TCDTimer_.tottime() << "\n"
0434                                    << TCDTimer_.tottime() << "\n"
0435                                    << "TrackletProcessorDisplaced" << setw(10) << TPDTimer_.ntimes() << setw(20)
0436                                    << setprecision(3) << TPDTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0437                                    << TPDTimer_.tottime();
0438     }
0439     edm::LogVerbatim("Tracklet") << "TrackletCalculator    " << setw(10) << TCTimer_.ntimes() << setw(20)
0440                                  << setprecision(3) << TCTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0441                                  << TCTimer_.tottime() << "\n"
0442                                  << "ProjectionRouter      " << setw(10) << PRTimer_.ntimes() << setw(20)
0443                                  << setprecision(3) << PRTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0444                                  << PRTimer_.tottime() << "\n"
0445                                  << "MatchEngine           " << setw(10) << METimer_.ntimes() << setw(20)
0446                                  << setprecision(3) << METimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0447                                  << METimer_.tottime() << "\n"
0448                                  << "MatchCalculator       " << setw(10) << MCTimer_.ntimes() << setw(20)
0449                                  << setprecision(3) << MCTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0450                                  << MCTimer_.tottime();
0451   }
0452   edm::LogVerbatim("Tracklet") << "FitTrack              " << setw(10) << FTTimer_.ntimes() << setw(20)
0453                                << setprecision(3) << FTTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0454                                << FTTimer_.tottime() << "\n"
0455                                << "PurgeDuplicate        " << setw(10) << PDTimer_.ntimes() << setw(20)
0456                                << setprecision(3) << PDTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
0457                                << PDTimer_.tottime();
0458 }