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  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, uint8_t>())
144  return ColumnType::UInt8;
145  else if constexpr (std::is_same<T, int16_t>())
146  return ColumnType::Int16;
147  else if constexpr (std::is_same<T, uint16_t>())
148  return ColumnType::UInt16;
149  else if constexpr (std::is_same<T, int32_t>())
150  return ColumnType::Int32;
151  else if constexpr (std::is_same<T, uint32_t>())
152  return ColumnType::UInt32;
153  else if constexpr (std::is_same<T, bool>())
154  return ColumnType::Bool;
155  else if constexpr (std::is_same<T, float>())
156  return ColumnType::Float;
157  else if constexpr (std::is_same<T, double>())
158  return ColumnType::Double;
159  else
160  static_assert(dependent_false<T>::value, "unsupported type");
161  }
162 
163  // this below needs to be public for ROOT, but it is to be considered private otherwise
164  struct Column {
167  unsigned int firstIndex;
168  Column() {} // for ROOT
169  Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
170  : name(aname), doc(docString), type(atype), firstIndex(anIndex) {}
171  };
172 
173  private:
174  template <typename T>
175  auto beginData(unsigned int column) const {
176  return bigVector<T>().cbegin() + columns_[column].firstIndex;
177  }
178  template <typename T>
179  auto beginData(unsigned int column) {
180  return bigVector<T>().begin() + columns_[column].firstIndex;
181  }
182 
183  template <typename T>
184  auto const &bigVector() const {
185  return bigVectorImpl<T>(*this);
186  }
187  template <typename T>
188  auto &bigVector() {
189  return bigVectorImpl<T>(*this);
190  }
191 
192  template <typename T, class This>
193  static auto &bigVectorImpl(This &table) {
194  // helper function to avoid code duplication, for the two accessor functions that differ only in const-ness
195  if constexpr (std::is_same<T, uint8_t>())
196  return table.uint8s_;
197  else if constexpr (std::is_same<T, int16_t>())
198  return table.int16s_;
199  else if constexpr (std::is_same<T, uint16_t>())
200  return table.uint16s_;
201  else if constexpr (std::is_same<T, int32_t>())
202  return table.int32s_;
203  else if constexpr (std::is_same<T, uint32_t>())
204  return table.uint32s_;
205  else if constexpr (std::is_same<T, bool>())
206  return table.uint8s_; // special case: bool stored as vector of uint8
207  else if constexpr (std::is_same<T, float>())
208  return table.floats_;
209  else if constexpr (std::is_same<T, double>())
210  return table.doubles_;
211  else
212  static_assert(dependent_false<T>::value, "unsupported type");
213  }
214 
215  unsigned int size_;
218  std::vector<Column> columns_;
219  std::vector<uint8_t> uint8s_;
220  std::vector<int16_t> int16s_;
221  std::vector<uint16_t> uint16s_;
222  std::vector<int32_t> int32s_;
223  std::vector<uint32_t> uint32s_;
224  std::vector<float> floats_;
225  std::vector<double> doubles_;
226  };
227 
228 } // namespace nanoaod
229 
230 #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:215
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:169
auto const & bigVector() const
Definition: FlatTable.h:184
std::vector< Column > columns_
Definition: FlatTable.h:218
std::vector< uint8_t > uint8s_
Definition: FlatTable.h:219
std::vector< int16_t > int16s_
Definition: FlatTable.h:220
auto & bigVector()
Definition: FlatTable.h:188
static auto & bigVectorImpl(This &table)
Definition: FlatTable.h:193
std::vector< uint32_t > uint32s_
Definition: FlatTable.h:223
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:224
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:222
const FlatTable * table_
Definition: FlatTable.h:109
std::string doc_
Definition: FlatTable.h:216
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:221
auto beginData(unsigned int column)
Definition: FlatTable.h:179
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: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:216
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:175
std::vector< double > doubles_
Definition: FlatTable.h:225
long double T
unsigned int size() const
Definition: FlatTable.h:59