CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DetSetVectorNew.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_DetSetVectorNew_h
2 #define DataFormats_Common_DetSetVectorNew_h
3 
5 // #include "DataFormats/Common/interface/DetSet.h" // to get det_id_type
8 
9 #include <boost/iterator_adaptors.hpp>
10 #include <boost/iterator/transform_iterator.hpp>
11 #include <boost/iterator/counting_iterator.hpp>
12 #include <boost/any.hpp>
13 #include <memory>
16 
17 
18 #include<vector>
19 #include <cassert>
20 
21 namespace edm { namespace refhelper { template<typename T> struct FindForNewDetSetVector; } }
22 
23 //FIXME remove New when ready
24 namespace edmNew {
25  typedef uint32_t det_id_type;
26 
27  struct CapacityExaustedException : public cms::Exception { CapacityExaustedException(): cms::Exception("Capacity exausted in DetSetVectorNew"){} };
28 
29  namespace dslv {
30  template< typename T> class LazyGetter;
31  }
32 
33  /* transient component of DetSetVector
34  * for pure conviniency of dictioanary declaration
35  */
36  namespace dstvdetails {
39  bool filling;
41 
42 
43  void swap(DetSetVectorTrans& rh) {
46  }
47 
48  typedef unsigned int size_type; // for persistency
49  typedef unsigned int id_type;
50 
51  struct Item {
52  Item(id_type i=0, int io=-1, size_type is=0) : id(i), offset(io), size(is){}
53  bool isValid() const { return offset>=0;}
55  int offset;
57  bool operator<(Item const &rh) const { return id<rh.id;}
58  operator id_type() const { return id;}
59  };
60 
61  };
62 
63  void errorFilling();
64  void errorIdExists(det_id_type iid);
65  void throw_range(det_id_type iid);
67 
68 
69  }
70 
81  template<typename T>
82  class DetSetVector : private dstvdetails::DetSetVectorTrans {
83  public:
85  typedef Trans::Item Item;
86  typedef unsigned int size_type; // for persistency
87  typedef unsigned int id_type;
88  typedef T data_type;
92  // FIXME not sure make sense....
93  typedef DetSet value_type;
94  typedef id_type key_type;
95 
96 
97  typedef std::vector<Item> IdContainer;
98  typedef std::vector<data_type> DataContainer;
99  typedef typename IdContainer::iterator IdIter;
100  typedef typename std::vector<data_type>::iterator DataIter;
101  typedef std::pair<IdIter,DataIter> IterPair;
102  typedef typename IdContainer::const_iterator const_IdIter;
103  typedef typename std::vector<data_type>::const_iterator const_DataIter;
104  typedef std::pair<const_IdIter,const_DataIter> const_IterPair;
105 
107 
108  struct IterHelp {
110  // IterHelp() : v(0),update(true){}
111  IterHelp() : v(0),update(false){}
112  IterHelp(DetSetVector<T> const & iv, bool iup) : v(&iv), update(iup){}
113 
114  result_type & operator()(Item const& item) const {
115  detset.set(*v,item,update);
116  return detset;
117  }
118  private:
121  bool update;
122  };
123 
124  typedef boost::transform_iterator<IterHelp,const_IdIter> const_iterator;
125  typedef std::pair<const_iterator,const_iterator> Range;
126 
127  /* fill the lastest inserted DetSet
128  */
129  class FastFiller {
130  public:
135 
136  FastFiller(DetSetVector<T> & iv, id_type id, bool isaveEmpty=false) :
137  v(iv), item(v.push_back(id)), saveEmpty(isaveEmpty) {
138  if (v.filling) dstvdetails::errorFilling();
139  v.filling=true;
140  }
141  FastFiller(DetSetVector<T> & iv, typename DetSetVector<T>::Item & it, bool isaveEmpty=false) :
142  v(iv), item(it), saveEmpty(isaveEmpty) {
143  if (v.filling) dstvdetails::errorFilling();
144  v.filling=true;
145  }
147  if (!saveEmpty && item.size==0) {
148  v.pop_back(item.id);
149  }
150  v.filling=false;
151  }
152 
153  void abort() {
154  v.pop_back(item.id);
155  saveEmpty=true; // avoid mess in destructor
156  }
157 
158  void checkCapacityExausted() const {
159  if (v.onDemand() && v.m_data.size()==v.m_data.capacity()) dstvdetails::throwCapacityExausted();
160  }
161 
163  if (v.onDemand() && v.m_data.size()+s>v.m_data.capacity()) dstvdetails::throwCapacityExausted();
164  }
165 
166 
168  if (item.offset+s <= v.m_data.capacity()) return;
169  if (v.onDemand()) dstvdetails::throwCapacityExausted();
170  v.m_data.reserve(item.offset+s);
171  }
172 
173 
176  v.m_data.resize(item.offset+s);
177  item.size=s;
178  }
179 
180  id_type id() const { return item.id;}
181  size_type size() const { return item.size;}
182  bool empty() const { return item.size==0;}
183 
185  return v.m_data[item.offset+i];
186  }
187  DataIter begin() { return v.m_data.begin()+ item.offset;}
188  DataIter end() { return v.m_data.end();}
189 
190  void push_back(data_type const & d) {
192  v.m_data.push_back(d);
193  item.size++;
194  }
195 #ifndef CMS_NOCXX11
196  void push_back(data_type && d) {
198  v.m_data.push_back(std::move(d));
199  item.size++;
200  }
201 #endif
202 
203  data_type & back() { return v.m_data.back();}
204 
205  private:
208  bool saveEmpty;
209  };
210  friend class FastFiller;
211 
212  class FindForDetSetVector : public std::binary_function<const edmNew::DetSetVector<T>&, unsigned int, const T*> {
213  public:
214  typedef FindForDetSetVector self;
215  typename self::result_type operator()(typename self::first_argument_type iContainer, typename self::second_argument_type iIndex) {
216  return &(iContainer.m_data[iIndex]);
217  }
218  };
219  friend class FindForDetSetVector;
220 
221  explicit DetSetVector(int isubdet=0) :
222  m_subdetId(isubdet) {}
223 
224  DetSetVector(std::shared_ptr<dslv::LazyGetter<T> > iGetter, const std::vector<det_id_type>& iDets,
225  int isubdet=0);
226 
227 
229  // delete content if T is pointer...
230  }
231 
232 
233  bool onDemand() const { return !getter.empty();}
234 
235  void swap(DetSetVector & rh) {
237  std::swap(m_subdetId,rh.m_subdetId);
238  std::swap(m_ids,rh.m_ids);
239  std::swap(m_data,rh.m_data);
240  }
241 
242  void swap(IdContainer & iic, DataContainer & idc) {
243  std::swap(m_ids,iic);
244  std::swap(m_data,idc);
245  }
246 
247  void reserve(size_t isize, size_t dsize) {
248  m_ids.reserve(isize);
249  m_data.reserve(dsize);
250  }
251 
252  void shrink_to_fit() {
253 #ifndef CMS_NOCXX11
254  m_ids.shrink_to_fit();
255  m_data.shrink_to_fit();
256 #endif
257  }
258 
259  void resize(size_t isize, size_t dsize) {
260  m_ids.resize(isize);
261  m_data.resize(dsize);
262  }
263 
264  // FIXME not sure what the best way to add one cell to cont
265  DetSet insert(id_type iid, data_type const * idata, size_type isize) {
266  Item & item = addItem(iid,isize);
267  m_data.resize(m_data.size()+isize);
268  std::copy(idata,idata+isize,m_data.begin()+item.offset);
269  return DetSet(*this,item,false);
270  }
271  //make space for it
273  Item & item = addItem(iid,isize);
274  m_data.resize(m_data.size()+isize);
275  return DetSet(*this,item,false);
276  }
277 
278  // to be used with a FastFiller
280  return addItem(iid,0);
281  }
282 
283  // remove last entry (usually only if empty...)
284  void pop_back(id_type iid) {
285  const_IdIter p = findItem(iid);
286  if (p==m_ids.end()) return; //bha!
287  // sanity checks... (shall we throw or assert?)
288  if ((*p).size>0&& (*p).offset>-1 &&
289  m_data.size()==(*p).offset+(*p).size)
290  m_data.resize((*p).offset);
291  m_ids.erase( m_ids.begin()+(p-m_ids.begin()));
292  }
293 
294  private:
295 
296  Item & addItem(id_type iid, size_type isize) {
297  Item it(iid,size_type(m_data.size()),isize);
298  IdIter p = std::lower_bound(m_ids.begin(),
299  m_ids.end(),
300  it);
301  if (p!=m_ids.end() && !(it<*p)) dstvdetails::errorIdExists(iid);
302  return *m_ids.insert(p,it);
303  }
304 
305 
306 
307  public:
308 
309 
310  //---------------------------------------------------------
311 
312  bool exists(id_type i) const {
313  return findItem(i)!=m_ids.end();
314  }
315 
316  bool isValid(id_type i) const {
317  const_IdIter p = findItem(i);
318  return p!=m_ids.end() && (*p).offset!=-1;
319  }
320 
321  /*
322  DetSet operator[](id_type i) {
323  const_IdIter p = findItem(i);
324  if (p==m_ids.end()) what???
325  return DetSet(*this,p-m_ids.begin());
326  }
327  */
328 
329 
331  const_IdIter p = findItem(i);
332  if (p==m_ids.end()) dstvdetails::throw_range(i);
333  return DetSet(*this,*p,true);
334  }
335 
336  // slow interface
337  // const_iterator find(id_type i, bool update=true) const {
338  const_iterator find(id_type i, bool update=false) const {
339  const_IdIter p = findItem(i);
340  return (p==m_ids.end()) ? end() :
341  boost::make_transform_iterator(p,
342  IterHelp(*this,update));
343  }
344 
345  // slow interface
347  std::pair<const_IdIter,const_IdIter> p =
348  std::equal_range(m_ids.begin(),m_ids.end(),Item(i));
349  return (p.first!=p.second) ? p.first : m_ids.end();
350  }
351 
352  // const_iterator begin(bool update=true) const {
353  const_iterator begin(bool update=false) const {
354  return boost::make_transform_iterator(m_ids.begin(),
355  IterHelp(*this,update));
356  }
357 
358  // const_iterator end(bool update=true) const {
359  const_iterator end(bool update=false) const {
360  return boost::make_transform_iterator(m_ids.end(),
361  IterHelp(*this,update));
362  }
363 
364 
365  // return an iterator range (implemented here to avoid dereference of detset)
366  template<typename CMP>
367  // Range equal_range(id_type i, CMP cmp, bool update=true) const {
368  Range equal_range(id_type i, CMP cmp, bool update=false) const {
369  std::pair<const_IdIter,const_IdIter> p =
370  std::equal_range(m_ids.begin(),m_ids.end(),i,cmp);
371  return Range(boost::make_transform_iterator(p.first,IterHelp(*this,update)),
372  boost::make_transform_iterator(p.second,IterHelp(*this,update))
373  );
374  }
375 
376  int subdetId() const { return m_subdetId; }
377 
378  bool empty() const { return m_ids.empty();}
379 
380 
381  size_type dataSize() const { return m_data.size(); }
382 
383  size_type size() const { return m_ids.size();}
384 
385  //FIXME fast interfaces, not consistent with associative nature of container....
386 
387  data_type operator()(size_t cell, size_t frame) const {
388  return m_data[m_ids[cell].offset+frame];
389  }
390 
391  data_type const * data(size_t cell) const {
392  return &m_data[m_ids[cell].offset];
393  }
394 
395  size_type detsetSize(size_t cell) const { return m_ids[cell].size; }
396 
397  id_type id(size_t cell) const {
398  return m_ids[cell].id;
399  }
400 
401  Item const & item(size_t cell) const {
402  return m_ids[cell];
403  }
404 
405  //------------------------------
406 
407  IdContainer const & ids() const { return m_ids;}
408  DataContainer const & data() const { return m_data;}
409 
410 
411  void update(Item const & item) const {
412  const_cast<self*>(this)->updateImpl(const_cast<Item&>(item));
413  }
414 
415  //Used by ROOT storage
417 
418  private:
419 
420  void updateImpl(Item & item);
421 
422  private:
423  // subdetector id (as returned by DetId::subdetId())
425 
426 
427  // Workaround for ROOT 6 bug.
428  // ROOT6 has a problem with this IdContainer typedef
429  //IdContainer m_ids;
430  std::vector<Trans::Item> m_ids;
432 
433  };
434 
435  namespace dslv {
436  template< typename T>
437  class LazyGetter {
438  public:
439  virtual ~LazyGetter() {}
440  virtual void fill(typename DetSetVector<T>::FastFiller&) = 0;
441  };
442  }
443 
444 
445 
446  template<typename T>
447  inline DetSetVector<T>::DetSetVector(std::shared_ptr<Getter> iGetter,
448  const std::vector<det_id_type>& iDets,
449  int isubdet):
450  m_subdetId(isubdet) {
451  getter=iGetter;
452 
453  m_ids.reserve(iDets.size());
454  det_id_type sanityCheck = 0;
455  for(std::vector<det_id_type>::const_iterator itDetId = iDets.begin(), itDetIdEnd = iDets.end();
456  itDetId != itDetIdEnd;
457  ++itDetId) {
458  assert(sanityCheck < *itDetId && "vector of det_id_type was not ordered");
459  sanityCheck = *itDetId;
460  m_ids.push_back(*itDetId);
461  }
462  }
463 
464  template<typename T>
465  inline void DetSetVector<T>::updateImpl(Item & item) {
466  // no getter or already updated
467  if (getter.empty()) assert(item.offset>=0);
468  if (item.offset!=-1 || getter.empty() ) return;
469  item.offset = int(m_data.size());
470  FastFiller ff(*this,item,true);
471  (*boost::any_cast<std::shared_ptr<Getter> >(&getter))->fill(ff);
472  }
473 
474 
475 
476 
477  template<typename T>
478  inline void DetSet<T>::set(DetSetVector<T> const & icont,
479  typename Container::Item const & item, bool update) {
480  if (update) {
481  icont.update(item);
482  assert(item.offset>=0);
483  }
484  m_id=item.id;
485  m_data=&icont.data();
486  m_offset = item.offset;
487  m_size=item.size;
488  }
489 
490 }
491 
493 #include <boost/mpl/assert.hpp>
494 #include <boost/type_traits/is_same.hpp>
495 
496 //specialize behavior of edm::Ref to get access to the 'Det'
497 namespace edm {
498  /* Reference to an item inside a new DetSetVector ... */
499  namespace refhelper {
500  template<typename T>
501  struct FindTrait<typename edmNew::DetSetVector<T>,T> {
503  };
504  }
505  /* ... as there was one for the original DetSetVector*/
506 
507  /* Probably this one is not that useful .... */
508  namespace refhelper {
509  template<typename T>
510  struct FindSetForNewDetSetVector : public std::binary_function<const edmNew::DetSetVector<T>&, unsigned int, edmNew::DetSet<T> > {
512  typename self::result_type operator()(typename self::first_argument_type iContainer, typename self::second_argument_type iIndex) {
513  return &(iContainer[iIndex]);
514  }
515  };
516 
517  template<typename T>
518  struct FindTrait<edmNew::DetSetVector<T>, edmNew::DetSet<T> > {
520  };
521  }
522  /* ... implementation is provided, just in case it's needed */
523 }
524 
525 namespace edmNew {
526  //helper function to make it easier to create a edm::Ref to a new DSV
527  template<class HandleT>
529  makeRefTo(const HandleT& iHandle,
530  typename HandleT::element_type::value_type::const_iterator itIter) {
531  BOOST_MPL_ASSERT((boost::is_same<typename HandleT::element_type, DetSetVector<typename HandleT::element_type::value_type::value_type> >));
532  typename HandleT::element_type::size_type index = (itIter - &*iHandle->data().begin());
533  return edm::Ref<typename HandleT::element_type,
535  (iHandle,index);
536  }
537 }
538 
540 
541 namespace edm {
542  template<typename T>
543  class ContainerMaskTraits<edmNew::DetSetVector<T> > {
544  public:
545  typedef T value_type;
546 
547  static size_t size(const edmNew::DetSetVector<T>* iContainer) { return iContainer->dataSize();}
548  static unsigned int indexFor(const value_type* iElement, const edmNew::DetSetVector<T>* iContainer) {
549  return iElement-&(iContainer->data().front());
550  }
551  };
552 }
553 
554 #endif
555 
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
bool isValid(id_type i) const
DataContainer const & data() const
edm::Ref< typename HandleT::element_type, typename HandleT::element_type::value_type::value_type > makeRefTo(const HandleT &iHandle, typename HandleT::element_type::value_type::const_iterator itIter)
static unsigned int indexFor(const value_type *iElement, const edmNew::DetSetVector< T > *iContainer)
int i
Definition: DBlmapReader.cc:9
void swap(DetSetVectorTrans &rh)
void push_back(data_type const &d)
void update(Item const &item) const
boost::transform_iterator< IterHelp, const_IdIter > const_iterator
const_iterator end(bool update=false) const
size_type dataSize() const
string fill
Definition: lumiContext.py:319
DetSetVector< T >::id_type id_type
Range equal_range(id_type i, CMP cmp, bool update=false) const
std::vector< data_type > DataContainer
Item & push_back(id_type iid)
const_IdIter findItem(id_type i) const
std::vector< Trans::Item > m_ids
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:34
IdContainer::iterator IdIter
unsigned int det_id_type
Definition: DetSetNew.h:10
void checkCapacityExausted(size_type s) const
void set(Container const &icont, typename Container::Item const &item, bool update=true)
assert(m_qm.get())
edmNew::DetSetVector< T >::FindForDetSetVector value
std::vector< data_type >::const_iterator const_DataIter
void swap(DetSetVector &rh)
void throw_range(det_id_type iid)
list namespace
Definition: asciidump.py:379
std::pair< const_IdIter, const_DataIter > const_IterPair
DetSetVector(int isubdet=0)
std::pair< const_iterator, const_iterator > Range
void pop_back(id_type iid)
#define CMS_CLASS_VERSION(_version_)
Definition: classes.h:31
id_type id(size_t cell) const
uint16_t size_type
dstvdetails::DetSetVectorTrans Trans
self::result_type operator()(typename self::first_argument_type iContainer, typename self::second_argument_type iIndex)
tuple d
Definition: ztail.py:151
Item const & item(size_t cell) const
DetSetVector< T > const * v
bool onDemand() const
IterHelp(DetSetVector< T > const &iv, bool iup)
Item & addItem(id_type iid, size_type isize)
DetSetVector< T >::data_type value_type
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
bool exists(id_type i) const
IdContainer const & ids() const
data_type const * data(size_t cell) const
FastFiller(DetSetVector< T > &iv, id_type id, bool isaveEmpty=false)
Item(id_type i=0, int io=-1, size_type is=0)
DetSetVector< T >::Item & item
void swap(IdContainer &iic, DataContainer &idc)
Container::value_type value_type
DetSetVector< T >::id_type key_type
static size_t size(const edmNew::DetSetVector< T > *iContainer)
void resize(size_t isize, size_t dsize)
size_type detsetSize(size_t cell) const
DetSet insert(id_type iid, data_type const *idata, size_type isize)
m_id("(unknown)")
std::vector< Item > IdContainer
friend class FindForDetSetVector
dslv::LazyGetter< T > Getter
void errorIdExists(det_id_type iid)
void reserve(size_t isize, size_t dsize)
std::vector< data_type >::iterator DataIter
result_type & operator()(Item const &item) const
const_iterator find(id_type i, bool update=false) const
#define private
Definition: FWFileEntry.h:17
DetSet insert(id_type iid, size_type isize)
size_type size() const
#define update(a, b)
std::pair< IdIter, DataIter > IterPair
void updateImpl(Item &item)
IdContainer::const_iterator const_IdIter
data_type operator()(size_t cell, size_t frame) const
self::result_type operator()(typename self::first_argument_type iContainer, typename self::second_argument_type iIndex)
edmNew::DetSet< T > DetSet
volatile std::atomic< bool > shutdown_flag false
FastFiller(DetSetVector< T > &iv, typename DetSetVector< T >::Item &it, bool isaveEmpty=false)
data_type & operator[](size_type i)
edm::refhelper::FindForNewDetSetVector< data_type > RefFinder
long double T
DetSetVector< T >::size_type size_type
DetSet operator[](id_type i) const
const_iterator begin(bool update=false) const
unsigned int size_type