LDMX Software
SimpleTableCondition.h
1
6#ifndef FRAMEWORK_SIMPLETABLECONDITION_H_
7#define FRAMEWORK_SIMPLETABLECONDITION_H_
8
9#include <cstdint>
10#include <ostream>
11#include <vector>
12
14#include "Framework/Exception/Exception.h"
15
16namespace conditions {
17
23 public:
25 BaseTableCondition(const std::string& name,
26 const std::vector<std::string>& columns)
28 columns_{columns},
29 idMask_{0xFFFFFFFFu} {
30 columnCount_ = (unsigned int)(columns.size());
31 }
32
36 unsigned int getColumnNumber(const std::string& colname) const {
37 for (unsigned int i = 0; i < columnCount_; i++)
38 if (colname == columns_[i]) return i;
39 return columnCount_;
40 }
41
45 unsigned int getColumnCount() const { return columnCount_; }
46
50 const std::string& getColumnName(unsigned int icol) const {
51 if (icol >= columnCount_) {
52 EXCEPTION_RAISE("ConditionsException",
53 std::string("Column index out of range in ") + getName());
54 }
55 return columns_[icol];
56 }
57
61 unsigned int getRowId(unsigned int irow) const {
62 if (irow >= getRowCount()) { // raise exception
63 EXCEPTION_RAISE("ConditionsException",
64 "Row out of range: " + std::to_string(irow));
65 }
66 return keys_[irow];
67 }
68
72 const std::vector<std::string>& getColumnNames() const { return columns_; }
73
77 std::size_t getRowCount() const { return keys_.size(); }
78
83 void setIdMask(unsigned int mask) { idMask_ = mask; }
84
89 unsigned int getIdMask() const { return idMask_; }
90
94 virtual std::ostream& streamRow(std::ostream& s, int irow) const {
95 return s << keys_[irow];
96 }
97
98 protected:
99 std::size_t findKey(unsigned int id) const;
100
101 std::size_t findKeyInsert(unsigned int id) const;
102
103 std::vector<std::string> columns_;
104 unsigned int columnCount_;
105 std::vector<uint32_t> keys_;
106 unsigned int idMask_;
107};
108
109template <class T>
111 public:
112 HomogenousTableCondition(const std::string& name,
113 const std::vector<std::string>& columns)
114 : BaseTableCondition(name, columns) {}
115
116 virtual ~HomogenousTableCondition() {}
117
121 void clear() {
122 keys_.clear();
123 values_.clear();
124 }
125
127 void add(unsigned int id, const std::vector<T>& values) {
128 if (values.size() != columnCount_) {
129 EXCEPTION_RAISE("ConditionsException",
130 getName() + ": Attempted to insert a row with " +
131 std::to_string(values.size()) +
132 " columns into a table with " +
133 std::to_string(columnCount_) + " columns");
134 }
135 std::size_t loc = findKey(id);
136 if (loc != getRowCount()) {
137 EXCEPTION_RAISE("ConditionsException",
138 "Attempted to add condition in " + getName() +
139 " for existing id " + std::to_string(id));
140 }
141 loc = findKeyInsert(id); // where to put it
142
143 // insert into the keys
144 keys_.insert(keys_.begin() + loc, id);
145 // insert into the values
146 values_.insert(values_.begin() + loc * columnCount_, values.begin(),
147 values.end());
148 }
149
154 T get(unsigned int id, unsigned int col) const {
155 std::size_t irow = findKey(id);
156 if (col >= columnCount_ || irow == getRowCount()) { // raise exception
157 EXCEPTION_RAISE("ConditionsException",
158 "No such column " + std::to_string(col) + " or id " +
159 std::to_string(id));
160 }
161 return values_[irow * columnCount_ + col];
162 }
163
168 std::pair<unsigned int, std::vector<T> > getRow(unsigned int irow) const {
169 if (irow >= getRowCount()) { // raise exception
170 EXCEPTION_RAISE("ConditionsException",
171 "Row out of range: " + std::to_string(irow));
172 }
173 std::vector<T> rv(&(values_[irow * columnCount_]),
174 &(values_[(irow + 1) * columnCount_]));
175 return std::pair<unsigned int, std::vector<T> >(keys_[irow], rv);
176 }
177
184 T getByName(unsigned int id, const std::string& colname) const {
185 return get(id, getColumnNumber(colname));
186 }
187
191 virtual std::ostream& streamRow(std::ostream& s, int irow) const {
192 if (irow >= getRowCount()) { // raise exception
193 EXCEPTION_RAISE("ConditionsException",
194 "Row out of range: " + std::to_string(irow));
195 }
196 s << keys_[irow];
197 for (int i = 0; i < columnCount_; i++)
198 s << ',' << values_[irow * columnCount_ + i];
199 return s << std::endl;
200 }
201
202 private:
203 std::vector<T> values_; // unrolled array
204};
205
211 public:
212 DoubleTableCondition(const std::string& name,
213 const std::vector<std::string>& columns)
214 : HomogenousTableCondition<double>(name, columns) {}
215
216 virtual ~DoubleTableCondition() {}
217};
218
224 public:
225 IntegerTableCondition(const std::string& name,
226 const std::vector<std::string>& columns)
227 : HomogenousTableCondition<int>(name, columns) {}
228
229 virtual ~IntegerTableCondition() {}
230};
231
232} // namespace conditions
233
234std::ostream& operator<<(std::ostream&, const conditions::BaseTableCondition&);
235
236#endif
Base class for conditions information like pedestals, gains, electronics maps, etc.
unsigned int getColumnNumber(const std::string &colname) const
Get a the column number for the given column name.
void setIdMask(unsigned int mask)
Set an AND mask to be applied to the id.
virtual std::ostream & streamRow(std::ostream &s, int irow) const
Streams a given row of this table.
unsigned int getIdMask() const
Get the AND mask to be applied to the id.
unsigned int getColumnCount() const
Get the number of columns.
const std::string & getColumnName(unsigned int icol) const
Get the name of the given column.
std::size_t getRowCount() const
Get the number of rows.
const std::vector< std::string > & getColumnNames() const
Get the names of the columns.
unsigned int getRowId(unsigned int irow) const
Get the id for the given row.
BaseTableCondition(const std::string &name, const std::vector< std::string > &columns)
Create table with given set of columns.
T get(unsigned int id, unsigned int col) const
Get an entry by DetectorId and number.
std::pair< unsigned int, std::vector< T > > getRow(unsigned int irow) const
Get a row by number Used primarily for persisting the SimpleTableCondition.
virtual std::ostream & streamRow(std::ostream &s, int irow) const
Streams a given row of this table.
T getByName(unsigned int id, const std::string &colname) const
Get a column by DetectorId and name Throws an exception when.
void add(unsigned int id, const std::vector< T > &values)
Add an entry to the table.
Base class for all conditions objects, very simple.
ConditionsObject(const std::string &name)
Class constructor.
std::string getName() const
Get the name of this object.
All classes in the ldmx-sw project use this namespace.
Definition PerfDict.cxx:45