1#include "Conditions/SimpleTableStreamers.h"
5#include "DetDescr/DetectorIDInterpreter.h"
6#include "boost/format.hpp"
11static void storeIdFields(
unsigned int id, std::ostream& s) {
14 for (
auto field : did.getFieldList()) {
15 s <<
",id:\"" << field->getFieldName() <<
'"';
19template <
class T,
class V>
20void storeT(
const T& t, std::ostream& s,
bool expandIds) {
23 if (expandIds && t.getRowCount() > 0) {
24 storeIdFields(t.getRowId(0), s);
26 for (
auto name : t.getColumnNames()) {
27 s <<
",\"" << name <<
"\"";
31 for (
unsigned int irow = 0; irow < t.getRowCount(); irow++) {
32 std::pair<unsigned int, std::vector<V> > row = t.getRow(irow);
34 s << boost::format(
"0x%08x") % row.first;
38 for (
int i = 0; i < did.getFieldCount(); i++)
39 s <<
',' << std::setprecision(10) << did.getFieldValue(i);
41 for (
auto col : row.second) s <<
',' << col;
46 std::ostream& s,
bool expandIds) {
47 storeT<IntegerTableCondition, int>(t, s, expandIds);
50 std::ostream& s,
bool expandIds) {
51 storeT<conditions::DoubleTableCondition, double>(t, s, expandIds);
54static int convert(
const std::string& s,
int dummy) {
55 return strtol(s.c_str(), 0, 0);
58static double convert(
const std::string& s,
double dummy) {
59 return atof(s.c_str());
62static std::vector<std::string> splitCSV(
const std::string& s) {
63 std::vector<std::string> rv;
69 }
else if ((chr ==
',' || chr ==
'\t') && !inquote) {
75 }
else if (isspace(chr) && !inquote) {
76 }
else if (chr ==
'#' && !inquote) {
88static std::vector<std::string>::const_iterator find(
89 const std::vector<std::string>& v,
const std::string& a) {
90 for (
auto i = v.begin(); i != v.end(); i++) {
91 if ((*i) == a)
return i;
96template <
class T,
class V>
97void loadT(T& table, std::istream& is) {
101 std::vector<std::string> split;
108 std::getline(is, line);
109 split = splitCSV(line);
110 if (split.size() < 1) {
114 if (find(split,
"DetID") == split.end() &&
115 find(split,
"subdetector") == split.end()) {
116 EXCEPTION_RAISE(
"ConditionsException",
117 "Malformed CSV file with no DetId or subdetector column");
123 EXCEPTION_RAISE(
"ConditionsException",
"CSV file has no valid header");
126 std::vector<std::string>::const_iterator
id = find(split,
"DetID");
127 if (
id != split.end()) {
129 i_det_id = int(
id - split.begin());
131 EXCEPTION_RAISE(
"MissingFeatureException",
132 "Cannot actually load CSV file without valid DetID column "
135 ncolin = split.size();
137 std::map<unsigned int, int> table_to_csv;
138 for (
unsigned int ic = 0; ic != table.getColumnCount(); ic++) {
139 auto fc = find(split, table.getColumnName(ic));
140 if (fc == split.end()) {
142 "ConditionsException",
143 "Missing column '" + table.getColumnName(ic) +
"' in CSV table load");
145 table_to_csv[ic] = int(fc - split.begin());
151 std::getline(is, line);
152 split = splitCSV(line);
153 if (split.size() == 0)
continue;
154 if (split.size() != ncolin) {
155 EXCEPTION_RAISE(
"ConditionsException",
"Mismatched number of columns (" +
156 std::to_string(split.size()) +
157 "!=" + std::to_string(ncolin) +
159 std::to_string(iline));
161 unsigned int temp_id(0);
162 std::vector<V> values(table_to_csv.size(), 0);
163 if (i_det_id >= 0) temp_id = strtoul(split[i_det_id].c_str(), 0, 0);
165 for (
auto icopy : table_to_csv) {
166 values[icopy.first] = convert(split[icopy.second], dummy);
169 table.add(temp_id, values);
176 loadT<IntegerTableCondition, int>(table, is);
181 loadT<conditions::DoubleTableCondition, double>(table, is);
static void load(IntegerTableCondition &, std::istream &)
Load the table from a stream Columns must be defined by the user.
static void store(const IntegerTableCondition &, std::ostream &, bool expandIds=true)
Convert the table into a stream.
Class provides an "introspection" capability for the 32-bit packed IDs used for uniquely identifying ...