CMS 3D CMS Logo

DataFrameContainer.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_DataFrameContainer_h
2 #define DataFormats_Common_DataFrameContainer_h
3 
6 
7 #include <boost/iterator/transform_iterator.hpp>
8 #include <boost/iterator/counting_iterator.hpp>
9 
10 #include <vector>
11 #include <algorithm>
12 #include <cstddef> // for size_t
13 #include <utility> // for pair
14 
15 class TestDataFrame;
16 
17 template <typename DigiCollection>
19 
20 namespace edm {
21 
34  public:
35  typedef unsigned int size_type; // for persistency
36  typedef unsigned int id_type;
37  typedef unsigned short data_type;
38  typedef std::vector<id_type> IdContainer;
39  typedef std::vector<data_type> DataContainer;
40  typedef std::vector<id_type>::iterator IdIter;
41  typedef std::vector<data_type>::iterator DataIter;
42  typedef std::pair<IdIter, DataIter> IterPair;
43  typedef std::vector<id_type>::const_iterator const_IdIter;
44  typedef std::vector<data_type>::const_iterator const_DataIter;
45  typedef std::pair<const_IdIter, const_DataIter> const_IterPair;
46 
47  struct IterHelp {
50 
51  DataFrame const& operator()(int i) const {
52  frame.set(v, i);
53  return frame;
54  }
55 
56  private:
59  };
60 
61  typedef boost::transform_iterator<IterHelp, boost::counting_iterator<int> > const_iterator;
62 
64 
65  explicit DataFrameContainer(size_t istride, int isubdet = 0, size_t isize = 0)
66  : m_subdetId(isubdet), m_stride(istride), m_ids(isize), m_data(isize * m_stride) {}
67 
71  m_ids.swap(rh.m_ids);
72  m_data.swap(rh.m_data);
73  }
74 
75  void swap(IdContainer& iic, DataContainer& idc) {
76  m_ids.swap(iic);
77  m_data.swap(idc);
78  }
79 
80  void reserve(size_t isize) {
81  m_ids.reserve(isize);
82  m_data.reserve(isize * m_stride);
83  }
84 
85  void resize(size_t isize) {
86  m_ids.resize(isize);
87  m_data.resize(isize * m_stride);
88  }
89 
90  void sort();
91 
92  // FIXME not sure what the best way to add one cell to cont
93  void push_back(id_type iid, data_type const* idata) {
94  m_ids.push_back(iid);
95  size_t cs = m_data.size();
96  m_data.resize(m_data.size() + m_stride);
97  std::copy(idata, idata + m_stride, m_data.begin() + cs);
98  }
99  //make space for it
100  void push_back(id_type iid) {
101  m_ids.push_back(iid);
102  m_data.resize(m_data.size() + m_stride);
103  }
104  // overwrite back (very ad hoc interface...)
105  void set_back(id_type iid, data_type const* idata) {
106  m_ids.back() = iid;
107  size_t cs = m_data.size() - m_stride;
108  std::copy(idata, idata + m_stride, m_data.begin() + cs);
109  }
110  void set_back(id_type iid) { m_ids.back() = iid; }
111  void set_back(data_type const* idata) {
112  size_t cs = m_data.size() - m_stride;
113  std::copy(idata, idata + m_stride, m_data.begin() + cs);
114  }
115 
116  DataFrame back() { return DataFrame(*this, size() - 1); }
117 
118  void pop_back() {
119  m_ids.resize(m_ids.size() - 1);
120  m_data.resize(m_data.size() - m_stride);
121  }
122 
123  //---------------------------------------------------------
124 
125  IterPair pair(size_t i) { return IterPair(m_ids.begin() + i, m_data.begin() + i * m_stride); }
126 
127  const_IterPair pair(size_t i) const { return const_IterPair(m_ids.begin() + i, m_data.begin() + i * m_stride); }
128 
129  DataFrame operator[](size_t i) { return DataFrame(*this, i); }
130 
131  DataFrame operator[](size_t i) const { return DataFrame(*this, i); }
132 
136  const_IdIter p = std::lower_bound(m_ids.begin(), m_ids.end(), i);
137  return (p == m_ids.end() || (*p) != i)
138  ? end()
139  : boost::make_transform_iterator(boost::counting_iterator<int>(p - m_ids.begin()), IterHelp(*this));
140  }
141 
144  return boost::make_transform_iterator(boost::counting_iterator<int>(0), IterHelp(*this));
145  }
146  const_iterator end() const {
147  return boost::make_transform_iterator(boost::counting_iterator<int>(size()), IterHelp(*this));
148  }
149 
150  int subdetId() const { return m_subdetId; }
151 
152  size_type stride() const { return m_stride; }
153 
154  bool empty() const { return m_ids.empty(); }
155 
156  size_type size() const { return m_ids.size(); }
157 
158  data_type operator()(size_t cell, size_t frame) const { return m_data[cell * m_stride + frame]; }
159 
160  data_type const* frame(size_t cell) const { return &m_data[cell * m_stride]; }
161 
162  id_type id(size_t cell) const { return m_ids[cell]; }
163 
164  IdContainer const& ids() const { return m_ids; }
165  DataContainer const& data() const { return m_data; }
166 
167  private:
168  //for testing
169  friend class ::TestDataFrame;
170 
171  template <typename DigiCollection>
172  friend class ::TestEcalDigi;
173 
174  // subdetector id (as returned by DetId::subdetId())
176 
177  // can be a enumerator, or a template argument
179 
182  };
183 
185  : m_id(icont.id(i)), m_data(icont.frame(i)), m_size(icont.stride()) {}
186 
187  inline void DataFrame::set(DataFrameContainer const& icont, size_type i) {
188  m_id = icont.id(i);
189  m_data = icont.frame(i);
190  m_size = icont.stride();
191  }
192 
193  // Free swap function
194  inline void swap(DataFrameContainer& lhs, DataFrameContainer& rhs) { lhs.swap(rhs); }
195 
196 } // namespace edm
197 
198 #endif // DataFormats_Common_DataFrameContainer_h
#define CMS_SA_ALLOW
data_type const * m_data
Definition: DataFrame.h:52
void resize(size_t isize)
std::vector< data_type >::iterator DataIter
void push_back(id_type iid)
id_type id(size_t cell) const
IterHelp(DataFrameContainer const &iv)
constexpr DataFrame()
Definition: DataFrame.h:23
DataFrameContainer(size_t istride, int isubdet=0, size_t isize=0)
id_type m_id
Definition: DataFrame.h:51
DataFrame operator[](size_t i) const
data_type operator()(size_t cell, size_t frame) const
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:112
unsigned int size_type
Definition: DataFrame.h:17
void swap(IdContainer &iic, DataContainer &idc)
constexpr uint32_t stride
Definition: HelixFit.h:22
data_type const * frame(size_t cell) const
std::vector< data_type > DataContainer
DataContainer const & data() const
IdContainer const & ids() const
std::vector< data_type >::const_iterator const_DataIter
std::vector< id_type >::const_iterator const_IdIter
void swap(DataFrameContainer &rh)
DataFrame const & operator()(int i) const
std::vector< id_type >::iterator IdIter
const_iterator end() const
void set_back(data_type const *idata)
void reserve(size_t isize)
void set(DataFrameContainer const &icont, size_type i)
DataFrameContainer const & v
const_iterator begin() const
The iterator returned can not safely be used across threads.
size_type m_size
Definition: DataFrame.h:53
std::pair< IdIter, DataIter > IterPair
void set_back(id_type iid)
std::pair< const_IdIter, const_DataIter > const_IterPair
void push_back(id_type iid, data_type const *idata)
boost::transform_iterator< IterHelp, boost::counting_iterator< int > > const_iterator
HLT enums.
std::vector< id_type > IdContainer
DataFrame operator[](size_t i)
void swap(DataFrameContainer &lhs, DataFrameContainer &rhs)
const_iterator find(id_type i) const
void set_back(id_type iid, data_type const *idata)
const_IterPair pair(size_t i) const