Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-01 23:47:18

0001 #include "CUDADataFormats/SiPixelCluster/interface/gpuClusteringConstants.h"
0002 #include "DataFormats/Common/interface/DetSetVector.h"
0003 #include "DataFormats/Common/interface/Handle.h"
0004 #include "DataFormats/DetId/interface/DetId.h"
0005 #include "DataFormats/SiPixelCluster/interface/SiPixelCluster.h"
0006 #include "DataFormats/SiPixelDigi/interface/PixelDigi.h"
0007 #include "DataFormats/SiPixelDigi/interface/SiPixelDigisSoA.h"
0008 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0009 #include "FWCore/Framework/interface/Event.h"
0010 #include "FWCore/Framework/interface/EventSetup.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "FWCore/Framework/interface/global/EDProducer.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0017 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0018 #include "Geometry/CommonTopologies/interface/SimplePixelTopology.h"
0019 
0020 // local include(s)
0021 #include "PixelClusterizerBase.h"
0022 #include "SiPixelClusterThresholds.h"
0023 
0024 class SiPixelDigisClustersFromSoA : public edm::global::EDProducer<> {
0025 public:
0026   explicit SiPixelDigisClustersFromSoA(const edm::ParameterSet& iConfig);
0027   ~SiPixelDigisClustersFromSoA() override = default;
0028 
0029   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0030 
0031 private:
0032   void produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const override;
0033 
0034   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> topoToken_;
0035 
0036   edm::EDGetTokenT<SiPixelDigisSoA> digiGetToken_;
0037 
0038   edm::EDPutTokenT<edm::DetSetVector<PixelDigi>> digiPutToken_;
0039   edm::EDPutTokenT<SiPixelClusterCollectionNew> clusterPutToken_;
0040 
0041   const SiPixelClusterThresholds clusterThresholds_;  // Cluster threshold in electrons
0042 
0043   const bool produceDigis_;
0044   const bool storeDigis_;
0045   const bool isPhase2_;
0046 };
0047 
0048 SiPixelDigisClustersFromSoA::SiPixelDigisClustersFromSoA(const edm::ParameterSet& iConfig)
0049     : topoToken_(esConsumes()),
0050       digiGetToken_(consumes<SiPixelDigisSoA>(iConfig.getParameter<edm::InputTag>("src"))),
0051       clusterPutToken_(produces<SiPixelClusterCollectionNew>()),
0052       clusterThresholds_{iConfig.getParameter<int>("clusterThreshold_layer1"),
0053                          iConfig.getParameter<int>("clusterThreshold_otherLayers")},
0054       produceDigis_(iConfig.getParameter<bool>("produceDigis")),
0055       storeDigis_(iConfig.getParameter<bool>("produceDigis") & iConfig.getParameter<bool>("storeDigis")),
0056       isPhase2_(iConfig.getParameter<bool>("isPhase2")) {
0057   if (produceDigis_)
0058     digiPutToken_ = produces<edm::DetSetVector<PixelDigi>>();
0059 }
0060 
0061 void SiPixelDigisClustersFromSoA::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0062   edm::ParameterSetDescription desc;
0063   desc.add<edm::InputTag>("src", edm::InputTag("siPixelDigisSoA"));
0064   desc.add<int>("clusterThreshold_layer1", kSiPixelClusterThresholdsDefaultPhase1.layer1);
0065   desc.add<int>("clusterThreshold_otherLayers", kSiPixelClusterThresholdsDefaultPhase1.otherLayers);
0066   desc.add<bool>("produceDigis", true);
0067   desc.add<bool>("storeDigis", true);
0068   desc.add<bool>("isPhase2", false);
0069   descriptions.addWithDefaultLabel(desc);
0070 }
0071 
0072 void SiPixelDigisClustersFromSoA::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0073   const auto& digis = iEvent.get(digiGetToken_);
0074   const uint32_t nDigis = digis.size();
0075   const auto& ttopo = iSetup.getData(topoToken_);
0076   auto maxModules = isPhase2_ ? phase2PixelTopology::numberOfModules : phase1PixelTopology::numberOfModules;
0077   std::unique_ptr<edm::DetSetVector<PixelDigi>> collection;
0078   if (produceDigis_)
0079     collection = std::make_unique<edm::DetSetVector<PixelDigi>>();
0080   if (storeDigis_)
0081     collection->reserve(maxModules);
0082   auto outputClusters = std::make_unique<SiPixelClusterCollectionNew>();
0083   outputClusters->reserve(maxModules, nDigis / 2);
0084 
0085   edm::DetSet<PixelDigi>* detDigis = nullptr;
0086   uint32_t detId = 0;
0087   for (uint32_t i = 0; i < nDigis; i++) {
0088     // check for uninitialized digis
0089     // this is set in RawToDigi_kernel in SiPixelRawToClusterGPUKernel.cu
0090     if (digis.rawIdArr(i) == 0)
0091       continue;
0092     // check for noisy/dead pixels (electrons set to 0)
0093     if (digis.adc(i) == 0)
0094       continue;
0095 
0096     detId = digis.rawIdArr(i);
0097     if (storeDigis_) {
0098       detDigis = &collection->find_or_insert(detId);
0099       if ((*detDigis).empty())
0100         (*detDigis).data.reserve(64);  // avoid the first relocations
0101     }
0102     break;
0103   }
0104 
0105   int32_t nclus = -1;
0106   PixelClusterizerBase::AccretionCluster aclusters[gpuClustering::maxNumClustersPerModules];
0107 #ifdef EDM_ML_DEBUG
0108   auto totClustersFilled = 0;
0109 #endif
0110 
0111   auto fillClusters = [&](uint32_t detId) {
0112     if (nclus < 0)
0113       return;  // this in reality should never happen
0114     edmNew::DetSetVector<SiPixelCluster>::FastFiller spc(*outputClusters, detId);
0115     auto layer = (DetId(detId).subdetId() == 1) ? ttopo.pxbLayer(detId) : 0;
0116     auto clusterThreshold = clusterThresholds_.getThresholdForLayerOnCondition(layer == 1);
0117     for (int32_t ic = 0; ic < nclus + 1; ++ic) {
0118       auto const& acluster = aclusters[ic];
0119       // in any case we cannot  go out of sync with gpu...
0120       if (acluster.charge < clusterThreshold and !isPhase2_)
0121         edm::LogWarning("SiPixelDigisClustersFromSoA") << "cluster below charge Threshold "
0122                                                        << "Layer/DetId/clusId " << layer << '/' << detId << '/' << ic
0123                                                        << " size/charge " << acluster.isize << '/' << acluster.charge;
0124       // sort by row (x)
0125       spc.emplace_back(acluster.isize, acluster.adc, acluster.x, acluster.y, acluster.xmin, acluster.ymin, ic);
0126       aclusters[ic].clear();
0127 #ifdef EDM_ML_DEBUG
0128       ++totClustersFilled;
0129       const auto& cluster{spc.back()};
0130       LogDebug("SiPixelDigisClustersFromSoA")
0131           << "putting in this cluster " << ic << " " << cluster.charge() << " " << cluster.pixelADC().size();
0132 #endif
0133       std::push_heap(spc.begin(), spc.end(), [](SiPixelCluster const& cl1, SiPixelCluster const& cl2) {
0134         return cl1.minPixelRow() < cl2.minPixelRow();
0135       });
0136     }
0137     nclus = -1;
0138     // sort by row (x)
0139     std::sort_heap(spc.begin(), spc.end(), [](SiPixelCluster const& cl1, SiPixelCluster const& cl2) {
0140       return cl1.minPixelRow() < cl2.minPixelRow();
0141     });
0142     if (spc.empty())
0143       spc.abort();
0144   };
0145 
0146   for (uint32_t i = 0; i < nDigis; i++) {
0147     // check for uninitialized digis
0148     if (digis.rawIdArr(i) == 0)
0149       continue;
0150     // check for noisy/dead pixels (electrons set to 0)
0151     if (digis.adc(i) == 0)
0152       continue;
0153     if (digis.clus(i) > 9000)
0154       continue;  // not in cluster; TODO add an assert for the size
0155 #ifdef EDM_ML_DEBUG
0156     assert(digis.rawIdArr(i) > 109999);
0157 #endif
0158     if (detId != digis.rawIdArr(i)) {
0159       // new module
0160       fillClusters(detId);
0161 #ifdef EDM_ML_DEBUG
0162       assert(nclus == -1);
0163 #endif
0164       detId = digis.rawIdArr(i);
0165       if (storeDigis_) {
0166         detDigis = &collection->find_or_insert(detId);
0167         if ((*detDigis).empty())
0168           (*detDigis).data.reserve(64);  // avoid the first relocations
0169         else {
0170           edm::LogWarning("SiPixelDigisClustersFromSoA")
0171               << "Problem det present twice in input! " << (*detDigis).detId();
0172         }
0173       }
0174     }
0175     PixelDigi dig(digis.pdigi(i));
0176     if (storeDigis_)
0177       (*detDigis).data.emplace_back(dig);
0178       // fill clusters
0179 #ifdef EDM_ML_DEBUG
0180     assert(digis.clus(i) >= 0);
0181     assert(digis.clus(i) < gpuClustering::maxNumClustersPerModules);
0182 #endif
0183     nclus = std::max(digis.clus(i), nclus);
0184     auto row = dig.row();
0185     auto col = dig.column();
0186     SiPixelCluster::PixelPos pix(row, col);
0187     aclusters[digis.clus(i)].add(pix, digis.adc(i));
0188   }
0189 
0190   // fill final clusters
0191   if (detId > 0)
0192     fillClusters(detId);
0193 
0194 #ifdef EDM_ML_DEBUG
0195   LogDebug("SiPixelDigisClustersFromSoA") << "filled " << totClustersFilled << " clusters";
0196 #endif
0197 
0198   if (produceDigis_)
0199     iEvent.put(digiPutToken_, std::move(collection));
0200   iEvent.put(clusterPutToken_, std::move(outputClusters));
0201 }
0202 
0203 DEFINE_FWK_MODULE(SiPixelDigisClustersFromSoA);