Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-11 23:28:09

0001 #include <memory>
0002 #include <stdexcept>
0003 #include <string>
0004 #include <utility>
0005 #include <vector>
0006 
0007 #include "CalibTracker/Records/interface/SiPixelGainCalibrationForHLTSoARcd.h"
0008 #include "CalibTracker/Records/interface/SiPixelMappingSoARecord.h"
0009 #include "CondFormats/DataRecord/interface/SiPixelFedCablingMapRcd.h"
0010 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingMap.h"
0011 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingTree.h"
0012 #include "CondFormats/SiPixelObjects/interface/alpaka/SiPixelGainCalibrationForHLTDevice.h"
0013 #include "CondFormats/SiPixelObjects/interface/alpaka/SiPixelMappingDevice.h"
0014 #include "CondFormats/SiPixelObjects/interface/alpaka/SiPixelMappingUtilities.h"
0015 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0016 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0017 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0018 #include "DataFormats/SiPixelClusterSoA/interface/alpaka/SiPixelClustersSoACollection.h"
0019 #include "DataFormats/SiPixelDigiSoA/interface/alpaka/SiPixelDigiErrorsSoACollection.h"
0020 #include "DataFormats/SiPixelDigiSoA/interface/alpaka/SiPixelDigisSoACollection.h"
0021 #include "DataFormats/SiPixelRawData/interface/SiPixelFormatterErrors.h"
0022 #include "EventFilter/SiPixelRawToDigi/interface/PixelDataFormatter.h"
0023 #include "EventFilter/SiPixelRawToDigi/interface/PixelUnpackingRegions.h"
0024 #include "FWCore/Framework/interface/ESWatcher.h"
0025 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0026 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0029 #include "FWCore/Utilities/interface/ESGetToken.h"
0030 #include "FWCore/Utilities/interface/InputTag.h"
0031 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/EDPutToken.h"
0032 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/ESGetToken.h"
0033 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/Event.h"
0034 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/stream/SynchronizingEDProducer.h"
0035 #include "HeterogeneousCore/AlpakaInterface/interface/config.h"
0036 #include "RecoLocalTracker/SiPixelClusterizer/interface/SiPixelClusterThresholds.h"
0037 
0038 #include "SiPixelRawToClusterKernel.h"
0039 
0040 namespace ALPAKA_ACCELERATOR_NAMESPACE {
0041 
0042   template <typename TrackerTraits>
0043   class SiPixelRawToCluster : public stream::SynchronizingEDProducer<> {
0044   public:
0045     explicit SiPixelRawToCluster(const edm::ParameterSet& iConfig);
0046     ~SiPixelRawToCluster() override = default;
0047 
0048     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0049     using Algo = pixelDetails::SiPixelRawToClusterKernel<TrackerTraits>;
0050 
0051   private:
0052     void acquire(device::Event const& iEvent, device::EventSetup const& iSetup) override;
0053     void produce(device::Event& iEvent, device::EventSetup const& iSetup) override;
0054 
0055     edm::EDGetTokenT<FEDRawDataCollection> rawGetToken_;
0056     edm::EDPutTokenT<SiPixelFormatterErrors> fmtErrorToken_;
0057     device::EDPutToken<SiPixelDigisSoACollection> digiPutToken_;
0058     device::EDPutToken<SiPixelDigiErrorsSoACollection> digiErrorPutToken_;
0059     device::EDPutToken<SiPixelClustersSoACollection> clusterPutToken_;
0060 
0061     edm::ESWatcher<SiPixelFedCablingMapRcd> recordWatcher_;
0062     const device::ESGetToken<SiPixelMappingDevice, SiPixelMappingSoARecord> mapToken_;
0063     const device::ESGetToken<SiPixelGainCalibrationForHLTDevice, SiPixelGainCalibrationForHLTSoARcd> gainsToken_;
0064     const edm::ESGetToken<SiPixelFedCablingMap, SiPixelFedCablingMapRcd> cablingMapToken_;
0065 
0066     std::unique_ptr<SiPixelFedCablingTree> cabling_;
0067     std::vector<unsigned int> fedIds_;
0068     const SiPixelFedCablingMap* cablingMap_ = nullptr;
0069     std::unique_ptr<PixelUnpackingRegions> regions_;
0070 
0071     Algo Algo_;
0072     PixelDataFormatter::Errors errors_;
0073 
0074     const bool includeErrors_;
0075     const bool useQuality_;
0076     uint32_t nDigis_;
0077     const SiPixelClusterThresholds clusterThresholds_;
0078   };
0079 
0080   template <typename TrackerTraits>
0081   SiPixelRawToCluster<TrackerTraits>::SiPixelRawToCluster(const edm::ParameterSet& iConfig)
0082       : rawGetToken_(consumes(iConfig.getParameter<edm::InputTag>("InputLabel"))),
0083         digiPutToken_(produces()),
0084         clusterPutToken_(produces()),
0085         mapToken_(esConsumes()),
0086         gainsToken_(esConsumes()),
0087         cablingMapToken_(esConsumes<SiPixelFedCablingMap, SiPixelFedCablingMapRcd>(
0088             edm::ESInputTag("", iConfig.getParameter<std::string>("CablingMapLabel")))),
0089         includeErrors_(iConfig.getParameter<bool>("IncludeErrors")),
0090         useQuality_(iConfig.getParameter<bool>("UseQualityInfo")),
0091         clusterThresholds_{iConfig.getParameter<int32_t>("clusterThreshold_layer1"),
0092                            iConfig.getParameter<int32_t>("clusterThreshold_otherLayers"),
0093                            static_cast<float>(iConfig.getParameter<double>("VCaltoElectronGain")),
0094                            static_cast<float>(iConfig.getParameter<double>("VCaltoElectronGain_L1")),
0095                            static_cast<float>(iConfig.getParameter<double>("VCaltoElectronOffset")),
0096                            static_cast<float>(iConfig.getParameter<double>("VCaltoElectronOffset_L1"))} {
0097     // Workaround until the ProductID problem in issue https://github.com/cms-sw/cmssw/issues/44643 is fixed
0098 #ifdef ALPAKA_ACC_CPU_B_SEQ_T_SEQ_ENABLED
0099     producesTemporarily("edm::DeviceProduct<alpaka_cuda_async::SiPixelDigisSoACollection>");
0100     producesTemporarily("edm::DeviceProduct<alpaka_cuda_async::SiPixelDigiErrorsSoACollection>");
0101     producesTemporarily("edm::DeviceProduct<alpaka_cuda_async::SiPixelClustersSoACollection>");
0102 #endif
0103 
0104     if (includeErrors_) {
0105       digiErrorPutToken_ = produces();
0106       fmtErrorToken_ = produces();
0107     }
0108 
0109     // regions
0110     if (!iConfig.getParameter<edm::ParameterSet>("Regions").getParameterNames().empty()) {
0111       regions_ = std::make_unique<PixelUnpackingRegions>(iConfig, consumesCollector());
0112     }
0113   }
0114 
0115   template <typename TrackerTraits>
0116   void SiPixelRawToCluster<TrackerTraits>::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0117     edm::ParameterSetDescription desc;
0118     desc.add<bool>("IncludeErrors", true);
0119     desc.add<bool>("UseQualityInfo", false);
0120     // Note: this parameter is obsolete: it is ignored and will have no effect.
0121     // It is kept to avoid breaking older configurations, and will not be printed in the generated cfi.py file.
0122     desc.addOptionalNode(edm::ParameterDescription<uint32_t>("MaxFEDWords", 0, true), false)
0123         ->setComment("This parameter is obsolete and will be ignored.");
0124     desc.add<int32_t>("clusterThreshold_layer1", pixelClustering::clusterThresholdLayerOne);
0125     desc.add<int32_t>("clusterThreshold_otherLayers", pixelClustering::clusterThresholdOtherLayers);
0126     desc.add<double>("VCaltoElectronGain", 47.f);
0127     desc.add<double>("VCaltoElectronGain_L1", 50.f);
0128     desc.add<double>("VCaltoElectronOffset", -60.f);
0129     desc.add<double>("VCaltoElectronOffset_L1", -670.f);
0130 
0131     desc.add<edm::InputTag>("InputLabel", edm::InputTag("rawDataCollector"));
0132     {
0133       edm::ParameterSetDescription psd0;
0134       psd0.addOptional<std::vector<edm::InputTag>>("inputs");
0135       psd0.addOptional<std::vector<double>>("deltaPhi");
0136       psd0.addOptional<std::vector<double>>("maxZ");
0137       psd0.addOptional<edm::InputTag>("beamSpot");
0138       desc.add<edm::ParameterSetDescription>("Regions", psd0)
0139           ->setComment("## Empty Regions PSet means complete unpacking");
0140     }
0141     desc.add<std::string>("CablingMapLabel", "")->setComment("CablingMap label");  //Tav
0142     descriptions.addWithDefaultLabel(desc);
0143   }
0144 
0145   template <typename TrackerTraits>
0146   void SiPixelRawToCluster<TrackerTraits>::acquire(device::Event const& iEvent, device::EventSetup const& iSetup) {
0147     auto const& hMap = iSetup.getData(mapToken_);
0148     auto const& dGains = iSetup.getData(gainsToken_);
0149 
0150     // initialize cabling map or update if necessary
0151     if (recordWatcher_.check(iSetup)) {
0152       // cabling map, which maps online address (fed->link->ROC->local pixel) to offline (DetId->global pixel)
0153       cablingMap_ = &iSetup.getData(cablingMapToken_);
0154       fedIds_ = cablingMap_->fedIds();
0155       cabling_ = cablingMap_->cablingTree();
0156       LogDebug("map version:") << cablingMap_->version();
0157     }
0158 
0159     // if used, the buffer is guaranteed to stay alive until the after the execution of makePhase1ClustersAsync completes
0160     std::optional<cms::alpakatools::device_buffer<Device, unsigned char[]>> modulesToUnpackRegional;
0161     const unsigned char* modulesToUnpack;
0162     if (regions_) {
0163       regions_->run(iEvent, iSetup);
0164       LogDebug("SiPixelRawToCluster") << "region2unpack #feds: " << regions_->nFEDs();
0165       LogDebug("SiPixelRawToCluster") << "region2unpack #modules (BPIX,EPIX,total): " << regions_->nBarrelModules()
0166                                       << " " << regions_->nForwardModules() << " " << regions_->nModules();
0167 
0168       modulesToUnpackRegional = SiPixelMappingUtilities::getModToUnpRegionalAsync(
0169           *(regions_->modulesToUnpack()), cabling_.get(), fedIds_, iEvent.queue());
0170       modulesToUnpack = modulesToUnpackRegional->data();
0171     } else {
0172       modulesToUnpack = hMap->modToUnpDefault();
0173     }
0174 
0175     const auto& buffers = iEvent.get(rawGetToken_);
0176 
0177     errors_.clear();
0178 
0179     // GPU specific: Data extraction for RawToDigi GPU
0180     unsigned int wordCounter = 0;
0181     unsigned int fedCounter = 0;
0182     bool errorsInEvent = false;
0183     std::vector<unsigned int> index(fedIds_.size(), 0);
0184     std::vector<cms_uint32_t const*> start(fedIds_.size(), nullptr);
0185     std::vector<ptrdiff_t> words(fedIds_.size(), 0);
0186     // In CPU algorithm this loop is part of PixelDataFormatter::interpretRawData()
0187     ErrorChecker errorcheck;
0188     for (uint32_t i = 0; i < fedIds_.size(); ++i) {
0189       const int fedId = fedIds_[i];
0190       if (regions_ && !regions_->mayUnpackFED(fedId))
0191         continue;
0192 
0193       // for GPU
0194       // first 150 index stores the fedId and next 150 will store the
0195       // start index of word in that fed
0196       assert(fedId >= FEDNumbering::MINSiPixeluTCAFEDID);
0197       fedCounter++;
0198 
0199       // get event data for this fed
0200       const FEDRawData& rawData = buffers.FEDData(fedId);
0201 
0202       // GPU specific
0203       int nWords = rawData.size() / sizeof(cms_uint64_t);
0204       if (nWords == 0) {
0205         continue;
0206       }
0207       // check CRC bit
0208       const cms_uint64_t* trailer = reinterpret_cast<const cms_uint64_t*>(rawData.data()) + (nWords - 1);
0209       if (not errorcheck.checkCRC(errorsInEvent, fedId, trailer, errors_)) {
0210         continue;
0211       }
0212       // check headers
0213       const cms_uint64_t* header = reinterpret_cast<const cms_uint64_t*>(rawData.data());
0214       header--;
0215       bool moreHeaders = true;
0216       while (moreHeaders) {
0217         header++;
0218         bool headerStatus = errorcheck.checkHeader(errorsInEvent, fedId, header, errors_);
0219         moreHeaders = headerStatus;
0220       }
0221 
0222       // check trailers
0223       bool moreTrailers = true;
0224       trailer++;
0225       while (moreTrailers) {
0226         trailer--;
0227         bool trailerStatus = errorcheck.checkTrailer(errorsInEvent, fedId, nWords, trailer, errors_);
0228         moreTrailers = trailerStatus;
0229       }
0230 
0231       const cms_uint32_t* bw = (const cms_uint32_t*)(header + 1);
0232       const cms_uint32_t* ew = (const cms_uint32_t*)(trailer);
0233 
0234       assert(0 == (ew - bw) % 2);
0235       index[i] = wordCounter;
0236       start[i] = bw;
0237       words[i] = (ew - bw);
0238       wordCounter += (ew - bw);
0239 
0240     }  // end of for loop
0241     nDigis_ = wordCounter;
0242     if (nDigis_ == 0)
0243       return;
0244 
0245     // copy the FED data to a single cpu buffer
0246     pixelDetails::WordFedAppender wordFedAppender(iEvent.queue(), nDigis_);
0247     for (uint32_t i = 0; i < fedIds_.size(); ++i) {
0248       wordFedAppender.initializeWordFed(fedIds_[i], index[i], start[i], words[i]);
0249     }
0250     Algo_.makePhase1ClustersAsync(iEvent.queue(),
0251                                   clusterThresholds_,
0252                                   hMap.const_view(),
0253                                   modulesToUnpack,
0254                                   dGains.const_view(),
0255                                   wordFedAppender,
0256                                   wordCounter,
0257                                   fedCounter,
0258                                   useQuality_,
0259                                   includeErrors_,
0260                                   edm::MessageDrop::instance()->debugEnabled);
0261   }
0262 
0263   template <typename TrackerTraits>
0264   void SiPixelRawToCluster<TrackerTraits>::produce(device::Event& iEvent, device::EventSetup const& iSetup) {
0265     if (nDigis_ == 0) {
0266       // Cannot use the default constructor here, as it would not allocate memory.
0267       // In the case of no digis, clusters_d are not being instantiated, but are
0268       // still used downstream to initialize TrackingRecHitSoADevice. If there
0269       // are no valid pointers to clusters' Collection columns, instantiation
0270       // of TrackingRecHits fail. Example: workflow 11604.0
0271 
0272       iEvent.emplace(digiPutToken_, nDigis_, iEvent.queue());
0273       iEvent.emplace(clusterPutToken_, pixelTopology::Phase1::numberOfModules, iEvent.queue());
0274       if (includeErrors_) {
0275         iEvent.emplace(digiErrorPutToken_);
0276         iEvent.emplace(fmtErrorToken_);
0277       }
0278       return;
0279     }
0280 
0281     iEvent.emplace(digiPutToken_, Algo_.getDigis());
0282     iEvent.emplace(clusterPutToken_, Algo_.getClusters());
0283     if (includeErrors_) {
0284       iEvent.emplace(digiErrorPutToken_, Algo_.getErrors());
0285       iEvent.emplace(fmtErrorToken_, std::move(errors_));
0286     }
0287   }
0288 
0289   using SiPixelRawToClusterPhase1 = SiPixelRawToCluster<pixelTopology::Phase1>;
0290   using SiPixelRawToClusterHIonPhase1 = SiPixelRawToCluster<pixelTopology::HIonPhase1>;
0291 
0292 }  // namespace ALPAKA_ACCELERATOR_NAMESPACE
0293 
0294 // define as framework plugin
0295 #include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
0296 DEFINE_FWK_ALPAKA_MODULE(SiPixelRawToClusterPhase1);
0297 DEFINE_FWK_ALPAKA_MODULE(SiPixelRawToClusterHIonPhase1);