CMS 3D CMS Logo

FlatTable.h
Go to the documentation of this file.
1 #ifndef DataFormats_NanoAOD_FlatTable_h
2 #define DataFormats_NanoAOD_FlatTable_h
3 
4 #include <cstdint>
5 #include <vector>
6 #include <string>
7 #include <boost/range/sub_range.hpp>
10 
11 namespace nanoaod {
12 
13  namespace flatTableHelper {
14  template <typename T>
17  inline T one(const T &val) const { return val; }
18  inline void bulk(boost::sub_range<std::vector<T>> data) const {}
19  };
20  template <>
22  int bits_;
23  MaybeMantissaReduce(int mantissaBits) : bits_(mantissaBits) {}
24  inline float one(const float &val) const {
25  return (bits_ > 0 ? MiniFloatConverter::reduceMantissaToNbitsRounding(val, bits_) : val);
26  }
27  inline void bulk(boost::sub_range<std::vector<float>> data) const {
28  if (bits_ > 0)
29  MiniFloatConverter::reduceMantissaToNbitsRounding(bits_, data.begin(), data.end(), data.begin());
30  }
31  };
32  } // namespace flatTableHelper
33 
34  class FlatTable {
35  public:
36  enum ColumnType {
40  BoolColumn
41  }; // We could have other Float types with reduced mantissa, and similar
42 
43  FlatTable() : size_(0) {}
44  FlatTable(unsigned int size, const std::string &name, bool singleton, bool extension = false)
45  : size_(size), name_(name), singleton_(singleton), extension_(extension) {}
47 
48  unsigned int nColumns() const { return columns_.size(); };
49  unsigned int nRows() const { return size_; };
50  unsigned int size() const { return size_; }
51  bool singleton() const { return singleton_; }
52  bool extension() const { return extension_; }
53  const std::string &name() const { return name_; }
54 
55  const std::string &columnName(unsigned int col) const { return columns_[col].name; }
56  int columnIndex(const std::string &name) const;
57 
58  ColumnType columnType(unsigned int col) const { return columns_[col].type; }
59 
60  void setDoc(const std::string &doc) { doc_ = doc; }
61  const std::string &doc() const { return doc_; }
62  const std::string &columnDoc(unsigned int col) const { return columns_[col].doc; }
63 
65  template <typename T>
66  boost::sub_range<const std::vector<T>> columnData(unsigned int column) const {
67  auto begin = beginData<T>(column);
68  return boost::sub_range<const std::vector<T>>(begin, begin + size_);
69  }
70 
72  template <typename T>
73  boost::sub_range<std::vector<T>> columnData(unsigned int column) {
74  auto begin = beginData<T>(column);
75  return boost::sub_range<std::vector<T>>(begin, begin + size_);
76  }
77 
79  template <typename T>
80  const T &columValue(unsigned int column) const {
81  if (!singleton())
82  throw cms::Exception("LogicError", "columnValue works only for singleton tables");
83  return *beginData<T>(column);
84  }
85 
86  double getAnyValue(unsigned int row, unsigned int column) const;
87 
88  class RowView {
89  public:
90  RowView() {}
91  RowView(const FlatTable &table, unsigned int row) : table_(&table), row_(row) {}
92  double getAnyValue(unsigned int column) const { return table_->getAnyValue(row_, column); }
93  double getAnyValue(const std::string &column) const {
94  return table_->getAnyValue(row_, table_->columnIndex(column));
95  }
96  const FlatTable &table() const { return *table_; }
97  unsigned int row() const { return row_; }
98 
99  private:
101  unsigned int row_;
102  };
103  RowView row(unsigned int row) const { return RowView(*this, row); }
104 
105  template <typename T, typename C = std::vector<T>>
106  void addColumn(const std::string &name,
107  const C &values,
108  const std::string &docString,
109  ColumnType type = defaultColumnType<T>(),
110  int mantissaBits = -1) {
111  if (columnIndex(name) != -1)
112  throw cms::Exception("LogicError", "Duplicated column: " + name);
113  if (values.size() != size())
114  throw cms::Exception("LogicError", "Mismatched size for " + name);
115  check_type<T>(type); // throws if type is wrong
116  auto &vec = bigVector<T>();
117  columns_.emplace_back(name, docString, type, vec.size());
118  vec.insert(vec.end(), values.begin(), values.end());
119  if (type == FloatColumn) {
120  flatTableHelper::MaybeMantissaReduce<T>(mantissaBits).bulk(columnData<T>(columns_.size() - 1));
121  }
122  }
123  template <typename T, typename C>
124  void addColumnValue(const std::string &name,
125  const C &value,
126  const std::string &docString,
127  ColumnType type = defaultColumnType<T>(),
128  int mantissaBits = -1) {
129  if (!singleton())
130  throw cms::Exception("LogicError", "addColumnValue works only for singleton tables");
131  if (columnIndex(name) != -1)
132  throw cms::Exception("LogicError", "Duplicated column: " + name);
133  check_type<T>(type); // throws if type is wrong
134  auto &vec = bigVector<T>();
135  columns_.emplace_back(name, docString, type, vec.size());
136  if (type == FloatColumn) {
138  } else {
139  vec.push_back(value);
140  }
141  }
142 
143  void addExtension(const FlatTable &extension);
144 
145  template <typename T>
147  throw cms::Exception("unsupported type");
148  }
149 
150  // this below needs to be public for ROOT, but it is to be considered private otherwise
151  struct Column {
154  unsigned int firstIndex;
155  Column() {} // for ROOT
156  Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
157  : name(aname), doc(docString), type(atype), firstIndex(anIndex) {}
158  };
159 
160  private:
161  template <typename T>
162  typename std::vector<T>::const_iterator beginData(unsigned int column) const {
163  const Column &col = columns_[column];
164  check_type<T>(col.type); // throws if type is wrong
165  return bigVector<T>().begin() + col.firstIndex;
166  }
167  template <typename T>
168  typename std::vector<T>::iterator beginData(unsigned int column) {
169  const Column &col = columns_[column];
170  check_type<T>(col.type); // throws if type is wrong
171  return bigVector<T>().begin() + col.firstIndex;
172  }
173 
174  template <typename T>
175  const std::vector<T> &bigVector() const {
176  throw cms::Exception("unsupported type");
177  }
178  template <typename T>
179  std::vector<T> &bigVector() {
180  throw cms::Exception("unsupported type");
181  }
182 
183  unsigned int size_;
185  bool singleton_, extension_;
186  std::vector<Column> columns_;
187  std::vector<float> floats_;
188  std::vector<int> ints_;
189  std::vector<uint8_t> uint8s_;
190 
191  template <typename T>
193  throw cms::Exception("unsupported type");
194  }
195  };
196 
197  template <>
198  inline void FlatTable::check_type<float>(FlatTable::ColumnType type) {
200  throw cms::Exception("mismatched type");
201  }
202  template <>
203  inline void FlatTable::check_type<int>(FlatTable::ColumnType type) {
204  if (type != FlatTable::IntColumn)
205  throw cms::Exception("mismatched type");
206  }
207  template <>
208  inline void FlatTable::check_type<uint8_t>(FlatTable::ColumnType type) {
210  throw cms::Exception("mismatched type");
211  }
212 
213  template <>
214  inline const std::vector<float> &FlatTable::bigVector<float>() const {
215  return floats_;
216  }
217  template <>
218  inline const std::vector<int> &FlatTable::bigVector<int>() const {
219  return ints_;
220  }
221  template <>
222  inline const std::vector<uint8_t> &FlatTable::bigVector<uint8_t>() const {
223  return uint8s_;
224  }
225  template <>
226  inline std::vector<float> &FlatTable::bigVector<float>() {
227  return floats_;
228  }
229  template <>
230  inline std::vector<int> &FlatTable::bigVector<int>() {
231  return ints_;
232  }
233  template <>
234  inline std::vector<uint8_t> &FlatTable::bigVector<uint8_t>() {
235  return uint8s_;
236  }
237 
238 } // namespace nanoaod
239 
240 #endif
boost::sub_range< const std::vector< T > > columnData(unsigned int column) const
get a column by index (const)
Definition: FlatTable.h:66
size
Write out results.
type
Definition: HCALResponse.h:21
RowView row(unsigned int row) const
Definition: FlatTable.h:103
unsigned int size_
Definition: FlatTable.h:183
std::vector< T > & bigVector()
Definition: FlatTable.h:179
void bulk(boost::sub_range< std::vector< float >> data) const
Definition: FlatTable.h:27
const std::vector< T > & bigVector() const
Definition: FlatTable.h:175
bool extension() const
Definition: FlatTable.h:52
std::vector< int > ints_
Definition: FlatTable.h:188
Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
Definition: FlatTable.h:156
std::vector< Column > columns_
Definition: FlatTable.h:186
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:58
std::vector< uint8_t > uint8s_
Definition: FlatTable.h:189
void addColumn(const std::string &name, const C &values, const std::string &docString, ColumnType type=defaultColumnType< T >(), int mantissaBits=-1)
Definition: FlatTable.h:106
const std::string & doc() const
Definition: FlatTable.h:61
double getAnyValue(unsigned int column) const
Definition: FlatTable.h:92
void setDoc(const std::string &doc)
Definition: FlatTable.h:60
std::vector< float > floats_
Definition: FlatTable.h:187
std::vector< T >::iterator beginData(unsigned int column)
Definition: FlatTable.h:168
const FlatTable * table_
Definition: FlatTable.h:100
const FlatTable & table() const
Definition: FlatTable.h:96
void bulk(boost::sub_range< std::vector< T >> data) const
Definition: FlatTable.h:18
Definition: value.py:1
unsigned int nRows() const
Definition: FlatTable.h:49
RowView(const FlatTable &table, unsigned int row)
Definition: FlatTable.h:91
void addColumnValue(const std::string &name, const C &value, const std::string &docString, ColumnType type=defaultColumnType< T >(), int mantissaBits=-1)
Definition: FlatTable.h:124
const std::string & name() const
Definition: FlatTable.h:53
const T & columValue(unsigned int column) const
get a column value for singleton (const)
Definition: FlatTable.h:80
double getAnyValue(const std::string &column) const
Definition: FlatTable.h:93
Table table_
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:55
boost::sub_range< std::vector< T > > columnData(unsigned int column)
get a column by index (non-const)
Definition: FlatTable.h:73
FlatTable(unsigned int size, const std::string &name, bool singleton, bool extension=false)
Definition: FlatTable.h:44
static void check_type(FlatTable::ColumnType type)
Definition: FlatTable.h:192
std::string name_
Definition: FlatTable.h:184
static ColumnType defaultColumnType()
Definition: FlatTable.h:146
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:102
unsigned int nColumns() const
Definition: FlatTable.h:48
unsigned int size() const
Definition: FlatTable.h:50
#define begin
Definition: vmac.h:32
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
col
Definition: cuy.py:1010
unsigned int row() const
Definition: FlatTable.h:97
std::vector< T >::const_iterator beginData(unsigned int column) const
Definition: FlatTable.h:162
long double T
bool singleton() const
Definition: FlatTable.h:51
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:62