diff --git a/edm4hep.yaml b/edm4hep.yaml index 9ea4413d3..82915d40c 100644 --- a/edm4hep.yaml +++ b/edm4hep.yaml @@ -127,8 +127,12 @@ components: ExtraCode: includes: "#include " declaration: " + constexpr CovMatrix2f() = default;\n + constexpr CovMatrix2f(const std::array v) : values(v) {}\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 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 +150,12 @@ components: ExtraCode: includes: "#include " declaration: " + constexpr CovMatrix3f() = default;\n + constexpr CovMatrix3f(const std::array v) : values(v) {}\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 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 +173,12 @@ components: ExtraCode: includes: "#include " declaration: " + constexpr CovMatrix4f() = default;\n + constexpr CovMatrix4f(const std::array v) : values(v) {}\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 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 +196,12 @@ components: ExtraCode: includes: "#include " declaration: " + constexpr CovMatrix6f() = default;\n + constexpr CovMatrix6f(const std::array v) : values(v) {}\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 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 +477,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 +504,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 +535,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 +596,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 +638,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..f57313408 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,35 @@ 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); +} + +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{};