forked from kvtsang/Supera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperaCSVReader.h
66 lines (56 loc) · 1.92 KB
/
SuperaCSVReader.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef __SUPERACSVREADER_H__
#define __SUPERACSVREADER_H__
#include <fstream>
#include <map>
#include <array>
#include <iostream>
#include "SuperaTypes.h"
namespace supera {
namespace csvreader {
inline bool line_to_point(const std::string& line_data,
supera::RSEID& id,
std::array<double,3>& point) {
size_t run, subrun, event,x,y,z;
run = subrun = event = x = y = z = 0;
try {
run = line_data.find(",");
subrun = line_data.find(",",run+1);
event = line_data.find(",",subrun+1);
x = line_data.find(",",event+1);
y = line_data.find(",",x+1);
z = line_data.find(" ",y+1);
id.run = std::atoi(line_data.substr(0,run).c_str());
id.subrun = std::atoi(line_data.substr(run+1,subrun-(run+1)).c_str());
id.event = std::atoi(line_data.substr(subrun+1,event-(subrun+1)).c_str());
point[0] = std::atof(line_data.substr(event+1,x-(event+1)).c_str());
point[1] = std::atof(line_data.substr(x+1,y-(x+1)).c_str());
point[2] = std::atof(line_data.substr(y+1,z-(y+1)).c_str());
}catch(const std::exception& err) {
std::cerr << "Failed to convert: \"" << line_data << "\"" << std::endl;
return false;
}
return true;
}
inline void read_constraint_file(std::string fname,
std::map<supera::RSEID,std::array<double,3> >& data)
{
std::ifstream fstrm(fname.c_str());
std::string line_data;
supera::RSEID id;
std::array<double,3> position;
std::getline(fstrm, line_data); // ignore 1st line
while(std::getline(fstrm, line_data)) {
if(line_data.empty()) continue;
if(supera::csvreader::line_to_point(line_data,id,position)) {
auto iter = data.find(id);
if(iter != data.end()) {
std::cerr << "Run=" << id.run << " Subrun=" << id.subrun << " Event=" << id.event << " is duplicated!" << std::endl;
throw std::exception();
}
data[id]=position;
}
}
}
}
}
#endif