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