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