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  //Int8, //removed due to mis-interpretation in ROOT/pyroot
41  enum class ColumnType {
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  auto index = table_->columnIndex(column);
104  if (index == -1)
105  throwUnknownColumn(column);
106  return table_->getAnyValue(row_, index);
107  }
108  const FlatTable &table() const { return *table_; }
109  unsigned int row() const { return row_; }
110 
111  private:
112  [[noreturn]] static void throwUnknownColumn(const std::string &column) noexcept(false);
114  unsigned int row_;
115  };
116  RowView row(unsigned int row) const { return RowView(*this, row); }
117 
118  template <typename T, typename C>
119  void addColumn(const std::string &name, const C &values, const std::string &docString, int mantissaBits = -1) {
120  if (columnIndex(name) != -1)
121  throw cms::Exception("LogicError", "Duplicated column: " + name);
122  if (values.size() != size())
123  throw cms::Exception("LogicError", "Mismatched size for " + name);
124  auto &vec = bigVector<T>();
125  columns_.emplace_back(name, docString, defaultColumnType<T>(), vec.size());
126  vec.insert(vec.end(), values.begin(), values.end());
127  flatTableHelper::MaybeMantissaReduce<T>(mantissaBits).bulk(columnData<T>(columns_.size() - 1));
128  }
129 
130  template <typename T, typename C>
131  void addColumnValue(const std::string &name, const C &value, const std::string &docString, int mantissaBits = -1) {
132  if (!singleton())
133  throw cms::Exception("LogicError", "addColumnValue works only for singleton tables");
134  if (columnIndex(name) != -1)
135  throw cms::Exception("LogicError", "Duplicated column: " + name);
136  auto &vec = bigVector<T>();
137  columns_.emplace_back(name, docString, defaultColumnType<T>(), vec.size());
139  }
140 
141  void addExtension(const FlatTable &extension);
142 
143  template <class T>
144  struct dependent_false : std::false_type {};
145  template <typename T>
147  if constexpr (std::is_same<T, uint8_t>())
148  return ColumnType::UInt8;
149  else if constexpr (std::is_same<T, int16_t>())
150  return ColumnType::Int16;
151  else if constexpr (std::is_same<T, uint16_t>())
152  return ColumnType::UInt16;
153  else if constexpr (std::is_same<T, int32_t>())
154  return ColumnType::Int32;
155  else if constexpr (std::is_same<T, uint32_t>())
156  return ColumnType::UInt32;
157  else if constexpr (std::is_same<T, bool>())
158  return ColumnType::Bool;
159  else if constexpr (std::is_same<T, float>())
160  return ColumnType::Float;
161  else if constexpr (std::is_same<T, double>())
162  return ColumnType::Double;
163  else
164  static_assert(dependent_false<T>::value, "unsupported type");
165  }
166 
167  // this below needs to be public for ROOT, but it is to be considered private otherwise
168  struct Column {
171  unsigned int firstIndex;
172  Column() {} // for ROOT
173  Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
174  : name(aname), doc(docString), type(atype), firstIndex(anIndex) {}
175  };
176 
177  private:
178  template <typename T>
179  auto beginData(unsigned int column) const {
180  return bigVector<T>().cbegin() + columns_[column].firstIndex;
181  }
182  template <typename T>
183  auto beginData(unsigned int column) {
184  return bigVector<T>().begin() + columns_[column].firstIndex;
185  }
186 
187  template <typename T>
188  auto const &bigVector() const {
189  return bigVectorImpl<T>(*this);
190  }
191  template <typename T>
192  auto &bigVector() {
193  return bigVectorImpl<T>(*this);
194  }
195 
196  template <typename T, class This>
197  static auto &bigVectorImpl(This &table) {
198  // helper function to avoid code duplication, for the two accessor functions that differ only in const-ness
199  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<uint8_t> uint8s_;
224  std::vector<int16_t> int16s_;
225  std::vector<uint16_t> uint16s_;
226  std::vector<int32_t> int32s_;
227  std::vector<uint32_t> uint32s_;
228  std::vector<float> floats_;
229  std::vector<double> doubles_;
230  };
231 
232 } // namespace nanoaod
233 
234 #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:116
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:173
auto const & bigVector() const
Definition: FlatTable.h:188
std::vector< Column > columns_
Definition: FlatTable.h:222
std::vector< uint8_t > uint8s_
Definition: FlatTable.h:223
std::vector< int16_t > int16s_
Definition: FlatTable.h:224
auto & bigVector()
Definition: FlatTable.h:192
static auto & bigVectorImpl(This &table)
Definition: FlatTable.h:197
std::vector< uint32_t > uint32s_
Definition: FlatTable.h:227
bool extension() const
Definition: FlatTable.h:61
static void throwUnknownColumn(const std::string &column) noexcept(false)
Definition: FlatTable.cc:70
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:228
double getAnyValue(unsigned int column) const
Definition: FlatTable.h:101
const FlatTable & table() const
Definition: FlatTable.h:108
const std::string & doc() const
Definition: FlatTable.h:70
std::vector< int32_t > int32s_
Definition: FlatTable.h:226
const FlatTable * table_
Definition: FlatTable.h:113
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:225
auto beginData(unsigned int column)
Definition: FlatTable.h:183
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:109
double getAnyValue(unsigned int row, unsigned int column) const
Definition: FlatTable.cc:46
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:146
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:119
void addColumnValue(const std::string &name, const C &value, const std::string &docString, int mantissaBits=-1)
Definition: FlatTable.h:131
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:179
std::vector< double > doubles_
Definition: FlatTable.h:229
long double T
unsigned int size() const
Definition: FlatTable.h:59