CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MP7FileReader.cc
Go to the documentation of this file.
2 
3 //#include <boost/filesystem.hpp>
4 #include <boost/algorithm/string.hpp>
5 #include <boost/regex.hpp>
6 #include <boost/foreach.hpp>
7 #include <boost/lexical_cast.hpp>
8 
9 #include <iostream>
10 #include <string>
11 
12 // Namespace resolution
13 using std::cout;
14 using std::endl;
15 
16 
17 // Constants initialization
18 boost::regex MP7FileReader::reBoard_("^Board (.+)");
19 boost::regex MP7FileReader::reLink_("^Link : (.*)");
20 boost::regex MP7FileReader::reQuadChan_("^Quad/Chan : (.*)");
21 boost::regex MP7FileReader::reFrame_("^Frame (\\d{4}) : (.*)");
22 boost::regex MP7FileReader::reValid_("([01])v([0-9a-fA-F]{8})");
23 
24 //____________________________________________________________________________//
25 const std::vector<uint64_t>&
26 FileData::link(uint32_t i) const {
27  LinkMap::const_iterator it = links_.find(i);
28  if ( it == links_.end() )
29  throw std::runtime_error("Link id not found");
30 
31  return it->second;
32 }
33 
34 //____________________________________________________________________________//
36  if (!file_.is_open()) {
37  //cout << "File " << path << " not found" << endl;
38  valid_ = false;
39  return;
40  }
41 
42  load();
43 }
44 
45 //____________________________________________________________________________//
47 }
48 
49 
50 //____________________________________________________________________________//
51 const FileData&
52 MP7FileReader::get(size_t k) const {
53  return buffers_.at(k);
54 }
55 
56 std::vector<std::string>
58  std::vector<std::string> names(buffers_.size());
59 
60  BOOST_FOREACH( const FileData& r, buffers_ ) {
61  names.push_back(r.name());
62  }
63  return names;
64 }
65 //____________________________________________________________________________//
66 void
68  using namespace boost;
69 
70 
71  // Data, to be stored in a BufferSnapshot object
72  while (file_.good()) {
73  std::string id = searchBoard();
74  //cout << "Id: " << id << endl;
75  std::vector<uint32_t> links = searchLinks();
76 
77  //cout << "Links (" << links.size() << ") : ";
78 
79  //BOOST_FOREACH(uint32_t l, links) {
80  //cout << l << ",";
81  //}
82  //cout << endl;
83 
84  std::vector< std::vector<uint64_t> > data = readRows();
85  //cout << "Data loaded (" << data.size() << ")" << endl;
86 
87  // Id, Link # and Data Loaded
88 
89  FileData s;
90  s.name_ = id;
91 
92  std::vector< std::vector<uint64_t> > chans( links.size(), std::vector<uint64_t>(data.size()) );
93 
94  // Transpose
95  for ( size_t i(0); i<links.size(); ++i) {
96  for ( size_t j(0); j<data.size(); ++j) {
97  chans[i][j] = data[j][i];
98  }
99  }
100 
101  // pack
102  for ( size_t i(0); i<links.size(); ++i) {
103  s.links_.insert( std::make_pair(links[i],chans[i]) );
104  }
105 
106  buffers_.push_back(s);
107  }
108 
109  // File successfully read
110  valid_ = true;
111 
112 }
113 
114 //____________________________________________________________________________//
118  std::string id;
119  boost::smatch what;
120 
121  while (getline(file_, line)) {
122 
123  // Trim and skip empties and comments
124  boost::trim(line);
125  if (line.empty()) continue;
126  if (line[0] == '#') 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  //cout << "Here '" << line << "'" << endl;
134  throw std::logic_error("Unexpected line found!");
135  }
136  }
137  throw std::logic_error("No board found");
138 }
139 
140 //____________________________________________________________________________//
141 std::vector<uint32_t>
144  boost::smatch what;
145 
146  while (getline(file_, line)) {
147 
148  boost::trim(line);
149  if (line.empty()) continue;
150  if (line[0] == '#') continue;
151 
152  if (boost::regex_match(line, what, reQuadChan_)) {
153  // Not used
154  continue;
155  }
156 
157  if (boost::regex_match(line, what, reLink_)) {
158 
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), boost::lexical_cast<uint32_t, const std::string&>);
168  return links;
169  } else {
170  throw std::logic_error("Unexpected line found!");
171  }
172  }
173  throw std::logic_error("No list of links found");
174 }
175 
177 
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(), 0x0, 16);
185  return value;
186 }
187 
188 //____________________________________________________________________________//
189 std::vector< std::vector<uint64_t> >
192  boost::smatch what;
193  std::vector< std::vector<uint64_t> > data;
194  int place = file_.tellg();
195  while (getline(file_, line)) {
196 
197  if (boost::regex_match(line, what, reBoard_)) {
198  // Upos, next board found. Go back by one line
199  file_.seekg(place);
200  return data;
201  }
202 
203  if (boost::regex_match(line, what, reFrame_)) {
204  // check frame number
205  uint32_t n = boost::lexical_cast<uint32_t>(what[1].str());
206 
207  if (n != data.size()) {
208  std::stringstream ss;
209  ss << "Frame misalignment! (expected " << data.size() << " found " << n;
210  throw std::logic_error(ss.str());
211  }
212  std::vector<std::string> tokens;
213  std::string tmp = what[2].str();
214  boost::trim(tmp);
215  boost::split(tokens, tmp, boost::is_any_of(" \t"), boost::token_compress_on);
216 
217  std::vector<uint64_t> row;
218  std::transform(tokens.begin(), tokens.end(), std::back_inserter(row), validStrToUint64);
219 
220  data.push_back(row);
221  }
222 
223  place = file_.tellg();
224  }
225 
226  return data;
227 
228 }
int i
Definition: DBlmapReader.cc:9
static boost::regex reBoard_
Definition: MP7FileReader.h:97
static boost::regex reFrame_
std::ifstream file_
Definition: MP7FileReader.h:90
virtual ~MP7FileReader()
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
tuple path
else: Piece not in the list, fine.
MP7FileReader(const std::string &path)
int j
Definition: DBlmapReader.cc:9
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
tuple cout
Definition: gather_cfg.py:121
volatile std::atomic< bool > shutdown_flag false
double split
Definition: MVATrainer.cc:139
std::vector< uint32_t > searchLinks()
std::string searchBoard()