CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SiPixelDigiMorphing.cc
Go to the documentation of this file.
12 
13 #include <bitset>
14 #include <iterator>
15 
17 public:
18  explicit SiPixelDigiMorphing(const edm::ParameterSet& conf);
19  ~SiPixelDigiMorphing() override = default;
20 
21  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
22  void produce(edm::Event& e, const edm::EventSetup& c) override;
23 
24 private:
27 
28  const int32_t nrows_;
29  const int32_t ncols_;
30  const int32_t nrocs_; // in Phase 1, this is ROCs, but could be any subset of a pixel row
31  const int32_t iters_;
32  const uint32_t fakeAdc_;
33 
34  int32_t ncols_r_; // number of columns per ROC
35  int32_t ksize_; // kernel size
36 
37  std::vector<uint64_t> kernel1_;
38  std::vector<uint64_t> kernel2_;
40 
42 
43  void morph(uint64_t* const imap, uint64_t* omap, uint64_t* const kernel, MorphOption op) const;
44 };
45 
47  : tPixelDigi_(consumes(conf.getParameter<edm::InputTag>("src"))),
48  tPutPixelDigi_(produces<edm::DetSetVector<PixelDigi>>()),
49  nrows_(conf.getParameter<int32_t>("nrows")),
50  ncols_(conf.getParameter<int32_t>("ncols")),
51  nrocs_(conf.getParameter<int32_t>("nrocs")),
52  iters_(conf.getParameter<int32_t>("iters")),
53  fakeAdc_(conf.getParameter<uint32_t>("fakeAdc")) {
54  if (ncols_ % nrocs_ == 0) {
56  } else {
57  throw cms::Exception("Configuration") << "[SiPixelDigiMorphing]:"
58  << " number of columns not divisible with"
59  << " number of ROCs\n";
60  }
61 
62  if (ncols_r_ + 2 * iters_ <= int(sizeof(uint64_t) * 8)) {
63  ksize_ = 2 * iters_ + 1;
64  } else {
65  throw cms::Exception("Configuration") << "[SiPixelDigiMorphing]:"
66  << " too many columns per ROC"
67  << " or too many iterations set\n"
68  << " Ncol/Nrocs+2*iters should not be"
69  << " more than " << sizeof(uint64_t) * 8 << "\n";
70  }
71 
72  std::vector<int32_t> k1(conf.getParameter<std::vector<int32_t>>("kernel1"));
73  std::vector<int32_t> k2(conf.getParameter<std::vector<int32_t>>("kernel2"));
74 
75  kernel1_.resize(ksize_, 0);
76  kernel2_.resize(ksize_, 0);
77  mask_ = 0;
78  int w = (ncols_r_ + 2 * iters_) / ksize_ + ((ncols_r_ + 2 * iters_) % ksize_ != 0);
79  for (int j = 0; j < w; j++) {
80  for (int ii = 0; ii < ksize_; ii++) {
81  kernel1_[ii] <<= ksize_;
82  kernel1_[ii] |= k1[ii];
83  kernel2_[ii] <<= ksize_;
84  kernel2_[ii] |= k2[ii];
85  }
86  mask_ <<= ksize_;
87  mask_ |= 1;
88  }
89  mask_ <<= iters_;
90 }
91 
94 
95  desc.add<edm::InputTag>("src", edm::InputTag("siPixelDigis"));
96  desc.add<int32_t>("nrows", 160);
97  desc.add<int32_t>("ncols", 416);
98  desc.add<int32_t>("nrocs", 8);
99  desc.add<int32_t>("iters", 1);
100  desc.add<std::vector<int32_t>>("kernel1", {7, 7, 7});
101  desc.add<std::vector<int32_t>>("kernel2", {2, 7, 2});
102  desc.add<uint32_t>("fakeAdc", 100);
103 
104  descriptions.addWithDefaultLabel(desc);
105 }
106 
108  auto const& inputDigi = e.get(tPixelDigi_);
109 
110  auto outputDigis = std::make_unique<edm::DetSetVector<PixelDigi>>();
111 
112  const int rocSize = nrows_ + 2 * iters_;
113  const int arrSize = nrocs_ * rocSize;
114 
115  uint64_t imap[arrSize];
116  uint64_t map1[arrSize];
117  uint64_t map2[arrSize];
118 
119  for (auto const& ds : inputDigi) {
120  auto rawId = ds.detId();
121  edm::DetSet<PixelDigi>* detDigis = nullptr;
122  detDigis = &(outputDigis->find_or_insert(rawId));
123 
124  memset(imap, 0, arrSize * sizeof(uint64_t));
125  for (auto const& di : ds) {
126  int r = int(di.column()) / ncols_r_;
127  int c = int(di.column()) % ncols_r_;
128  imap[r * rocSize + di.row() + iters_] |= uint64_t(1) << (c + iters_);
129  if (r > 0 && c < iters_) {
130  imap[(r - 1) * rocSize + di.row() + iters_] |= uint64_t(1) << (c + ncols_r_ + iters_);
131  } else if (++r < nrocs_ && c >= ncols_r_ - iters_) {
132  imap[r * rocSize + di.row() + iters_] |= uint64_t(1) << (c - ncols_r_ + iters_);
133  }
134  (*detDigis).data.emplace_back(di.row(), di.column(), di.adc(), 0);
135  }
136 
137  std::memcpy(map1, imap, arrSize * sizeof(uint64_t));
138  memset(map2, 0, arrSize * sizeof(uint64_t));
139 
140  morph(map1, map2, kernel1_.data(), kDilate);
141  morph(map2, map1, kernel2_.data(), kErode);
142 
143  uint64_t* i = imap + iters_;
144  uint64_t* o = map1 + iters_;
145  for (int roc = 0; roc < nrocs_; roc++, i += 2 * iters_, o += 2 * iters_) {
146  for (int row = 0; row < nrows_; row++, i++, o++) {
147  if (*o == 0)
148  continue;
149  *o >>= iters_;
150  *i >>= iters_;
151  for (int col = 0; col < ncols_r_; col++, (*i) >>= 1, (*o) >>= 1) {
152  if (*o == 0)
153  break;
154  if (((*i) & uint64_t(1)) == 1)
155  continue;
156  if (((*o) & uint64_t(1)) == 1) {
157  (*detDigis).data.emplace_back(row, roc * ncols_r_ + col, fakeAdc_, 1);
158  }
159  }
160  }
161  }
162  }
163  e.put(tPutPixelDigi_, std::move(outputDigis));
164 }
165 
166 void SiPixelDigiMorphing::morph(uint64_t* const imap, uint64_t* omap, uint64_t* const kernel, MorphOption op) const {
167  uint64_t* i[ksize_]; // i(nput)
168  uint64_t* o = omap + iters_; // o(output)
169  unsigned char valid = 0;
170  unsigned char const validMask = (1 << ksize_) - 1;
171  uint64_t m[ksize_]; // m(ask)
172 
173  for (int ii = 0; ii < ksize_; ii++) {
174  i[ii] = imap + ii;
175  valid = (valid << 1) | (*i[ii] != 0);
176  m[ii] = mask_ << ii;
177  }
178 
179  for (int roc = 0; roc < nrocs_; roc++, o += 2 * iters_) {
180  for (int row = 0; row < nrows_; row++, o++) {
181  if ((valid & validMask) != 0) {
182  for (int jj = 0; jj < ksize_; jj++) {
183  for (int ii = 0; ii < ksize_; ii++) {
184  uint64_t v = (*i[ii]) & (kernel[ii] << jj); // v(ector)
185  if (op == kErode)
186  v ^= (kernel[ii] << jj);
187  uint64_t vv = v; // vv(vector bit - shifted and contracted)
188  for (int b = 1; b < ksize_; b++)
189  vv |= (v >> b);
190  *o |= ((vv << iters_) & m[jj]);
191  }
192  if (op == kErode)
193  *o ^= m[jj];
194  }
195  }
196  for (int ii = 0; ii < ksize_; ii++)
197  i[ii]++;
198  valid = (valid << 1) | (*i[ksize_ - 1] != 0);
199  }
200  for (int ii = 0; ii < ksize_; ii++) {
201  i[ii] += 2 * iters_;
202  valid = (valid << 1) | (*i[ii] != 0);
203  }
204  }
205 }
206 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
std::vector< uint64_t > kernel2_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
const edm::EventSetup & c
SiPixelDigiMorphing(const edm::ParameterSet &conf)
void produce(edm::Event &e, const edm::EventSetup &c) override
~SiPixelDigiMorphing() override=default
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int ii
Definition: cuy.py:589
edm::EDPutTokenT< edm::DetSetVector< PixelDigi > > tPutPixelDigi_
def move
Definition: eostools.py:511
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
edm::EDGetTokenT< edm::DetSetVector< PixelDigi > > tPixelDigi_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
unsigned long long uint64_t
Definition: Time.h:13
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
double b
Definition: hdecay.h:118
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< uint64_t > kernel1_
T w() const
void morph(uint64_t *const imap, uint64_t *omap, uint64_t *const kernel, MorphOption op) const
int col
Definition: cuy.py:1009