diff --git a/DataFormats/simulation/include/SimulationDataFormat/MCCompLabel.h b/DataFormats/simulation/include/SimulationDataFormat/MCCompLabel.h index e630320bcdd5f..5be32d31fb9eb 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 @@ -102,4 +105,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..668f40fdcca67 100644 --- a/DataFormats/simulation/test/testMCCompLabel.cxx +++ b/DataFormats/simulation/test/testMCCompLabel.cxx @@ -25,11 +25,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 +39,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); + } }