LDMX Software
SimpleTableCondition.h
1
6#ifndef FRAMEWORK_SIMPLETABLECONDITION_H_
7#define FRAMEWORK_SIMPLETABLECONDITION_H_
8
9#include <ostream>
10#include <vector>
11
13#include "Framework/Exception/Exception.h"
14
15namespace conditions {
16
22 public:
24 BaseTableCondition(const std::string& name,
25 const std::vector<std::string>& columns)
27 columns_{columns},
28 idMask_{0xFFFFFFFFu} {
29 columnCount_ = (unsigned int)(columns.size());
30 }
31
35 unsigned int getColumnNumber(const std::string& colname) const {
36 for (unsigned int i = 0; i < columnCount_; i++)
37 if (colname == columns_[i]) return i;
38 return columnCount_;
39 }
40
44 unsigned int getColumnCount() const { return columnCount_; }
45
49 const std::string& getColumnName(unsigned int icol) const {
50 if (icol >= columnCount_) {
51 EXCEPTION_RAISE("ConditionsException",
52 std::string("Column index out of range in ") + getName());
53 }
54 return columns_[icol];
55 }
56
60 unsigned int getRowId(unsigned int irow) const {
61 if (irow >= getRowCount()) { // raise exception
62 EXCEPTION_RAISE("ConditionsException",
63 "Row out of range: " + std::to_string(irow));
64 }
65 return keys_[irow];
66 }
67
71 const std::vector<std::string>& getColumnNames() const { return columns_; }
72
76 std::size_t getRowCount() const { return keys_.size(); }
77
82 void setIdMask(unsigned int mask) { idMask_ = mask; }
83
88 unsigned int getIdMask() const { return idMask_; }
89
93 virtual std::ostream& streamRow(std::ostream& s, int irow) const {
94 return s << keys_[irow];
95 }
96
97 protected:
98 std::size_t findKey(unsigned int id) const;
99
100 std::size_t findKeyInsert(unsigned int id) const;
101
102 std::vector<std::string> columns_;
103 unsigned int columnCount_;
104 std::vector<uint32_t> keys_;
105 unsigned int idMask_;
106};
107
108template <class T>
110 public:
111 HomogenousTableCondition(const std::string& name,
112 const std::vector<std::string>& columns)
113 : BaseTableCondition(name, columns) {}
114
115 virtual ~HomogenousTableCondition() {}
116
120 void clear() {
121 keys_.clear();
122 values_.clear();
123 }
124
126 void add(unsigned int id, const std::vector<T>& values) {
127 if (values.size() != columnCount_) {
128 EXCEPTION_RAISE("ConditionsException",
129 getName() + ": Attempted to insert a row with " +
130 std::to_string(values.size()) +
131 " columns into a table with " +
132 std::to_string(columnCount_) + " columns");
133 }
134 std::size_t loc = findKey(id);
135 if (loc != getRowCount()) {
136 EXCEPTION_RAISE("ConditionsException",
137 "Attempted to add condition in " + getName() +
138 " for existing id " + std::to_string(id));
139 }
140 loc = findKeyInsert(id); // where to put it
141
142 // insert into the keys
143 keys_.insert(keys_.begin() + loc, id);
144 // insert into the values
145 values_.insert(values_.begin() + loc * columnCount_, values.begin(),
146 values.end());
147 }
148
153 T get(unsigned int id, unsigned int col) const {
154 std::size_t irow = findKey(id);
155 if (col >= columnCount_ || irow == getRowCount()) { // raise exception
156 EXCEPTION_RAISE("ConditionsException",
157 "No such column " + std::to_string(col) + " or id " +
158 std::to_string(id));
159 }
160 return values_[irow * columnCount_ + col];
161 }
162
167 std::pair<unsigned int, std::vector<T> > getRow(unsigned int irow) const {
168 if (irow >= getRowCount()) { // raise exception
169 EXCEPTION_RAISE("ConditionsException",
170 "Row out of range: " + std::to_string(irow));
171 }
172 std::vector<T> rv(&(values_[irow * columnCount_]),
173 &(values_[(irow + 1) * columnCount_]));
174 return std::pair<unsigned int, std::vector<T> >(keys_[irow], rv);
175 }
176
183 T getByName(unsigned int id, const std::string& colname) const {
184 return get(id, getColumnNumber(colname));
185 }
186
190 virtual std::ostream& streamRow(std::ostream& s, int irow) const {
191 if (irow >= getRowCount()) { // raise exception
192 EXCEPTION_RAISE("ConditionsException",
193 "Row out of range: " + std::to_string(irow));
194 }
195 s << keys_[irow];
196 for (int i = 0; i < columnCount_; i++)
197 s << ',' << values_[irow * columnCount_ + i];
198 return s << std::endl;
199 }
200
201 private:
202 std::vector<T> values_; // unrolled array
203};
204
210 public:
211 DoubleTableCondition(const std::string& name,
212 const std::vector<std::string>& columns)
213 : HomogenousTableCondition<double>(name, columns) {}
214
215 virtual ~DoubleTableCondition() {}
216};
217
223 public:
224 IntegerTableCondition(const std::string& name,
225 const std::vector<std::string>& columns)
226 : HomogenousTableCondition<int>(name, columns) {}
227
228 virtual ~IntegerTableCondition() {}
229};
230
231} // namespace conditions
232
233std::ostream& operator<<(std::ostream&, const conditions::BaseTableCondition&);
234
235#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