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 HEFRecHitGPU : public edm::stream::EDProducer<> {
0025 public:
0026   explicit HEFRecHitGPU(const edm::ParameterSet &ps);
0027   ~HEFRecHitGPU() 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<HGChefUncalibratedRecHitCollection> uncalibRecHitCPUToken_;
0034   edm::EDPutTokenT<cms::cuda::Product<HGCRecHitGPUProduct>> recHitGPUToken_;
0035 
0036   std::unique_ptr<HGChefRecHitCollection> rechits_;
0037 
0038   //constants
0039   HGChefUncalibRecHitConstantData cdata_;
0040   HGCConstantVectorData vdata_;
0041 
0042   //memory
0043   std::string assert_error_message_(std::string, const size_t &, 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 HGChefUncalibratedRecHitCollection &);
0051   void convert_constant_data_(KernelConstantData<HGChefUncalibRecHitConstantData> *);
0052 
0053   HGCRecHitGPUProduct prod_;
0054   HGCUncalibRecHitDevice d_uncalib_;
0055   HGCUncalibRecHitHost<HGChefUncalibratedRecHitCollection> h_uncalib_;
0056 
0057   KernelConstantData<HGChefUncalibRecHitConstantData> *kcdata_;
0058 };
0059 
0060 HEFRecHitGPU::HEFRecHitGPU(const edm::ParameterSet &ps)
0061     : uncalibRecHitCPUToken_{consumes<HGCUncalibratedRecHitCollection>(
0062           ps.getParameter<edm::InputTag>("HGCHEFUncalibRecHitsTok"))},
0063       recHitGPUToken_{produces<cms::cuda::Product<HGCRecHitGPUProduct>>()} {
0064   cdata_.keV2DIGI_ = ps.getParameter<double>("HGCHEF_keV2DIGI");
0065   cdata_.xmin_ = ps.getParameter<double>("minValSiPar");  //float
0066   cdata_.xmax_ = ps.getParameter<double>("maxValSiPar");  //float
0067   cdata_.aterm_ = ps.getParameter<double>("noiseSiPar");  //float
0068   cdata_.cterm_ = ps.getParameter<double>("constSiPar");  //float
0069   vdata_.fCPerMIP_ = ps.getParameter<std::vector<double>>("HGCHEF_fCPerMIP");
0070   vdata_.cce_ = ps.getParameter<edm::ParameterSet>("HGCHEF_cce").getParameter<std::vector<double>>("values");
0071   vdata_.noise_fC_ = ps.getParameter<edm::ParameterSet>("HGCHEF_noise_fC").getParameter<std::vector<double>>("values");
0072   vdata_.rcorr_ = ps.getParameter<std::vector<double>>("rcorr");
0073   vdata_.weights_ = ps.getParameter<std::vector<double>>("weights");
0074   cdata_.uncalib2GeV_ = 1e-6 / cdata_.keV2DIGI_;
0075   cdata_.layerOffset_ = 28;
0076   assert_sizes_constants_(vdata_);
0077 
0078   kcdata_ = new KernelConstantData<HGChefUncalibRecHitConstantData>(cdata_, vdata_);
0079   convert_constant_data_(kcdata_);
0080 
0081   tools_ = std::make_unique<hgcal::RecHitTools>();
0082 }
0083 
0084 HEFRecHitGPU::~HEFRecHitGPU() { delete kcdata_; }
0085 
0086 std::string HEFRecHitGPU::assert_error_message_(std::string var, const size_t &s1, const size_t &s2) {
0087   std::string str1 = "The '";
0088   std::string str2 = "' array must be of size ";
0089   std::string str3 = " to hold the configuration data, but is of size ";
0090   return str1 + var + str2 + std::to_string(s1) + str3 + std::to_string(s2);
0091 }
0092 
0093 void HEFRecHitGPU::assert_sizes_constants_(const HGCConstantVectorData &vd) {
0094   if (vdata_.fCPerMIP_.size() != HGChefUncalibRecHitConstantData::hef_fCPerMIP)
0095     edm::LogError("WrongSize") << this->assert_error_message_(
0096         "fCPerMIP", HGChefUncalibRecHitConstantData::hef_fCPerMIP, vdata_.fCPerMIP_.size());
0097   else if (vdata_.cce_.size() != HGChefUncalibRecHitConstantData::hef_cce)
0098     edm::LogError("WrongSize") << this->assert_error_message_(
0099         "cce", HGChefUncalibRecHitConstantData::hef_cce, vdata_.cce_.size());
0100   else if (vdata_.noise_fC_.size() != HGChefUncalibRecHitConstantData::hef_noise_fC)
0101     edm::LogError("WrongSize") << this->assert_error_message_(
0102         "noise_fC", HGChefUncalibRecHitConstantData::hef_noise_fC, vdata_.noise_fC_.size());
0103   else if (vdata_.rcorr_.size() != HGChefUncalibRecHitConstantData::hef_rcorr)
0104     edm::LogError("WrongSize") << this->assert_error_message_(
0105         "rcorr", HGChefUncalibRecHitConstantData::hef_rcorr, vdata_.rcorr_.size());
0106   else if (vdata_.weights_.size() != HGChefUncalibRecHitConstantData::hef_weights)
0107     edm::LogError("WrongSize") << this->assert_error_message_(
0108         "weights", HGChefUncalibRecHitConstantData::hef_weights, vdata_.weights_.size());
0109 }
0110 
0111 void HEFRecHitGPU::beginRun(edm::Run const &, edm::EventSetup const &setup) {}
0112 
0113 void HEFRecHitGPU::produce(edm::Event &event, const edm::EventSetup &setup) {
0114   cms::cuda::ScopedContextProduce ctx{event.streamID()};
0115 
0116   const auto &hits = event.get(uncalibRecHitCPUToken_);
0117   unsigned int nhits(hits.size());
0118   rechits_ = std::make_unique<HGCRecHitCollection>();
0119 
0120   if (nhits == 0)
0121     edm::LogError("HEFRecHitGPU") << "WARNING: no input hits!";
0122 
0123   prod_ = HGCRecHitGPUProduct(nhits, ctx.stream());
0124   d_uncalib_ = HGCUncalibRecHitDevice(nhits, ctx.stream());
0125   h_uncalib_ = HGCUncalibRecHitHost<HGChefUncalibratedRecHitCollection>(nhits, hits, ctx.stream());
0126 
0127   KernelManagerHGCalRecHit km(h_uncalib_.get(), d_uncalib_.get(), prod_.get());
0128   km.run_kernels(kcdata_, ctx.stream());
0129 
0130   ctx.emplace(event, recHitGPUToken_, std::move(prod_));
0131 }
0132 
0133 void HEFRecHitGPU::convert_constant_data_(KernelConstantData<HGChefUncalibRecHitConstantData> *kcdata) {
0134   for (size_t i = 0; i < kcdata->vdata_.fCPerMIP_.size(); ++i)
0135     kcdata->data_.fCPerMIP_[i] = kcdata->vdata_.fCPerMIP_[i];
0136   for (size_t i = 0; i < kcdata->vdata_.cce_.size(); ++i)
0137     kcdata->data_.cce_[i] = kcdata->vdata_.cce_[i];
0138   for (size_t i = 0; i < kcdata->vdata_.noise_fC_.size(); ++i)
0139     kcdata->data_.noise_fC_[i] = kcdata->vdata_.noise_fC_[i];
0140   for (size_t i = 0; i < kcdata->vdata_.rcorr_.size(); ++i)
0141     kcdata->data_.rcorr_[i] = kcdata->vdata_.rcorr_[i];
0142   for (size_t i = 0; i < kcdata->vdata_.weights_.size(); ++i)
0143     kcdata->data_.weights_[i] = kcdata->vdata_.weights_[i];
0144 }
0145 
0146 #include "FWCore/Framework/interface/MakerMacros.h"
0147 DEFINE_FWK_MODULE(HEFRecHitGPU);