File indexing completed on 2024-04-06 12:25:54
0001 #include <iostream>
0002 #include <string>
0003
0004 #include "FWCore/Framework/interface/stream/EDProducer.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/Utilities/interface/EDGetToken.h"
0009 #include "FWCore/Utilities/interface/EDPutToken.h"
0010 #include "FWCore/Utilities/interface/InputTag.h"
0011
0012 #include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h"
0013
0014 #include "HeterogeneousCore/CUDACore/interface/ScopedContext.h"
0015 #include "HeterogeneousCore/CUDACore/interface/ContextState.h"
0016 #include "HeterogeneousCore/CUDAUtilities/interface/cudaCheck.h"
0017 #include "HeterogeneousCore/CUDAUtilities/interface/MessageLogger.h"
0018
0019 #include "RecoLocalCalo/HGCalRecProducers/plugins/KernelManagerHGCalRecHit.h"
0020 #include "CUDADataFormats/HGCal/interface/HGCRecHitGPUProduct.h"
0021 #include "CUDADataFormats/HGCal/interface/HGCUncalibRecHitDevice.h"
0022 #include "CUDADataFormats/HGCal/interface/HGCUncalibRecHitHost.h"
0023
0024 class HEBRecHitGPU : public edm::stream::EDProducer<> {
0025 public:
0026 explicit HEBRecHitGPU(const edm::ParameterSet &ps);
0027 ~HEBRecHitGPU() override;
0028
0029 void produce(edm::Event &, const edm::EventSetup &) override;
0030
0031 private:
0032 edm::EDGetTokenT<HGChebUncalibratedRecHitCollection> uncalibRecHitCPUToken_;
0033 edm::EDPutTokenT<cms::cuda::Product<HGCRecHitGPUProduct>> recHitGPUToken_;
0034
0035 std::unique_ptr<HGChebRecHitCollection> rechits_;
0036
0037
0038 HGChebUncalibRecHitConstantData cdata_;
0039 HGCConstantVectorData vdata_;
0040
0041
0042 std::string assert_error_message_(std::string, const size_t &);
0043 void assert_sizes_constants_(const HGCConstantVectorData &);
0044
0045
0046 std::unique_ptr<hgcal::RecHitTools> tools_;
0047
0048
0049 void convert_collection_data_to_soa_(const uint32_t &, const HGChebUncalibratedRecHitCollection &);
0050 void convert_constant_data_(KernelConstantData<HGChebUncalibRecHitConstantData> *);
0051
0052 HGCRecHitGPUProduct prod_;
0053 HGCUncalibRecHitDevice d_uncalib_;
0054 HGCUncalibRecHitHost<HGChebUncalibratedRecHitCollection> h_uncalib_;
0055
0056 KernelConstantData<HGChebUncalibRecHitConstantData> *kcdata_;
0057 };
0058
0059 HEBRecHitGPU::HEBRecHitGPU(const edm::ParameterSet &ps)
0060 : uncalibRecHitCPUToken_{consumes<HGCUncalibratedRecHitCollection>(
0061 ps.getParameter<edm::InputTag>("HGCHEBUncalibRecHitsTok"))},
0062 recHitGPUToken_{produces<cms::cuda::Product<HGCRecHitGPUProduct>>()} {
0063 cdata_.keV2DIGI_ = ps.getParameter<double>("HGCHEB_keV2DIGI");
0064 cdata_.noise_MIP_ = ps.getParameter<edm::ParameterSet>("HGCHEB_noise_MIP").getParameter<double>("noise_MIP");
0065 vdata_.weights_ = ps.getParameter<std::vector<double>>("weights");
0066 cdata_.uncalib2GeV_ = 1e-6 / cdata_.keV2DIGI_;
0067 cdata_.layerOffset_ = 28;
0068 assert_sizes_constants_(vdata_);
0069
0070 kcdata_ = new KernelConstantData<HGChebUncalibRecHitConstantData>(cdata_, vdata_);
0071 convert_constant_data_(kcdata_);
0072
0073 tools_ = std::make_unique<hgcal::RecHitTools>();
0074 }
0075
0076 HEBRecHitGPU::~HEBRecHitGPU() { delete kcdata_; }
0077
0078 std::string HEBRecHitGPU::assert_error_message_(std::string var, const size_t &s) {
0079 std::string str1 = "The '";
0080 std::string str2 = "' array must be of size ";
0081 std::string str3 = " to hold the configuration data.";
0082 return str1 + var + str2 + std::to_string(s) + str3;
0083 }
0084
0085 void HEBRecHitGPU::assert_sizes_constants_(const HGCConstantVectorData &vd) {
0086 if (vdata_.weights_.size() != HGChebUncalibRecHitConstantData::heb_weights)
0087 edm::LogError("WrongSize") << this->assert_error_message_("weights", vdata_.fCPerMIP_.size());
0088 }
0089
0090 void HEBRecHitGPU::produce(edm::Event &event, const edm::EventSetup &setup) {
0091 cms::cuda::ScopedContextProduce ctx{event.streamID()};
0092
0093 const auto &hits = event.get(uncalibRecHitCPUToken_);
0094 unsigned int nhits(hits.size());
0095 rechits_ = std::make_unique<HGCRecHitCollection>();
0096
0097 if (nhits == 0)
0098 edm::LogError("HEBRecHitGPU") << "WARNING: no input hits!";
0099
0100 prod_ = HGCRecHitGPUProduct(nhits, ctx.stream());
0101 d_uncalib_ = HGCUncalibRecHitDevice(nhits, ctx.stream());
0102 h_uncalib_ = HGCUncalibRecHitHost<HGChebUncalibratedRecHitCollection>(nhits, hits, ctx.stream());
0103
0104 KernelManagerHGCalRecHit km(h_uncalib_.get(), d_uncalib_.get(), prod_.get());
0105 km.run_kernels(kcdata_, ctx.stream());
0106
0107 ctx.emplace(event, recHitGPUToken_, std::move(prod_));
0108 }
0109
0110 void HEBRecHitGPU::convert_constant_data_(KernelConstantData<HGChebUncalibRecHitConstantData> *kcdata) {
0111 for (size_t i = 0; i < kcdata->vdata_.weights_.size(); ++i)
0112 kcdata->data_.weights_[i] = kcdata->vdata_.weights_[i];
0113 }
0114
0115 #include "FWCore/Framework/interface/MakerMacros.h"
0116 DEFINE_FWK_MODULE(HEBRecHitGPU);