Skip to content

Commit

Permalink
Make CovMatrixNf components behave more array like for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Mar 14, 2024
1 parent bcccb68 commit e3476db
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
25 changes: 25 additions & 0 deletions edm4hep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,13 @@ components:
ExtraCode:
includes: "#include <edm4hep/utils/cov_matrix_utils.h>"
declaration: "
constexpr CovMatrix2f() = default;\n
constexpr CovMatrix2f(const std::array<float, 3> v) : values(v) {}\n
constexpr CovMatrix2f& operator=(std::array<float, 3> 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
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
Expand All @@ -146,8 +151,13 @@ components:
ExtraCode:
includes: "#include <edm4hep/utils/cov_matrix_utils.h>"
declaration: "
constexpr CovMatrix3f() = default;\n
constexpr CovMatrix3f(const std::array<float, 6> v) : values(v) {}\n
constexpr CovMatrix3f& operator=(std::array<float, 6> 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
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
Expand All @@ -165,8 +175,13 @@ components:
ExtraCode:
includes: "#include <edm4hep/utils/cov_matrix_utils.h>"
declaration: "
constexpr CovMatrix4f() = default;\n
constexpr CovMatrix4f(const std::array<float, 10> v) : values(v) {}\n
constexpr CovMatrix4f& operator=(std::array<float, 10> 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
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
Expand All @@ -184,8 +199,13 @@ components:
ExtraCode:
includes: "#include <edm4hep/utils/cov_matrix_utils.h>"
declaration: "
constexpr CovMatrix6f() = default;\n
constexpr CovMatrix6f(const std::array<float, 21> v) : values(v) {}\n
constexpr CovMatrix6f& operator=(std::array<float, 21> 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
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
Expand Down Expand Up @@ -461,6 +481,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<float, 6> values) { getPositionError() = values; }
"

#------------- TrackerHit
Expand All @@ -487,6 +508,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<float, 6> values) { getCovMatrix() = values; }
"

#------------- TrackerHitPlane
Expand Down Expand Up @@ -517,6 +539,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<float, 6> values) { getCovMatrix() = values; }
"

#---------- RawTimeSeries
Expand Down Expand Up @@ -577,6 +600,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<float, 6> values) { getCovMatrix() = values; }
"

#------- ReconstructedParticle
Expand Down Expand Up @@ -618,6 +642,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<float, 10> values) { getCovMatrix() = values; }
"

edm4hep::MCRecoParticleAssociation:
Expand Down
30 changes: 30 additions & 0 deletions test/utils/test_covmatrix_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "edm4hep/Constants.h"
#include "edm4hep/CovMatrix3f.h"
#include "edm4hep/MutableTrackerHit3D.h"
#include "edm4hep/TrackState.h"
#include "edm4hep/TrackerHit3D.h"
Expand Down Expand Up @@ -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<float, 6>{0, 0, 2.13f, 3.14f, 0, 0});
REQUIRE(covMatrix != std::array<float, 6>{});
}

TEST_CASE("TrackState covariance", "[cov_matrix_utils]") {
auto trackState = edm4hep::TrackState{};

Expand Down

0 comments on commit e3476db

Please sign in to comment.