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 
7 
8 #include <cstdint>
9 #include <vector>
10 #include <string>
11 #include <type_traits>
12 
13 namespace nanoaod {
14 
15  namespace flatTableHelper {
16  template <typename T>
19  inline T one(const T &val) const { return val; }
20  template <typename Span>
21  inline void bulk(Span const &data) const {}
22  };
23  template <>
25  int bits_;
27  inline float one(const float &val) const {
28  return (bits_ > 0 ? MiniFloatConverter::reduceMantissaToNbitsRounding(val, bits_) : val);
29  }
30  template <typename Span>
31  inline void bulk(Span &&data) const {
32  if (bits_ > 0)
33  MiniFloatConverter::reduceMantissaToNbitsRounding(bits_, data.begin(), data.end(), data.begin());
34  }
35  };
36  } // namespace flatTableHelper
37 
38  class FlatTable {
39  public:
40  enum class ColumnType {
41  Int8,
42  UInt8,
43  Int16,
44  UInt16,
45  Int32,
46  UInt32,
47  Bool,
48  Float,
49  Double,
50  }; // We could have other Float types with reduced mantissa, and similar
51 
52  FlatTable() : size_(0) {}
53  FlatTable(unsigned int size, const std::string &name, bool singleton, bool extension = false)
56 
57  unsigned int nColumns() const { return columns_.size(); };
58  unsigned int nRows() const { return size_; };
59  unsigned int size() const { return size_; }
60  bool singleton() const { return singleton_; }
61  bool extension() const { return extension_; }
62  const std::string &name() const { return name_; }
63 
64  const std::string &columnName(unsigned int col) const { return columns_[col].name; }
65  int columnIndex(const std::string &name) const;
66 
67  ColumnType columnType(unsigned int col) const { return columns_[col].type; }
68 
69  void setDoc(const std::string &doc) { doc_ = doc; }
70  const std::string &doc() const { return doc_; }
71  const std::string &columnDoc(unsigned int col) const { return columns_[col].doc; }
72 
74  template <typename T>
75  auto columnData(unsigned int column) const {
76  auto begin = beginData<T>(column);
77  return edm::Span(begin, begin + size_);
78  }
79 
81  template <typename T>
82  auto columnData(unsigned int column) {
83  auto begin = beginData<T>(column);
84  return edm::Span(begin, begin + size_);
85  }
86 
88  template <typename T>
89  const auto &columValue(unsigned int column) const {
90  if (!singleton())
91  throw cms::Exception("LogicError", "columnValue works only for singleton tables");
92  return *beginData<T>(column);
93  }
94 
95  double getAnyValue(unsigned int row, unsigned int column) const;
96 
97  class RowView {
98  public:
99  RowView() {}
100  RowView(const FlatTable &table, unsigned int row) : table_(&table), row_(row) {}
101  double getAnyValue(unsigned int column) const { return table_->getAnyValue(row_, column); }
102  double getAnyValue(const std::string &column) const {
103  return table_->getAnyValue(row_, table_->columnIndex(column));
104  }
105  const FlatTable &table() const { return *table_; }
106  unsigned int row() const { return row_; }
107 
108  private:
110  unsigned int row_;
111  };
112  RowView row(unsigned int row) const { return RowView(*this, row); }
113 
114  template <typename T, typename C>
115  void addColumn(const std::string &name, const C &values, const std::string &docString, int mantissaBits = -1) {
116  if (columnIndex(name) != -1)
117  throw cms::Exception("LogicError", "Duplicated column: " + name);
118  if (values.size() != size())
119  throw cms::Exception("LogicError", "Mismatched size for " + name);
120  auto &vec = bigVector<T>();
121  columns_.emplace_back(name, docString, defaultColumnType<T>(), vec.size());
122  vec.insert(vec.end(), values.begin(), values.end());
123  flatTableHelper::MaybeMantissaReduce<T>(mantissaBits).bulk(columnData<T>(columns_.size() - 1));
124  }
125 
126  template <typename T, typename C>
127  void addColumnValue(const std::string &name, const C &value, const std::string &docString, int mantissaBits = -1) {
128  if (!singleton())
129  throw cms::Exception("LogicError", "addColumnValue works only for singleton tables");
130  if (columnIndex(name) != -1)
131  throw cms::Exception("LogicError", "Duplicated column: " + name);
132  auto &vec = bigVector<T>();
133  columns_.emplace_back(name, docString, defaultColumnType<T>(), vec.size());
135  }
136 
137  void addExtension(const FlatTable &extension);
138 
139  template <class T>
140  struct dependent_false : std::false_type {};
141  template <typename T>
143  if constexpr (std::is_same<T, int8_t>())
144  return ColumnType::Int8;
145  else if constexpr (std::is_same<T, uint8_t>())
146  return ColumnType::UInt8;
147  else if constexpr (std::is_same<T, int16_t>())
148  return ColumnType::Int16;
149  else if constexpr (std::is_same<T, uint16_t>())
150  return ColumnType::UInt16;
151  else if constexpr (std::is_same<T, int32_t>())
152  return ColumnType::Int32;
153  else if constexpr (std::is_same<T, uint32_t>())
154  return ColumnType::UInt32;
155  else if constexpr (std::is_same<T, bool>())
156  return ColumnType::Bool;
157  else if constexpr (std::is_same<T, float>())
158  return ColumnType::Float;
159  else if constexpr (std::is_same<T, double>())
160  return ColumnType::Double;
161  else
162  static_assert(dependent_false<T>::value, "unsupported type");
163  }
164 
165  // this below needs to be public for ROOT, but it is to be considered private otherwise
166  struct Column {
169  unsigned int firstIndex;
170  Column() {} // for ROOT
171  Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
172  : name(aname), doc(docString), type(atype), firstIndex(anIndex) {}
173  };
174 
175  private:
176  template <typename T>
177  auto beginData(unsigned int column) const {
178  return bigVector<T>().cbegin() + columns_[column].firstIndex;
179  }
180  template <typename T>
181  auto beginData(unsigned int column) {
182  return bigVector<T>().begin() + columns_[column].firstIndex;
183  }
184 
185  template <typename T>
186  auto const &bigVector() const {
187  return bigVectorImpl<T>(*this);
188  }
189  template <typename T>
190  auto &bigVector() {
191  return bigVectorImpl<T>(*this);
192  }
193 
194  template <typename T, class This>
195  static auto &bigVectorImpl(This &table) {
196  // helper function to avoid code duplication, for the two accessor functions that differ only in const-ness
197  if constexpr (std::is_same<T, int8_t>())
198  return table.int8s_;
199  else if constexpr (std::is_same<T, uint8_t>())
200  return table.uint8s_;
201  else if constexpr (std::is_same<T, int16_t>())
202  return table.int16s_;
203  else if constexpr (std::is_same<T, uint16_t>())
204  return table.uint16s_;
205  else if constexpr (std::is_same<T, int32_t>())
206  return table.int32s_;
207  else if constexpr (std::is_same<T, uint32_t>())
208  return table.uint32s_;
209  else if constexpr (std::is_same<T, bool>())
210  return table.uint8s_; // special case: bool stored as vector of uint8
211  else if constexpr (std::is_same<T, float>())
212  return table.floats_;
213  else if constexpr (std::is_same<T, double>())
214  return table.doubles_;
215  else
216  static_assert(dependent_false<T>::value, "unsupported type");
217  }
218 
219  unsigned int size_;
222  std::vector<Column> columns_;
223  std::vector<int8_t> int8s_;
224  std::vector<uint8_t> uint8s_;
225  std::vector<int16_t> int16s_;
226  std::vector<uint16_t> uint16s_;
227  std::vector<int32_t> int32s_;
228  std::vector<uint32_t> uint32s_;
229  std::vector<float> floats_;
230  std::vector<double> doubles_;
231  };
232 
233 } // namespace nanoaod
234 
235 #endif
void addExtension(const FlatTable &extension)
Definition: FlatTable.cc:11
double getAnyValue(const std::string &column) const
Definition: FlatTable.h:102
unsigned int size_
Definition: FlatTable.h:219
auto columnData(unsigned int column)
get a column by index (non-const)
Definition: FlatTable.h:82
RowView row(unsigned int row) const
Definition: FlatTable.h:112
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:64
Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
Definition: FlatTable.h:171
auto const & bigVector() const
Definition: FlatTable.h:186
std::vector< Column > columns_
Definition: FlatTable.h:222
std::vector< uint8_t > uint8s_
Definition: FlatTable.h:224
std::vector< int16_t > int16s_
Definition: FlatTable.h:225
auto & bigVector()
Definition: FlatTable.h:190
static auto & bigVectorImpl(This &table)
Definition: FlatTable.h:195
std::vector< uint32_t > uint32s_
Definition: FlatTable.h:228
bool extension() const
Definition: FlatTable.h:61
const std::string & name() const
Definition: FlatTable.h:62
void setDoc(const std::string &doc)
Definition: FlatTable.h:69
std::vector< float > floats_
Definition: FlatTable.h:229
double getAnyValue(unsigned int column) const
Definition: FlatTable.h:101
const FlatTable & table() const
Definition: FlatTable.h:105
const std::string & doc() const
Definition: FlatTable.h:70
std::vector< int32_t > int32s_
Definition: FlatTable.h:227
const FlatTable * table_
Definition: FlatTable.h:109
std::string doc_
Definition: FlatTable.h:220
int columnIndex(const std::string &name) const
Definition: FlatTable.cc:3
unsigned int nRows() const
Definition: FlatTable.h:58
const auto & columValue(unsigned int column) const
get a column value for singleton (const)
Definition: FlatTable.h:89
std::vector< uint16_t > uint16s_
Definition: FlatTable.h:226
auto beginData(unsigned int column)
Definition: FlatTable.h:181
Definition: value.py:1
RowView(const FlatTable &table, unsigned int row)
Definition: FlatTable.h:100
auto columnData(unsigned int column) const
get a column by index (const)
Definition: FlatTable.h:75
unsigned int row() const
Definition: FlatTable.h:106
double getAnyValue(unsigned int row, unsigned int column) const
Definition: FlatTable.cc:49
std::vector< int8_t > int8s_
Definition: FlatTable.h:223
void bulk(Span const &data) const
Definition: FlatTable.h:21
FlatTable(unsigned int size, const std::string &name, bool singleton, bool extension=false)
Definition: FlatTable.h:53
std::string name_
Definition: FlatTable.h:220
static ColumnType defaultColumnType()
Definition: FlatTable.h:142
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:79
bool singleton() const
Definition: FlatTable.h:60
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:67
Definition: Span.h:16
col
Definition: cuy.py:1009
void addColumn(const std::string &name, const C &values, const std::string &docString, int mantissaBits=-1)
Definition: FlatTable.h:115
void addColumnValue(const std::string &name, const C &value, const std::string &docString, int mantissaBits=-1)
Definition: FlatTable.h:127
unsigned int nColumns() const
Definition: FlatTable.h:57
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:71
auto beginData(unsigned int column) const
Definition: FlatTable.h:177
std::vector< double > doubles_
Definition: FlatTable.h:230
long double T
unsigned int size() const
Definition: FlatTable.h:59