Skip to content

Commit 80ea2bb

Browse files
mfasDashahor02
authored andcommitted
[EMCAL-600] Make arguments for row, col and hwaddress unsigned in mapper
Simplifies error handling: Addresses and positions are defined only for positive values, implicit casts from potential negative entries will always be outside the accepted range which are already handled with several exceptions.
1 parent fa1c93b commit 80ea2bb

2 files changed

Lines changed: 25 additions & 20 deletions

File tree

Detectors/EMCAL/base/include/EMCALBase/Mapper.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,13 @@ class Mapper
238238
/// \return Channel params corresponding to the hardware address
239239
/// \throw InitStatusException in case the mapping was not properly initialized
240240
/// \throw AddressNotFoundException in case of invalid hardware address
241-
ChannelID getChannelID(int hardawareaddress) const;
241+
ChannelID getChannelID(unsigned int hardawareaddress) const;
242242

243243
/// \brief Get channel row for a given hardware address
244244
/// \return Row corresponding to the hardware address
245245
/// \throw InitStatusException in case the mapping was not properly initialized
246246
/// \throw AddressNotFoundException in case of invalid hardware address
247-
int getRow(int hardawareaddress) const
247+
uint8_t getRow(unsigned int hardawareaddress) const
248248
{
249249
return getChannelID(hardawareaddress).mRow;
250250
}
@@ -253,7 +253,7 @@ class Mapper
253253
/// \return Column corresponding to the hardware address
254254
/// \throw InitStatusException in case the mapping was not properly initialized
255255
/// \throw AddressNotFoundException in case of invalid hardware address
256-
int getColumn(int hardawareaddress) const
256+
uint8_t getColumn(unsigned int hardawareaddress) const
257257
{
258258
return getChannelID(hardawareaddress).mColumn;
259259
}
@@ -262,7 +262,7 @@ class Mapper
262262
/// \return Channel type corresponding to the hardware address
263263
/// \throw InitStatusException in case the mapping was not properly initialized
264264
/// \throw AddressNotFoundException in case of invalid hardware address
265-
ChannelType_t getChannelType(int hardawareaddress) const
265+
ChannelType_t getChannelType(unsigned int hardawareaddress) const
266266
{
267267
return getChannelID(hardawareaddress).mChannelType;
268268
}
@@ -273,7 +273,7 @@ class Mapper
273273
/// \param channeltype type of the channel
274274
/// \return Harware address of the channel
275275
/// \throw InitStatusException in case the mapping was not properly initialized
276-
int getHardwareAddress(int row, int col, ChannelType_t channeltype) const;
276+
unsigned int getHardwareAddress(uint8_t row, uint8_t col, ChannelType_t channeltype) const;
277277

278278
/// \brief Initialize with new
279279
/// \param inputfile Name of the input file from which to read the mapping
@@ -290,9 +290,9 @@ class Mapper
290290
/// \throw ChannelTypeException in case hardware address with unkknown channel types are found
291291
void init(const std::string_view inputfile);
292292

293-
std::unordered_map<int, ChannelID> mMapping; ///< Mapping between hardware address and col / row /caloflag
294-
std::unordered_map<ChannelID, int, ChannelIDHasher> mInverseMapping; ///< Inverse Mapping of channel type to hardware address
295-
bool mInitStatus = false; ///< Initialization status
293+
std::unordered_map<unsigned int, ChannelID> mMapping; ///< Mapping between hardware address and col / row /caloflag
294+
std::unordered_map<ChannelID, unsigned int, ChannelIDHasher> mInverseMapping; ///< Inverse Mapping of channel type to hardware address
295+
bool mInitStatus = false; ///< Initialization status
296296

297297
ClassDefNV(Mapper, 1);
298298
};

Detectors/EMCAL/base/src/Mapper.cxx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ void Mapper::init(const std::string_view filename)
6666

6767
auto chantype = o2::emcal::intToChannelType(caloflag);
6868

69-
mMapping.insert(std::pair<int, ChannelID>(address, {uint8_t(row), uint8_t(col), chantype}));
70-
mInverseMapping.insert(std::pair<ChannelID, int>({uint8_t(row), uint8_t(col), chantype}, address));
69+
mMapping.insert(std::pair<int, ChannelID>(static_cast<unsigned int>(address), {uint8_t(row), uint8_t(col), chantype}));
70+
mInverseMapping.insert(std::pair<ChannelID, int>({uint8_t(row), uint8_t(col), chantype}, static_cast<unsigned int>(address)));
7171
}
7272

7373
mInitStatus = true;
7474
}
7575

76-
Mapper::ChannelID Mapper::getChannelID(int hardawareaddress) const
76+
Mapper::ChannelID Mapper::getChannelID(unsigned int hardawareaddress) const
7777
{
7878
if (!mInitStatus) {
7979
throw InitStatusException();
@@ -85,12 +85,12 @@ Mapper::ChannelID Mapper::getChannelID(int hardawareaddress) const
8585
return res->second;
8686
}
8787

88-
int Mapper::getHardwareAddress(int row, int col, ChannelType_t channeltype) const
88+
unsigned int Mapper::getHardwareAddress(uint8_t row, uint8_t col, ChannelType_t channeltype) const
8989
{
9090
if (!mInitStatus) {
9191
throw InitStatusException();
9292
}
93-
ChannelID channelToFind{uint8_t(row), uint8_t(col), channeltype};
93+
ChannelID channelToFind{row, col, channeltype};
9494
auto found = mInverseMapping.find(channelToFind);
9595
if (found == mInverseMapping.end()) {
9696
throw ChannelNotFoundException(channelToFind);
@@ -101,9 +101,9 @@ int Mapper::getHardwareAddress(int row, int col, ChannelType_t channeltype) cons
101101
MappingHandler::MappingHandler()
102102
{
103103
const std::array<char, 2> SIDES = {{'A', 'C'}};
104-
const int NDDL = 2;
105-
for (int iside = 0; iside < 2; iside++) {
106-
for (int iddl = 0; iddl < NDDL; iddl++) {
104+
const unsigned int NDDL = 2;
105+
for (unsigned int iside = 0; iside < 2; iside++) {
106+
for (unsigned int iddl = 0; iddl < NDDL; iddl++) {
107107
mMappings[iside * NDDL + iddl].setMapping(Form("%s/share/Detectors/EMC/files/RCU%d%c.data", gSystem->Getenv("O2_ROOT"), iddl, SIDES[iside]));
108108
}
109109
}
@@ -114,10 +114,15 @@ Mapper& MappingHandler::getMappingForDDL(unsigned int ddl)
114114
if (ddl >= 40) {
115115
throw MappingHandler::DDLInvalid(ddl);
116116
}
117-
const int NDDLSM = 2, NSIDES = 2;
118-
int ddlInSM = ddl % NDDLSM,
119-
sideID = (ddl / NDDLSM) % NSIDES;
120-
return mMappings[sideID * NDDLSM + ddlInSM];
117+
const unsigned int NDDLSM = 2, NSIDES = 2;
118+
unsigned int ddlInSM = ddl % NDDLSM,
119+
sideID = (ddl / NDDLSM) % NSIDES;
120+
unsigned int mappingIndex = sideID * NDDLSM + ddlInSM;
121+
if (mappingIndex < 0 || mappingIndex >= mMappings.size()) {
122+
std::cout << "Access to invalid mapping position for ddl " << ddl << std::endl;
123+
throw MappingHandler::DDLInvalid(ddl);
124+
}
125+
return mMappings[mappingIndex];
121126
}
122127

123128
int MappingHandler::getFEEForChannelInDDL(unsigned int ddl, unsigned int channelFEC, unsigned int branch)

0 commit comments

Comments
 (0)