Skip to content

Commit 1188ecb

Browse files
matthiasrichtersawenzel
authored andcommitted
Implementing std::hash specialization for MCCompLabel
Restore PR AliceO2Group#1645 after resolving compilation issue. Previous implementation in commit b8470f8 has been reverted in commit 9497df2. Compilation error on macos was simply caused by missing header <unordered_map>.
1 parent 6d23c71 commit 1188ecb

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

DataFormats/simulation/include/SimulationDataFormat/MCCompLabel.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class MCCompLabel
2525

2626
ULong64_t mLabel = NotSet; ///< MC label encoding MCtrack ID and MCevent origin
2727

28-
void checkFieldConsistensy();
29-
3028
public:
3129
// number of bits reserved for MC track ID, DON'T modify this, since the
3230
// track ID might be negative
@@ -35,6 +33,11 @@ class MCCompLabel
3533
static constexpr int nbitsSrcID = 8; // number of bits reserved for MC source ID
3634
// the rest of the bits is reserved at the moment
3735

36+
// check if the fields are defined consistently
37+
static_assert(nbitsTrackID == sizeof(int) * 8, "TrackID must have int size");
38+
static_assert(nbitsTrackID + nbitsEvID + nbitsSrcID <= sizeof(ULong64_t) * 8,
39+
"Fields cannot be stored in 64 bits");
40+
3841
// mask to extract MC track ID
3942
static constexpr ULong64_t maskTrackID = (ul0x1 << nbitsTrackID) - 1;
4043
// mask to extract MC track ID
@@ -105,4 +108,17 @@ class MCCompLabel
105108

106109
std::ostream& operator<<(std::ostream& os, const o2::MCCompLabel& c);
107110

111+
namespace std
112+
{
113+
// defining std::hash for MCCompLabel in order to be used with unordered_maps
114+
template <>
115+
struct hash<o2::MCCompLabel> {
116+
public:
117+
size_t operator()(o2::MCCompLabel const& label) const
118+
{
119+
return static_cast<uint64_t>(label);
120+
}
121+
};
122+
} // namespace std
123+
108124
#endif

DataFormats/simulation/src/MCCompLabel.cxx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,3 @@ std::ostream& operator<<(std::ostream& os, const o2::MCCompLabel& c)
3737
}
3838
return os;
3939
}
40-
41-
//_____________________________________________
42-
void MCCompLabel::checkFieldConsistensy()
43-
{
44-
// check if the fields are defined consistently
45-
static_assert(nbitsTrackID==sizeof(int)*8, "TrackID must have int size");
46-
static_assert(nbitsTrackID+nbitsEvID+nbitsSrcID<=sizeof(ULong64_t)*8,
47-
"Fields cannot be stored in 64 bits");
48-
}

DataFormats/simulation/test/testMCCompLabel.cxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <iomanip>
1616
#include <ios>
1717
#include <iostream>
18+
#include <unordered_map>
1819
#include "SimulationDataFormat/MCCompLabel.h"
1920

2021
using namespace o2;
@@ -25,16 +26,22 @@ BOOST_AUTO_TEST_CASE(MCCompLabel_test)
2526
BOOST_CHECK(!lbUndef.isSet()); // test invalid label status
2627

2728
int ev = 200, src = 10;
29+
std::unordered_map<MCCompLabel, int> labelMap;
2830
for (int tr=-100;tr<200;tr+=150) {
2931
MCCompLabel lb(tr, ev, src);
3032
std::cout << "Input: [" << src << '/' << ev << '/'
3133
<< std::setw(6) << tr << ']' << std::endl;
3234
std::cout << "Encoded: " << lb << " (packed: " << ULong_t(lb) << ")" << std::endl;
35+
labelMap[lb] = tr;
3336
int trE, evE, srcE;
3437
lb.get(trE, evE, srcE);
3538
std::cout << "Decoded: [" << srcE << '/' << evE << '/'
3639
<< std::setw(6) << trE << ']' << std::endl;
3740

3841
BOOST_CHECK(tr == trE && ev == evE && src == srcE);
3942
}
43+
44+
for (auto& [key, value] : labelMap) {
45+
BOOST_CHECK(key.getTrackID() == value);
46+
}
4047
}

0 commit comments

Comments
 (0)