CMS 3D CMS Logo

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