Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-14 02:33:45

0001 
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 
0004 #include "EventFilter/L1TRawToDigi/interface/MP7FileReader.h"
0005 
0006 #include <boost/algorithm/string.hpp>
0007 #include <boost/regex.hpp>
0008 
0009 #include <iostream>
0010 #include <string>
0011 
0012 // Namespace resolution
0013 using std::cout;
0014 using std::endl;
0015 
0016 // Constants initialization
0017 boost::regex MP7FileReader::reBoard_("^Board (.+)");
0018 boost::regex MP7FileReader::reLink_("^Link : (.*)");
0019 boost::regex MP7FileReader::reQuadChan_("^Quad/Chan : (.*)");
0020 boost::regex MP7FileReader::reFrame_("^Frame (\\d{4}) : (.*)");
0021 boost::regex MP7FileReader::reValid_("([01])v([0-9a-fA-F]{8})");
0022 
0023 //____________________________________________________________________________//
0024 const std::vector<uint64_t>& FileData::link(uint32_t i) const {
0025   LinkMap::const_iterator it = links_.find(i);
0026   if (it == links_.end()) {
0027     edm::LogError("L1T") << "Link id " << i << " not found";
0028   }
0029   return it->second;
0030 }
0031 
0032 //____________________________________________________________________________//
0033 MP7FileReader::MP7FileReader(const std::string& path) : valid_(false), path_(path), file_(path) {
0034   if (!file_.is_open()) {
0035     edm::LogError("L1T") << "File " << path << " not found";
0036     valid_ = false;
0037     return;
0038   } else {
0039     LogDebug("L1T") << "Reading file " << path;
0040   }
0041 
0042   load();
0043 
0044   LogDebug("L1T") << "# buffers " << buffers_.size();
0045 
0046   if (!buffers_.empty()) {
0047     LogDebug("L1T") << "# links " << buffers_.at(0).size();
0048     if (buffers_.at(0).size() > 0) {
0049       LogDebug("L1T") << "# frames " << buffers_.at(0).link(0).size();
0050     }
0051   }
0052 }
0053 
0054 //____________________________________________________________________________//
0055 MP7FileReader::~MP7FileReader() {}
0056 
0057 //____________________________________________________________________________//
0058 const FileData& MP7FileReader::get(size_t k) const { return buffers_.at(k); }
0059 
0060 std::vector<std::string> MP7FileReader::names() const {
0061   std::vector<std::string> names(buffers_.size());
0062 
0063   for (auto const& r : buffers_) {
0064     names.push_back(r.name());
0065   }
0066   return names;
0067 }
0068 //____________________________________________________________________________//
0069 void MP7FileReader::load() {
0070   using namespace boost;
0071 
0072   // Data, to be stored in a BufferSnapshot object
0073   while (file_.good()) {
0074     std::string id = searchBoard();
0075     //cout << "Id: " << id << endl;
0076     std::vector<uint32_t> links = searchLinks();
0077 
0078     //cout << "Links (" << links.size() << ") : ";
0079 
0080     //for(uint32_t l : links) {
0081     //cout << l << ",";
0082     //}
0083     //cout << endl;
0084 
0085     std::vector<std::vector<uint64_t> > data = readRows();
0086     //cout << "Data loaded (" << data.size() << ")" << endl;
0087 
0088     // Id, Link # and Data Loaded
0089 
0090     FileData s;
0091     s.name_ = id;
0092 
0093     std::vector<std::vector<uint64_t> > chans(links.size(), std::vector<uint64_t>(data.size()));
0094 
0095     // Transpose
0096     for (size_t i(0); i < links.size(); ++i) {
0097       for (size_t j(0); j < data.size(); ++j) {
0098         chans[i][j] = data[j][i];
0099       }
0100     }
0101 
0102     // pack
0103     for (size_t i(0); i < links.size(); ++i) {
0104       s.links_.insert(std::make_pair(links[i], chans[i]));
0105     }
0106 
0107     buffers_.push_back(s);
0108   }
0109 
0110   // File successfully read
0111   valid_ = true;
0112 }
0113 
0114 //____________________________________________________________________________//
0115 std::string MP7FileReader::searchBoard() {
0116   std::string line;
0117   std::string id;
0118   boost::smatch what;
0119 
0120   while (getline(file_, line)) {
0121     // Trim and skip empties and comments
0122     boost::trim(line);
0123     if (line.empty())
0124       continue;
0125     if (line[0] == '#')
0126       continue;
0127 
0128     if (boost::regex_match(line, what, reBoard_)) {
0129       // Create a new buffer snapshot
0130       id = what[1];
0131       return id;
0132     } else {
0133       edm::LogError("L1T") << "Unexpected line found";
0134       return std::string("");
0135     }
0136   }
0137   edm::LogError("L1T") << "No board found";
0138   return std::string("");
0139 }
0140 
0141 //____________________________________________________________________________//
0142 std::vector<uint32_t> MP7FileReader::searchLinks() {
0143   std::string line;
0144   boost::smatch what;
0145 
0146   while (getline(file_, line)) {
0147     boost::trim(line);
0148     if (line.empty())
0149       continue;
0150     if (line[0] == '#')
0151       continue;
0152 
0153     if (boost::regex_match(line, what, reQuadChan_)) {
0154       // Not used
0155       continue;
0156     }
0157 
0158     if (boost::regex_match(line, what, reLink_)) {
0159       std::vector<std::string> tokens;
0160       std::string tmp = what[1].str();
0161       // Trim the line
0162       boost::trim(tmp);
0163       // Split line into tokens
0164       boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
0165       // Convert it into uint32 s
0166       std::vector<uint32_t> links;
0167       std::transform(tokens.begin(), tokens.end(), std::back_inserter(links), [](const std::string& str) {
0168         return std::stoul(str);
0169       });
0170     } else {
0171       throw std::logic_error("Unexpected line found!");
0172     }
0173   }
0174   throw std::logic_error("No list of links found");
0175 }
0176 
0177 uint64_t MP7FileReader::validStrToUint64(const std::string& token) {
0178   boost::smatch what;
0179   if (!boost::regex_match(token, what, reValid_)) {
0180     throw std::logic_error("Token '" + token + "' doesn't match the valid format");
0181   }
0182 
0183   uint64_t value = (uint64_t)(what[1] == "1") << 32;
0184   value += std::stoul(what[2].str(), nullptr, 16);
0185   return value;
0186 }
0187 
0188 //____________________________________________________________________________//
0189 std::vector<std::vector<uint64_t> > MP7FileReader::readRows() {
0190   std::string line;
0191   boost::smatch what;
0192   std::vector<std::vector<uint64_t> > data;
0193   int place = file_.tellg();
0194   while (getline(file_, line)) {
0195     if (boost::regex_match(line, what, reBoard_)) {
0196       // Upos, next board found. Go back by one line
0197       file_.seekg(place);
0198       return data;
0199     }
0200 
0201     if (boost::regex_match(line, what, reFrame_)) {
0202       // check frame number
0203       uint32_t n = std::stoul(what[1].str());
0204 
0205       if (n != data.size()) {
0206         std::stringstream ss;
0207         ss << "Frame misalignment! (expected " << data.size() << " found " << n;
0208         throw std::logic_error(ss.str());
0209       }
0210       std::vector<std::string> tokens;
0211       std::string tmp = what[2].str();
0212       boost::trim(tmp);
0213       boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
0214 
0215       std::vector<uint64_t> row;
0216       std::transform(tokens.begin(), tokens.end(), std::back_inserter(row), validStrToUint64);
0217 
0218       data.push_back(row);
0219     }
0220 
0221     place = file_.tellg();
0222   }
0223 
0224   return data;
0225 }