CMS 3D CMS Logo

SiPixelRecHitSoAFromLegacy.cc
Go to the documentation of this file.
1 #include <cuda_runtime.h>
2 
27 
28 #include "gpuPixelRecHits.h"
29 
31 public:
32  explicit SiPixelRecHitSoAFromLegacy(const edm::ParameterSet& iConfig);
33  ~SiPixelRecHitSoAFromLegacy() override = default;
34 
35  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
36 
37  using HitModuleStart = std::array<uint32_t, gpuClustering::maxNumModules + 1>;
39 
40 private:
41  void produce(edm::StreamID streamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
42 
49  const bool convert2Legacy_;
50  const bool isPhase2_;
51 };
52 
54  : geomToken_(esConsumes()),
55  cpeToken_(esConsumes(edm::ESInputTag("", iConfig.getParameter<std::string>("CPE")))),
56  bsGetToken_{consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"))},
57  clusterToken_{consumes<SiPixelClusterCollectionNew>(iConfig.getParameter<edm::InputTag>("src"))},
58  tokenHit_{produces<TrackingRecHit2DCPU>()},
59  tokenModuleStart_{produces<HMSstorage>()},
60  convert2Legacy_(iConfig.getParameter<bool>("convertToLegacy")),
61  isPhase2_(iConfig.getParameter<bool>("isPhase2")) {
62  if (convert2Legacy_)
63  produces<SiPixelRecHitCollectionNew>();
64 }
65 
68 
69  desc.add<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot"));
70  desc.add<edm::InputTag>("src", edm::InputTag("siPixelClustersPreSplitting"));
71  desc.add<std::string>("CPE", "PixelCPEFast");
72  desc.add<bool>("convertToLegacy", false);
73  desc.add<bool>("isPhase2", false);
74  descriptions.addWithDefaultLabel(desc);
75 }
76 
78  const TrackerGeometry* geom_ = &es.getData(geomToken_);
79  PixelCPEFast const* fcpe = dynamic_cast<const PixelCPEFast*>(&es.getData(cpeToken_));
80  if (not fcpe) {
81  throw cms::Exception("Configuration") << "SiPixelRecHitSoAFromLegacy can only use a CPE of type PixelCPEFast";
82  }
83  auto const& cpeView = fcpe->getCPUProduct();
84 
85  const reco::BeamSpot& bs = iEvent.get(bsGetToken_);
86 
87  BeamSpotPOD bsHost;
88  bsHost.x = bs.x0();
89  bsHost.y = bs.y0();
90  bsHost.z = bs.z0();
91 
93  iEvent.getByToken(clusterToken_, hclusters);
94  auto const& input = *hclusters;
95 
98 
101 
102  // allocate a buffer for the indices of the clusters
103  auto hmsp = std::make_unique<uint32_t[]>(nMaxModules + 1);
104  // hitsModuleStart is a non-owning pointer to the buffer
105  auto hitsModuleStart = hmsp.get();
106  // wrap the buffer in a HostProduct
107  auto hms = std::make_unique<HMSstorage>(std::move(hmsp));
108  // move the HostProduct to the Event, without reallocating the buffer or affecting hitsModuleStart
110 
111  // legacy output
112  auto legacyOutput = std::make_unique<SiPixelRecHitCollectionNew>();
113 
114  // storage
115  std::vector<uint16_t> xx;
116  std::vector<uint16_t> yy;
117  std::vector<uint16_t> adc;
118  std::vector<uint16_t> moduleInd;
119  std::vector<int32_t> clus;
120 
121  std::vector<edm::Ref<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster>> clusterRef;
122 
123  constexpr uint32_t maxHitsInModule = gpuClustering::maxHitsInModule();
124 
125  HitModuleStart moduleStart_; // index of the first pixel of each module
126  HitModuleStart clusInModule_;
127  memset(&clusInModule_, 0, sizeof(HitModuleStart)); // needed??
128  memset(&moduleStart_, 0, sizeof(HitModuleStart));
129  assert(gpuClustering::maxNumModules + 1 == clusInModule_.size());
130  assert(0 == clusInModule_[gpuClustering::maxNumModules]);
131  uint32_t moduleId_;
132  moduleStart_[1] = 0; // we run sequentially....
133 
135  moduleStart_.data(), clusInModule_.data(), &moduleId_, hitsModuleStart};
136 
137  // fill cluster arrays
138  int numberOfClusters = 0;
139  for (auto const& dsv : input) {
140  unsigned int detid = dsv.detId();
141  DetId detIdObject(detid);
142  const GeomDetUnit* genericDet = geom_->idToDetUnit(detIdObject);
143  auto gind = genericDet->index();
144  assert(gind < nMaxModules);
145  auto const nclus = dsv.size();
146  clusInModule_[gind] = nclus;
147  numberOfClusters += nclus;
148  }
149  hitsModuleStart[0] = 0;
150 
151  for (int i = 1, n = nMaxModules + 1; i < n; ++i)
152  hitsModuleStart[i] = hitsModuleStart[i - 1] + clusInModule_[i - 1];
153 
154  assert(numberOfClusters == int(hitsModuleStart[nMaxModules]));
155 
156  // output SoA
157  // element 96 is the start of BPIX2 (i.e. the number of clusters in BPIX1)
158 
159  auto output = std::make_unique<TrackingRecHit2DCPU>(
160  numberOfClusters, isPhase2_, hitsModuleStart[startBPIX2], &cpeView, hitsModuleStart, nullptr);
161  assert(output->nMaxModules() == uint32_t(nMaxModules));
162 
163  if (0 == numberOfClusters) {
164  iEvent.put(std::move(output));
165  if (convert2Legacy_)
166  iEvent.put(std::move(legacyOutput));
167  return;
168  }
169 
170  if (convert2Legacy_)
171  legacyOutput->reserve(nMaxModules, numberOfClusters);
172 
173  int numberOfDetUnits = 0;
174  int numberOfHits = 0;
175  for (auto const& dsv : input) {
176  numberOfDetUnits++;
177  unsigned int detid = dsv.detId();
178  DetId detIdObject(detid);
179  const GeomDetUnit* genericDet = geom_->idToDetUnit(detIdObject);
180  auto const gind = genericDet->index();
181  assert(gind < nMaxModules);
182  const PixelGeomDetUnit* pixDet = dynamic_cast<const PixelGeomDetUnit*>(genericDet);
183  assert(pixDet);
184  auto const nclus = dsv.size();
185  assert(clusInModule_[gind] == nclus);
186  if (0 == nclus)
187  continue; // is this really possible?
188 
189  auto const fc = hitsModuleStart[gind];
190  auto const lc = hitsModuleStart[gind + 1];
191  assert(lc > fc);
192  LogDebug("SiPixelRecHitSoAFromLegacy") << "in det " << gind << ": conv " << nclus << " hits from " << dsv.size()
193  << " legacy clusters" << ' ' << fc << ',' << lc;
194  assert((lc - fc) == nclus);
195  if (nclus > maxHitsInModule)
196  printf(
197  "WARNING: too many clusters %d in Module %d. Only first %d Hits converted\n", nclus, gind, maxHitsInModule);
198 
199  // fill digis
200  xx.clear();
201  yy.clear();
202  adc.clear();
203  moduleInd.clear();
204  clus.clear();
205  clusterRef.clear();
206  moduleId_ = gind;
207  uint32_t ic = 0;
208  uint32_t ndigi = 0;
209  for (auto const& clust : dsv) {
210  assert(clust.size() > 0);
211  for (int i = 0, nd = clust.size(); i < nd; ++i) {
212  auto px = clust.pixel(i);
213  xx.push_back(px.x);
214  yy.push_back(px.y);
215  adc.push_back(px.adc);
216  moduleInd.push_back(gind);
217  clus.push_back(ic);
218  ++ndigi;
219  }
220 
221  if (convert2Legacy_)
222  clusterRef.emplace_back(edmNew::makeRefTo(hclusters, &clust));
223  ic++;
224  }
225  assert(nclus == ic);
226  assert(clus.size() == ndigi);
227  numberOfHits += nclus;
228  // filled creates view
229  SiPixelDigisCUDASOAView digiView;
230  digiView.xx_ = xx.data();
231  digiView.yy_ = yy.data();
232  digiView.adc_ = adc.data();
233  digiView.moduleInd_ = moduleInd.data();
234  digiView.clus_ = clus.data();
235  digiView.pdigi_ = nullptr;
236  digiView.rawIdArr_ = nullptr;
237  assert(digiView.adc(0) != 0);
238  // we run on blockId.x==0
239  gpuPixelRecHits::getHits(&cpeView, &bsHost, digiView, ndigi, &clusterView, output->view());
240  for (auto h = fc; h < lc; ++h)
241  if (h - fc < maxHitsInModule)
242  assert(gind == output->view()->detectorIndex(h));
243  else
244  assert(gpuClustering::invalidModuleId == output->view()->detectorIndex(h));
245  if (convert2Legacy_) {
246  SiPixelRecHitCollectionNew::FastFiller recHitsOnDetUnit(*legacyOutput, detid);
247  for (auto h = fc; h < lc; ++h) {
248  auto ih = h - fc;
249 
250  if (ih >= maxHitsInModule)
251  break;
252  assert(ih < clusterRef.size());
253  LocalPoint lp(output->view()->xLocal(h), output->view()->yLocal(h));
254  LocalError le(output->view()->xerrLocal(h), 0, output->view()->yerrLocal(h));
256  SiPixelRecHit hit(lp, le, rqw, *genericDet, clusterRef[ih]);
257  recHitsOnDetUnit.push_back(hit);
258  }
259  }
260  }
261 
262  assert(numberOfHits == numberOfClusters);
263 
264  // fill data structure to support CA
266  for (auto i = 0U; i < nLayers + 1; ++i) {
267  output->hitsLayerStart()[i] = hitsModuleStart[cpeView.layerGeometry().layerStart[i]];
268  LogDebug("SiPixelRecHitSoAFromLegacy")
269  << "Layer n." << i << " - starting at module: " << cpeView.layerGeometry().layerStart[i]
270  << " - starts ad cluster: " << output->hitsLayerStart()[i] << "\n";
271  }
272 
273  cms::cuda::fillManyFromVector(output->phiBinner(),
274  nLayers,
275  output->iphi(),
276  output->hitsLayerStart(),
277  numberOfHits,
278  256,
279  output->phiBinnerStorage());
280 
281  LogDebug("SiPixelRecHitSoAFromLegacy") << "created HitSoa for " << numberOfClusters << " clusters in "
282  << numberOfDetUnits << " Dets";
283  iEvent.put(std::move(output));
284  if (convert2Legacy_)
285  iEvent.put(std::move(legacyOutput));
286 }
287 
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)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void push_back(data_type const &d)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
constexpr int nMaxModules
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
const TrackerGeomDet * idToDetUnit(DetId) const override
Return the pointer to the GeomDetUnit corresponding to a given DetId.
constexpr uint32_t numberOfLayers
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int index() const
Definition: GeomDet.h:83
constexpr uint32_t layerStart[numberOfLayers+1]
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
constexpr uint32_t maxHitsInModule()
const edm::EDGetTokenT< reco::BeamSpot > bsGetToken_
constexpr uint32_t numberOfModules
assert(be >=bs)
~SiPixelRecHitSoAFromLegacy() override=default
static std::string const input
Definition: EdmProvDump.cc:47
int iEvent
Definition: GenABIO.cc:224
constexpr int startBPIX2
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > geomToken_
constexpr uint16_t maxNumModules
bool getData(T &iHolder) const
Definition: EventSetup.h:122
const uint16_t * adc() const
constexpr uint16_t invalidModuleId
Definition: DetId.h:17
constexpr uint32_t numberOfModules
std::array< uint32_t, gpuClustering::maxNumModules+1 > HitModuleStart
const edm::ESGetToken< PixelClusterParameterEstimator, TkPixelCPERecord > cpeToken_
constexpr uint8_t numberOfLayers
Pixel cluster – collection of neighboring pixels above threshold.
HLT enums.
const edm::EDPutTokenT< HMSstorage > tokenModuleStart_
constexpr uint32_t layerStart[numberOfLayers+1]
const edm::EDPutTokenT< TrackingRecHit2DCPU > tokenHit_
const edm::EDGetTokenT< SiPixelClusterCollectionNew > clusterToken_
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
void produce(edm::StreamID streamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
SiPixelRecHitSoAFromLegacy(const edm::ParameterSet &iConfig)
def move(src, dest)
Definition: eostools.py:511
uint16_t *__restrict__ uint16_t const *__restrict__ adc
Our base class.
Definition: SiPixelRecHit.h:23
#define LogDebug(id)