CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiStripDetSetVectorFiller.h
Go to the documentation of this file.
1 #ifndef EventFilter_SiStripRawToDigi_SiStripDetSetVectorFiller_H
2 #define EventFilter_SiStripRawToDigi_SiStripDetSetVectorFiller_H
3 
5 #include "boost/cstdint.hpp"
6 #include <vector>
7 #include <algorithm>
8 #include <memory>
9 class SiStripRawDigi;
10 class SiStripDigi;
11 
12 namespace sistrip {
13 
14  //A class to fill a DetSetVector from data from different channels only sorting once
15 
16  //T is type of object in DetSetVector (SiStripRawDigi, SiStripDigi), dsvIsSparse should be false for raw digis so null digis are inserted
17  template<typename T, bool dsvIsSparse> class DetSetVectorFiller
18  {
19  public:
20  DetSetVectorFiller(const size_t registrySize, const size_t dataSize);
22 
23  void newChannel(const uint32_t key, const uint16_t firstItem = 0);
24  void addItem(const T& item);
25  std::auto_ptr< edm::DetSetVector<T> > createDetSetVector();
26  private:
28  {
29  ChannelRegistryItem(const uint32_t theKey, const uint32_t theStartIndex, const uint16_t theLength, const uint16_t theFirstItem)
30  : key(theKey), startIndex(theStartIndex), length(theLength), firstItem(theFirstItem) {}
31  bool operator < (const ChannelRegistryItem& other) const
32  { return ( (this->key != other.key) ? (this->key < other.key) : (this->firstItem < other.firstItem) ); }
33  uint32_t key; //ID of DetSet in DetSetVector
34  uint32_t startIndex; //index of first item in data container
35  uint16_t length; //number of items
36  uint16_t firstItem; //index of first item in final DetSet
37  };
38  typedef std::vector<ChannelRegistryItem> Registry;
39  typedef std::vector<T> Data;
40 
43  };
44 
45  template<typename T, bool dsvIsSparse> inline DetSetVectorFiller<T,dsvIsSparse>::DetSetVectorFiller(const size_t registrySize, const size_t dataSize)
46  {
47  registry_.reserve(registrySize);
48  data_.reserve(dataSize);
49  }
50 
51  template<typename T, bool dsvIsSparse> inline DetSetVectorFiller<T,dsvIsSparse>::~DetSetVectorFiller()
52  {}
53 
54  template<typename T, bool dsvIsSparse> inline void DetSetVectorFiller<T,dsvIsSparse>::newChannel(const uint32_t key, const uint16_t firstItem)
55  {
56  registry_.push_back( ChannelRegistryItem(key,data_.size(),0,firstItem) );
57  }
58 
59  template<typename T, bool dsvIsSparse> inline void DetSetVectorFiller<T,dsvIsSparse>::addItem(const T& item)
60  {
61  data_.push_back(item);
62  registry_.back().length++;
63  }
64 
65  template<typename T, bool dsvIsSparse> std::auto_ptr< edm::DetSetVector<T> > DetSetVectorFiller<T,dsvIsSparse>::createDetSetVector()
66  {
67  std::sort(registry_.begin(),registry_.end());
68  std::vector< edm::DetSet<T> > sorted_and_merged;
69  sorted_and_merged.reserve(registry_.size());
70  typename Registry::const_iterator iReg = registry_.begin();
71  const typename Registry::const_iterator endReg = registry_.end();
72  while (iReg != endReg) {
73  sorted_and_merged.push_back( edm::DetSet<T>(iReg->key) );
74  std::vector<T>& detSetData = sorted_and_merged.back().data;
75  typename Registry::const_iterator jReg;
76  if (dsvIsSparse) {
77  uint16_t length = 0;
78  for (jReg = iReg; (jReg != endReg) && (jReg->key == iReg->key); ++jReg) length += jReg->length;
79  detSetData.reserve(length);
80  for (jReg = iReg; (jReg != endReg) && (jReg->key == iReg->key); ++jReg) {
81  detSetData.insert(detSetData.end(), data_.begin()+jReg->startIndex, data_.begin()+jReg->startIndex+jReg->length);
82  }
83  } else {
84  uint16_t detLength = 0;
85  uint16_t firstItemOfLastChannel = 0;
86  for (jReg = iReg; (jReg != endReg) && (jReg->key == iReg->key); ++jReg) {
87  if (!detLength) detLength = jReg->length;
88  else if (detLength != jReg->length) throw cms::Exception("DetSetVectorFiller") << "Cannot fill non-sparse DetSet if channels are not unformly sized.";
89  firstItemOfLastChannel = jReg->firstItem;
90  }
91  detSetData.resize(firstItemOfLastChannel+detLength);
92  for (jReg = iReg; (jReg != endReg) && (jReg->key == iReg->key); ++jReg) {
93  std::copy(data_.begin()+jReg->startIndex, data_.begin()+jReg->startIndex+jReg->length, detSetData.begin()+jReg->firstItem);
94  }
95  }
96  iReg = jReg;
97  }
98  return typename std::auto_ptr< edm::DetSetVector<T> >( new edm::DetSetVector<T>(sorted_and_merged, true) );
99  }
100 
103 }
104 
105 #endif // EventFilter_SiStripRawToDigi_SiStripDetSetVectorFiller_H
DetSetVectorFiller< SiStripDigi, true > ZSDigiDetSetVectorFiller
void newChannel(const uint32_t key, const uint16_t firstItem=0)
DetSetVectorFiller(const size_t registrySize, const size_t dataSize)
DetSetVectorFiller< SiStripRawDigi, false > RawDigiDetSetVectorFiller
std::auto_ptr< edm::DetSetVector< T > > createDetSetVector()
A Digi for the silicon strip detector, containing both strip and adc information, and suitable for st...
Definition: SiStripDigi.h:12
std::vector< ChannelRegistryItem > Registry
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
bool operator<(const ChannelRegistryItem &other) const
ChannelRegistryItem(const uint32_t theKey, const uint32_t theStartIndex, const uint16_t theLength, const uint16_t theFirstItem)
A Digi for the silicon strip detector, containing only adc information, and suitable for storing raw ...
long double T