Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "EventFilter/L1TRawToDigi/interface/MP7FileReader.h"

#include <boost/algorithm/string.hpp>
#include <boost/regex.hpp>

#include <iostream>
#include <string>

// Namespace resolution
using std::cout;
using std::endl;

// Constants initialization
boost::regex MP7FileReader::reBoard_("^Board (.+)");
boost::regex MP7FileReader::reLink_("^Link : (.*)");
boost::regex MP7FileReader::reQuadChan_("^Quad/Chan : (.*)");
boost::regex MP7FileReader::reFrame_("^Frame (\\d{4}) : (.*)");
boost::regex MP7FileReader::reValid_("([01])v([0-9a-fA-F]{8})");

//____________________________________________________________________________//
const std::vector<uint64_t>& FileData::link(uint32_t i) const {
  LinkMap::const_iterator it = links_.find(i);
  if (it == links_.end()) {
    edm::LogError("L1T") << "Link id " << i << " not found";
  }
  return it->second;
}

//____________________________________________________________________________//
MP7FileReader::MP7FileReader(const std::string& path) : valid_(false), path_(path), file_(path) {
  if (!file_.is_open()) {
    edm::LogError("L1T") << "File " << path << " not found";
    valid_ = false;
    return;
  } else {
    LogDebug("L1T") << "Reading file " << path;
  }

  load();

  LogDebug("L1T") << "# buffers " << buffers_.size();

  if (!buffers_.empty()) {
    LogDebug("L1T") << "# links " << buffers_.at(0).size();
    if (buffers_.at(0).size() > 0) {
      LogDebug("L1T") << "# frames " << buffers_.at(0).link(0).size();
    }
  }
}

//____________________________________________________________________________//
MP7FileReader::~MP7FileReader() {}

//____________________________________________________________________________//
const FileData& MP7FileReader::get(size_t k) const { return buffers_.at(k); }

std::vector<std::string> MP7FileReader::names() const {
  std::vector<std::string> names(buffers_.size());

  for (auto const& r : buffers_) {
    names.push_back(r.name());
  }
  return names;
}
//____________________________________________________________________________//
void MP7FileReader::load() {
  using namespace boost;

  // Data, to be stored in a BufferSnapshot object
  while (file_.good()) {
    std::string id = searchBoard();
    //cout << "Id: " << id << endl;
    std::vector<uint32_t> links = searchLinks();

    //cout << "Links (" << links.size() << ") : ";

    //for(uint32_t l : links) {
    //cout << l << ",";
    //}
    //cout << endl;

    std::vector<std::vector<uint64_t> > data = readRows();
    //cout << "Data loaded (" << data.size() << ")" << endl;

    // Id, Link # and Data Loaded

    FileData s;
    s.name_ = id;

    std::vector<std::vector<uint64_t> > chans(links.size(), std::vector<uint64_t>(data.size()));

    // Transpose
    for (size_t i(0); i < links.size(); ++i) {
      for (size_t j(0); j < data.size(); ++j) {
        chans[i][j] = data[j][i];
      }
    }

    // pack
    for (size_t i(0); i < links.size(); ++i) {
      s.links_.insert(std::make_pair(links[i], chans[i]));
    }

    buffers_.push_back(s);
  }

  // File successfully read
  valid_ = true;
}

//____________________________________________________________________________//
std::string MP7FileReader::searchBoard() {
  std::string line;
  std::string id;
  boost::smatch what;

  while (getline(file_, line)) {
    // Trim and skip empties and comments
    boost::trim(line);
    if (line.empty())
      continue;
    if (line[0] == '#')
      continue;

    if (boost::regex_match(line, what, reBoard_)) {
      // Create a new buffer snapshot
      id = what[1];
      return id;
    } else {
      edm::LogError("L1T") << "Unexpected line found";
      return std::string("");
    }
  }
  edm::LogError("L1T") << "No board found";
  return std::string("");
}

//____________________________________________________________________________//
std::vector<uint32_t> MP7FileReader::searchLinks() {
  std::string line;
  boost::smatch what;

  while (getline(file_, line)) {
    boost::trim(line);
    if (line.empty())
      continue;
    if (line[0] == '#')
      continue;

    if (boost::regex_match(line, what, reQuadChan_)) {
      // Not used
      continue;
    }

    if (boost::regex_match(line, what, reLink_)) {
      std::vector<std::string> tokens;
      std::string tmp = what[1].str();
      // Trim the line
      boost::trim(tmp);
      // Split line into tokens
      boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
      // Convert it into uint32 s
      std::vector<uint32_t> links;
      std::transform(tokens.begin(), tokens.end(), std::back_inserter(links), [](const std::string& str) {
        return std::stoul(str);
      });
      return links;
    } else {
      throw std::logic_error("Unexpected line found!");
    }
  }
  throw std::logic_error("No list of links found");
}

uint64_t MP7FileReader::validStrToUint64(const std::string& token) {
  boost::smatch what;
  if (!boost::regex_match(token, what, reValid_)) {
    throw std::logic_error("Token '" + token + "' doesn't match the valid format");
  }

  uint64_t value = (uint64_t)(what[1] == "1") << 32;
  value += std::stoul(what[2].str(), nullptr, 16);
  return value;
}

//____________________________________________________________________________//
std::vector<std::vector<uint64_t> > MP7FileReader::readRows() {
  std::string line;
  boost::smatch what;
  std::vector<std::vector<uint64_t> > data;
  int place = file_.tellg();
  while (getline(file_, line)) {
    if (boost::regex_match(line, what, reBoard_)) {
      // Upos, next board found. Go back by one line
      file_.seekg(place);
      return data;
    }

    if (boost::regex_match(line, what, reFrame_)) {
      // check frame number
      uint32_t n = std::stoul(what[1].str());

      if (n != data.size()) {
        std::stringstream ss;
        ss << "Frame misalignment! (expected " << data.size() << " found " << n;
        throw std::logic_error(ss.str());
      }
      std::vector<std::string> tokens;
      std::string tmp = what[2].str();
      boost::trim(tmp);
      boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);

      std::vector<uint64_t> row;
      std::transform(tokens.begin(), tokens.end(), std::back_inserter(row), validStrToUint64);

      data.push_back(row);
    }

    place = file_.tellg();
  }

  return data;
}