1 #ifndef RecoLocalCalo_EcalRecProducers_plugins_alpaka_AmplitudeComputationCommonKernels_h 2 #define RecoLocalCalo_EcalRecProducers_plugins_alpaka_AmplitudeComputationCommonKernels_h 6 #include <alpaka/alpaka.hpp> 31 template <
typename TAcc,
typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>>
33 EcalDigiDeviceCollection::ConstView digisDevEB,
34 EcalDigiDeviceCollection::ConstView digisDevEE,
37 EcalMultifitConditionsDevice::ConstView conditionsDev,
40 bool* hasSwitchToGain6,
41 bool* hasSwitchToGain1,
51 auto const offsetForHashes = conditionsDev.offsetEE();
53 auto const nchannelsEB = digisDevEB.size();
54 auto const nchannelsEE = digisDevEE.size();
55 auto const nchannels = nchannelsEB + nchannelsEE;
56 auto const totalElements = nchannels * nsamples;
58 auto const elemsPerBlock = alpaka::getWorkDiv<alpaka::Block, alpaka::Elems>(acc)[0u];
60 char* shared_mem = alpaka::getDynSharedMem<char>(acc);
61 auto* shr_hasSwitchToGain6 =
reinterpret_cast<bool*
>(shared_mem);
62 auto* shr_hasSwitchToGain1 = shr_hasSwitchToGain6 + elemsPerBlock;
63 auto* shr_hasSwitchToGain0 = shr_hasSwitchToGain1 + elemsPerBlock;
64 auto* shr_isSaturated = shr_hasSwitchToGain0 + elemsPerBlock;
65 auto* shr_hasSwitchToGain0_tmp = shr_isSaturated + elemsPerBlock;
66 auto* shr_counts =
reinterpret_cast<char*
>(shr_hasSwitchToGain0_tmp) + elemsPerBlock;
71 if (
idx.global == 0) {
72 uncalibRecHitsEB.size() = nchannelsEB;
73 uncalibRecHitsEE.size() = nchannelsEE;
76 auto const ch =
idx.global / nsamples;
78 int const inputTx = ch >= nchannelsEB ?
idx.global - nchannelsEB * nsamples :
idx.global;
80 auto const* digis_in = ch >= nchannelsEB ? digisDevEE.data()->data() : digisDevEB.data()->data();
87 shr_hasSwitchToGain0[
idx.local] = shr_hasSwitchToGain0_tmp[
idx.local];
88 shr_counts[
idx.local] = 0;
91 alpaka::syncBlockThreads(acc);
97 if (
idx.local <= elemsPerBlock - 5) {
99 for (
int i = 0;
i < 5; ++
i)
100 shr_counts[
idx.local] += shr_hasSwitchToGain0[
idx.local +
i];
102 shr_isSaturated[
idx.local] = shr_counts[
idx.local] == 5;
108 shr_hasSwitchToGain6[
idx.local] = shr_hasSwitchToGain6[
idx.local] || shr_hasSwitchToGain6[
idx.local + 5];
109 shr_hasSwitchToGain1[
idx.local] = shr_hasSwitchToGain1[
idx.local] || shr_hasSwitchToGain1[
idx.local + 5];
113 shr_hasSwitchToGain0_tmp[
idx.local] =
114 shr_hasSwitchToGain0_tmp[
idx.local] || shr_hasSwitchToGain0_tmp[
idx.local + 5];
118 alpaka::syncBlockThreads(acc);
121 auto const sample =
idx.local % nsamples;
125 shr_hasSwitchToGain6[
idx.local] = shr_hasSwitchToGain6[
idx.local] || shr_hasSwitchToGain6[
idx.local + 2] ||
126 shr_hasSwitchToGain6[
idx.local + 3];
127 shr_hasSwitchToGain1[
idx.local] = shr_hasSwitchToGain1[
idx.local] || shr_hasSwitchToGain1[
idx.local + 2] ||
128 shr_hasSwitchToGain1[
idx.local + 3];
130 shr_hasSwitchToGain0_tmp[
idx.local] = shr_hasSwitchToGain0_tmp[
idx.local] ||
131 shr_hasSwitchToGain0_tmp[
idx.local + 2] ||
132 shr_hasSwitchToGain0_tmp[
idx.local + 3];
137 shr_isSaturated[
idx.local] = shr_isSaturated[
idx.local + 3] || shr_isSaturated[
idx.local + 4];
141 alpaka::syncBlockThreads(acc);
144 auto const ch =
idx.global / nsamples;
145 auto const sample =
idx.local % nsamples;
148 shr_hasSwitchToGain6[
idx.local] = shr_hasSwitchToGain6[
idx.local] || shr_hasSwitchToGain6[
idx.local + 1];
149 shr_hasSwitchToGain1[
idx.local] = shr_hasSwitchToGain1[
idx.local] || shr_hasSwitchToGain1[
idx.local + 1];
150 shr_hasSwitchToGain0_tmp[
idx.local] =
151 shr_hasSwitchToGain0_tmp[
idx.local] || shr_hasSwitchToGain0_tmp[
idx.local + 1];
153 hasSwitchToGain6[ch] = shr_hasSwitchToGain6[
idx.local];
154 hasSwitchToGain1[ch] = shr_hasSwitchToGain1[
idx.local];
156 shr_isSaturated[
idx.local + 3] = shr_isSaturated[
idx.local] || shr_isSaturated[
idx.local + 1];
164 alpaka::syncBlockThreads(acc);
167 auto const ch =
idx.global / nsamples;
168 auto const sample =
idx.local % nsamples;
171 int const inputCh = ch >= nchannelsEB ? ch - nchannelsEB : ch;
172 int const inputTx = ch >= nchannelsEB ?
idx.global - nchannelsEB * nsamples :
idx.global;
174 auto const* dids = ch >= nchannelsEB ? digisDevEE.id() : digisDevEB.id();
175 auto const did =
DetId{dids[inputCh]};
182 auto const* digis_in = ch >= nchannelsEB ? digisDevEE.data()->data() : digisDevEB.data()->data();
185 ch >= nchannelsEB ? uncalibRecHitsEE.outOfTimeAmplitudes()->data()
186 : uncalibRecHitsEB.outOfTimeAmplitudes()->data());
187 auto* energies = ch >= nchannelsEB ? uncalibRecHitsEE.amplitude() : uncalibRecHitsEB.amplitude();
188 auto*
chi2 = ch >= nchannelsEB ? uncalibRecHitsEE.chi2() : uncalibRecHitsEB.chi2();
189 auto* g_pedestal = ch >= nchannelsEB ? uncalibRecHitsEE.pedestal() : uncalibRecHitsEB.pedestal();
190 auto* dids_out = ch >= nchannelsEB ? uncalibRecHitsEE.id() : uncalibRecHitsEB.id();
191 auto*
flags = ch >= nchannelsEB ? uncalibRecHitsEE.flags() : uncalibRecHitsEB.flags();
201 pedestal = conditionsDev.pedestals_mean_x1()[hashedId];
202 gainratio = conditionsDev.gain6Over1()[hashedId] * conditionsDev.gain12Over6()[hashedId];
203 gainsNoise[ch](
sample) = 2;
205 pedestal = conditionsDev.pedestals_mean_x12()[hashedId];
207 gainsNoise[ch](
sample) = 0;
209 pedestal = conditionsDev.pedestals_mean_x6()[hashedId];
210 gainratio = conditionsDev.gain12Over6()[hashedId];
211 gainsNoise[ch](
sample) = 1;
221 #ifdef ECAL_RECO_ALPAKA_DEBUG 224 printf(
"adc is zero\n");
230 amplitudesForMinimization[inputCh](
sample) = 0;
235 if (
sample == sample_max) {
240 energies[inputCh] = 0;
242 g_pedestal[inputCh] = 0;
244 dids_out[inputCh] = did.rawId();
247 auto const chStart =
idx.local - sample_max;
249 auto const threadMax =
idx.local;
253 if (shr_hasSwitchToGain6[chStart])
255 if (shr_hasSwitchToGain1[chStart])
261 if (
sample == 0 && shr_hasSwitchToGain0_tmp[
idx.local]) {
267 bool saturated_before_max =
false;
269 for (
char ii = 0;
ii < 5; ++
ii)
270 saturated_before_max = saturated_before_max || shr_hasSwitchToGain0[chStart +
ii];
273 if (!saturated_before_max && shr_hasSwitchToGain0[threadMax])
274 energies[inputCh] = 49140;
290 auto shape_value = conditionsDev.pulseShapes()[hashedId][full_pulse_max - 7];
293 shr_hasSwitchToGain6[chStart] || shr_hasSwitchToGain1[chStart] || shr_isSaturated[chStart + 3];
297 if (hasGainSwitch && gainSwitchUseMaxSample) {
299 energies[inputCh] = max_amplitude / shape_value;
306 auto const rmsForChecking = conditionsDev.pedestals_rms_x12()[hashedId];
311 if (rmsForChecking == 0) {
331 template <
typename TAcc,
typename = std::enable_if_t<alpaka::isAccelerator<TAcc>>>
333 EcalDigiDeviceCollection::ConstView digisDevEB,
334 EcalDigiDeviceCollection::ConstView digisDevEE,
335 EcalMultifitConditionsDevice::ConstView conditionsDev,
339 bool const* hasSwitchToGain6,
340 bool const* hasSwitchToGain1,
343 auto const offsetForHashes = conditionsDev.offsetEE();
344 auto const nchannelsEB = digisDevEB.size();
345 constexpr float addPedestalUncertainty = 0.f;
350 auto const* pulse_shapes =
reinterpret_cast<const EcalPulseShape*
>(conditionsDev.pulseShapes()->data());
352 auto const blockDimX = alpaka::getWorkDiv<alpaka::Grid, alpaka::Blocks>(acc)[1u];
353 auto const elemsPerBlockX = alpaka::getWorkDiv<alpaka::Block, alpaka::Elems>(acc)[1u];
354 auto const elemsPerBlockY = alpaka::getWorkDiv<alpaka::Block, alpaka::Elems>(acc)[0u];
355 Vec2D const size_2d = {elemsPerBlockY, blockDimX * elemsPerBlockX};
358 auto const ch = ndindex[1] / nsamples;
359 auto const tx = ndindex[1] % nsamples;
360 auto const ty = ndindex[0];
363 int const inputCh = ch >= nchannelsEB ? ch - nchannelsEB : ch;
364 auto const* dids = ch >= nchannelsEB ? digisDevEE.id() : digisDevEB.id();
366 auto const did =
DetId{dids[inputCh]};
370 auto const* G12SamplesCorrelation =
isBarrel ? conditionsDev.sampleCorrelation_EB_G12().data()
371 : conditionsDev.sampleCorrelation_EE_G12().data();
372 auto const* G6SamplesCorrelation =
373 isBarrel ? conditionsDev.sampleCorrelation_EB_G6().data() : conditionsDev.sampleCorrelation_EE_G6().data();
374 auto const* G1SamplesCorrelation =
375 isBarrel ? conditionsDev.sampleCorrelation_EB_G1().data() : conditionsDev.sampleCorrelation_EE_G1().data();
376 auto const hasGainSwitch = hasSwitchToGain6[ch] || hasSwitchToGain1[ch] ||
isSaturated[ch];
378 auto const vidx =
std::abs(static_cast<int>(ty) - static_cast<int>(tx));
383 float noise_value = 0;
391 auto const gainidx = gainsNoise[ch][isample_max];
395 auto const rms_x12 = conditionsDev.pedestals_rms_x12()[hashedId];
396 noise_value = rms_x12 * rms_x12 * G12SamplesCorrelation[vidx];
397 }
else if (gainidx == 1) {
398 auto const gain12Over6 = conditionsDev.gain12Over6()[hashedId];
399 auto const rms_x6 = conditionsDev.pedestals_rms_x6()[hashedId];
400 noise_value = gain12Over6 * gain12Over6 * rms_x6 * rms_x6 * G6SamplesCorrelation[vidx];
401 }
else if (gainidx == 2) {
402 auto const gain12Over6 = conditionsDev.gain12Over6()[hashedId];
403 auto const gain6Over1 = conditionsDev.gain6Over1()[hashedId];
404 auto const gain12Over1 = gain12Over6 * gain6Over1;
405 auto const rms_x1 = conditionsDev.pedestals_rms_x1()[hashedId];
406 noise_value = gain12Over1 * gain12Over1 * rms_x1 * rms_x1 * G1SamplesCorrelation[vidx];
408 if (!dynamicPedestal && addPedestalUncertainty > 0.
f)
409 noise_value += addPedestalUncertainty * addPedestalUncertainty;
415 auto const rms_x12 = conditionsDev.pedestals_rms_x12()[hashedId];
416 noise_value += rms_x12 * rms_x12 *
pedestal * G12SamplesCorrelation[vidx];
418 if (!dynamicPedestal && addPedestalUncertainty > 0.
f) {
419 noise_value += addPedestalUncertainty * addPedestalUncertainty *
pedestal;
426 auto const gain12Over6 = conditionsDev.gain12Over6()[hashedId];
427 auto const rms_x6 = conditionsDev.pedestals_rms_x6()[hashedId];
428 noise_value += gain12Over6 * gain12Over6 * rms_x6 * rms_x6 *
pedestal * G6SamplesCorrelation[vidx];
430 if (!dynamicPedestal && addPedestalUncertainty > 0.
f) {
431 noise_value += gain12Over6 * gain12Over6 * addPedestalUncertainty * addPedestalUncertainty *
pedestal;
438 auto const gain6Over1 = conditionsDev.gain6Over1()[hashedId];
439 auto const gain12Over1 = gain12Over6 * gain6Over1;
440 auto const rms_x1 = conditionsDev.pedestals_rms_x1()[hashedId];
441 noise_value += gain12Over1 * gain12Over1 * rms_x1 * rms_x1 *
pedestal * G1SamplesCorrelation[vidx];
443 if (!dynamicPedestal && addPedestalUncertainty > 0.
f) {
444 noise_value += gain12Over1 * gain12Over1 * addPedestalUncertainty * addPedestalUncertainty *
pedestal;
448 noisecov[ch](ty, tx) = noise_value;
450 auto const rms = conditionsDev.pedestals_rms_x12()[hashedId];
451 float noise_value =
rms *
rms * G12SamplesCorrelation[vidx];
452 if (!dynamicPedestal && addPedestalUncertainty > 0.
f) {
454 noise_value += addPedestalUncertainty * addPedestalUncertainty;
456 noisecov[ch](ty, tx) = noise_value;
459 auto const posToAccess = 9 -
static_cast<int>(tx) + static_cast<int>(ty);
460 float const value = posToAccess >= 7 ? pulse_shapes[hashedId].pdfval[posToAccess - 7] : 0;
461 pulse_matrix[ch](ty, tx) =
value;
472 template <
typename TAcc>
475 template <
typename TVec,
typename... TArgs>
477 TVec
const& threadsPerBlock,
478 TVec
const& elemsPerThread,
479 TArgs
const&...) -> std::size_t {
481 std::size_t bytes = threadsPerBlock[0u] * elemsPerThread[0u] * (5 *
sizeof(
bool) +
sizeof(
char));
487 #endif // RecoLocalCalo_EcalRecProducers_plugins_AmplitudeComputationCommonKernels_h
Eigen::Matrix< data_type, SampleVectorSize, SampleVectorSize > SampleMatrix
ALPAKA_FN_ACC void operator()(TAcc const &acc, EcalDigiDeviceCollection::ConstView digisDevEB, EcalDigiDeviceCollection::ConstView digisDevEE, EcalUncalibratedRecHitDeviceCollection::View uncalibRecHitsEB, EcalUncalibratedRecHitDeviceCollection::View uncalibRecHitsEE, EcalMultifitConditionsDevice::ConstView conditionsDev, ::ecal::multifit::SampleVector *amplitudes, ::ecal::multifit::SampleGainVector *gainsNoise, bool *hasSwitchToGain6, bool *hasSwitchToGain1, bool *isSaturated, char *acState, ::ecal::multifit::BXVectorType *bxs, bool const gainSwitchUseMaxSampleEB, bool const gainSwitchUseMaxSampleEE) const
static ALPAKA_FN_HOST_ACC auto getBlockSharedMemDynSizeBytes(Kernel_prep_1d_and_initialize const &, TVec const &threadsPerBlock, TVec const &elemsPerThread, TArgs const &...) -> std::size_t
Eigen::Matrix< data_type, SampleVectorSize, 1 > SampleVector
Eigen::Matrix< data_type, SampleVectorSize, SampleVectorSize > PulseMatrixType
ALPAKA_FN_ACC uint32_t hashedIndexEB(uint32_t id)
#define EcalMgpaBitwiseGain1
Eigen::Matrix< char, SampleVectorSize, 1 > SampleGainVector
simplifiedNoiseModelForGainSwitch
ALPAKA_FN_ACC uint32_t hashedIndexEE(uint32_t id)
Abs< T >::type abs(const T &t)
constexpr int adc(sample_type sample)
get the ADC sample (12 bits)
constexpr int gainId(sample_type sample)
get the gainId (2 bits)
constexpr int gainId(sample_type sample)
get the gainId (2 bits)
bool isSaturated(const Digi &digi, const int &maxADCvalue, int ifirst, int n)
Eigen::Matrix< char, SampleVectorSize, 1 > BXVectorType
#define EcalMgpaBitwiseGain6
static constexpr int MAXSAMPLES
#define EcalMgpaBitwiseGain0
ALPAKA_FN_ACC void operator()(TAcc const &acc, EcalDigiDeviceCollection::ConstView digisDevEB, EcalDigiDeviceCollection::ConstView digisDevEE, EcalMultifitConditionsDevice::ConstView conditionsDev, ::ecal::multifit::SampleGainVector const *gainsNoise, ::ecal::multifit::SampleMatrix *noisecov, ::ecal::multifit::PulseMatrixType *pulse_matrix, bool const *hasSwitchToGain6, bool const *hasSwitchToGain1, bool const *isSaturated) const
uint16_t *__restrict__ uint16_t const *__restrict__ adc