Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-11-23 02:08:59

0001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0002 #include "L1Trigger/DemonstratorTools/interface/codecs/tracks.h"
0003 
0004 namespace l1t::demo::codecs {
0005 
0006   // Return true if a track is contained within a collection
0007   bool trackInCollection(
0008       const edm::Ref<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>>& trackRef,
0009       const edm::Handle<edm::RefVector<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>>>& trackRefCollection) {
0010     auto it = std::find_if(
0011         trackRefCollection->begin(),
0012         trackRefCollection->end(),
0013         [&trackRef](edm::Ref<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>> const& obj) { return obj == trackRef; });
0014     if (it != trackRefCollection->end())
0015       return true;
0016     else
0017       return false;
0018   }
0019 
0020   // Encodes a single track into a 96-bit track word
0021   ap_uint<96> encodeTrack(const TTTrack_TrackWord& t) { return t.getTrackWord(); }
0022 
0023   // Return the 96-bit track words from a given track collection and place them on the appropriate 18 'logical' links
0024   std::array<std::vector<ap_uint<96>>, 18> getTrackWords(const edm::View<TTTrack<Ref_Phase2TrackerDigi_>>& tracks) {
0025     std::array<std::vector<ap_uint<96>>, 18> trackWords;
0026     for (const auto& track : tracks) {
0027       trackWords.at(gttLinkID(track)).push_back(encodeTrack(track));
0028     }
0029     return trackWords;
0030   }
0031 
0032   // Return the 96-bit track words from a given track collection and place them on the appropriate 18 'logical' links
0033   std::array<std::vector<ap_uint<96>>, 18> getTrackWords(
0034       const edm::Handle<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>>& referenceTracks,
0035       const edm::Handle<edm::RefVector<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>>>& tracks) {
0036     std::array<std::vector<ap_uint<96>>, 18> trackWords;
0037     for (unsigned int itrack = 0; itrack < referenceTracks->size(); itrack++) {
0038       const auto& referenceTrack = referenceTracks->at(itrack);
0039       edm::Ref<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>> referenceTrackRef(referenceTracks, itrack);
0040 
0041       if (trackInCollection(referenceTrackRef, tracks)) {
0042         trackWords.at(gttLinkID(referenceTrack)).push_back(encodeTrack(referenceTrack));
0043       } else {
0044         trackWords.at(gttLinkID(referenceTrack)).push_back(ap_uint<96>(0));
0045       }
0046     }
0047     return trackWords;
0048   }
0049 
0050   // Encodes a set of tracks onto a set of links
0051   size_t encodeLinks(std::array<std::vector<ap_uint<96>>, 18>& trackWords,
0052                      std::array<std::vector<ap_uint<64>>, 18>& linkData) {
0053     size_t counter = 0;
0054     for (size_t i = 0; i < linkData.size(); i++) {
0055       // Pad track vectors -> full packet length (156 frames = 104 tracks)
0056       trackWords.at(i).resize(104, 0);
0057       linkData.at(i).resize(156, {0});
0058 
0059       for (size_t j = 0; (j < trackWords.at(i).size()); j += 2) {
0060         linkData.at(i).at(3 * j / 2) = trackWords.at(i).at(j)(63, 0);
0061         linkData.at(i).at(3 * j / 2 + 1) =
0062             (ap_uint<32>(trackWords.at(i).at(j + 1)(31, 0)), ap_uint<32>(trackWords.at(i).at(j)(95, 64)));
0063         linkData.at(i).at(3 * j / 2 + 2) = trackWords.at(i).at(j + 1)(95, 32);
0064         counter += trackWords.at(i).at(j)(95, 95) + trackWords.at(i).at(j + 1)(95, 95);
0065       }
0066     }
0067     return counter;
0068   }
0069 
0070   // Encodes track collection onto 18 output links (2x9 eta-phi sectors; , -/+ eta pairs)
0071   std::array<std::vector<ap_uint<64>>, 18> encodeTracks(const edm::View<TTTrack<Ref_Phase2TrackerDigi_>>& tracks,
0072                                                         int debug) {
0073     if (debug > 0) {
0074       edm::LogInfo("l1t::demo::codecs") << "encodeTrack::Encoding " << tracks.size() << " tracks";
0075     }
0076 
0077     std::array<std::vector<ap_uint<96>>, 18> trackWords = getTrackWords(tracks);
0078     std::array<std::vector<ap_uint<64>>, 18> linkData;
0079     size_t counter = encodeLinks(trackWords, linkData);
0080 
0081     if (debug > 0) {
0082       edm::LogInfo("l1t::demo::codecs") << "encodeTrack::Encoded " << counter << " tracks";
0083     }
0084 
0085     return linkData;
0086   }
0087 
0088   // Encodes a track collection based off the ordering of another track collection
0089   // Requirement: The second collection must be a subset of the first
0090   std::array<std::vector<ap_uint<64>>, 18> encodeTracks(
0091       const edm::Handle<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>>& referenceTracks,
0092       const edm::Handle<edm::RefVector<std::vector<TTTrack<Ref_Phase2TrackerDigi_>>>>& tracks,
0093       int debug) {
0094     if (debug > 0) {
0095       edm::LogInfo("l1t::demo::codecs") << "encodeTrack::Encoding " << tracks->size() << " tracks";
0096     }
0097 
0098     std::array<std::vector<ap_uint<96>>, 18> trackWords = getTrackWords(referenceTracks, tracks);
0099     std::array<std::vector<ap_uint<64>>, 18> linkData;
0100     size_t counter = encodeLinks(trackWords, linkData);
0101 
0102     if (debug > 0) {
0103       edm::LogInfo("l1t::demo::codecs") << "encodeTrack::Encoded " << counter << " tracks";
0104     }
0105 
0106     return linkData;
0107   }
0108 
0109   std::vector<TTTrack_TrackWord> decodeTracks(const std::vector<ap_uint<64>>& frames) {
0110     std::vector<TTTrack_TrackWord> tracks;
0111 
0112     if ((frames.size() % 3) != 0) {
0113       std::stringstream message;
0114       message << "The number of track frames (" << frames.size() << ") is not evenly divisible by 3";
0115       throw std::runtime_error(message.str());
0116     }
0117 
0118     for (size_t i = 0; i < frames.size(); i += 3) {
0119       TTTrack_TrackWord::tkword_t combination1 = (ap_uint<32>(frames.at(i + 1)(31, 0)), frames.at(i)(63, 0));
0120       TTTrack_TrackWord::tkword_t combination2 = (frames.at(i + 2)(63, 0), ap_uint<32>(frames.at(i + 1)(63, 32)));
0121       TTTrack_TrackWord track1, track2;
0122       track1.setTrackWord(
0123           TTTrack_TrackWord::valid_t(combination1(TTTrack_TrackWord::kValidMSB, TTTrack_TrackWord::kValidLSB)),
0124           TTTrack_TrackWord::rinv_t(combination1(TTTrack_TrackWord::kRinvMSB, TTTrack_TrackWord::kRinvLSB)),
0125           TTTrack_TrackWord::phi_t(combination1(TTTrack_TrackWord::kPhiMSB, TTTrack_TrackWord::kPhiLSB)),
0126           TTTrack_TrackWord::tanl_t(combination1(TTTrack_TrackWord::kTanlMSB, TTTrack_TrackWord::kTanlLSB)),
0127           TTTrack_TrackWord::z0_t(combination1(TTTrack_TrackWord::kZ0MSB, TTTrack_TrackWord::kZ0LSB)),
0128           TTTrack_TrackWord::d0_t(combination1(TTTrack_TrackWord::kD0MSB, TTTrack_TrackWord::kD0LSB)),
0129           TTTrack_TrackWord::chi2rphi_t(combination1(TTTrack_TrackWord::kChi2RPhiMSB, TTTrack_TrackWord::kChi2RPhiLSB)),
0130           TTTrack_TrackWord::chi2rz_t(combination1(TTTrack_TrackWord::kChi2RZMSB, TTTrack_TrackWord::kChi2RZLSB)),
0131           TTTrack_TrackWord::bendChi2_t(combination1(TTTrack_TrackWord::kBendChi2MSB, TTTrack_TrackWord::kBendChi2LSB)),
0132           TTTrack_TrackWord::hit_t(combination1(TTTrack_TrackWord::kHitPatternMSB, TTTrack_TrackWord::kHitPatternLSB)),
0133           TTTrack_TrackWord::qualityMVA_t(
0134               combination1(TTTrack_TrackWord::kMVAQualityMSB, TTTrack_TrackWord::kMVAQualityLSB)),
0135           TTTrack_TrackWord::otherMVA_t(
0136               combination1(TTTrack_TrackWord::kMVAOtherMSB, TTTrack_TrackWord::kMVAOtherLSB)));
0137       track2.setTrackWord(
0138           TTTrack_TrackWord::valid_t(combination2(TTTrack_TrackWord::kValidMSB, TTTrack_TrackWord::kValidLSB)),
0139           TTTrack_TrackWord::rinv_t(combination2(TTTrack_TrackWord::kRinvMSB, TTTrack_TrackWord::kRinvLSB)),
0140           TTTrack_TrackWord::phi_t(combination2(TTTrack_TrackWord::kPhiMSB, TTTrack_TrackWord::kPhiLSB)),
0141           TTTrack_TrackWord::tanl_t(combination2(TTTrack_TrackWord::kTanlMSB, TTTrack_TrackWord::kTanlLSB)),
0142           TTTrack_TrackWord::z0_t(combination2(TTTrack_TrackWord::kZ0MSB, TTTrack_TrackWord::kZ0LSB)),
0143           TTTrack_TrackWord::d0_t(combination2(TTTrack_TrackWord::kD0MSB, TTTrack_TrackWord::kD0LSB)),
0144           TTTrack_TrackWord::chi2rphi_t(combination2(TTTrack_TrackWord::kChi2RPhiMSB, TTTrack_TrackWord::kChi2RPhiLSB)),
0145           TTTrack_TrackWord::chi2rz_t(combination2(TTTrack_TrackWord::kChi2RZMSB, TTTrack_TrackWord::kChi2RZLSB)),
0146           TTTrack_TrackWord::bendChi2_t(combination2(TTTrack_TrackWord::kBendChi2MSB, TTTrack_TrackWord::kBendChi2LSB)),
0147           TTTrack_TrackWord::hit_t(combination2(TTTrack_TrackWord::kHitPatternMSB, TTTrack_TrackWord::kHitPatternLSB)),
0148           TTTrack_TrackWord::qualityMVA_t(
0149               combination2(TTTrack_TrackWord::kMVAQualityMSB, TTTrack_TrackWord::kMVAQualityLSB)),
0150           TTTrack_TrackWord::otherMVA_t(
0151               combination2(TTTrack_TrackWord::kMVAOtherMSB, TTTrack_TrackWord::kMVAOtherLSB)));
0152       tracks.push_back(track1);
0153       tracks.push_back(track2);
0154     }
0155 
0156     return tracks;
0157   }
0158 
0159   // Decodes the tracks from 18 'logical' output links (2x9 eta-phi sectors; , -/+ eta pairs)
0160   std::array<std::vector<TTTrack_TrackWord>, 18> decodeTracks(const std::array<std::vector<ap_uint<64>>, 18>& frames) {
0161     std::array<std::vector<TTTrack_TrackWord>, 18> tracks;
0162 
0163     for (size_t i = 0; i < tracks.size(); i++) {
0164       tracks.at(i) = decodeTracks(frames.at(i));
0165     }
0166 
0167     return tracks;
0168   }
0169 
0170 }  // namespace l1t::demo::codecs