Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-03-30 08:50:55

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   void beginRun(edm::Run const &, edm::EventSetup const &) override;
0029 
0030   void produce(edm::Event &, const edm::EventSetup &) override;
0031 
0032 private:
0033   edm::EDGetTokenT<HGChebUncalibratedRecHitCollection> uncalibRecHitCPUToken_;
0034   edm::EDPutTokenT<cms::cuda::Product<HGCRecHitGPUProduct>> recHitGPUToken_;
0035 
0036   std::unique_ptr<HGChebRecHitCollection> rechits_;
0037 
0038   //constants
0039   HGChebUncalibRecHitConstantData cdata_;
0040   HGCConstantVectorData vdata_;
0041 
0042   //memory
0043   std::string assert_error_message_(std::string, const size_t &);
0044   void assert_sizes_constants_(const HGCConstantVectorData &);
0045 
0046   //conditions (geometry, topology, ...)
0047   std::unique_ptr<hgcal::RecHitTools> tools_;
0048 
0049   //data processing
0050   void convert_collection_data_to_soa_(const uint32_t &, const HGChebUncalibratedRecHitCollection &);
0051   void convert_constant_data_(KernelConstantData<HGChebUncalibRecHitConstantData> *);
0052 
0053   HGCRecHitGPUProduct prod_;
0054   HGCUncalibRecHitDevice d_uncalib_;
0055   HGCUncalibRecHitHost<HGChebUncalibratedRecHitCollection> h_uncalib_;
0056 
0057   KernelConstantData<HGChebUncalibRecHitConstantData> *kcdata_;
0058 };
0059 
0060 HEBRecHitGPU::HEBRecHitGPU(const edm::ParameterSet &ps)
0061     : uncalibRecHitCPUToken_{consumes<HGCUncalibratedRecHitCollection>(
0062           ps.getParameter<edm::InputTag>("HGCHEBUncalibRecHitsTok"))},
0063       recHitGPUToken_{produces<cms::cuda::Product<HGCRecHitGPUProduct>>()} {
0064   cdata_.keV2DIGI_ = ps.getParameter<double>("HGCHEB_keV2DIGI");
0065   cdata_.noise_MIP_ = ps.getParameter<edm::ParameterSet>("HGCHEB_noise_MIP").getParameter<double>("noise_MIP");
0066   vdata_.weights_ = ps.getParameter<std::vector<double>>("weights");
0067   cdata_.uncalib2GeV_ = 1e-6 / cdata_.keV2DIGI_;
0068   cdata_.layerOffset_ = 28;
0069   assert_sizes_constants_(vdata_);
0070 
0071   kcdata_ = new KernelConstantData<HGChebUncalibRecHitConstantData>(cdata_, vdata_);
0072   convert_constant_data_(kcdata_);
0073 
0074   tools_ = std::make_unique<hgcal::RecHitTools>();
0075 }
0076 
0077 HEBRecHitGPU::~HEBRecHitGPU() { delete kcdata_; }
0078 
0079 std::string HEBRecHitGPU::assert_error_message_(std::string var, const size_t &s) {
0080   std::string str1 = "The '";
0081   std::string str2 = "' array must be of size ";
0082   std::string str3 = " to hold the configuration data.";
0083   return str1 + var + str2 + std::to_string(s) + str3;
0084 }
0085 
0086 void HEBRecHitGPU::assert_sizes_constants_(const HGCConstantVectorData &vd) {
0087   if (vdata_.weights_.size() != HGChebUncalibRecHitConstantData::heb_weights)
0088     edm::LogError("WrongSize") << this->assert_error_message_("weights", vdata_.fCPerMIP_.size());
0089 }
0090 
0091 void HEBRecHitGPU::beginRun(edm::Run const &, edm::EventSetup const &setup) {}
0092 
0093 void HEBRecHitGPU::produce(edm::Event &event, const edm::EventSetup &setup) {
0094   cms::cuda::ScopedContextProduce ctx{event.streamID()};
0095 
0096   const auto &hits = event.get(uncalibRecHitCPUToken_);
0097   unsigned int nhits(hits.size());
0098   rechits_ = std::make_unique<HGCRecHitCollection>();
0099 
0100   if (nhits == 0)
0101     edm::LogError("HEBRecHitGPU") << "WARNING: no input hits!";
0102 
0103   prod_ = HGCRecHitGPUProduct(nhits, ctx.stream());
0104   d_uncalib_ = HGCUncalibRecHitDevice(nhits, ctx.stream());
0105   h_uncalib_ = HGCUncalibRecHitHost<HGChebUncalibratedRecHitCollection>(nhits, hits, ctx.stream());
0106 
0107   KernelManagerHGCalRecHit km(h_uncalib_.get(), d_uncalib_.get(), prod_.get());
0108   km.run_kernels(kcdata_, ctx.stream());
0109 
0110   ctx.emplace(event, recHitGPUToken_, std::move(prod_));
0111 }
0112 
0113 void HEBRecHitGPU::convert_constant_data_(KernelConstantData<HGChebUncalibRecHitConstantData> *kcdata) {
0114   for (size_t i = 0; i < kcdata->vdata_.weights_.size(); ++i)
0115     kcdata->data_.weights_[i] = kcdata->vdata_.weights_[i];
0116 }
0117 
0118 #include "FWCore/Framework/interface/MakerMacros.h"
0119 DEFINE_FWK_MODULE(HEBRecHitGPU);