diff --git a/Examples/Io/Csv/CMakeLists.txt b/Examples/Io/Csv/CMakeLists.txt index 5c42e19fa4b..fc62aa46678 100644 --- a/Examples/Io/Csv/CMakeLists.txt +++ b/Examples/Io/Csv/CMakeLists.txt @@ -15,6 +15,7 @@ add_library( src/CsvTrackParameterReader.cpp src/CsvTrackParameterWriter.cpp src/CsvMultiTrajectoryWriter.cpp + src/CsvProtoTrackWriter.cpp src/CsvSpacePointWriter.cpp src/CsvBFieldWriter.cpp) target_include_directories( diff --git a/Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp b/Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp new file mode 100644 index 00000000000..c0ce3463eb3 --- /dev/null +++ b/Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp @@ -0,0 +1,71 @@ +// 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/Definitions/TrackParametrization.hpp" +#include "Acts/EventData/Measurement.hpp" +#include "Acts/Geometry/GeometryContext.hpp" +#include "Acts/Geometry/GeometryHierarchyMap.hpp" +#include "Acts/Geometry/GeometryIdentifier.hpp" +#include "ActsExamples/Digitization/DigitizationConfig.hpp" +#include "ActsExamples/EventData/Index.hpp" +#include "ActsExamples/EventData/ProtoTrack.hpp" +#include "ActsExamples/EventData/SimSpacePoint.hpp" +#include "ActsExamples/Framework/WriterT.hpp" + +#include + +namespace ActsExamples { + +/// @class CsvProtoTrackWriter +/// +class CsvProtoTrackWriter final : public WriterT { + public: + struct Config { + /// Which prototracks to write + std::string inputPrototracks; + /// Spacepoint collection + std::string inputSpacepoints; + /// Output directory + std::string outputDir; + /// Number of decimal digits for floating point precision in output. + int outputPrecision = std::numeric_limits::max_digits10; + }; + + /// Constructor with + /// @param config configuration struct + /// @param level logging level + CsvProtoTrackWriter(const Config& config, Acts::Logging::Level level); + + /// Virtual destructor + ~CsvProtoTrackWriter() override; + + /// End-of-run hook + ProcessCode finalize() override; + + /// Get readonly access to the config parameters + const Config& config() const { return m_cfg; } + + protected: + /// This implementation holds the actual writing method + /// and is called by the WriterT<>::write interface + /// + /// @param ctx The Algorithm context with per event information + /// @param measurements is the data to be written out + ProcessCode writeT(const AlgorithmContext& ctx, + const ProtoTrackContainer& tracks) override; + + private: + Config m_cfg; + + ReadDataHandle m_inputSpacepoints{this, + "inputSpacepoints"}; +}; + +} // namespace ActsExamples diff --git a/Examples/Io/Csv/src/CsvOutputData.hpp b/Examples/Io/Csv/src/CsvOutputData.hpp index 1903306fb0c..2eb166104c6 100644 --- a/Examples/Io/Csv/src/CsvOutputData.hpp +++ b/Examples/Io/Csv/src/CsvOutputData.hpp @@ -11,6 +11,8 @@ #pragma once +#include + #include #include @@ -304,4 +306,12 @@ struct TrackParameterData { cov_qopphi, cov_qoptheta); }; +struct ProtoTrackData { + std::size_t trackId; + Index measurementId; + double x, y, z; + + DFE_NAMEDTUPLE(ProtoTrackData, trackId, measurementId, x, y, z); +}; + } // namespace ActsExamples diff --git a/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp b/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp new file mode 100644 index 00000000000..0f7b1b0adfd --- /dev/null +++ b/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp @@ -0,0 +1,67 @@ +// 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/. + +#include "ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp" + +#include "Acts/Definitions/Units.hpp" +#include "Acts/Surfaces/Surface.hpp" +#include "Acts/Utilities/Intersection.hpp" +#include "ActsExamples/EventData/Index.hpp" +#include "ActsExamples/EventData/SimSpacePoint.hpp" +#include "ActsExamples/Framework/WhiteBoard.hpp" +#include "ActsExamples/Utilities/EventDataTransforms.hpp" +#include "ActsExamples/Utilities/Paths.hpp" +#include "ActsExamples/Utilities/Range.hpp" + +#include +#include +#include + +#include + +#include "CsvOutputData.hpp" + +ActsExamples::CsvProtoTrackWriter::CsvProtoTrackWriter( + const ActsExamples::CsvProtoTrackWriter::Config& config, + Acts::Logging::Level level) + : WriterT(config.inputPrototracks, "CsvProtoTrackWriter", level), + m_cfg(config) { + m_inputSpacepoints.initialize(m_cfg.inputSpacepoints); +} + +ActsExamples::CsvProtoTrackWriter::~CsvProtoTrackWriter() = default; + +ActsExamples::ProcessCode ActsExamples::CsvProtoTrackWriter::finalize() { + // Write the tree + return ProcessCode::SUCCESS; +} + +ActsExamples::ProcessCode ActsExamples::CsvProtoTrackWriter::writeT( + const AlgorithmContext& ctx, const ProtoTrackContainer& tracks) { + const auto& spacepoints = m_inputSpacepoints(ctx); + + // Open per-event file for all components + std::string path = + perEventFilepath(m_cfg.outputDir, "prototracks.csv", ctx.eventNumber); + + dfe::NamedTupleCsvWriter writer(path, m_cfg.outputPrecision); + + for (auto trackId = 0ul; trackId < tracks.size(); ++trackId) { + for (Index measurmentId : tracks[trackId]) { + const auto spr = findSpacePointForIndex(measurmentId, spacepoints); + if (spr == nullptr) { + ACTS_WARNING("Could not convert index " << measurmentId + << " to spacepoint"); + continue; + } + const auto& sp = *spr; + writer.append({trackId, measurmentId, sp.x(), sp.y(), sp.z()}); + } + } + return ActsExamples::ProcessCode::SUCCESS; +} diff --git a/Examples/Python/src/Output.cpp b/Examples/Python/src/Output.cpp index 443f01cd8c9..4a05c9e583c 100644 --- a/Examples/Python/src/Output.cpp +++ b/Examples/Python/src/Output.cpp @@ -18,6 +18,7 @@ #include "ActsExamples/Io/Csv/CsvMultiTrajectoryWriter.hpp" #include "ActsExamples/Io/Csv/CsvParticleWriter.hpp" #include "ActsExamples/Io/Csv/CsvPlanarClusterWriter.hpp" +#include "ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp" #include "ActsExamples/Io/Csv/CsvSimHitWriter.hpp" #include "ActsExamples/Io/Csv/CsvSpacepointWriter.hpp" #include "ActsExamples/Io/Csv/CsvTrackParameterWriter.hpp" @@ -387,6 +388,10 @@ void addOutput(Context& ctx) { inputTrajectories, outputDir, outputStem, outputPrecision); + ACTS_PYTHON_DECLARE_WRITER(ActsExamples::CsvProtoTrackWriter, mex, + "CsvProtoTrackWriter", inputSpacepoints, + inputPrototracks, outputDir); + { using Writer = ActsExamples::CsvBFieldWriter;