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 
4 #include <cstdint>
5 #include <vector>
6 #include <string>
7 #include <boost/range/sub_range.hpp>
10 
11 namespace nanoaod {
12 
13 namespace flatTableHelper {
14  template<typename T> struct MaybeMantissaReduce {
15  MaybeMantissaReduce(int mantissaBits) {}
16  inline T one(const T &val) const { return val; }
17  inline void bulk(boost::sub_range<std::vector<T>> data) const { }
18  };
19  template<> struct MaybeMantissaReduce<float> {
20  int bits_;
21  MaybeMantissaReduce(int mantissaBits) : bits_(mantissaBits) {}
22  inline float one(const float &val) const { return (bits_ > 0 ? MiniFloatConverter::reduceMantissaToNbitsRounding(val, bits_) : val); }
23  inline void bulk(boost::sub_range<std::vector<float>> data) const { if (bits_ > 0) MiniFloatConverter::reduceMantissaToNbitsRounding(bits_, data.begin(), data.end(), data.begin()); }
24  };
25 }
26 
27 class FlatTable {
28  public:
29  enum ColumnType { FloatColumn, IntColumn, UInt8Column, BoolColumn }; // We could have other Float types with reduced mantissa, and similar
30 
31  FlatTable() : size_(0) {}
32  FlatTable(unsigned int size, const std::string & name, bool singleton, bool extension=false) : size_(size), name_(name), singleton_(singleton), extension_(extension) {}
34 
35  unsigned int nColumns() const { return columns_.size(); };
36  unsigned int nRows() const { return size_; };
37  unsigned int size() const { return size_; }
38  bool singleton() const { return singleton_; }
39  bool extension() const { return extension_; }
40  const std::string & name() const { return name_; }
41 
42  const std::string & columnName(unsigned int col) const { return columns_[col].name; }
43  int columnIndex(const std::string & name) const ;
44 
45  ColumnType columnType(unsigned int col) const { return columns_[col].type; }
46 
47  void setDoc(const std::string & doc) { doc_ = doc; }
48  const std::string & doc() const { return doc_; }
49  const std::string & columnDoc(unsigned int col) const { return columns_[col].doc; }
50 
52  template<typename T>
53  boost::sub_range<const std::vector<T>> columnData(unsigned int column) const {
54  auto begin = beginData<T>(column);
55  return boost::sub_range<const std::vector<T>>(begin, begin+size_);
56  }
57 
59  template<typename T>
60  boost::sub_range<std::vector<T>> columnData(unsigned int column) {
61  auto begin = beginData<T>(column);
62  return boost::sub_range<std::vector<T>>(begin, begin+size_);
63  }
64 
66  template<typename T>
67  const T & columValue(unsigned int column) const {
68  if (!singleton()) throw cms::Exception("LogicError", "columnValue works only for singleton tables");
69  return * beginData<T>(column);
70  }
71 
72  double getAnyValue(unsigned int row, unsigned int column) const ;
73 
74  class RowView {
75  public:
76  RowView() {}
77  RowView(const FlatTable & table, unsigned int row) : table_(&table), row_(row) {}
78  double getAnyValue(unsigned int column) const { return table_->getAnyValue(row_, column); }
79  double getAnyValue(const std::string & column) const { return table_->getAnyValue(row_, table_->columnIndex(column)); }
80  const FlatTable & table() const { return *table_; }
81  unsigned int row() const { return row_; }
82  private:
83  const FlatTable * table_;
84  unsigned int row_;
85  };
86  RowView row(unsigned int row) const { return RowView(*this, row); }
87 
88  template<typename T, typename C = std::vector<T>>
89  void addColumn(const std::string & name, const C & values, const std::string & docString, ColumnType type = defaultColumnType<T>(),int mantissaBits=-1) {
90  if (columnIndex(name) != -1) throw cms::Exception("LogicError", "Duplicated column: "+name);
91  if (values.size() != size()) throw cms::Exception("LogicError", "Mismatched size for "+name);
92  check_type<T>(type); // throws if type is wrong
93  auto & vec = bigVector<T>();
94  columns_.emplace_back(name,docString,type,vec.size());
95  vec.insert(vec.end(), values.begin(), values.end());
96  if (type == FloatColumn) {
97  flatTableHelper::MaybeMantissaReduce<T>(mantissaBits).bulk(columnData<T>(columns_.size()-1));
98  }
99  }
100  template<typename T, typename C>
101  void addColumnValue(const std::string & name, const C & value, const std::string & docString, ColumnType type = defaultColumnType<T>(),int mantissaBits=-1) {
102  if (!singleton()) throw cms::Exception("LogicError", "addColumnValue works only for singleton tables");
103  if (columnIndex(name) != -1) throw cms::Exception("LogicError", "Duplicated column: "+name);
104  check_type<T>(type); // throws if type is wrong
105  auto & vec = bigVector<T>();
106  columns_.emplace_back(name,docString,type,vec.size());
107  if (type == FloatColumn) {
108  vec.push_back( flatTableHelper::MaybeMantissaReduce<T>(mantissaBits).one(value) );
109  } else {
110  vec.push_back( value );
111  }
112  }
113 
114  void addExtension(const FlatTable & extension) ;
115 
116  template<typename T> static ColumnType defaultColumnType() { throw cms::Exception("unsupported type"); }
117 
118  // this below needs to be public for ROOT, but it is to be considered private otherwise
119  struct Column {
122  unsigned int firstIndex;
123  Column() {} // for ROOT
124  Column(const std::string & aname, const std::string & docString, ColumnType atype, unsigned int anIndex) : name(aname), doc(docString), type(atype), firstIndex(anIndex) {}
125  };
126 
127  private:
128 
129  template<typename T>
130  typename std::vector<T>::const_iterator beginData(unsigned int column) const {
131  const Column & col = columns_[column];
132  check_type<T>(col.type); // throws if type is wrong
133  return bigVector<T>().begin() + col.firstIndex;
134  }
135  template<typename T>
136  typename std::vector<T>::iterator beginData(unsigned int column) {
137  const Column & col = columns_[column];
138  check_type<T>(col.type); // throws if type is wrong
139  return bigVector<T>().begin() + col.firstIndex;
140  }
141 
142  template<typename T>
143  const std::vector<T> & bigVector() const { throw cms::Exception("unsupported type"); }
144  template<typename T>
145  std::vector<T> & bigVector() { throw cms::Exception("unsupported type"); }
146 
147 
148  unsigned int size_;
150  bool singleton_, extension_;
151  std::vector<Column> columns_;
152  std::vector<float> floats_;
153  std::vector<int> ints_;
154  std::vector<uint8_t> uint8s_;
155 
156  template<typename T>
157  static void check_type(FlatTable::ColumnType type) { throw cms::Exception("unsupported type"); }
158 };
159 
160 template<> inline void FlatTable::check_type<float>(FlatTable::ColumnType type) {
161  if (type != FlatTable::FloatColumn) throw cms::Exception("mismatched type");
162 }
163 template<> inline void FlatTable::check_type<int>(FlatTable::ColumnType type) {
164  if (type != FlatTable::IntColumn) throw cms::Exception("mismatched type");
165 }
166 template<> inline void FlatTable::check_type<uint8_t>(FlatTable::ColumnType type) {
167  if (type != FlatTable::UInt8Column && type != FlatTable::BoolColumn) throw cms::Exception("mismatched type");
168 }
169 
170 
171 
172 template<> inline const std::vector<float> & FlatTable::bigVector<float>() const { return floats_; }
173 template<> inline const std::vector<int> & FlatTable::bigVector<int>() const { return ints_; }
174 template<> inline const std::vector<uint8_t> & FlatTable::bigVector<uint8_t>() const { return uint8s_; }
175 template<> inline std::vector<float> & FlatTable::bigVector<float>() { return floats_; }
176 template<> inline std::vector<int> & FlatTable::bigVector<int>() { return ints_; }
177 template<> inline std::vector<uint8_t> & FlatTable::bigVector<uint8_t>() { return uint8s_; }
178 
179 } // nanoaod
180 
181 #endif
boost::sub_range< const std::vector< T > > columnData(unsigned int column) const
get a column by index (const)
Definition: FlatTable.h:53
size
Write out results.
type
Definition: HCALResponse.h:21
RowView row(unsigned int row) const
Definition: FlatTable.h:86
unsigned int size_
Definition: FlatTable.h:148
std::vector< T > & bigVector()
Definition: FlatTable.h:145
void bulk(boost::sub_range< std::vector< float >> data) const
Definition: FlatTable.h:23
const std::vector< T > & bigVector() const
Definition: FlatTable.h:143
bool extension() const
Definition: FlatTable.h:39
std::vector< int > ints_
Definition: FlatTable.h:153
Column(const std::string &aname, const std::string &docString, ColumnType atype, unsigned int anIndex)
Definition: FlatTable.h:124
std::vector< Column > columns_
Definition: FlatTable.h:151
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:45
std::vector< uint8_t > uint8s_
Definition: FlatTable.h:154
void addColumn(const std::string &name, const C &values, const std::string &docString, ColumnType type=defaultColumnType< T >(), int mantissaBits=-1)
Definition: FlatTable.h:89
const std::string & doc() const
Definition: FlatTable.h:48
double getAnyValue(unsigned int column) const
Definition: FlatTable.h:78
void setDoc(const std::string &doc)
Definition: FlatTable.h:47
std::vector< float > floats_
Definition: FlatTable.h:152
std::vector< T >::iterator beginData(unsigned int column)
Definition: FlatTable.h:136
const FlatTable * table_
Definition: FlatTable.h:83
const FlatTable & table() const
Definition: FlatTable.h:80
void bulk(boost::sub_range< std::vector< T >> data) const
Definition: FlatTable.h:17
Definition: value.py:1
unsigned int nRows() const
Definition: FlatTable.h:36
RowView(const FlatTable &table, unsigned int row)
Definition: FlatTable.h:77
void addColumnValue(const std::string &name, const C &value, const std::string &docString, ColumnType type=defaultColumnType< T >(), int mantissaBits=-1)
Definition: FlatTable.h:101
const std::string & name() const
Definition: FlatTable.h:40
const T & columValue(unsigned int column) const
get a column value for singleton (const)
Definition: FlatTable.h:67
double getAnyValue(const std::string &column) const
Definition: FlatTable.h:79
Table table_
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:42
boost::sub_range< std::vector< T > > columnData(unsigned int column)
get a column by index (non-const)
Definition: FlatTable.h:60
FlatTable(unsigned int size, const std::string &name, bool singleton, bool extension=false)
Definition: FlatTable.h:32
static void check_type(FlatTable::ColumnType type)
Definition: FlatTable.h:157
std::string name_
Definition: FlatTable.h:149
static ColumnType defaultColumnType()
Definition: FlatTable.h:116
static float reduceMantissaToNbitsRounding(const float &f)
Definition: libminifloat.h:86
unsigned int nColumns() const
Definition: FlatTable.h:35
unsigned int size() const
Definition: FlatTable.h:37
#define begin
Definition: vmac.h:30
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
col
Definition: cuy.py:1008
unsigned int row() const
Definition: FlatTable.h:81
std::vector< T >::const_iterator beginData(unsigned int column) const
Definition: FlatTable.h:130
long double T
bool singleton() const
Definition: FlatTable.h:38
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:49