File indexing completed on 2023-10-25 09:45:40
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
0013 using std::cout;
0014 using std::endl;
0015
0016
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
0073 while (file_.good()) {
0074 std::string id = searchBoard();
0075
0076 std::vector<uint32_t> links = searchLinks();
0077
0078
0079
0080
0081
0082
0083
0084
0085 std::vector<std::vector<uint64_t> > data = readRows();
0086
0087
0088
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
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
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
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
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
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
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
0162 boost::trim(tmp);
0163
0164 boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
0165
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 return links;
0171 } else {
0172 throw std::logic_error("Unexpected line found!");
0173 }
0174 }
0175 throw std::logic_error("No list of links found");
0176 }
0177
0178 uint64_t MP7FileReader::validStrToUint64(const std::string& token) {
0179 boost::smatch what;
0180 if (!boost::regex_match(token, what, reValid_)) {
0181 throw std::logic_error("Token '" + token + "' doesn't match the valid format");
0182 }
0183
0184 uint64_t value = (uint64_t)(what[1] == "1") << 32;
0185 value += std::stoul(what[2].str(), nullptr, 16);
0186 return value;
0187 }
0188
0189
0190 std::vector<std::vector<uint64_t> > MP7FileReader::readRows() {
0191 std::string line;
0192 boost::smatch what;
0193 std::vector<std::vector<uint64_t> > data;
0194 int place = file_.tellg();
0195 while (getline(file_, line)) {
0196 if (boost::regex_match(line, what, reBoard_)) {
0197
0198 file_.seekg(place);
0199 return data;
0200 }
0201
0202 if (boost::regex_match(line, what, reFrame_)) {
0203
0204 uint32_t n = std::stoul(what[1].str());
0205
0206 if (n != data.size()) {
0207 std::stringstream ss;
0208 ss << "Frame misalignment! (expected " << data.size() << " found " << n;
0209 throw std::logic_error(ss.str());
0210 }
0211 std::vector<std::string> tokens;
0212 std::string tmp = what[2].str();
0213 boost::trim(tmp);
0214 boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
0215
0216 std::vector<uint64_t> row;
0217 std::transform(tokens.begin(), tokens.end(), std::back_inserter(row), validStrToUint64);
0218
0219 data.push_back(row);
0220 }
0221
0222 place = file_.tellg();
0223 }
0224
0225 return data;
0226 }