CMS 3D CMS Logo

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  return links;
171  } else {
172  throw std::logic_error("Unexpected line found!");
173  }
174  }
175  throw std::logic_error("No list of links found");
176 }
177 
179  boost::smatch what;
180  if (!boost::regex_match(token, what, reValid_)) {
181  throw std::logic_error("Token '" + token + "' doesn't match the valid format");
182  }
183 
184  uint64_t value = (uint64_t)(what[1] == "1") << 32;
185  value += std::stoul(what[2].str(), nullptr, 16);
186  return value;
187 }
188 
189 //____________________________________________________________________________//
190 std::vector<std::vector<uint64_t> > MP7FileReader::readRows() {
192  boost::smatch what;
193  std::vector<std::vector<uint64_t> > data;
194  int place = file_.tellg();
195  while (getline(file_, line)) {
196  if (boost::regex_match(line, what, reBoard_)) {
197  // Upos, next board found. Go back by one line
198  file_.seekg(place);
199  return data;
200  }
201 
202  if (boost::regex_match(line, what, reFrame_)) {
203  // check frame number
204  uint32_t n = std::stoul(what[1].str());
205 
206  if (n != data.size()) {
207  std::stringstream ss;
208  ss << "Frame misalignment! (expected " << data.size() << " found " << n;
209  throw std::logic_error(ss.str());
210  }
211  std::vector<std::string> tokens;
212  std::string tmp = what[2].str();
213  boost::trim(tmp);
214  boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
215 
216  std::vector<uint64_t> row;
217  std::transform(tokens.begin(), tokens.end(), std::back_inserter(row), validStrToUint64);
218 
219  data.push_back(row);
220  }
221 
222  place = file_.tellg();
223  }
224 
225  return data;
226 }
static boost::regex reBoard_
Definition: MP7FileReader.h:92
const std::vector< uint64_t > & link(uint32_t i) const
Definition: CLHEP.h:16
const std::string & path() const
source file path
Definition: MP7FileReader.h:56
static boost::regex reFrame_
Definition: MP7FileReader.h:95
std::ifstream file_
Definition: MP7FileReader.h:86
virtual ~MP7FileReader()
static void trim(std::string &s)
LinkMap links_
Definition: MP7FileReader.h:38
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)
Definition: value.py:1
static uint64_t validStrToUint64(const std::string &token)
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_
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
static boost::regex reValid_
Definition: MP7FileReader.h:96
const FileData & get(size_t k) const
data getter via index
#define str(s)
tmp
align.sh
Definition: createJobs.py:716
std::vector< std::string > names() const
raw data name collector
std::vector< uint32_t > searchLinks()
#define LogDebug(id)
std::string searchBoard()
unsigned transform(const HcalDetId &id, unsigned transformCode)