CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MP7FileReader.cc
Go to the documentation of this file.
1 
3 
5 
6 #include <boost/algorithm/string.hpp>
7 #include <boost/regex.hpp>
8 
9 #include <iostream>
10 #include <string>
11 
12 // Namespace resolution
13 using std::cout;
14 using std::endl;
15 
16 // Constants initialization
17 boost::regex MP7FileReader::reBoard_("^Board (.+)");
18 boost::regex MP7FileReader::reLink_("^Link : (.*)");
19 boost::regex MP7FileReader::reQuadChan_("^Quad/Chan : (.*)");
20 boost::regex MP7FileReader::reFrame_("^Frame (\\d{4}) : (.*)");
21 boost::regex MP7FileReader::reValid_("([01])v([0-9a-fA-F]{8})");
22 
23 //____________________________________________________________________________//
24 const std::vector<uint64_t>& FileData::link(uint32_t i) const {
25  LinkMap::const_iterator it = links_.find(i);
26  if (it == links_.end()) {
27  edm::LogError("L1T") << "Link id " << i << " not found";
28  }
29  return it->second;
30 }
31 
32 //____________________________________________________________________________//
34  if (!file_.is_open()) {
35  edm::LogError("L1T") << "File " << path << " not found";
36  valid_ = false;
37  return;
38  } else {
39  LogDebug("L1T") << "Reading file " << path;
40  }
41 
42  load();
43 
44  LogDebug("L1T") << "# buffers " << buffers_.size();
45 
46  if (!buffers_.empty()) {
47  LogDebug("L1T") << "# links " << buffers_.at(0).size();
48  if (buffers_.at(0).size() > 0) {
49  LogDebug("L1T") << "# frames " << buffers_.at(0).link(0).size();
50  }
51  }
52 }
53 
54 //____________________________________________________________________________//
56 
57 //____________________________________________________________________________//
58 const FileData& MP7FileReader::get(size_t k) const { return buffers_.at(k); }
59 
60 std::vector<std::string> MP7FileReader::names() const {
61  std::vector<std::string> names(buffers_.size());
62 
63  for (auto const& r : buffers_) {
64  names.push_back(r.name());
65  }
66  return names;
67 }
68 //____________________________________________________________________________//
70  using namespace boost;
71 
72  // Data, to be stored in a BufferSnapshot object
73  while (file_.good()) {
74  std::string id = searchBoard();
75  //cout << "Id: " << id << endl;
76  std::vector<uint32_t> links = searchLinks();
77 
78  //cout << "Links (" << links.size() << ") : ";
79 
80  //for(uint32_t l : links) {
81  //cout << l << ",";
82  //}
83  //cout << endl;
84 
85  std::vector<std::vector<uint64_t> > data = readRows();
86  //cout << "Data loaded (" << data.size() << ")" << endl;
87 
88  // Id, Link # and Data Loaded
89 
90  FileData s;
91  s.name_ = id;
92 
93  std::vector<std::vector<uint64_t> > chans(links.size(), std::vector<uint64_t>(data.size()));
94 
95  // Transpose
96  for (size_t i(0); i < links.size(); ++i) {
97  for (size_t j(0); j < data.size(); ++j) {
98  chans[i][j] = data[j][i];
99  }
100  }
101 
102  // pack
103  for (size_t i(0); i < links.size(); ++i) {
104  s.links_.insert(std::make_pair(links[i], chans[i]));
105  }
106 
107  buffers_.push_back(s);
108  }
109 
110  // File successfully read
111  valid_ = true;
112 }
113 
114 //____________________________________________________________________________//
117  std::string id;
118  boost::smatch what;
119 
120  while (getline(file_, line)) {
121  // Trim and skip empties and comments
122  boost::trim(line);
123  if (line.empty())
124  continue;
125  if (line[0] == '#')
126  continue;
127 
128  if (boost::regex_match(line, what, reBoard_)) {
129  // Create a new buffer snapshot
130  id = what[1];
131  return id;
132  } else {
133  edm::LogError("L1T") << "Unexpected line found";
134  return std::string("");
135  }
136  }
137  edm::LogError("L1T") << "No board found";
138  return std::string("");
139 }
140 
141 //____________________________________________________________________________//
142 std::vector<uint32_t> MP7FileReader::searchLinks() {
144  boost::smatch what;
145 
146  while (getline(file_, line)) {
147  boost::trim(line);
148  if (line.empty())
149  continue;
150  if (line[0] == '#')
151  continue;
152 
153  if (boost::regex_match(line, what, reQuadChan_)) {
154  // Not used
155  continue;
156  }
157 
158  if (boost::regex_match(line, what, reLink_)) {
159  std::vector<std::string> tokens;
160  std::string tmp = what[1].str();
161  // Trim the line
162  boost::trim(tmp);
163  // Split line into tokens
164  boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
165  // Convert it into uint32 s
166  std::vector<uint32_t> links;
167  std::transform(tokens.begin(), tokens.end(), std::back_inserter(links), [](const std::string& str) {
168  return std::stoul(str);
169  });
170  } else {
171  throw std::logic_error("Unexpected line found!");
172  }
173  }
174  throw std::logic_error("No list of links found");
175 }
176 
178  boost::smatch what;
179  if (!boost::regex_match(token, what, reValid_)) {
180  throw std::logic_error("Token '" + token + "' doesn't match the valid format");
181  }
182 
183  uint64_t value = (uint64_t)(what[1] == "1") << 32;
184  value += std::stoul(what[2].str(), nullptr, 16);
185  return value;
186 }
187 
188 //____________________________________________________________________________//
189 std::vector<std::vector<uint64_t> > MP7FileReader::readRows() {
191  boost::smatch what;
192  std::vector<std::vector<uint64_t> > data;
193  int place = file_.tellg();
194  while (getline(file_, line)) {
195  if (boost::regex_match(line, what, reBoard_)) {
196  // Upos, next board found. Go back by one line
197  file_.seekg(place);
198  return data;
199  }
200 
201  if (boost::regex_match(line, what, reFrame_)) {
202  // check frame number
203  uint32_t n = std::stoul(what[1].str());
204 
205  if (n != data.size()) {
206  std::stringstream ss;
207  ss << "Frame misalignment! (expected " << data.size() << " found " << n;
208  throw std::logic_error(ss.str());
209  }
210  std::vector<std::string> tokens;
211  std::string tmp = what[2].str();
212  boost::trim(tmp);
213  boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
214 
215  std::vector<uint64_t> row;
216  std::transform(tokens.begin(), tokens.end(), std::back_inserter(row), validStrToUint64);
217 
218  data.push_back(row);
219  }
220 
221  place = file_.tellg();
222  }
223 
224  return data;
225 }
static boost::regex reBoard_
Definition: MP7FileReader.h:92
uint16_t *__restrict__ id
static boost::regex reFrame_
Definition: MP7FileReader.h:95
std::ifstream file_
Definition: MP7FileReader.h:86
virtual ~MP7FileReader()
static void trim(std::string &s)
const std::string & path() const
source file path
Definition: MP7FileReader.h:56
LinkMap links_
Definition: MP7FileReader.h:38
const FileData & get(size_t k) const
data getter via index
std::vector< std::vector< uint64_t > > readRows()
Log< level::Error, false > LogError
static const uint16_t valid_
Definition: Constants.h:17
static boost::regex reLink_
Definition: MP7FileReader.h:93
MP7FileReader(const std::string &path)
static uint64_t validStrToUint64(const std::string &token)
std::string name_
Definition: MP7FileReader.h:37
static boost::regex reQuadChan_
Definition: MP7FileReader.h:94
unsigned long long uint64_t
Definition: Time.h:13
std::vector< FileData > buffers_
Definition: MP7FileReader.h:89
std::string & path_
std::vector< std::string > names() const
raw data name collector
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
static boost::regex reValid_
Definition: MP7FileReader.h:96
const std::vector< uint64_t > & link(uint32_t i) const
tuple cout
Definition: gather_cfg.py:144
#define str(s)
tmp
align.sh
Definition: createJobs.py:716
std::vector< uint32_t > searchLinks()
#define LogDebug(id)
std::string searchBoard()
unsigned transform(const HcalDetId &id, unsigned transformCode)