Skip to content

Commit b77beb9

Browse files
authored
Merge pull request #47829 from RSalvatico/HGCALCalibrationCell
Add treatment of HGCAL calibration and surrounding cells
2 parents e04f43e + c61b77e commit b77beb9

File tree

7 files changed

+162
-16
lines changed

7 files changed

+162
-16
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, caliboffset),
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: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "FWCore/ParameterSet/interface/FileInPath.h"
2+
#include "FWCore/MessageLogger/interface/MessageLogger.h"
23
#include "FWCore/ParameterSet/interface/ParameterSet.h"
34
#include "FWCore/Utilities/interface/ESGetToken.h"
45

@@ -12,6 +13,7 @@
1213
#include "CondFormats/HGCalObjects/interface/HGCalMappingCellIndexer.h"
1314
#include "CondFormats/HGCalObjects/interface/HGCalMappingParameterHost.h"
1415
#include "CondFormats/HGCalObjects/interface/alpaka/HGCalMappingParameterDevice.h"
16+
#include "CondFormats/HGCalObjects/interface/HGCalMappingModuleIndexer.h"
1517
#include "DataFormats/HGCalDigi/interface/HGCalElectronicsId.h"
1618
#include "DataFormats/ForwardDetId/interface/HGCSiliconDetId.h"
1719
#include "DataFormats/ForwardDetId/interface/HGCScintillatorDetId.h"
@@ -21,6 +23,7 @@
2123
#include <iostream>
2224
#include <fstream>
2325
#include <sstream>
26+
#include <charconv>
2427

2528
namespace ALPAKA_ACCELERATOR_NAMESPACE {
2629

@@ -30,9 +33,12 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
3033
public:
3134
//
3235
HGCalMappingCellESProducer(const edm::ParameterSet& iConfig)
33-
: ESProducer(iConfig), filelist_(iConfig.getParameter<std::vector<std::string> >("filelist")) {
36+
: ESProducer(iConfig),
37+
filelist_(iConfig.getParameter<std::vector<std::string> >("filelist")),
38+
offsetfile_(iConfig.getParameter<edm::FileInPath>("offsetfile")) {
3439
auto cc = setWhatProduced(this);
3540
cellIndexTkn_ = cc.consumes(iConfig.getParameter<edm::ESInputTag>("cellindexer"));
41+
moduleIndexTkn_ = cc.consumes(iConfig.getParameter<edm::ESInputTag>("moduleindexer"));
3642
}
3743

3844
//
@@ -41,18 +47,59 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
4147
desc.add<std::vector<std::string> >("filelist", std::vector<std::string>({}))
4248
->setComment("list of files with the readout cells of each module");
4349
desc.add<edm::ESInputTag>("cellindexer", edm::ESInputTag(""))->setComment("Dense cell index tool");
50+
desc.add<edm::ESInputTag>("moduleindexer", edm::ESInputTag(""))->setComment("Module index tool");
51+
desc.add<edm::FileInPath>(
52+
"offsetfile",
53+
edm::FileInPath("Geometry/HGCalMapping/data/CellMaps/calibration_to_surrounding_offsetMap.txt"))
54+
->setComment("file containing the offsets between calibration and surrounding cells");
4455
descriptions.addWithDefaultLabel(desc);
4556
}
4657

58+
std::map<int, int> makeOffsetMap(edm::FileInPath input_offsetfile,
59+
const HGCalMappingCellIndexer& cellIndexer,
60+
const HGCalMappingModuleIndexer& moduleIndexer) {
61+
std::map<int, int> offsetMap;
62+
const auto& offsetfile = input_offsetfile.fullPath();
63+
::hgcal::mappingtools::HGCalEntityList omap;
64+
edm::FileInPath fip(offsetfile);
65+
omap.buildFrom(fip.fullPath());
66+
auto& mapEntries = omap.getEntries();
67+
68+
for (auto row : mapEntries) {
69+
std::string typecode = omap.getAttr("Typecode", row);
70+
const auto& allTypecodes = moduleIndexer.getTypecodeMap();
71+
// Skip if typecode is not in the module indexer
72+
bool typecodeFound = false;
73+
for (const auto& key : allTypecodes) {
74+
if (key.first.find(typecode) != std::string::npos) {
75+
typecodeFound = true;
76+
break;
77+
}
78+
}
79+
if (!typecodeFound)
80+
continue;
81+
int roc = omap.getIntAttr("ROC", row);
82+
int halfroc = omap.getIntAttr("HalfROC", row);
83+
int readoutsequence = omap.getIntAttr("Seq", row);
84+
int offset = omap.getIntAttr("Offset", row);
85+
int idx = cellIndexer.denseIndex(typecode, roc, halfroc, readoutsequence);
86+
offsetMap[idx] = offset;
87+
}
88+
return offsetMap;
89+
}
90+
4791
//
4892
std::optional<HGCalMappingCellParamHost> produce(const HGCalElectronicsMappingRcd& iRecord) {
49-
//get cell indexer
93+
//get cell and module indexers
5094
const HGCalMappingCellIndexer& cellIndexer = iRecord.get(cellIndexTkn_);
95+
const HGCalMappingModuleIndexer& moduleIndexer = iRecord.get(moduleIndexTkn_);
5196
const uint32_t size = cellIndexer.maxDenseIndex(); // channel-level size
5297
HGCalMappingCellParamHost cellParams(size, cms::alpakatools::host());
5398
for (uint32_t i = 0; i < size; i++)
5499
cellParams.view()[i].valid() = false;
55100

101+
auto offsetMap = makeOffsetMap(offsetfile_, cellIndexer, moduleIndexer);
102+
56103
//loop over cell types and then over cells
57104
for (const auto& url : filelist_) {
58105
::hgcal::mappingtools::HGCalEntityList pmap;
@@ -113,6 +160,10 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
113160
cell.trace() = pmap.getFloatAttr("trace", row);
114161
cell.eleid() = HGCalElectronicsId(false, 0, 0, 0, chip * 2 + half, seq).raw();
115162
cell.detid() = detid;
163+
164+
int offset = (iscalib && offsetMap.find(idx) != offsetMap.end()) ? offsetMap[idx] : 0;
165+
cell.caliboffset() = offset;
166+
116167
} //end loop over entities
117168
} //end loop over cell types
118169

@@ -121,7 +172,9 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
121172

122173
private:
123174
edm::ESGetToken<HGCalMappingCellIndexer, HGCalElectronicsMappingRcd> cellIndexTkn_;
175+
edm::ESGetToken<HGCalMappingModuleIndexer, HGCalElectronicsMappingRcd> moduleIndexTkn_;
124176
const std::vector<std::string> filelist_;
177+
edm::FileInPath offsetfile_;
125178
};
126179

127180
} // 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/calibration_to_surrounding_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.FileInPath(offsetfile))
2224
process.hgCalMappingModuleESProducer = cms.ESProducer('hgcal::HGCalMappingModuleESProducer@alpaka',
2325
filename=cms.FileInPath(modules),
2426
moduleindexer=cms.ESInputTag(''))

Geometry/HGCalMapping/test/testMappingModuleIndexer_cfg.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
info="Path to Si cell mapper. Absolute, or relative to CMSSW src directory")
1010
options.register('sipmcells','Geometry/HGCalMapping/data/CellMaps/channels_sipmontile.hgcal.txt',mytype=VarParsing.varType.string,
1111
info="Path to SiPM-on-tile cell mapper. Absolute, or relative to CMSSW src directory")
12+
options.register('offsetfile','Geometry/HGCalMapping/data/CellMaps/calibration_to_surrounding_offsetMap.txt',mytype=VarParsing.varType.cms.FileInPath,
13+
info="Path to calibration-to-surrounding cell offset file. Absolute, or relative to CMSSW src directory")
14+
1215
options.parseArguments()
1316

1417
process.source = cms.Source('EmptySource')
@@ -22,7 +25,8 @@
2225
process = customise_hgcalmapper(process,
2326
modules=options.modules,
2427
sicells=options.sicells,
25-
sipmcells=options.sipmcells)
28+
sipmcells=options.sipmcells,
29+
offsetfile=options.offsetfile)
2630

2731
#Geometry
2832
process.load('Configuration.Geometry.GeometryExtended2026D99Reco_cff')

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].caliboffset(); //Calibration-to-surrounding cell 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

0 commit comments

Comments
 (0)