Skip to content

Commit

Permalink
Rewrite the HGCalRecHitProducer and HGCalRecHitCalibrationAlgorithms …
Browse files Browse the repository at this point in the history
…to fix various problems
  • Loading branch information
fwyzard committed Jun 12, 2023
1 parent eebec2d commit 05b5c65
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
namespace ALPAKA_ACCELERATOR_NAMESPACE {

using namespace cms::alpakatools;
using namespace std;

class HGCalRecHitCalibrationKernel_digisToRecHits {
public:
template <typename TAcc, typename T>
ALPAKA_FN_ACC void operator()(TAcc const& acc, const T &digis,
HGCalDeviceRecHitCollection::View recHits) const {


auto ToA_to_time = [&](uint16_t ToA){return float(ToA);};
auto ADC_to_energy = [&](uint16_t ADC){return float(ADC);};
template <typename TAcc>
ALPAKA_FN_ACC void operator()(TAcc const& acc, HGCalDeviceDigiCollection::ConstView digis, HGCalDeviceRecHitCollection::View recHits) const {
auto ToA_to_time = [&](uint16_t ToA) { return float(ToA); };
auto ADC_to_energy = [&](uint16_t ADC) { return float(ADC); };
// auto ToT_to_energy = [&](uint16_t ToT){return float(ToT);};

// dummy digis -> rechits conversion (to be replaced by the actual formula)
Expand All @@ -33,99 +29,107 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
};

struct HGCalRecHitCalibrationKernel_pedestalCorrection {
template <typename TAcc, typename T>
ALPAKA_FN_ACC void operator()(TAcc const& acc, T recHits, float pedestalValue) const {
template <typename TAcc>
ALPAKA_FN_ACC void operator()(TAcc const& acc, HGCalDeviceRecHitCollection::View recHits, float pedestalValue) const {
for (auto index : elements_with_stride(acc, recHits.metadata().size())) {
recHits[index].energy() -= pedestalValue;
}
}
};

struct HGCalRecHitCalibrationKernel_commonModeCorrection {
template <typename TAcc, typename T>
ALPAKA_FN_ACC void operator()(TAcc const& acc, T recHits, float commonModeValue) const {
template <typename TAcc>
ALPAKA_FN_ACC void operator()(TAcc const& acc, HGCalDeviceRecHitCollection::View recHits, float commonModeValue) const {
for (auto index : elements_with_stride(acc, recHits.metadata().size())) {
recHits[index].energy() -= commonModeValue;
}
}
};

struct HGCalRecHitCalibrationKernel_ADCmCorrection {
template <typename TAcc, typename T>
ALPAKA_FN_ACC void operator()(TAcc const& acc, T recHits, float ADCmValue) const {
template <typename TAcc>
ALPAKA_FN_ACC void operator()(TAcc const& acc, HGCalDeviceRecHitCollection::View recHits, float ADCmValue) const {
for (auto index : elements_with_stride(acc, recHits.metadata().size())) {
recHits[index].energy() -= ADCmValue;
}
}
};

unique_ptr<HGCalDeviceRecHitCollection> HGCalRecHitCalibrationAlgorithms::calibrate(const HGCalHostDigiCollection &digis)
{
cout<<"\n\nINFO -- Start of calibrate\n\n"<<endl;

auto queue = Queue(devices<Platform>()[0]); // should pick the right device somehow...
auto grid = make_workdiv<Acc1D>(4, 4);

cout << "Input digis: " << endl;
print(digis, 10);

cout<<"\n\nINFO -- converting digis to rechits"<<endl;
auto recHits = make_unique<HGCalDeviceRecHitCollection>(digis.view().metadata().size(), queue);

alpaka::exec<Acc1D>(queue,
grid,
HGCalRecHitCalibrationKernel_digisToRecHits{},
digis.view(),
recHits->view()
);

cout << "RecHits before calibration: " << endl;
print(recHits, 10);

float pedestalValue = 10;
alpaka::exec<Acc1D>(queue, grid, HGCalRecHitCalibrationKernel_pedestalCorrection{}, recHits->view(), pedestalValue);
cout << "RecHits after pedestal calibration: " << endl;
print(recHits, 10);

float commonModeValue = 10;
alpaka::exec<Acc1D>(queue, grid, HGCalRecHitCalibrationKernel_commonModeCorrection{}, recHits->view(), commonModeValue);
cout << "RecHits after CM calibration: " << endl;
print(recHits, 10);

float ADCmValue = 10;
alpaka::exec<Acc1D>(queue, grid, HGCalRecHitCalibrationKernel_ADCmCorrection{}, recHits->view(), ADCmValue);
cout << "RecHits after ADCm calibration: " << endl;
print(recHits, 10);

return recHits;
}

void HGCalRecHitCalibrationAlgorithms::print(const HGCalHostDigiCollection &digis, int max){
int max_ = max > 0 ? max : digis.view().metadata().size();
for(int i=0; i<max_; i++){
cout<<i;
cout<<"\t"<<digis.view()[i].electronicsId();
cout<<"\t"<<digis.view()[i].raw();
cout<<"\t"<<digis.view()[i].cm();
cout<<"\t"<<digis.view()[i].flags();
cout<<endl;
struct HGCalRecHitCalibrationKernel_printRecHits {
template <typename TAcc>
ALPAKA_FN_ACC void operator()(TAcc const& acc, HGCalDeviceRecHitCollection::ConstView view, int size) const {
for (int i = 0; i < size; ++i) {
auto const& rechit = view[i];
printf("%d\t%d\t%f\t%f\t%d\n", i, rechit.detid(), rechit.energy(), rechit.time(), rechit.flags());
}
printf("\n");
}
};

std::unique_ptr<HGCalDeviceRecHitCollection> HGCalRecHitCalibrationAlgorithms::calibrate(Queue& queue, HGCalHostDigiCollection const& digis) {
std::cout << "\n\nINFO -- Start of calibrate\n\n" << std::endl;

auto grid = make_workdiv<Acc1D>(4, 4);

std::cout << "Input digis: " << std::endl;
print(digis, 10);

std::cout << "\n\nINFO -- allocating rechits buffer" << std::endl;
auto recHits = std::make_unique<HGCalDeviceRecHitCollection>(digis.view().metadata().size(), queue);
print(queue, *recHits, 10);

std::cout << "\n\nINFO -- copying the digis to the device\n\n" << std::endl;
HGCalDeviceDigiCollection device_digis(digis.view().metadata().size(), queue);
alpaka::memcpy(queue, device_digis.buffer(), digis.const_buffer());

std::cout << "\n\nINFO -- converting digis to rechits" << std::endl;
alpaka::exec<Acc1D>(queue, grid, HGCalRecHitCalibrationKernel_digisToRecHits{}, device_digis.const_view(), recHits->view());

std::cout << "RecHits before calibration: " << std::endl;
print(queue, *recHits, 10);

float pedestalValue = 10;
alpaka::exec<Acc1D>(queue, grid, HGCalRecHitCalibrationKernel_pedestalCorrection{}, recHits->view(), pedestalValue);

std::cout << "RecHits after pedestal calibration: " << std::endl;
print(queue, *recHits, 10);

float commonModeValue = 10;
alpaka::exec<Acc1D>(queue, grid, HGCalRecHitCalibrationKernel_commonModeCorrection{}, recHits->view(), commonModeValue);

std::cout << "RecHits after CM calibration: " << std::endl;
print(queue, *recHits, 10);

float ADCmValue = 10;
alpaka::exec<Acc1D>(queue, grid, HGCalRecHitCalibrationKernel_ADCmCorrection{}, recHits->view(), ADCmValue);

std::cout << "RecHits after ADCm calibration: " << std::endl;
print(queue, *recHits, 10);

return recHits;
}
}

void HGCalRecHitCalibrationAlgorithms::print(const unique_ptr<HGCalDeviceRecHitCollection> &recHits, int max){
int max_ = max > 0 ? max : recHits->view().metadata().size();
for(int i=0; i<max_; i++){
cout<<i;
cout<<"\t"<<recHits->view()[i].detid();
cout<<"\t"<<recHits->view()[i].energy();
cout<<"\t"<<recHits->view()[i].time();
cout<<"\t"<<recHits->view()[i].flags();
cout<<endl;

void HGCalRecHitCalibrationAlgorithms::print(HGCalHostDigiCollection const& digis, int max) const {
int max_ = max > 0 ? max : digis.view().metadata().size();
for (int i = 0; i < max_; i++) {
std::cout << i;
std::cout << "\t" << digis.view()[i].electronicsId();
std::cout << "\t" << digis.view()[i].raw();
std::cout << "\t" << digis.view()[i].cm();
std::cout << "\t" << digis.view()[i].flags();
std::cout << std::endl;
}
}
}

} // namespace ALPAKA_ACCELERATOR_NAMESPACE
void HGCalRecHitCalibrationAlgorithms::print(Queue& queue, HGCalDeviceRecHitCollection const& recHits, int max) const {
auto grid = make_workdiv<Acc1D>(1, 1);
auto size = max > 0 ? max : recHits.view().metadata().size();
alpaka::exec<Acc1D>(queue, grid, HGCalRecHitCalibrationKernel_printRecHits{}, recHits.view(), size);
// ensure that the print operations are complete before returning
alpaka::wait(queue);
}

} // namespace ALPAKA_ACCELERATOR_NAMESPACE

//
// Some potentially useful code snippets:
Expand All @@ -144,4 +148,4 @@ void HGCalRecHitCalibrationAlgorithms::print(const unique_ptr<HGCalDeviceRecHitC
// auto workDiv = make_workdiv<Acc1D>(groups, items);

// alpaka::exec<Acc1D>(queue, workDiv, HGCalRecHitCalibrationKernel{}, collection.view(), collection->metadata().size(), xvalue);
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {

class HGCalRecHitCalibrationAlgorithms {
public:
std::unique_ptr<HGCalDeviceRecHitCollection> calibrate(const HGCalHostDigiCollection &digis);
std::unique_ptr<HGCalDeviceRecHitCollection> calibrate(Queue& queue, HGCalHostDigiCollection const& digis);

// if converting host digis to device rechits turns out too slow, we should copy host digis to device digis and then
// convert to device rechits on device
// std::unique_ptr<HGCalDeviceRecHitCollection> calibrate(const std::unique_ptr<HGCalDeviceDigiCollection> &digis);
// std::unique_ptr<HGCalDeviceRecHitCollection> calibrate(Queue& queue, const std::unique_ptr<HGCalDeviceDigiCollection> &digis);

private:
void print(const std::unique_ptr<HGCalDeviceRecHitCollection> &recHits, int max=-1);
void print(const HGCalHostDigiCollection &digis, int max=-1);
void print(Queue& queue, HGCalDeviceRecHitCollection const& recHits, int max = -1) const;
void print(HGCalHostDigiCollection const& digis, int max = -1) const;
};

} // namespace ALPAKA_ACCELERATOR_NAMESPACE
} // namespace ALPAKA_ACCELERATOR_NAMESPACE

#endif // __HGCalRecHitCalibrationAlgorithms_H__
#endif // __HGCalRecHitCalibrationAlgorithms_H__
65 changes: 27 additions & 38 deletions RecoLocalCalo/HGCalRecAlgos/plugins/alpaka/HGCalRecHitProducer.cc
Original file line number Diff line number Diff line change
@@ -1,66 +1,54 @@
// Typical CMSSW imports
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
// CMSSW includes
#include "DataFormats/HGCalDigi/interface/HGCalHostDigiCollection.h"
#include "DataFormats/HGCalDigi/interface/alpaka/HGCalDeviceDigiCollection.h"
#include "DataFormats/HGCalRecHit/interface/HGCalHostRecHitCollection.h"
#include "DataFormats/HGCalRecHit/interface/alpaka/HGCalDeviceRecHitCollection.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Utilities/interface/StreamID.h"

// Alpaka-related imports
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/EDPutToken.h"
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/Event.h"
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/stream/EDProducer.h"
#include "HeterogeneousCore/AlpakaInterface/interface/CopyToDevice.h"

// HGCal digis and RecHits
#include "DataFormats/HGCalDigi/interface/HGCalHostDigiCollection.h"
#include "DataFormats/HGCalDigi/interface/alpaka/HGCalDeviceDigiCollection.h"

#include "DataFormats/HGCalRecHit/interface/HGCalHostRecHitCollection.h"
#include "DataFormats/HGCalRecHit/interface/alpaka/HGCalDeviceRecHitCollection.h"

// Alpaka-based calibration class
#include "RecoLocalCalo/HGCalRecAlgos/plugins/alpaka/HGCalRecHitCalibrationAlgorithms.h"


namespace ALPAKA_ACCELERATOR_NAMESPACE {

using namespace cms::alpakatools;
using namespace std;

class HGCalRecHitProducer : public edm::stream::EDProducer<> {
class HGCalRecHitProducer : public stream::EDProducer<> {
public:
explicit HGCalRecHitProducer(const edm::ParameterSet&);
static void fillDescriptions(edm::ConfigurationDescriptions&);

private:
void produce(edm::Event&, const edm::EventSetup&) override;
void produce(device::Event&, device::EventSetup const&) override;

const edm::EDGetTokenT<HGCalHostDigiCollection> digisToken_;
const edm::EDPutTokenT<HGCalDeviceRecHitCollection> recHitsToken_;
const std::unique_ptr<HGCalRecHitCalibrationAlgorithms> calibrator;
const device::EDPutToken<HGCalDeviceRecHitCollection> recHitsToken_;
HGCalRecHitCalibrationAlgorithms calibrator_; // cannot be "const" because the calibrate() method is not const
};

HGCalRecHitProducer::HGCalRecHitProducer(const edm::ParameterSet& iConfig) :
digisToken_(consumes<HGCalHostDigiCollection>(iConfig.getParameter<edm::InputTag>("digis"))),
recHitsToken_{produces()},
calibrator(std::make_unique<HGCalRecHitCalibrationAlgorithms>())
{}
HGCalRecHitProducer::HGCalRecHitProducer(const edm::ParameterSet& iConfig)
: digisToken_{consumes<HGCalHostDigiCollection>(iConfig.getParameter<edm::InputTag>("digis"))},
recHitsToken_{produces()},
calibrator_{} {}

void HGCalRecHitProducer::produce(device::Event& iEvent, device::EventSetup const& iSetup) {
auto queue = iEvent.queue();

void HGCalRecHitProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
{
cout<<"\n\nINFO -- Start of produce\n\n"<<endl;
std::cout << "\n\nINFO -- Start of produce\n\n" << std::endl;
// Read digis
auto const& hostDigis = iEvent.get(digisToken_);
cout<<"Loaded host digis: "<<hostDigis.view().metadata().size()<<endl;
std::cout << "Loaded host digis: " << hostDigis.view().metadata().size() << std::endl;

cout<<"\n\nINFO -- calling calibrate method"<<endl;
std::unique_ptr<HGCalDeviceRecHitCollection> recHits = calibrator->calibrate(hostDigis);
cout<<"\n\nINFO -- storing rec hits in the event"<<endl;
iEvent.emplace(recHitsToken_, std::move(*recHits));
std::cout << "\n\nINFO -- calling calibrate method" << std::endl;
auto recHits = calibrator_.calibrate(queue, hostDigis);

std::cout << "\n\nINFO -- storing rec hits in the event" << std::endl;
iEvent.put(recHitsToken_, std::move(recHits));
}

void HGCalRecHitProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
Expand All @@ -71,4 +59,5 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
} // namespace ALPAKA_ACCELERATOR_NAMESPACE

// define this as a plug-in
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/MakerMacros.h"
DEFINE_FWK_ALPAKA_MODULE(HGCalRecHitProducer);

0 comments on commit 05b5c65

Please sign in to comment.