Skip to content

Commit

Permalink
save step in between
Browse files Browse the repository at this point in the history
  • Loading branch information
asalzburger committed Mar 20, 2024
1 parent ac5c6a6 commit d2c4cc5
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions Core/include/Acts/Utilities/GridAccessHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ class LocalSubspace final : public IBoundToGridLocal {
std::make_integer_sequence<std::size_t, sizeof...(Args)>{});
return accessed;
}

};

class BoundCylinderToZPhi final : public IBoundToGridLocal {
Expand Down
39 changes: 39 additions & 0 deletions Plugins/Json/include/Acts/Plugins/Json/GridJsonConverter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Acts/Plugins/Json/ActsJson.hpp"
#include "Acts/Utilities/IAxis.hpp"
#include "Acts/Utilities/detail/AxisFwd.hpp"
#include "Acts/Utilities/GridAccessHelpers.hpp"

#include <iostream>

Expand All @@ -35,6 +36,44 @@ nlohmann::json toJsonDetray(const IAxis& ia);

} // namespace AxisJsonConverter

namespace GridAccessJsonConverter {

/// Convert a global to local access to json
///
/// @param ga the global to local access
///
/// @return a json object to represent global class
nlohmann::json toJson(const GridAccess::IGlobalToGridLocal& ga);

/// Create a global grid to local instance
///
/// @param jga the json snippet
///
/// @return a newly created objet
std::unique_ptr<GridAccess::IGlobalToGridLocal> globalToGridLocalFromJson(
const nlohmann::json& jga);


/// Convert a local to local access to json
///
/// @param la the local to local access
///
/// @return a json object to represent local class
nlohmann::json toJson(const GridAccess::IBoundToGridLocal& la);


/// Create a local grid to local instance
///
/// @param jla the json snippet
///
/// @return a newly created objet
std::unique_ptr<GridAccess::IBoundToGridLocal> boundToGridLocalFromJson(
const nlohmann::json& jla);


} // namespace GridAccessJsonConverter


namespace GridJsonConverter {

/// @brief Templated grid conversion to json
Expand Down
128 changes: 128 additions & 0 deletions Plugins/Json/src/GridJsonConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,131 @@ nlohmann::json Acts::AxisJsonConverter::toJsonDetray(const IAxis& ia) {
}
return jAxis;
}

template <typename Subspace>
void encodeSubspace(nlohmann::json& j,
const Acts::GridAccess::IGlobalToGridLocal& ga,
const Subspace& /*unused*/) {
const Subspace* subspace = dynamic_cast<const Subspace*>(&ga);
if (subspace != nullptr) {
j["type"] = "subspace";
j["accessors"] = subspace->accessors;
}
}

template <typename... Args>
void encodeSubspaces(nlohmann::json& j,
const Acts::GridAccess::IGlobalToGridLocal& ga,
std::tuple<Args...>& t) {
std::apply([&](auto&&... vals) { (encodeSubspace(j, ga, vals), ...); }, t);
}

nlohmann::json Acts::GridAccessJsonConverter::toJson(
const GridAccess::IGlobalToGridLocal& ga) {
nlohmann::json jga;

// One dimensional sub spaces
const std::tuple<
GridAccess::GlobalSubspace<binX>, GridAccess::GlobalSubspace<binY>,
GridAccess::GlobalSubspace<binZ>, GridAccess::GlobalSubspace<binR>,
GridAccess::GlobalSubspace<binPhi>, GridAccess::GlobalSubspace<binEta>>
oneDimAccessors = {};

encodeSubspaces(jga, ga, oneDimAccessors);
if (!jga.empty()) {
return jga;
}

// Usfeul two dimensional sub spaces
const std::tuple<
GridAccess::GlobalSubspace<binX, binY>, GridAccess::GlobalSubspace<binY, binX>,
GridAccess::GlobalSubspace<binX, binZ>, GridAccess::GlobalSubspace<binZ, binX>,
GridAccess::GlobalSubspace<binY, binZ>, GridAccess::GlobalSubspace<binZ, binY>,
GridAccess::GlobalSubspace<binR, binPhi>, GridAccess::GlobalSubspace<binPhi, binR>,
GridAccess::GlobalSubspace<binZ, binPhi>, GridAccess::GlobalSubspace<binPhi, binZ>>
twoDimAccessors = {};

encodeSubspaces(jga, ga, twoDimAccessors);
if (!jga.empty()) {
return jga;
}



return jga;
}

std::unique_ptr<Acts::GridAccess::IGlobalToGridLocal>
Acts::GridAccessJsonConverter::globalToGridLocalFromJson(
const nlohmann::json& jga) {
std::unique_ptr<Acts::GridAccess::IGlobalToGridLocal> ga;
return ga;
}

nlohmann::json Acts::GridAccessJsonConverter::toJson(
const GridAccess::IBoundToGridLocal& la) {
nlohmann::json jla;

auto localSubSpace0 = dynamic_cast<const GridAccess::LocalSubspace<0u>*>(&la);
if (localSubSpace0 != nullptr) {
jla["type"] = "subspace";
jla["accessors"] = localSubSpace0->accessors;
}

auto localSubSpace1 = dynamic_cast<const GridAccess::LocalSubspace<1u>*>(&la);
if (localSubSpace1 != nullptr) {
jla["type"] = "subspace";
jla["accessors"] = localSubSpace1->accessors;
}

auto localSubSpace01 =
dynamic_cast<const GridAccess::LocalSubspace<0u, 1u>*>(&la);
if (localSubSpace01 != nullptr) {
jla["type"] = "subspace";
jla["accessors"] = localSubSpace01->accessors;
}

auto localSubSpace10 =
dynamic_cast<const GridAccess::LocalSubspace<1u, 0u>*>(&la);
if (localSubSpace10 != nullptr) {
jla["type"] = "subspace";
jla["accessors"] = localSubSpace10->accessors;
}

auto boundCylinderToZPhi =
dynamic_cast<const GridAccess::BoundCylinderToZPhi*>(&la);
if (boundCylinderToZPhi != nullptr) {
jla["type"] = "cylinder_to_zphi";
jla["radius"] = boundCylinderToZPhi->radius;
jla["shift"] = boundCylinderToZPhi->shift;
}

return jla;
}

std::unique_ptr<Acts::GridAccess::IBoundToGridLocal>
Acts::GridAccessJsonConverter::boundToGridLocalFromJson(
const nlohmann::json& jla) {
std::unique_ptr<Acts::GridAccess::IBoundToGridLocal> la;
std::string type = jla.at("type").get<std::string>();
if (type == "subspace") {
std::vector<std::size_t> accessors =
jla.at("accessors").get<std::vector<std::size_t>>();
if (accessors.size() == 1 && accessors[0] == 0) {
la = std::make_unique<Acts::GridAccess::LocalSubspace<0u>>();
} else if (accessors.size() == 1 && accessors[0] == 1) {
la = std::make_unique<Acts::GridAccess::LocalSubspace<1u>>();
} else if (accessors.size() == 2 && accessors[0] == 0 &&
accessors[1] == 1) {
la = std::make_unique<Acts::GridAccess::LocalSubspace<0u, 1u>>();
} else if (accessors.size() == 2 && accessors[0] == 1 &&
accessors[1] == 0) {
la = std::make_unique<Acts::GridAccess::LocalSubspace<1u, 0u>>();
}
} else if (type == "cylinder_to_zphi") {
ActsScalar radius = jla.at("radius").get<ActsScalar>();
ActsScalar shift = jla.at("shift").get<ActsScalar>();
la = std::make_unique<Acts::GridAccess::BoundCylinderToZPhi>(radius, shift);
}
return la;
}

0 comments on commit d2c4cc5

Please sign in to comment.