1#include "Conditions/SimpleTableStreamers.h"
6#include "DetDescr/DetectorIDInterpreter.h"
7#include "boost/format.hpp"
12static void storeIdFields(
unsigned int id, std::ostream& s) {
15 for (
auto field : did.getFieldList()) {
16 s <<
",id:\"" << field->getFieldName() <<
'"';
20template <
class T,
class V>
21void storeT(
const T& t, std::ostream& s,
bool expandIds) {
24 if (expandIds && t.getRowCount() > 0) {
25 storeIdFields(t.getRowId(0), s);
27 for (
auto name : t.getColumnNames()) {
28 s <<
",\"" << name <<
"\"";
32 for (
unsigned int irow = 0; irow < t.getRowCount(); irow++) {
33 std::pair<unsigned int, std::vector<V> > row = t.getRow(irow);
35 s << boost::format(
"0x%08x") % row.first;
39 for (
int i = 0; i < did.getFieldCount(); i++)
40 s <<
',' << std::setprecision(10) << did.getFieldValue(i);
42 for (
auto col : row.second) s <<
',' << col;
47 std::ostream& s,
bool expandIds) {
48 storeT<IntegerTableCondition, int>(t, s, expandIds);
51 std::ostream& s,
bool expandIds) {
52 storeT<conditions::DoubleTableCondition, double>(t, s, expandIds);
55static int convert(
const std::string& s,
int dummy) {
56 return strtol(s.c_str(), 0, 0);
59static double convert(
const std::string& s,
double dummy) {
60 return atof(s.c_str());
63static std::vector<std::string> splitCSV(
const std::string& s) {
64 std::vector<std::string> rv;
70 }
else if ((chr ==
',' || chr ==
'\t') && !inquote) {
76 }
else if (isspace(chr) && !inquote) {
77 }
else if (chr ==
'#' && !inquote) {
89static std::vector<std::string>::const_iterator find(
90 const std::vector<std::string>& v,
const std::string& a) {
91 for (
auto i = v.begin(); i != v.end(); i++) {
92 if ((*i) == a)
return i;
97template <
class T,
class V>
98void loadT(T& table, std::istream& is) {
102 std::vector<std::string> split;
109 std::getline(is, line);
110 split = splitCSV(line);
111 if (split.size() < 1) {
115 if (find(split,
"DetID") == split.end() &&
116 find(split,
"subdetector") == split.end()) {
117 EXCEPTION_RAISE(
"ConditionsException",
118 "Malformed CSV file with no DetId or subdetector column");
124 EXCEPTION_RAISE(
"ConditionsException",
"CSV file has no valid header");
127 std::vector<std::string>::const_iterator
id = find(split,
"DetID");
128 if (
id != split.end()) {
130 iDetID = int(
id - split.begin());
132 EXCEPTION_RAISE(
"MissingFeatureException",
133 "Cannot actually load CSV file without valid DetID column "
136 ncolin = split.size();
138 std::map<unsigned int, int> table_to_csv;
139 for (
unsigned int ic = 0; ic != table.getColumnCount(); ic++) {
140 auto fc = find(split, table.getColumnName(ic));
141 if (fc == split.end()) {
143 "ConditionsException",
144 "Missing column '" + table.getColumnName(ic) +
"' in CSV table load");
146 table_to_csv[ic] = int(fc - split.begin());
152 std::getline(is, line);
153 split = splitCSV(line);
154 if (split.size() == 0)
continue;
155 if (split.size() != ncolin) {
156 EXCEPTION_RAISE(
"ConditionsException",
"Mismatched number of columns (" +
157 std::to_string(split.size()) +
158 "!=" + std::to_string(ncolin) +
160 std::to_string(iline));
162 unsigned int tempId(0);
163 std::vector<V> values(table_to_csv.size(), 0);
164 if (iDetID >= 0) tempId = strtoul(split[iDetID].c_str(), 0, 0);
166 for (
auto icopy : table_to_csv) {
167 values[icopy.first] = convert(split[icopy.second], dummy);
170 table.add(tempId, values);
177 loadT<IntegerTableCondition, int>(table, is);
182 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 ...