-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding json writing, reading infrastructure (#2283)
This PR adds the reading/writing infrastructure of the new `Experimental` detector feature to and from json. At the same time it streamlines how we use the `json::nlohmann` module: - As we need polymorphism, the intrinsic `to_json` and `from_json` nomenclature that would allow auto-translation is practically unusable for the `Detector`. Hence it is replaced by a consistent `ObjectJsonConverter::toJson` and `ObjectJsonConverter::fromJson` naming scheme. - It changes enum types from hand-written string writing to the `nlohmann::enum` handling macro consistently - It introduces nested `Options` struct for future refinement of json writing - It adds a dedicated `detray` writing mode for conversion into detray formal --------- Co-authored-by: Joana Niermann <53186085+niermann999@users.noreply.github.com>
- Loading branch information
1 parent
8833e70
commit bb68534
Showing
35 changed files
with
2,926 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
Plugins/Json/include/Acts/Plugins/Json/DetectorJsonConverter.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2023 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Geometry/GeometryContext.hpp" | ||
#include "Acts/Plugins/Json/ActsJson.hpp" | ||
#include "Acts/Plugins/Json/DetectorVolumeJsonConverter.hpp" | ||
|
||
// Custom Json encoder/decoders | ||
|
||
namespace Acts { | ||
|
||
namespace Experimental { | ||
class Detector; | ||
} | ||
|
||
namespace DetectorJsonConverter { | ||
|
||
struct Options { | ||
DetectorVolumeJsonConverter::Options volumeOptions = | ||
DetectorVolumeJsonConverter::Options{}; | ||
}; | ||
|
||
/// @brief Convert to json format | ||
/// | ||
/// @param gctx the geometry context | ||
/// @param detector the detector instance | ||
/// @param options the writing options that propagate | ||
/// to the downstream converters | ||
/// | ||
/// @return a json object | ||
nlohmann::json toJson(const GeometryContext& gctx, | ||
const Experimental::Detector& detector, | ||
const Options& options = Options{}); | ||
|
||
/// @brief Convert to detray json format | ||
/// | ||
/// @param gctx the geometry context | ||
/// @param detector the detector instance | ||
/// @param options the writing options that propagate | ||
/// to the downstream converters | ||
/// | ||
/// @return a json object in detray format | ||
nlohmann::json toJsonDetray(const GeometryContext& gctx, | ||
const Experimental::Detector& detector, | ||
const Options& options = Options{}); | ||
|
||
/// @brief convert from json format | ||
/// | ||
/// @param gctx the geometry context | ||
/// @param jDetector the json object | ||
/// | ||
/// @return a newly created shared Detector object | ||
std::shared_ptr<Experimental::Detector> fromJson( | ||
const GeometryContext& gctx, const nlohmann::json& jDetector); | ||
|
||
} // namespace DetectorJsonConverter | ||
} // namespace Acts |
77 changes: 77 additions & 0 deletions
77
Plugins/Json/include/Acts/Plugins/Json/DetectorVolumeJsonConverter.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// This file is part of the Acts project. | ||
// | ||
// Copyright (C) 2023 CERN for the benefit of the Acts project | ||
// | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
#pragma once | ||
|
||
#include "Acts/Geometry/GeometryContext.hpp" | ||
#include "Acts/Plugins/Json/ActsJson.hpp" | ||
#include "Acts/Plugins/Json/AlgebraJsonConverter.hpp" | ||
#include "Acts/Plugins/Json/PortalJsonConverter.hpp" | ||
#include "Acts/Plugins/Json/SurfaceJsonConverter.hpp" | ||
|
||
// Custom Json encoder/decoders | ||
|
||
namespace Acts { | ||
|
||
namespace Experimental { | ||
class DetectorVolume; | ||
class Portal; | ||
} // namespace Experimental | ||
|
||
namespace DetectorVolumeJsonConverter { | ||
|
||
struct Options { | ||
// The options how surfaces are written out | ||
SurfaceJsonConverter::Options surfaceOptions; | ||
// The options how portals are written out | ||
PortalJsonConverter::Options portalOptions; | ||
// The options how transforms are written out | ||
Transform3JsonConverter::Options transformOptions; | ||
}; | ||
|
||
/// @brief Convert to json format | ||
/// | ||
/// @param gctx the geometry context | ||
/// @param volume the detector volume instance | ||
/// @param detectorVolumes the list of other detector volumes | ||
/// @param portals the list of portals for saving the portal links | ||
/// @param options the options for the conversion | ||
/// | ||
/// @return a json object representing the detector volume | ||
nlohmann::json toJson( | ||
const GeometryContext& gctx, const Experimental::DetectorVolume& volume, | ||
const std::vector<const Experimental::DetectorVolume*>& detectorVolumes, | ||
const std::vector<const Experimental::Portal*>& portals = {}, | ||
const Options& options = Options{}); | ||
|
||
/// @brief Convert to json detray format | ||
/// | ||
/// @param gctx the geometry context | ||
/// @param volume the detector volume instance | ||
/// @param detectorVolumes the list of other detector volumes | ||
/// @param options the options for the conversion | ||
/// | ||
/// @return a json object representing the detector volume | ||
nlohmann::json toJsonDetray( | ||
const GeometryContext& gctx, const Experimental::DetectorVolume& volume, | ||
const std::vector<const Experimental::DetectorVolume*>& detectorVolumes, | ||
const Options& options = Options{}); | ||
|
||
/// @brief convert from json format | ||
/// | ||
/// @param gctx the geometry context | ||
/// @param jVolume the json object representing the detector volume | ||
/// | ||
/// @note this only creates a volume in a stand-alone context, not in a detector | ||
/// context. For the latter, use the DetectorJsonConverter that will patch | ||
/// the portals accordingly | ||
std::shared_ptr<Experimental::DetectorVolume> fromJson( | ||
const GeometryContext& gctx, const nlohmann::json& jVolume); | ||
|
||
} // namespace DetectorVolumeJsonConverter | ||
} // namespace Acts |
Oops, something went wrong.