Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:09

0001 #include "EventFilter/L1TRawToDigi/interface/MP7PacketReader.h"
0002 
0003 #include <iostream>
0004 
0005 using std::cout;
0006 using std::endl;
0007 
0008 MP7PacketReader::MP7PacketReader(const std::string& path, uint32_t striphdr, uint32_t stripftr, uint32_t ikey)
0009     : reader_(path), header_(striphdr), footer_(stripftr), ikey_(ikey) {
0010   if (!reader_.valid())
0011     return;
0012   load();
0013 }
0014 
0015 //MP7PacketReader::MP7PacketReader(MP7FileReader rdr,
0016 //               uint32_t striphdr,
0017 //               uint32_t stripftr) :
0018 //  reader_( rdr ),
0019 //  header_(striphdr),
0020 //  footer_(stripftr)
0021 //{
0022 //  if ( !reader_.valid() ) return;
0023 //  load();
0024 //}
0025 
0026 MP7PacketReader::~MP7PacketReader() {}
0027 
0028 void MP7PacketReader::load() {
0029   buffers_.reserve(reader_.size());
0030   MP7FileReader::const_iterator it = reader_.begin();
0031   for (; it != reader_.end(); ++it) {
0032     const FileData& raw = *it;
0033 
0034     // Check channel alignment
0035     std::set<std::vector<PacketRange> > rangeSet;
0036 
0037     for (size_t k(0); k < raw.size(); ++k) {
0038       std::vector<PacketRange> ranges = findPackets(raw.link(ikey_));
0039       rangeSet.insert(ranges);
0040     }
0041     //cout << "Number of different patterns: " << rangeSet.size() << endl;
0042     if (rangeSet.size() != 1)
0043       throw std::runtime_error("Links are not aligned!");
0044 
0045     std::vector<PacketRange> pr = *(rangeSet.begin());
0046 
0047     // Create the container
0048     PacketData data;
0049     data.name_ = raw.name();
0050 
0051     data.packets_.reserve(pr.size());
0052 
0053     // loop over the ranges to build packets
0054     for (auto const& p : pr) {
0055       // Check if the header/footer zeroed the packet
0056       if (p.second - p.first - header_ - footer_ <= 0) {
0057         // Turn this into an error message
0058         //cout << "Error: packet length is zero (or less) after header/footer stripping. Skipping." << endl;
0059         continue;
0060       }
0061 
0062       Packet pkt;
0063       FileData::const_iterator lIt = raw.begin();
0064       for (; lIt != raw.end(); ++lIt) {
0065         // Here the 64 bit uint is converted into a 32 bit uint, the data valid bit is stripped in the 64->32 bit conversion.
0066         pkt.links_[lIt->first] = std::vector<uint32_t>(lIt->second.begin() + p.first + header_,
0067                                                        lIt->second.begin() + p.second - footer_ + 1);
0068       }
0069       pkt.first_ = p.first + header_;
0070       pkt.last_ = p.second - footer_;
0071 
0072       data.packets_.push_back(pkt);
0073     }
0074 
0075     buffers_.push_back(data);
0076   }
0077 }
0078 
0079 std::vector<PacketRange> MP7PacketReader::findPackets(std::vector<uint64_t> data) {
0080   std::vector<PacketRange> ranges;
0081   bool v = false;
0082   int32_t begin(-1), end(-1);
0083   for (size_t i(0); i < data.size(); ++i) {
0084     uint64_t x = data[i];
0085     if (not v) {
0086       if ((x >> 32) & 1) {
0087         v = true;
0088         begin = i;
0089       }
0090       continue;
0091     } else {
0092       if (not((x >> 32) & 1)) {
0093         v = false;
0094         end = i - 1;
0095         ranges.push_back(std::make_pair(begin, end));
0096       }
0097       continue;
0098     }
0099   }
0100 
0101   if (v && (begin != -1)) {
0102     end = data.size() - 1;
0103     ranges.push_back(std::make_pair(begin, end));
0104   }
0105 
0106   return ranges;
0107 }