CMS 3D CMS Logo

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