From 86b4113919c51acbee7d429cc476d6a6def20c82 Mon Sep 17 00:00:00 2001 From: Matthias Richter Date: Mon, 25 Feb 2019 21:11:37 +0100 Subject: [PATCH] Implementing std::hash specialization for MCCompLabel Restore PR #1645 after resolving compilation issue. Previous implementation in commit b8470f8bde8d283fa0cec33da9aa24e91ed3e142 has been reverted in commit 9497df2fe71655ed4a2519dfd54e8ff74de6fb39. Compilation error on macos was simply caused by missing header . --- .../SimulationDataFormat/MCCompLabel.h | 20 +++++++++++++++++-- DataFormats/simulation/src/MCCompLabel.cxx | 9 --------- .../simulation/test/testMCCompLabel.cxx | 7 +++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/DataFormats/simulation/include/SimulationDataFormat/MCCompLabel.h b/DataFormats/simulation/include/SimulationDataFormat/MCCompLabel.h index 07e04b0ddb90b..0e535a973ca7e 100644 --- a/DataFormats/simulation/include/SimulationDataFormat/MCCompLabel.h +++ b/DataFormats/simulation/include/SimulationDataFormat/MCCompLabel.h @@ -25,8 +25,6 @@ class MCCompLabel ULong64_t mLabel = NotSet; ///< MC label encoding MCtrack ID and MCevent origin - void checkFieldConsistensy(); - public: // number of bits reserved for MC track ID, DON'T modify this, since the // track ID might be negative @@ -35,6 +33,11 @@ class MCCompLabel static constexpr int nbitsSrcID = 8; // number of bits reserved for MC source ID // the rest of the bits is reserved at the moment + // check if the fields are defined consistently + static_assert(nbitsTrackID == sizeof(int) * 8, "TrackID must have int size"); + static_assert(nbitsTrackID + nbitsEvID + nbitsSrcID <= sizeof(ULong64_t) * 8, + "Fields cannot be stored in 64 bits"); + // mask to extract MC track ID static constexpr ULong64_t maskTrackID = (ul0x1 << nbitsTrackID) - 1; // mask to extract MC track ID @@ -105,4 +108,17 @@ class MCCompLabel std::ostream& operator<<(std::ostream& os, const o2::MCCompLabel& c); +namespace std +{ +// defining std::hash for MCCompLabel in order to be used with unordered_maps +template <> +struct hash { + public: + size_t operator()(o2::MCCompLabel const& label) const + { + return static_cast(label); + } +}; +} // namespace std + #endif diff --git a/DataFormats/simulation/src/MCCompLabel.cxx b/DataFormats/simulation/src/MCCompLabel.cxx index 3ec39a41bd5f9..4477a7b6071a8 100644 --- a/DataFormats/simulation/src/MCCompLabel.cxx +++ b/DataFormats/simulation/src/MCCompLabel.cxx @@ -37,12 +37,3 @@ std::ostream& operator<<(std::ostream& os, const o2::MCCompLabel& c) } return os; } - - //_____________________________________________ -void MCCompLabel::checkFieldConsistensy() -{ - // check if the fields are defined consistently - static_assert(nbitsTrackID==sizeof(int)*8, "TrackID must have int size"); - static_assert(nbitsTrackID+nbitsEvID+nbitsSrcID<=sizeof(ULong64_t)*8, - "Fields cannot be stored in 64 bits"); -} diff --git a/DataFormats/simulation/test/testMCCompLabel.cxx b/DataFormats/simulation/test/testMCCompLabel.cxx index 61bb5610018df..713566ac228ce 100644 --- a/DataFormats/simulation/test/testMCCompLabel.cxx +++ b/DataFormats/simulation/test/testMCCompLabel.cxx @@ -15,6 +15,7 @@ #include #include #include +#include #include "SimulationDataFormat/MCCompLabel.h" using namespace o2; @@ -25,11 +26,13 @@ BOOST_AUTO_TEST_CASE(MCCompLabel_test) BOOST_CHECK(!lbUndef.isSet()); // test invalid label status int ev = 200, src = 10; + std::unordered_map labelMap; for (int tr=-100;tr<200;tr+=150) { MCCompLabel lb(tr, ev, src); std::cout << "Input: [" << src << '/' << ev << '/' << std::setw(6) << tr << ']' << std::endl; std::cout << "Encoded: " << lb << " (packed: " << ULong_t(lb) << ")" << std::endl; + labelMap[lb] = tr; int trE, evE, srcE; lb.get(trE, evE, srcE); std::cout << "Decoded: [" << srcE << '/' << evE << '/' @@ -37,4 +40,8 @@ BOOST_AUTO_TEST_CASE(MCCompLabel_test) BOOST_CHECK(tr == trE && ev == evE && src == srcE); } + + for (auto& [key, value] : labelMap) { + BOOST_CHECK(key.getTrackID() == value); + } }