Skip to content

Commit 47143af

Browse files
committed
add treatment of HGCAL calibration and surrounding cells
1 parent eb45085 commit 47143af

File tree

6 files changed

+145
-14
lines changed

6 files changed

+145
-14
lines changed

CondFormats/HGCalObjects/interface/HGCalMappingParameterSoA.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace hgcal {
3535
SOA_COLUMN(bool, valid),
3636
SOA_COLUMN(bool, isHD),
3737
SOA_COLUMN(bool, iscalib),
38+
SOA_COLUMN(int, offset),
3839
SOA_COLUMN(bool, isSiPM),
3940
SOA_COLUMN(uint16_t, typeidx),
4041
SOA_COLUMN(uint16_t, chip),

Geometry/HGCalMapping/plugins/alpaka/HGCalMappingCellESProducer.cc

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
3030
public:
3131
//
3232
HGCalMappingCellESProducer(const edm::ParameterSet& iConfig)
33-
: ESProducer(iConfig), filelist_(iConfig.getParameter<std::vector<std::string> >("filelist")) {
33+
: ESProducer(iConfig),
34+
filelist_(iConfig.getParameter<std::vector<std::string> >("filelist")),
35+
offsetfile_(iConfig.getParameter<std::string>("offsetfile")) {
3436
auto cc = setWhatProduced(this);
3537
cellIndexTkn_ = cc.consumes(iConfig.getParameter<edm::ESInputTag>("cellindexer"));
3638
}
@@ -41,9 +43,41 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
4143
desc.add<std::vector<std::string> >("filelist", std::vector<std::string>({}))
4244
->setComment("list of files with the readout cells of each module");
4345
desc.add<edm::ESInputTag>("cellindexer", edm::ESInputTag(""))->setComment("Dense cell index tool");
46+
desc.add<std::string>("offsetfile", std::string({}))
47+
->setComment("file containing the offsets between calibration and surrounding cells");
4448
descriptions.addWithDefaultLabel(desc);
4549
}
4650

51+
std::map<std::tuple<std::string, int, int>, int> makeOffsetMap(std::string input_offsetfile) {
52+
std::map<std::tuple<std::string, int, int>, int> offsetMap;
53+
auto offsetfile = edm::FileInPath(input_offsetfile).fullPath();
54+
std::ifstream stream_offsetfile(offsetfile);
55+
std::string line;
56+
// Skip the first line (description of column content)
57+
std::getline(stream_offsetfile, line);
58+
while (std::getline(stream_offsetfile, line)) {
59+
std::istringstream iss(line);
60+
std::string r_typecode_str, r_chip_str, r_half_str, offset_str;
61+
if (!(iss >> r_typecode_str >> r_chip_str >> r_half_str >> offset_str)) {
62+
std::cout << "Error reading offset file" << std::endl;
63+
break;
64+
}
65+
try {
66+
int r_chip = std::stoi(r_chip_str);
67+
int r_half = std::stoi(r_half_str);
68+
int offset = std::stoi(offset_str);
69+
offsetMap[std::make_tuple(r_typecode_str, r_chip, r_half)] = offset;
70+
} catch (const std::invalid_argument& e) {
71+
std::cout << "Invalid argument: " << e.what() << std::endl;
72+
break;
73+
} catch (const std::out_of_range& e) {
74+
std::cout << "Out of range: " << e.what() << std::endl;
75+
break;
76+
}
77+
}
78+
return offsetMap;
79+
}
80+
4781
//
4882
std::optional<HGCalMappingCellParamHost> produce(const HGCalElectronicsMappingRcd& iRecord) {
4983
//get cell indexer
@@ -53,6 +87,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
5387
for (uint32_t i = 0; i < size; i++)
5488
cellParams.view()[i].valid() = false;
5589

90+
auto offsetMap = makeOffsetMap(offsetfile_);
91+
5692
//loop over cell types and then over cells
5793
for (const auto& url : filelist_) {
5894
::hgcal::mappingtools::HGCalEntityList pmap;
@@ -113,6 +149,12 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
113149
cell.trace() = pmap.getFloatAttr("trace", row);
114150
cell.eleid() = HGCalElectronicsId(false, 0, 0, 0, chip * 2 + half, seq).raw();
115151
cell.detid() = detid;
152+
153+
//calibration cell-to-surrounding cell offset. Can be !=0 only for calibration cells
154+
auto mapKey = std::make_tuple(typecode, chip, half);
155+
int offset = (iscalib && offsetMap.find(mapKey) != offsetMap.end()) ? offsetMap[mapKey] : 0;
156+
cell.offset() = offset;
157+
116158
} //end loop over entities
117159
} //end loop over cell types
118160

@@ -122,6 +164,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
122164
private:
123165
edm::ESGetToken<HGCalMappingCellIndexer, HGCalElectronicsMappingRcd> cellIndexTkn_;
124166
const std::vector<std::string> filelist_;
167+
std::string offsetfile_;
125168
};
126169

127170
} // namespace hgcal

Geometry/HGCalMapping/python/hgcalmapping_cff.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
def customise_hgcalmapper(process,
44
modules = 'Geometry/HGCalMapping/data/ModuleMaps/modulelocator_test.txt',
55
sicells = 'Geometry/HGCalMapping/data/CellMaps/WaferCellMapTraces.txt',
6-
sipmcells = 'Geometry/HGCalMapping/data/CellMaps/channels_sipmontile.hgcal.txt'):
6+
sipmcells = 'Geometry/HGCalMapping/data/CellMaps/channels_sipmontile.hgcal.txt',
7+
offsetfile = 'Geometry/HGCalMapping/data/CellMaps/offsetMap.txt'):
78
"""the following function configures the mapping producers
89
NOTE: for production-targetted configs should be avoided as it checks if the process as
910
already the Accelerators sequence loaded, if not it loads it to the process"""
@@ -18,7 +19,8 @@ def customise_hgcalmapper(process,
1819

1920
process.hgCalMappingCellESProducer = cms.ESProducer('hgcal::HGCalMappingCellESProducer@alpaka',
2021
filelist=cms.vstring(sicells, sipmcells),
21-
cellindexer=cms.ESInputTag(''))
22+
cellindexer=cms.ESInputTag(''),
23+
offsetfile=cms.string(offsetfile))
2224
process.hgCalMappingModuleESProducer = cms.ESProducer('hgcal::HGCalMappingModuleESProducer@alpaka',
2325
filename=cms.FileInPath(modules),
2426
moduleindexer=cms.ESInputTag(''))

RecoLocalCalo/HGCalRecAlgos/interface/alpaka/HGCalRecHitCalibrationAlgorithms.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
#include "DataFormats/HGCalRecHit/interface/alpaka/HGCalRecHitDevice.h"
1717
#include "CondFormats/HGCalObjects/interface/HGCalCalibrationParameterHost.h"
1818
#include "CondFormats/HGCalObjects/interface/alpaka/HGCalCalibrationParameterDevice.h"
19+
#include "CondFormats/HGCalObjects/interface/HGCalMappingParameterHost.h"
20+
#include "CondFormats/HGCalObjects/interface/alpaka/HGCalMappingParameterDevice.h"
1921

2022
namespace ALPAKA_ACCELERATOR_NAMESPACE {
2123

2224
using namespace hgcaldigi;
2325
using namespace hgcalrechit;
26+
using namespace hgcal;
2427

2528
class HGCalRecHitCalibrationAlgorithms {
2629
public:
@@ -29,7 +32,9 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
2932
HGCalRecHitDevice calibrate(Queue& queue,
3033
HGCalDigiHost const& host_digis,
3134
HGCalCalibParamDevice const& device_calib,
32-
HGCalConfigParamDevice const& device_config) const;
35+
HGCalConfigParamDevice const& device_config,
36+
HGCalMappingCellParamDevice const& device_mapping,
37+
HGCalDenseIndexInfoDevice const& device_index) const;
3338

3439
private:
3540
void print(HGCalDigiHost const& digis, int max = -1) const;

RecoLocalCalo/HGCalRecAlgos/plugins/alpaka/HGCalRecHitCalibrationAlgorithms.dev.cc

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
2828
auto digi = digis[idx];
2929
auto digiflags = digi.flags();
3030
//recHits[idx].flags() = digiflags;
31-
bool isAvailable((digiflags != hgcal::DIGI_FLAG::Invalid) && (digiflags != hgcal::DIGI_FLAG::NotAvailable) &&
32-
calibvalid);
33-
bool isToAavailable((digiflags != hgcal::DIGI_FLAG::ZS_ToA) && (digiflags != hgcal::DIGI_FLAG::ZS_ToA_ADCm1));
31+
bool isAvailable((digiflags != ::hgcal::DIGI_FLAG::Invalid) &&
32+
(digiflags != ::hgcal::DIGI_FLAG::NotAvailable) && calibvalid);
33+
bool isToAavailable((digiflags != ::hgcal::DIGI_FLAG::ZS_ToA) &&
34+
(digiflags != ::hgcal::DIGI_FLAG::ZS_ToA_ADCm1));
3435
recHits[idx].flags() = (!isAvailable) * hgcalrechit::HGCalRecHitFlags::EnergyInvalid +
3536
(!isToAavailable) * hgcalrechit::HGCalRecHitFlags::TimeInvalid;
3637
}
@@ -61,8 +62,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
6162
bool calibvalid = calib.valid();
6263
auto digi = digis[idx];
6364
auto digiflags = digi.flags();
64-
bool isAvailable((digiflags != hgcal::DIGI_FLAG::Invalid) && (digiflags != hgcal::DIGI_FLAG::NotAvailable) &&
65-
calibvalid);
65+
bool isAvailable((digiflags != ::hgcal::DIGI_FLAG::Invalid) &&
66+
(digiflags != ::hgcal::DIGI_FLAG::NotAvailable) && calibvalid);
6667
bool useTOT((digi.tctp() == 3) && isAvailable);
6768
bool useADC(!useTOT && isAvailable);
6869
recHits[idx].energy() = useADC * adc_denoise(digi.adc(),
@@ -111,9 +112,10 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
111112
bool calibvalid = calib.valid();
112113
auto digi = digis[idx];
113114
auto digiflags = digi.flags();
114-
bool isAvailable((digiflags != hgcal::DIGI_FLAG::Invalid) && (digiflags != hgcal::DIGI_FLAG::NotAvailable) &&
115-
calibvalid);
116-
bool isToAavailable((digiflags != hgcal::DIGI_FLAG::ZS_ToA) && (digiflags != hgcal::DIGI_FLAG::ZS_ToA_ADCm1));
115+
bool isAvailable((digiflags != ::hgcal::DIGI_FLAG::Invalid) &&
116+
(digiflags != ::hgcal::DIGI_FLAG::NotAvailable) && calibvalid);
117+
bool isToAavailable((digiflags != ::hgcal::DIGI_FLAG::ZS_ToA) &&
118+
(digiflags != ::hgcal::DIGI_FLAG::ZS_ToA_ADCm1));
117119
bool isGood(isAvailable && isToAavailable);
118120
//INL correction
119121
auto toa = isGood * toa_inl_corr(digi.toa(), calib.TOA_CTDC(), calib.TOA_FTDC());
@@ -125,6 +127,59 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
125127
}
126128
};
127129

130+
struct HGCalRecHitCalibrationKernel_handleCalibCell {
131+
ALPAKA_FN_ACC void operator()(Acc1D const& acc,
132+
HGCalDigiDevice::View digis,
133+
HGCalRecHitDevice::View recHits,
134+
HGCalCalibParamDevice::ConstView calibs,
135+
HGCalMappingCellParamDevice::ConstView maps,
136+
HGCalDenseIndexInfoDevice::ConstView index) const {
137+
auto time_average = [&](float time_surr, float time_calib, float energy_surr, float energy_calib) {
138+
bool is_time_surr(time_surr > 0);
139+
bool is_time_calib(time_calib > 0);
140+
float totalEn = (is_time_surr * energy_surr + is_time_calib * energy_calib);
141+
float weighted_average =
142+
(totalEn > 0)
143+
? (is_time_surr * energy_surr * time_surr + is_time_calib * energy_calib * time_calib) / totalEn
144+
: 0.0f;
145+
return weighted_average;
146+
};
147+
148+
for (auto idx : uniform_elements(acc, digis.metadata().size())) {
149+
auto calib = calibs[idx];
150+
bool calibvalid = calib.valid();
151+
auto digi = digis[idx];
152+
auto digiflags = digi.flags();
153+
bool isAvailable((digiflags != ::hgcal::DIGI_FLAG::Invalid) &&
154+
(digiflags != ::hgcal::DIGI_FLAG::NotAvailable) && calibvalid);
155+
bool isToAavailable((digiflags != ::hgcal::DIGI_FLAG::ZS_ToA) &&
156+
(digiflags != ::hgcal::DIGI_FLAG::ZS_ToA_ADCm1));
157+
158+
auto cellIndex = index[idx].cellInfoIdx();
159+
bool isCalibCell(maps[cellIndex].iscalib());
160+
int offset = maps[cellIndex].offset();
161+
bool is_surr_cell((offset != 0) && isAvailable && isCalibCell);
162+
163+
//Effectively operate only on the cell that surrounds the calibration cells
164+
if (!is_surr_cell) {
165+
continue;
166+
}
167+
168+
recHits[idx + offset].flags() = hgcalrechit::HGCalRecHitFlags::Normal;
169+
170+
recHits[idx + offset].time() = isToAavailable * time_average(recHits[idx + offset].time(),
171+
recHits[idx].time(),
172+
recHits[idx + offset].energy(),
173+
recHits[idx].energy());
174+
175+
bool is_negative_surr_energy(recHits[idx + offset].energy() < 0);
176+
auto negative_energy_correction = (-1.0 * recHits[idx + offset].energy()) * is_negative_surr_energy;
177+
178+
recHits[idx + offset].energy() += (negative_energy_correction + recHits[idx].energy());
179+
}
180+
}
181+
};
182+
128183
struct HGCalRecHitCalibrationKernel_printRecHits {
129184
ALPAKA_FN_ACC void operator()(Acc1D const& acc, HGCalRecHitDevice::ConstView view, int size) const {
130185
for (int i = 0; i < size; ++i) {
@@ -137,7 +192,9 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
137192
HGCalRecHitDevice HGCalRecHitCalibrationAlgorithms::calibrate(Queue& queue,
138193
HGCalDigiHost const& host_digis,
139194
HGCalCalibParamDevice const& device_calib,
140-
HGCalConfigParamDevice const& device_config) const {
195+
HGCalConfigParamDevice const& device_config,
196+
HGCalMappingCellParamDevice const& device_mapping,
197+
HGCalDenseIndexInfoDevice const& device_index) const {
141198
LogDebug("HGCalRecHitCalibrationAlgorithms") << "\n\nINFO -- Start of calibrate\n\n" << std::endl;
142199

143200
LogDebug("HGCalRecHitCalibrationAlgorithms") << "\n\nINFO -- Copying the digis to the device\n\n" << std::endl;
@@ -176,6 +233,14 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
176233
device_digis.view(),
177234
device_recHits.view(),
178235
device_calib.view());
236+
alpaka::exec<Acc1D>(queue,
237+
grid,
238+
HGCalRecHitCalibrationKernel_handleCalibCell{},
239+
device_digis.view(),
240+
device_recHits.view(),
241+
device_calib.view(),
242+
device_mapping.view(),
243+
device_index.view());
179244

180245
LogDebug("HGCalRecHitCalibrationAlgorithms") << "Input recHits: " << std::endl;
181246
#ifdef EDM_ML_DEBUG

RecoLocalCalo/HGCalRecAlgos/plugins/alpaka/HGCalRecHitProducers.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "CondFormats/HGCalObjects/interface/HGCalCalibrationParameterHost.h"
3333
#include "CondFormats/HGCalObjects/interface/alpaka/HGCalCalibrationParameterDevice.h"
3434
#include "RecoLocalCalo/HGCalRecAlgos/interface/alpaka/HGCalRecHitCalibrationAlgorithms.h"
35+
#include "CondFormats/HGCalObjects/interface/alpaka/HGCalMappingParameterDevice.h"
36+
#include "CondFormats/DataRecord/interface/HGCalDenseIndexInfoRcd.h"
3537

3638
// flag to assist the computational performance test
3739
// #define HGCAL_PERF_TEST
@@ -52,6 +54,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
5254
const edm::EDGetTokenT<hgcaldigi::HGCalDigiHost> digisToken_;
5355
edm::ESGetToken<hgcalrechit::HGCalCalibParamHost, HGCalModuleConfigurationRcd> calibToken_;
5456
device::ESGetToken<hgcalrechit::HGCalConfigParamDevice, HGCalModuleConfigurationRcd> configToken_;
57+
device::ESGetToken<hgcal::HGCalMappingCellParamDevice, HGCalElectronicsMappingRcd> mappingToken_;
58+
device::ESGetToken<hgcal::HGCalDenseIndexInfoDevice, HGCalDenseIndexInfoRcd> indexingToken_;
5559
const device::EDPutToken<hgcalrechit::HGCalRecHitDevice> recHitsToken_;
5660
const HGCalRecHitCalibrationAlgorithms calibrator_;
5761
const int n_hits_scale;
@@ -62,6 +66,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
6266
digisToken_{consumes<hgcaldigi::HGCalDigiHost>(iConfig.getParameter<edm::InputTag>("digis"))},
6367
calibToken_{esConsumes(iConfig.getParameter<edm::ESInputTag>("calibSource"))},
6468
configToken_{esConsumes(iConfig.getParameter<edm::ESInputTag>("configSource"))},
69+
mappingToken_{esConsumes(iConfig.getParameter<edm::ESInputTag>("mappingSource"))},
70+
indexingToken_{esConsumes(iConfig.getParameter<edm::ESInputTag>("indexingSource"))},
6571
recHitsToken_{produces()},
6672
calibrator_{iConfig.getParameter<int>("n_blocks"), iConfig.getParameter<int>("n_threads")},
6773
n_hits_scale{iConfig.getParameter<int>("n_hits_scale")} {
@@ -77,6 +83,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
7783
desc.add<edm::InputTag>("digis", edm::InputTag("hgcalDigis", "DIGI", "TEST"));
7884
desc.add("calibSource", edm::ESInputTag{})->setComment("Label for calibration parameters");
7985
desc.add("configSource", edm::ESInputTag{})->setComment("Label for ROC configuration parameters");
86+
desc.add("mappingSource", edm::ESInputTag{})->setComment("Label for cell mapping parameters");
87+
desc.add("indexingSource", edm::ESInputTag{})->setComment("Label for cell dense indexer");
8088
desc.add<int>("n_blocks", -1);
8189
desc.add<int>("n_threads", -1);
8290
desc.add<int>("n_hits_scale", -1);
@@ -89,6 +97,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
8997
// Read digis
9098
auto const& hostCalibParamProvider = iSetup.getData(calibToken_);
9199
auto const& deviceConfigParamProvider = iSetup.getData(configToken_);
100+
auto const& deviceMappingCellParamProvider = iSetup.getData(mappingToken_);
101+
auto const& deviceIndexingParamProvider = iSetup.getData(indexingToken_);
92102
auto const& hostDigisIn = iEvent.get(digisToken_);
93103

94104
//printout new conditions if available
@@ -161,7 +171,12 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
161171
HGCalRecHitDevice recHits(oldSize, queue);
162172
alpaka::memcpy(queue, recHits.buffer(), tmpRecHits.const_buffer(), oldSize);
163173
#else
164-
auto recHits = calibrator_.calibrate(queue, hostDigis, deviceCalibParam, deviceConfigParamProvider);
174+
auto recHits = calibrator_.calibrate(queue,
175+
hostDigis,
176+
deviceCalibParam,
177+
deviceConfigParamProvider,
178+
deviceMappingCellParamProvider,
179+
deviceIndexingParamProvider);
165180
#endif
166181

167182
#ifdef EDM_ML_DEBUG

0 commit comments

Comments
 (0)