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