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 
6 
7 #include <boost/range/sub_range.hpp>
8 
9 #include <cstdint>
10 #include <vector>
11 #include <string>
12 
13 namespace nanoaod {
14 
15  namespace flatTableHelper {
16  template <typename T>
19  inline T one(const T &val) const { return val; }
20  inline void bulk(boost::sub_range<std::vector<T>> data) const {}
21  };
22  template <>
24  int bits_;
26  inline float one(const float &val) const {
27  return (bits_ > 0 ? MiniFloatConverter::reduceMantissaToNbitsRounding(val, bits_) : val);
28  }
29  inline void bulk(boost::sub_range<std::vector<float>> data) const {
30  if (bits_ > 0)
31  MiniFloatConverter::reduceMantissaToNbitsRounding(bits_, data.begin(), data.end(), data.begin());
32  }
33  };
34  } // namespace flatTableHelper
35 
36  class FlatTable {
37  public:
38  enum ColumnType {
43  }; // We could have other Float types with reduced mantissa, and similar
44 
45  FlatTable() : size_(0) {}
46  FlatTable(unsigned int size, const std::string &name, bool singleton, bool extension = false)
49 
50  unsigned int nColumns() const { return columns_.size(); };
51  unsigned int nRows() const { return size_; };
52  unsigned int size() const { return size_; }
53  bool singleton() const { return singleton_; }
54  bool extension() const { return extension_; }
55  const std::string &name() const { return name_; }
56 
57  const std::string &columnName(unsigned int col) const { return columns_[col].name; }
58  int columnIndex(const std::string &name) const;
59 
60  ColumnType columnType(unsigned int col) const { return columns_[col].type; }
61 
62  void setDoc(const std::string &doc) { doc_ = doc; }
63  const std::string &doc() const { return doc_; }
64  const std::string &columnDoc(unsigned int col) const { return columns_[col].doc; }
65 
67  template <typename T>
68  boost::sub_range<const std::vector<T>> columnData(unsigned int column) const {
69  auto begin = beginData<T>(column);
70  return boost::sub_range<const std::vector<T>>(begin, begin + size_);
71  }
72 
74  template <typename T>
75  boost::sub_range<std::vector<T>> columnData(unsigned int column) {
76  auto begin = beginData<T>(column);
77  return boost::sub_range<std::vector<T>>(begin, begin + size_);
78  }
79 
81  template <typename T>
82  const T &columValue(unsigned int column) const {
83  if (!singleton())
84  throw cms::Exception("LogicError", "columnValue works only for singleton tables");
85  return *beginData<T>(column);
86  }
87 
88  double getAnyValue(unsigned int row, unsigned int column) const;
89 
90  class RowView {
91  public:
92  RowView() {}
93  RowView(const FlatTable &table, unsigned int row) : table_(&table), row_(row) {}
94  double getAnyValue(unsigned int column) const { return table_->getAnyValue(row_, column); }
95  double getAnyValue(const std::string &column) const {
96  return table_->getAnyValue(row_, table_->columnIndex(column));
97  }
98  const FlatTable &table() const { return *table_; }
99  unsigned int row() const { return row_; }
100 
101  private:
103  unsigned int row_;
104  };
105  RowView row(unsigned int row) const { return RowView(*this, row); }
106 
107  template <typename T, typename C = std::vector<T>>
109  const C &values,
110  const std::string &docString,
111  ColumnType type = defaultColumnType<T>(),
112  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  check_type<T>(type); // throws if type is wrong
118  auto &vec = bigVector<T>();
119  columns_.emplace_back(name, docString, type, vec.size());
120  vec.insert(vec.end(), values.begin(), values.end());
121  if (type == FloatColumn) {
123  }
124  }
125  template <typename T, typename C>
127  const C &value,
128  const std::string &docString,
129  ColumnType type = defaultColumnType<T>(),
130  int mantissaBits = -1) {
131  if (!singleton())
132  throw cms::Exception("LogicError", "addColumnValue works only for singleton tables");
133  if (columnIndex(name) != -1)
134  throw cms::Exception("LogicError", "Duplicated column: " + name);
135  check_type<T>(type); // throws if type is wrong
136  auto &vec = bigVector<T>();
137  columns_.emplace_back(name, docString, type, vec.size());
138  if (type == FloatColumn) {
140  } else {
141  vec.push_back(value);
142  }
143  }
144 
145  void addExtension(const FlatTable &extension);
146 
147  template <typename T>
149  throw cms::Exception("unsupported type");
150  }
151 
152  // this below needs to be public for ROOT, but it is to be considered private otherwise
153  struct Column {
156  unsigned int firstIndex;
157  Column() {} // for ROOT
158  Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
159  : name(aname), doc(docString), type(atype), firstIndex(anIndex) {}
160  };
161 
162  private:
163  template <typename T>
164  typename std::vector<T>::const_iterator beginData(unsigned int column) const {
165  const Column &col = columns_[column];
166  check_type<T>(col.type); // throws if type is wrong
167  return bigVector<T>().begin() + col.firstIndex;
168  }
169  template <typename T>
170  typename std::vector<T>::iterator beginData(unsigned int column) {
171  const Column &col = columns_[column];
172  check_type<T>(col.type); // throws if type is wrong
173  return bigVector<T>().begin() + col.firstIndex;
174  }
175 
176  template <typename T>
177  const std::vector<T> &bigVector() const {
178  throw cms::Exception("unsupported type");
179  }
180  template <typename T>
181  std::vector<T> &bigVector() {
182  throw cms::Exception("unsupported type");
183  }
184 
185  unsigned int size_;
188  std::vector<Column> columns_;
189  std::vector<float> floats_;
190  std::vector<int> ints_;
191  std::vector<uint8_t> uint8s_;
192 
193  template <typename T>
195  throw cms::Exception("unsupported type");
196  }
197  };
198 
199  template <>
200  inline void FlatTable::check_type<float>(FlatTable::ColumnType type) {
202  throw cms::Exception("mismatched type");
203  }
204  template <>
205  inline void FlatTable::check_type<int>(FlatTable::ColumnType type) {
206  if (type != FlatTable::IntColumn)
207  throw cms::Exception("mismatched type");
208  }
209  template <>
210  inline void FlatTable::check_type<uint8_t>(FlatTable::ColumnType type) {
212  throw cms::Exception("mismatched type");
213  }
214 
215  template <>
216  inline const std::vector<float> &FlatTable::bigVector<float>() const {
217  return floats_;
218  }
219  template <>
220  inline const std::vector<int> &FlatTable::bigVector<int>() const {
221  return ints_;
222  }
223  template <>
224  inline const std::vector<uint8_t> &FlatTable::bigVector<uint8_t>() const {
225  return uint8s_;
226  }
227  template <>
228  inline std::vector<float> &FlatTable::bigVector<float>() {
229  return floats_;
230  }
231  template <>
232  inline std::vector<int> &FlatTable::bigVector<int>() {
233  return ints_;
234  }
235  template <>
236  inline std::vector<uint8_t> &FlatTable::bigVector<uint8_t>() {
237  return uint8s_;
238  }
239 
240 } // namespace nanoaod
241 
242 #endif
nanoaod::FlatTable::FloatColumn
Definition: FlatTable.h:39
nanoaod::FlatTable::FlatTable
FlatTable()
Definition: FlatTable.h:45
nanoaod::FlatTable::Column::Column
Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
Definition: FlatTable.h:158
nanoaod::FlatTable::ints_
std::vector< int > ints_
Definition: FlatTable.h:190
nanoaod::FlatTable::RowView::row
unsigned int row() const
Definition: FlatTable.h:99
nanoaod
Definition: FlatTable.h:13
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
nanoaod::FlatTable::columnData
boost::sub_range< std::vector< T > > columnData(unsigned int column)
get a column by index (non-const)
Definition: FlatTable.h:75
nanoaod::FlatTable::RowView::RowView
RowView(const FlatTable &table, unsigned int row)
Definition: FlatTable.h:93
nanoaod::FlatTable::singleton_
bool singleton_
Definition: FlatTable.h:187
nanoaod::FlatTable::singleton
bool singleton() const
Definition: FlatTable.h:53
nanoaod::flatTableHelper::MaybeMantissaReduce::bulk
void bulk(boost::sub_range< std::vector< T >> data) const
Definition: FlatTable.h:20
cuy.col
col
Definition: cuy.py:1010
hgcalConcentratorProducer_cfi.mantissaBits
mantissaBits
Definition: hgcalConcentratorProducer_cfi.py:67
nanoaod::FlatTable::setDoc
void setDoc(const std::string &doc)
Definition: FlatTable.h:62
nanoaod::flatTableHelper::MaybeMantissaReduce::MaybeMantissaReduce
MaybeMantissaReduce(int mantissaBits)
Definition: FlatTable.h:18
nanoaod::FlatTable::doc_
std::string doc_
Definition: FlatTable.h:186
nanoaod::flatTableHelper::MaybeMantissaReduce
Definition: FlatTable.h:17
nanoaod::FlatTable::row
RowView row(unsigned int row) const
Definition: FlatTable.h:105
nanoaod::FlatTable::doc
const std::string & doc() const
Definition: FlatTable.h:63
nanoaod::FlatTable::ColumnType
ColumnType
Definition: FlatTable.h:38
nanoaod::FlatTable::addColumnValue
void addColumnValue(const std::string &name, const C &value, const std::string &docString, ColumnType type=defaultColumnType< T >(), int mantissaBits=-1)
Definition: FlatTable.h:126
nanoaod::FlatTable::addColumn
void addColumn(const std::string &name, const C &values, const std::string &docString, ColumnType type=defaultColumnType< T >(), int mantissaBits=-1)
Definition: FlatTable.h:108
electrons_cff.docString
docString
Definition: electrons_cff.py:493
nanoaod::flatTableHelper::MaybeMantissaReduce::one
T one(const T &val) const
Definition: FlatTable.h:19
nanoaod::FlatTable::Column::doc
std::string doc
Definition: FlatTable.h:154
libminifloat.h
nanoaod::FlatTable::columnDoc
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:64
contentValuesCheck.values
values
Definition: contentValuesCheck.py:38
nanoaod::flatTableHelper::MaybeMantissaReduce< float >::MaybeMantissaReduce
MaybeMantissaReduce(int mantissaBits)
Definition: FlatTable.h:25
nanoaod::FlatTable::columnType
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:60
nanoaod::FlatTable::RowView::table
const FlatTable & table() const
Definition: FlatTable.h:98
nanoaod::FlatTable::extension
bool extension() const
Definition: FlatTable.h:54
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
MiniFloatConverter::reduceMantissaToNbitsRounding
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:102
nanoaod::FlatTable::uint8s_
std::vector< uint8_t > uint8s_
Definition: FlatTable.h:191
nanoaod::FlatTable::Column::firstIndex
unsigned int firstIndex
Definition: FlatTable.h:156
nanoaod::FlatTable::beginData
std::vector< T >::const_iterator beginData(unsigned int column) const
Definition: FlatTable.h:164
nanoaod::flatTableHelper::MaybeMantissaReduce< float >::one
float one(const float &val) const
Definition: FlatTable.h:26
nanoaod::FlatTable::size_
unsigned int size_
Definition: FlatTable.h:185
nanoaod::FlatTable::nRows
unsigned int nRows() const
Definition: FlatTable.h:51
nanoaod::FlatTable::Column::type
ColumnType type
Definition: FlatTable.h:155
nanoaod::FlatTable::check_type
static void check_type(FlatTable::ColumnType type)
Definition: FlatTable.h:194
value
Definition: value.py:1
nanoaod::FlatTable::bigVector
const std::vector< T > & bigVector() const
Definition: FlatTable.h:177
nanoaod::FlatTable::UInt8Column
Definition: FlatTable.h:41
nanoaod::FlatTable::columns_
std::vector< Column > columns_
Definition: FlatTable.h:188
nanoaod::flatTableHelper::MaybeMantissaReduce< float >::bulk
void bulk(boost::sub_range< std::vector< float >> data) const
Definition: FlatTable.h:29
nanoaod::FlatTable::~FlatTable
~FlatTable()
Definition: FlatTable.h:48
nanoaod::FlatTable::name
const std::string & name() const
Definition: FlatTable.h:55
nanoaod::FlatTable::defaultColumnType
static ColumnType defaultColumnType()
Definition: FlatTable.h:148
nanoaod::FlatTable::Column::Column
Column()
Definition: FlatTable.h:157
nanoaod::FlatTable::columValue
const T & columValue(unsigned int column) const
get a column value for singleton (const)
Definition: FlatTable.h:82
nanoaod::FlatTable::name_
std::string name_
Definition: FlatTable.h:186
nanoaod::FlatTable::Column::name
std::string name
Definition: FlatTable.h:154
nanoaod::FlatTable
Definition: FlatTable.h:36
nanoaod::FlatTable::floats_
std::vector< float > floats_
Definition: FlatTable.h:189
nanoaod::FlatTable::IntColumn
Definition: FlatTable.h:40
nanoaod::FlatTable::RowView::getAnyValue
double getAnyValue(const std::string &column) const
Definition: FlatTable.h:95
type
type
Definition: HCALResponse.h:21
heppy_batch.val
val
Definition: heppy_batch.py:351
nanoaod::FlatTable::columnData
boost::sub_range< const std::vector< T > > columnData(unsigned int column) const
get a column by index (const)
Definition: FlatTable.h:68
nanoaod::FlatTable::columnName
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:57
nanoaod::FlatTable::RowView
Definition: FlatTable.h:90
nanoaod::FlatTable::Column
Definition: FlatTable.h:153
nanoaod::FlatTable::bigVector
std::vector< T > & bigVector()
Definition: FlatTable.h:181
gen::C
C
Definition: PomwigHadronizer.cc:76
nanoaod::FlatTable::nColumns
unsigned int nColumns() const
Definition: FlatTable.h:50
T
long double T
Definition: Basic3DVectorLD.h:48
nanoaod::FlatTable::getAnyValue
double getAnyValue(unsigned int row, unsigned int column) const
Definition: FlatTable.cc:30
nanoaod::FlatTable::BoolColumn
Definition: FlatTable.h:42
Exception
Definition: hltDiff.cc:246
Exception.h
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
nanoaod::FlatTable::extension_
bool extension_
Definition: FlatTable.h:187
cms::Exception
Definition: Exception.h:70
nanoaod::FlatTable::RowView::getAnyValue
double getAnyValue(unsigned int column) const
Definition: FlatTable.h:94
nanoaod::FlatTable::RowView::table_
const FlatTable * table_
Definition: FlatTable.h:102
nanoaod::FlatTable::beginData
std::vector< T >::iterator beginData(unsigned int column)
Definition: FlatTable.h:170
nanoaod::FlatTable::FlatTable
FlatTable(unsigned int size, const std::string &name, bool singleton, bool extension=false)
Definition: FlatTable.h:46
nanoaod::FlatTable::RowView::RowView
RowView()
Definition: FlatTable.h:92
begin
#define begin
Definition: vmac.h:32
nanoaod::flatTableHelper::MaybeMantissaReduce< float >::bits_
int bits_
Definition: FlatTable.h:24
nanoaod::FlatTable::RowView::row_
unsigned int row_
Definition: FlatTable.h:103
nanoaod::FlatTable::size
unsigned int size() const
Definition: FlatTable.h:52