Skip to content
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: Add JSON detector element #3851

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "Acts/Geometry/GeometryHierarchyMap.hpp"
#include "Acts/Plugins/Json/JsonDetectorElement.hpp"

#include <memory>
#include <string>
Expand Down Expand Up @@ -43,4 +44,13 @@ Acts::GeometryHierarchyMap<std::shared_ptr<Acts::Surface>> readHierarchyMap(
/// @return a vector of surfaces
std::vector<std::shared_ptr<Acts::Surface>> readVector(const Options& options);

/// @brief Read the surfaces from the input file and create
/// detector elements
///
/// @param inputFile is the input file to read from
///
/// @return a vector of surfaces
std::vector<std::shared_ptr<Acts::JsonDetectorElement>> readDetectorElements(
const Options& options, double thickness);

} // namespace ActsExamples::JsonSurfacesReader
23 changes: 23 additions & 0 deletions Examples/Io/Json/src/JsonSurfacesReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,27 @@ std::vector<std::shared_ptr<Acts::Surface>> JsonSurfacesReader::readVector(
return surfaces;
}

std::vector<std::shared_ptr<Acts::JsonDetectorElement>>
JsonSurfacesReader::readDetectorElements(const Options& options,
double thickness = 0.0) {
nlohmann::json j;
{
std::ifstream in(options.inputFile);
in >> j;
}

// Walk down the path to the surface entries
nlohmann::json jSurfaces = j;
for (const auto& jep : options.jsonEntryPath) {
jSurfaces = jSurfaces[jep];
}

std::vector<std::shared_ptr<Acts::JsonDetectorElement>> elements;
for (const auto& jSurface : jSurfaces) {
elements.emplace_back(
std::make_shared<Acts::JsonDetectorElement>(jSurface, thickness));
}
return elements;
}

} // namespace ActsExamples
10 changes: 10 additions & 0 deletions Examples/Python/src/Json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ void addJson(Context& ctx) {

mex.def("readSurfaceVectorFromJson",
ActsExamples::JsonSurfacesReader::readVector);

py::class_<Acts::JsonDetectorElement, Acts::DetectorElementBase,
std::shared_ptr<Acts::JsonDetectorElement>>(
mex, "JsonDetectorElement")
.def("surface", [](Acts::JsonDetectorElement& self) {
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved
return self.surface().getSharedPtr();
});

mex.def("readDetectorElementsFromJson",
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved
ActsExamples::JsonSurfacesReader::readDetectorElements);
}

{
Expand Down
1 change: 1 addition & 0 deletions Plugins/Json/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_library(
src/VolumeJsonConverter.cpp
src/AmbiguityConfigJsonConverter.cpp
src/DetrayJsonHelper.cpp
src/JsonDetectorElement.cpp
)
target_include_directories(
ActsPluginJson
Expand Down
32 changes: 32 additions & 0 deletions Plugins/Json/include/Acts/Plugins/Json/JsonDetectorElement.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
#pragma once

#include "Acts/Geometry/DetectorElementBase.hpp"

#include <nlohmann/json.hpp>

namespace Acts {

class JsonDetectorElement : public DetectorElementBase {
std::shared_ptr<Surface> m_surface;
Transform3 m_transform{};
double m_thickness{};
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved

public:
JsonDetectorElement(const nlohmann::json &surfaceJson, double thickness);

Surface &surface() override;
const Surface &surface() const override;

double thickness() const override;

const Transform3 &transform(const GeometryContext &gctx) const override;
};

} // namespace Acts
40 changes: 40 additions & 0 deletions Plugins/Json/src/JsonDetectorElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// This file is part of the ACTS project.
//
// Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.

#include "Acts/Plugins/Json/JsonDetectorElement.hpp"

#include "Acts/Plugins/Json/SurfaceJsonConverter.hpp"

namespace Acts {

JsonDetectorElement::JsonDetectorElement(const nlohmann::json &jSurface,
double thickness)
: m_thickness(thickness) {
m_surface = Acts::SurfaceJsonConverter::fromJson(jSurface);
m_transform = m_surface->transform({});
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved
m_surface->assignDetectorElement(*this);
}

const Surface &JsonDetectorElement::surface() const {
return *m_surface;
}

Surface &JsonDetectorElement::surface() {
return *m_surface;
}
benjaminhuth marked this conversation as resolved.
Show resolved Hide resolved

const Transform3 &JsonDetectorElement::transform(
const GeometryContext &) const {
return m_transform;
}

double JsonDetectorElement::thickness() const {
return m_thickness;
}

} // namespace Acts
Loading