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  }; // 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)
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());
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());
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
nanoaod::FlatTable::ColumnType::Float
nanoaod::FlatTable::FlatTable
FlatTable()
Definition: FlatTable.h:49
nanoaod::FlatTable::Column::Column
Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
Definition: FlatTable.h:162
nanoaod::FlatTable::ints_
std::vector< int > ints_
Definition: FlatTable.h:209
nanoaod::FlatTable::RowView::row
unsigned int row() const
Definition: FlatTable.h:103
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:97
nanoaod::FlatTable::addColumn
void addColumn(const std::string &name, const C &values, const std::string &docString, int mantissaBits=-1)
Definition: FlatTable.h:112
nanoaod::FlatTable::bigVectorImpl
static auto & bigVectorImpl(This &table)
Definition: FlatTable.h:186
nanoaod::FlatTable::uint32s_
std::vector< uint32_t > uint32s_
Definition: FlatTable.h:211
nanoaod::FlatTable::singleton_
bool singleton_
Definition: FlatTable.h:206
nanoaod::FlatTable::singleton
bool singleton() const
Definition: FlatTable.h:57
cuy.col
col
Definition: cuy.py:1009
hgcalConcentratorProducer_cfi.mantissaBits
mantissaBits
Definition: hgcalConcentratorProducer_cfi.py:68
nanoaod::FlatTable::setDoc
void setDoc(const std::string &doc)
Definition: FlatTable.h:66
nanoaod::FlatTable::ColumnType::UInt32
nanoaod::flatTableHelper::MaybeMantissaReduce::MaybeMantissaReduce
MaybeMantissaReduce(int mantissaBits)
Definition: FlatTable.h:18
nanoaod::FlatTable::doc_
std::string doc_
Definition: FlatTable.h:205
nanoaod::flatTableHelper::MaybeMantissaReduce
Definition: FlatTable.h:17
nanoaod::FlatTable::row
RowView row(unsigned int row) const
Definition: FlatTable.h:109
nanoaod::FlatTable::doc
const std::string & doc() const
Definition: FlatTable.h:67
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:521
nanoaod::flatTableHelper::MaybeMantissaReduce::one
T one(const T &val) const
Definition: FlatTable.h:19
nanoaod::FlatTable::Column::doc
std::string doc
Definition: FlatTable.h:158
nanoaod::FlatTable::columnData
auto columnData(unsigned int column) const
get a column by index (const)
Definition: FlatTable.h:72
libminifloat.h
nanoaod::FlatTable::columnDoc
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:68
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:64
nanoaod::FlatTable::RowView::table
const FlatTable & table() const
Definition: FlatTable.h:102
nanoaod::FlatTable::extension
bool extension() const
Definition: FlatTable.h:58
nanoaod::FlatTable::ColumnType::Double
nanoaod::FlatTable::bigVector
const auto & bigVector() const
Definition: FlatTable.h:177
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::doubles_
std::vector< double > doubles_
Definition: FlatTable.h:212
nanoaod::FlatTable::beginData
auto beginData(unsigned int column)
Definition: FlatTable.h:172
MiniFloatConverter::reduceMantissaToNbitsRounding
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:102
nanoaod::FlatTable::dependent_false
Definition: FlatTable.h:137
nanoaod::FlatTable::uint8s_
std::vector< uint8_t > uint8s_
Definition: FlatTable.h:210
nanoaod::FlatTable::Column::firstIndex
unsigned int firstIndex
Definition: FlatTable.h:160
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:204
type
type
Definition: SiPixelVCal_PayloadInspector.cc:39
nanoaod::FlatTable::nRows
unsigned int nRows() const
Definition: FlatTable.h:55
nanoaod::FlatTable::addColumnValue
void addColumnValue(const std::string &name, const C &value, const std::string &docString, int mantissaBits=-1)
Definition: FlatTable.h:124
nanoaod::FlatTable::Column::type
ColumnType type
Definition: FlatTable.h:159
value
Definition: value.py:1
nanoaod::FlatTable::columns_
std::vector< Column > columns_
Definition: FlatTable.h:207
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
nanoaod::FlatTable::~FlatTable
~FlatTable()
Definition: FlatTable.h:52
nanoaod::FlatTable::name
const std::string & name() const
Definition: FlatTable.h:59
nanoaod::FlatTable::defaultColumnType
static ColumnType defaultColumnType()
Definition: FlatTable.h:139
nanoaod::FlatTable::Column::Column
Column()
Definition: FlatTable.h:161
nanoaod::FlatTable::name_
std::string name_
Definition: FlatTable.h:205
nanoaod::FlatTable::ColumnType::Int
nanoaod::FlatTable::Column::name
std::string name
Definition: FlatTable.h:158
nanoaod::FlatTable
Definition: FlatTable.h:38
nanoaod::FlatTable::floats_
std::vector< float > floats_
Definition: FlatTable.h:208
nanoaod::FlatTable::RowView::getAnyValue
double getAnyValue(const std::string &column) const
Definition: FlatTable.h:99
heppy_batch.val
val
Definition: heppy_batch.py:351
nanoaod::FlatTable::columnName
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:61
nanoaod::FlatTable::RowView
Definition: FlatTable.h:94
nanoaod::FlatTable::Column
Definition: FlatTable.h:157
nanoaod::FlatTable::columValue
const auto & columValue(unsigned int column) const
get a column value for singleton (const)
Definition: FlatTable.h:86
gen::C
C
Definition: PomwigHadronizer.cc:78
nanoaod::FlatTable::nColumns
unsigned int nColumns() const
Definition: FlatTable.h:54
T
long double T
Definition: Basic3DVectorLD.h:48
nanoaod::FlatTable::getAnyValue
double getAnyValue(unsigned int row, unsigned int column) const
Definition: FlatTable.cc:40
Exception
Definition: hltDiff.cc:245
nanoaod::FlatTable::columnData
auto columnData(unsigned int column)
get a column by index (non-const)
Definition: FlatTable.h:79
Exception.h
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
nanoaod::FlatTable::extension_
bool extension_
Definition: FlatTable.h:206
cms::Exception
Definition: Exception.h:70
nanoaod::FlatTable::RowView::getAnyValue
double getAnyValue(unsigned int column) const
Definition: FlatTable.h:98
nanoaod::FlatTable::bigVector
auto & bigVector()
Definition: FlatTable.h:181
TableParser.table
table
Definition: TableParser.py:111
nanoaod::FlatTable::RowView::table_
const FlatTable * table_
Definition: FlatTable.h:106
nanoaod::FlatTable::FlatTable
FlatTable(unsigned int size, const std::string &name, bool singleton, bool extension=false)
Definition: FlatTable.h:50
nanoaod::FlatTable::ColumnType::Bool
nanoaod::FlatTable::RowView::RowView
RowView()
Definition: FlatTable.h:96
edm::Span
Definition: Span.h:16
nanoaod::FlatTable::beginData
auto beginData(unsigned int column) const
Definition: FlatTable.h:168
nanoaod::flatTableHelper::MaybeMantissaReduce< float >::bits_
int bits_
Definition: FlatTable.h:25
nanoaod::FlatTable::RowView::row_
unsigned int row_
Definition: FlatTable.h:107
nanoaod::FlatTable::size
unsigned int size() const
Definition: FlatTable.h:56