From 47f1e5f96475bc44561846da2cc6f4df54d4fdf9 Mon Sep 17 00:00:00 2001 From: tmadlener Date: Thu, 14 Mar 2024 15:56:39 +0100 Subject: [PATCH] Make CovMatrixNf components behave more array like for compatibility --- edm4hep.yaml | 41 +++++++++++++++++++++++++++++ test/utils/test_covmatrix_utils.cpp | 39 +++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/edm4hep.yaml b/edm4hep.yaml index 9ea4413d3..c8768a76e 100644 --- a/edm4hep.yaml +++ b/edm4hep.yaml @@ -127,8 +127,17 @@ components: ExtraCode: includes: "#include " declaration: " + constexpr CovMatrix2f() = default;\n + constexpr CovMatrix2f(const std::array v) : values(v) {}\n + constexpr CovMatrix2f& operator=(std::array v) { values = v; return *this; }\n bool operator==(const CovMatrix2f& v) const { return v.values == values; }\n bool operator!=(const CovMatrix2f& v) const { return v.values != values; }\n + constexpr float operator[](unsigned i) const { return values[i]; }\n + constexpr float& operator[](unsigned i) { return values[i]; }\n + constexpr auto begin() const { return values.begin(); }\n + constexpr auto begin() { return values.begin(); }\n + constexpr auto end() const { return values.begin(); }\n + constexpr auto end() { return values.begin(); }\n float* data() { return values.data(); }\n const float* data() const { return values.data(); }\n /// Get the value of the covariance matrix for the two passed dimensions\n @@ -146,8 +155,17 @@ components: ExtraCode: includes: "#include " declaration: " + constexpr CovMatrix3f() = default;\n + constexpr CovMatrix3f(const std::array v) : values(v) {}\n + constexpr CovMatrix3f& operator=(std::array v) { values = v; return *this; }\n bool operator==(const CovMatrix3f& v) const { return v.values == values; }\n bool operator!=(const CovMatrix3f& v) const { return v.values != values; }\n + constexpr float operator[](unsigned i) const { return values[i]; }\n + constexpr float& operator[](unsigned i) { return values[i]; }\n + constexpr auto begin() const { return values.begin(); }\n + constexpr auto begin() { return values.begin(); }\n + constexpr auto end() const { return values.begin(); }\n + constexpr auto end() { return values.begin(); }\n float* data() { return values.data(); }\n const float* data() const { return values.data(); }\n /// Get the value of the covariance matrix for the two passed dimensions\n @@ -165,8 +183,17 @@ components: ExtraCode: includes: "#include " declaration: " + constexpr CovMatrix4f() = default;\n + constexpr CovMatrix4f(const std::array v) : values(v) {}\n + constexpr CovMatrix4f& operator=(std::array v) { values = v; return *this; }\n bool operator==(const CovMatrix4f& v) const { return v.values == values; }\n bool operator!=(const CovMatrix4f& v) const { return v.values != values; }\n + constexpr float operator[](unsigned i) const { return values[i]; }\n + constexpr float& operator[](unsigned i) { return values[i]; }\n + constexpr auto begin() const { return values.begin(); }\n + constexpr auto begin() { return values.begin(); }\n + constexpr auto end() const { return values.begin(); }\n + constexpr auto end() { return values.begin(); }\n float* data() { return values.data(); }\n const float* data() const { return values.data(); }\n /// Get the value of the covariance matrix for the two passed dimensions\n @@ -184,8 +211,17 @@ components: ExtraCode: includes: "#include " declaration: " + constexpr CovMatrix6f() = default;\n + constexpr CovMatrix6f(const std::array v) : values(v) {}\n + constexpr CovMatrix6f& operator=(std::array v) { values = v; return *this; }\n bool operator==(const CovMatrix6f& v) const { return v.values == values; }\n bool operator!=(const CovMatrix6f& v) const { return v.values != values; }\n + constexpr float operator[](unsigned i) const { return values[i]; }\n + constexpr float& operator[](unsigned i) { return values[i]; }\n + constexpr auto begin() const { return values.begin(); }\n + constexpr auto begin() { return values.begin(); }\n + constexpr auto end() const { return values.begin(); }\n + constexpr auto end() { return values.begin(); }\n float* data() { return values.data(); }\n const float* data() const { return values.data(); }\n /// Get the value of the covariance matrix for the two passed dimensions\n @@ -461,6 +497,7 @@ datatypes: declaration: " /// Set the postion error value for the two passed dimensions\n void setPositionError(float value, edm4hep::Cartesian dimI, edm4hep::Cartesian dimJ) { return getPositionError().setValue(value, dimI, dimJ); }\n + void setPositionError(std::array values) { getPositionError() = values; } " #------------- TrackerHit @@ -487,6 +524,7 @@ datatypes: declaration: " /// Set the postion covariance matrix value for the two passed dimensions\n void setCovMatrix(float value, edm4hep::Cartesian dimI, edm4hep::Cartesian dimJ) { getCovMatrix().setValue(value, dimI, dimJ); }\n + void setCovMatrix(std::array values) { getCovMatrix() = values; } " #------------- TrackerHitPlane @@ -517,6 +555,7 @@ datatypes: declaration: " /// Set the postion covariance matrix value for the two passed dimensions\n void setCovMatrix(float value, edm4hep::Cartesian dimI, edm4hep::Cartesian dimJ) { getCovMatrix().setValue(value, dimI, dimJ); }\n + void setCovMatrix(std::array values) { getCovMatrix() = values; } " #---------- RawTimeSeries @@ -577,6 +616,7 @@ datatypes: declaration: " /// Set the postion covariance matrix value for the two passed dimensions\n void setCovMatrix(float value, edm4hep::Cartesian dimI, edm4hep::Cartesian dimJ) { getCovMatrix().setValue(value, dimI, dimJ); }\n + void setCovMatrix(std::array values) { getCovMatrix() = values; } " #------- ReconstructedParticle @@ -618,6 +658,7 @@ datatypes: void setType(int32_t pdg) { setPDG(pdg); }\n /// Set the four momentum covariance matrix value for the two passed dimensions\n void setCovMatrix(float value, edm4hep::FourMomCoords dimI, edm4hep::FourMomCoords dimJ) { getCovMatrix().setValue(value, dimI, dimJ); }\n + void setCovMatrix(std::array values) { getCovMatrix() = values; } " edm4hep::MCRecoParticleAssociation: diff --git a/test/utils/test_covmatrix_utils.cpp b/test/utils/test_covmatrix_utils.cpp index 28ab4d61b..4c32bf259 100644 --- a/test/utils/test_covmatrix_utils.cpp +++ b/test/utils/test_covmatrix_utils.cpp @@ -1,4 +1,5 @@ #include "edm4hep/Constants.h" +#include "edm4hep/CovMatrix3f.h" #include "edm4hep/MutableTrackerHit3D.h" #include "edm4hep/TrackState.h" #include "edm4hep/TrackerHit3D.h" @@ -48,6 +49,44 @@ TEST_CASE("CovarianceMatrix indexing", "[cov_matrix_utils]") { STATIC_REQUIRE(to_lower_tri(2, 5) == 17); } +TEST_CASE("CovMatrixNf array access", "[cov_matrix_utils]") { + // We use the 3D version here, but since the ExtraCode is effectively + // duplicated for the others as well it shouldn't really matter + auto covMatrix = edm4hep::CovMatrix3f{}; + + covMatrix[3] = 3.14f; + REQUIRE(covMatrix[3] == 3.14f); + + REQUIRE(covMatrix.data()[3] == 3.14f); + covMatrix.data()[2] = 2.13f; + REQUIRE(covMatrix[2] == 2.13f); + + float i = 0.f; + for (auto& v : covMatrix) { + v = i++; + } + i = 0.f; + for (const auto& v : covMatrix) { + REQUIRE(v == i++); + } +} + +TEST_CASE("CovMatrixNf enum access", "[cov_matrix_utils]") { + enum class TestDims : uint32_t { a = 0, b, c }; + + auto covMatrix = edm4hep::CovMatrix3f{}; + covMatrix.setValue(1.23f, TestDims::a, TestDims::c); + REQUIRE(covMatrix.getValue(TestDims::a, TestDims::c) == 1.23f); +} + +TEST_CASE("CovMatrixNf equality operators", "[cov_matrix_utils]") { + auto covMatrix = edm4hep::CovMatrix3f{}; + covMatrix[3] = 3.14f; + covMatrix[2] = 2.13f; + REQUIRE(covMatrix == std::array{0, 0, 2.13f, 3.14f, 0, 0}); + REQUIRE(covMatrix != std::array{}); +} + TEST_CASE("TrackState covariance", "[cov_matrix_utils]") { auto trackState = edm4hep::TrackState{};