-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow bare Surface pointers to be written to JSON #3493
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,11 @@ void to_json(nlohmann::json& j, const Surface& surface); | |
/// @note it will take the default context | ||
void to_json(nlohmann::json& j, const std::shared_ptr<const Surface>& surface); | ||
|
||
/// Non-contextual conversion of a surface | ||
/// | ||
/// @note it will take the default context | ||
void to_json(nlohmann::json& j, const Surface* surface); | ||
Comment on lines
+49
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Unsafe, this implementation is! No null check, I see! Found the implementation in SurfaceJsonConverter.cpp, I have. Dereference the raw pointer without null check, it does: void Acts::to_json(nlohmann::json& j, const Acts::Surface* surface) {
Acts::GeometryContext gctx;
j = SurfaceJsonConverter::toJson(gctx, *surface); // Unsafe dereference!
} Fix this, you must:
Already a std::shared_ptr overload exists, unnecessary this raw pointer version may be. 🔗 Analysis chainHmmmm, careful with raw pointers, we must be! A raw pointer conversion function, you have added. But handle null pointers safely, we must. Verify the implementation, I shall. Consider these points of wisdom, you should:
The first attempt failed because the implementation might be in a cpp file or have a different pattern. Let me try these alternative approaches to find and verify the null pointer handling in the implementation. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Search for the implementation to verify null pointer handling
ast-grep --pattern 'void Acts::to_json(nlohmann::json& j, const Acts::Surface* surface)'
Length of output: 90 Script: #!/bin/bash
# Search for implementation in cpp files
rg -l "void\s+to_json.*Surface\*\s*surface" --type cpp
# Look for the implementation pattern without namespace
ast-grep --pattern 'to_json($j, $surface) {
$$$
}'
# Check existing implementations for comparison
rg "to_json.*Surface" -A 5
Length of output: 4129 |
||
|
||
/// Contextual conversion of a surface | ||
/// | ||
/// @param j the json to be filled | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,11 @@ void Acts::to_json(nlohmann::json& j, const Acts::Surface& surface) { | |
j = SurfaceJsonConverter::toJson(gctx, surface); | ||
} | ||
|
||
void Acts::to_json(nlohmann::json& j, const Acts::Surface* surface) { | ||
Acts::GeometryContext gctx; | ||
j = SurfaceJsonConverter::toJson(gctx, *surface); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs to check if the surface does not have an associated detector element, otherwise, this is invalid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mmm, we have the same functionality for I guess we could throw and exception? That the default context is used is already pointed out in the comments. This is needed because the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would argue in that case, the conversion for shared pointers is also not safe. Didn't we add a way to serialize the surface with an explicit context? The problem with a default constructed context is that it's up to the detector element how it's handled. Sort of fundamentally, surfaces with detector elements can't really be serialized by writing out the transform, since it's only a snapshot. For the podio Track edm, I explicitly use the identifier of the surface if it has one, so it can be tied back to the detector element. Otherwise, adding a check and an exception here might be good enough. |
||
} | ||
|
||
void Acts::to_json(nlohmann::json& j, | ||
const std::shared_ptr<const Acts::Surface>& surface) { | ||
Acts::GeometryContext gctx; | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -203,6 +203,35 @@ BOOST_AUTO_TEST_CASE(PerigeeRoundTripTests) { | |||||||||||||||||||||||||||||||||
BOOST_CHECK_EQUAL(perigeeTest->geometryId(), perigeeRef->geometryId()); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
BOOST_AUTO_TEST_CASE(CylinderToJsonPlainPointerReference) { | ||||||||||||||||||||||||||||||||||
Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.)); | ||||||||||||||||||||||||||||||||||
auto tube = std::make_shared<CylinderBounds>(5., 20.); | ||||||||||||||||||||||||||||||||||
auto cylinderRef = Surface::makeShared<CylinderSurface>(trf, tube); | ||||||||||||||||||||||||||||||||||
cylinderRef->assignGeometryId(GeometryIdentifier(11u)); | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate GeometryId detected, hmmmm. Same GeometryId (11u) as CylinderSurfaceRoundTripTests, this has. Confusion in test results, it may cause. Unique identifier, you must assign. - cylinderRef->assignGeometryId(GeometryIdentifier(11u));
+ cylinderRef->assignGeometryId(GeometryIdentifier(14u)); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
const Surface* surf = cylinderRef.get(); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// Test a cylinder | ||||||||||||||||||||||||||||||||||
nlohmann::json cylinderOut = surf; | ||||||||||||||||||||||||||||||||||
out.open("CylinderSurfacePlainPointer.json"); | ||||||||||||||||||||||||||||||||||
out << cylinderOut.dump(2); | ||||||||||||||||||||||||||||||||||
out.close(); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
auto in = std::ifstream("CylinderSurfacePlainPointer.json", | ||||||||||||||||||||||||||||||||||
std::ifstream::in | std::ifstream::binary); | ||||||||||||||||||||||||||||||||||
BOOST_CHECK(in.good()); | ||||||||||||||||||||||||||||||||||
nlohmann::json cylinderIn; | ||||||||||||||||||||||||||||||||||
in >> cylinderIn; | ||||||||||||||||||||||||||||||||||
in.close(); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
auto cylinderTest = SurfaceJsonConverter::fromJson(cylinderIn); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
BOOST_CHECK( | ||||||||||||||||||||||||||||||||||
cylinderTest->transform(gctx).isApprox(cylinderRef->transform(gctx))); | ||||||||||||||||||||||||||||||||||
BOOST_CHECK_EQUAL(cylinderTest->geometryId(), cylinderRef->geometryId()); | ||||||||||||||||||||||||||||||||||
BOOST_CHECK_EQUAL(cylinderTest->bounds(), cylinderRef->bounds()); | ||||||||||||||||||||||||||||||||||
Comment on lines
+227
to
+232
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Additional verification for pointer behavior, suggest I do. Well-structured the basic tests are, but pointer-specific scenarios, we must also verify. Type information preservation and proper pointer handling, essential they are. auto cylinderTest = SurfaceJsonConverter::fromJson(cylinderIn);
BOOST_CHECK(
cylinderTest->transform(gctx).isApprox(cylinderRef->transform(gctx)));
BOOST_CHECK_EQUAL(cylinderTest->geometryId(), cylinderRef->geometryId());
BOOST_CHECK_EQUAL(cylinderTest->bounds(), cylinderRef->bounds());
+ // Verify type information is preserved
+ BOOST_CHECK(dynamic_cast<const CylinderSurface*>(cylinderTest.get()) != nullptr);
+ // Verify the surface type matches
+ BOOST_CHECK_EQUAL(cylinderTest->type(), Surface::Type::Cylinder); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
BOOST_AUTO_TEST_CASE(SurfacesDetrayTests) { | ||||||||||||||||||||||||||||||||||
Transform3 trf(Transform3::Identity() * Translation3(0., 0., -7.)); | ||||||||||||||||||||||||||||||||||
auto trapezoid = std::make_shared<TrapezoidBounds>(2., 3., 4.); | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle file operations with care, young Padawan must.
Hmmmm. Missing error handling for file operations, I sense. A disturbance in the Force, this could cause.
Apply this diff to strengthen your code with the Force of error handling:
📝 Committable suggestion