CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
49  IterHelp(DataFrameContainer const& iv) : v(iv) {}
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 
77  this->swap(temp);
78  return *this;
79  }
80 
81  void swap(IdContainer& iic, DataContainer& idc) {
82  m_ids.swap(iic);
83  m_data.swap(idc);
84  }
85 
86  void reserve(size_t isize) {
87  m_ids.reserve(isize);
88  m_data.reserve(isize * m_stride);
89  }
90 
91  void resize(size_t isize) {
92  m_ids.resize(isize);
93  m_data.resize(isize * m_stride);
94  }
95 
96  void sort();
97 
98  // FIXME not sure what the best way to add one cell to cont
99  void push_back(id_type iid, data_type const* idata) {
100  m_ids.push_back(iid);
101  size_t cs = m_data.size();
102  m_data.resize(m_data.size() + m_stride);
103  std::copy(idata, idata + m_stride, m_data.begin() + cs);
104  }
105  //make space for it
106  void push_back(id_type iid) {
107  m_ids.push_back(iid);
108  m_data.resize(m_data.size() + m_stride);
109  }
110  // overwrite back (very ad hoc interface...)
111  void set_back(id_type iid, data_type const* idata) {
112  m_ids.back() = iid;
113  size_t cs = m_data.size() - m_stride;
114  std::copy(idata, idata + m_stride, m_data.begin() + cs);
115  }
116  void set_back(id_type iid) { m_ids.back() = iid; }
117  void set_back(data_type const* idata) {
118  size_t cs = m_data.size() - m_stride;
119  std::copy(idata, idata + m_stride, m_data.begin() + cs);
120  }
121 
122  DataFrame back() { return DataFrame(*this, size() - 1); }
123 
124  void pop_back() {
125  m_ids.resize(m_ids.size() - 1);
126  m_data.resize(m_data.size() - m_stride);
127  }
128 
129  //---------------------------------------------------------
130 
131  IterPair pair(size_t i) { return IterPair(m_ids.begin() + i, m_data.begin() + i * m_stride); }
132 
133  const_IterPair pair(size_t i) const { return const_IterPair(m_ids.begin() + i, m_data.begin() + i * m_stride); }
134 
135  DataFrame operator[](size_t i) { return DataFrame(*this, i); }
136 
137  DataFrame operator[](size_t i) const { return DataFrame(*this, i); }
138 
142  const_IdIter p = std::lower_bound(m_ids.begin(), m_ids.end(), i);
143  return (p == m_ids.end() || (*p) != i)
144  ? end()
145  : boost::make_transform_iterator(boost::counting_iterator<int>(p - m_ids.begin()), IterHelp(*this));
146  }
147 
150  return boost::make_transform_iterator(boost::counting_iterator<int>(0), IterHelp(*this));
151  }
152  const_iterator end() const {
153  return boost::make_transform_iterator(boost::counting_iterator<int>(size()), IterHelp(*this));
154  }
155 
156  int subdetId() const { return m_subdetId; }
157 
158  size_type stride() const { return m_stride; }
159 
160  bool empty() const { return m_ids.empty(); }
161 
162  size_type size() const { return m_ids.size(); }
163 
164  data_type operator()(size_t cell, size_t frame) const { return m_data[cell * m_stride + frame]; }
165 
166  data_type const* frame(size_t cell) const { return &m_data[cell * m_stride]; }
167 
168  id_type id(size_t cell) const { return m_ids[cell]; }
169 
170  IdContainer const& ids() const { return m_ids; }
171  DataContainer const& data() const { return m_data; }
172 
173  private:
174  //for testing
175  friend class ::TestDataFrame;
176 
177  template <typename DigiCollection>
178  friend class ::TestEcalDigi;
179 
180  // subdetector id (as returned by DetId::subdetId())
182 
183  // can be a enumerator, or a template argument
185 
188  };
189 
191  : m_id(icont.id(i)), m_data(icont.frame(i)), m_size(icont.stride()) {}
192 
193  inline void DataFrame::set(DataFrameContainer const& icont, size_type i) {
194  m_id = icont.id(i);
195  m_data = icont.frame(i);
196  m_size = icont.stride();
197  }
198 
199  // Free swap function
200  inline void swap(DataFrameContainer& lhs, DataFrameContainer& rhs) { lhs.swap(rhs); }
201 
202 } // namespace edm
203 
204 // The standard allows us to specialize std::swap for non-templates.
205 // This ensures that DataFrameContainer::swap() will be used in algorithms.
206 
207 namespace std {
208  template <>
210  lhs.swap(rhs);
211  }
212 } // namespace std
213 
214 #endif // DataFormats_Common_DataFrameContainer_h
data_type operator()(size_t cell, size_t frame) const
IdContainer const & ids() const
#define CMS_SA_ALLOW
data_type const * m_data
Definition: DataFrame.h:52
void resize(size_t isize)
int32_t *__restrict__ iv
uint16_t *__restrict__ id
std::vector< data_type >::iterator DataIter
void push_back(id_type iid)
unique_ptr< ClusterSequence > cs
IterHelp(DataFrameContainer const &iv)
constexpr DataFrame()
Definition: DataFrame.h:23
size_type stride() const
DataFrameContainer(size_t istride, int isubdet=0, size_t isize=0)
const_iterator begin() const
The iterator returned can not safely be used across threads.
DataFrame operator[](size_t i) const
id_type m_id
Definition: DataFrame.h:51
id_type id(size_t cell) const
void swap(Association< C > &lhs, Association< C > &rhs)
Definition: Association.h:117
unsigned int size_type
Definition: DataFrame.h:17
void swap(IdContainer &iic, DataContainer &idc)
DataFrame const & operator()(int i) const
const_iterator find(id_type i) const
std::vector< data_type > DataContainer
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::vector< data_type >::const_iterator const_DataIter
std::vector< id_type >::const_iterator const_IdIter
DataFrameContainer & operator=(DataFrameContainer const &rhs)
void swap(DataFrameContainer &rh)
std::vector< id_type >::iterator IdIter
m_id("(unknown)")
void set_back(data_type const *idata)
void reserve(size_t isize)
void set(DataFrameContainer const &icont, size_type i)
DataFrameContainer const & v
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
const_iterator end() const
__host__ __device__ constexpr RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
std::vector< id_type > IdContainer
DataFrame operator[](size_t i)
const_IterPair pair(size_t i) const
DataContainer const & data() const
void set_back(id_type iid, data_type const *idata)
data_type const * frame(size_t cell) const