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