From 98b5b19257e9ffa5f171513bb15d144c5d801c71 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Thu, 5 Oct 2023 12:03:50 +0200 Subject: [PATCH 1/3] add writer --- Examples/Io/Csv/CMakeLists.txt | 1 + .../Io/Csv/CsvProtoTrackWriter.hpp | 70 +++++++++++++++++++ Examples/Io/Csv/src/CsvOutputData.hpp | 10 +++ Examples/Io/Csv/src/CsvProtoTrackWriter.cpp | 67 ++++++++++++++++++ Examples/Python/src/Output.cpp | 5 ++ 5 files changed, 153 insertions(+) create mode 100644 Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp create mode 100644 Examples/Io/Csv/src/CsvProtoTrackWriter.cpp 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..af1f839b0c3 --- /dev/null +++ b/Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp @@ -0,0 +1,70 @@ +// This file is part of the Acts project. +// +// Copyright (C) 2021 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 { + std::string inputPrototracks; + /// Which measurement collection to write. + std::string inputSpacepoints; + /// Which cluster collection to write (optional) + 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..0c03d75ac07 --- /dev/null +++ b/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp @@ -0,0 +1,67 @@ +// This file is part of the Acts project. +// +// Copyright (C) 2021 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 (not spr) { + 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; From 5f637250a8695c44d19a6ce8b061cb3820fd710d Mon Sep 17 00:00:00 2001 From: Benjamin Huth <37871400+benjaminhuth@users.noreply.github.com> Date: Thu, 5 Oct 2023 13:43:11 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Andreas Stefl --- .../include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp | 7 ++++--- Examples/Io/Csv/src/CsvProtoTrackWriter.cpp | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp b/Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp index af1f839b0c3..c0ce3463eb3 100644 --- a/Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp +++ b/Examples/Io/Csv/include/ActsExamples/Io/Csv/CsvProtoTrackWriter.hpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2021 CERN for the benefit 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 @@ -28,10 +28,11 @@ namespace ActsExamples { class CsvProtoTrackWriter final : public WriterT { public: struct Config { + /// Which prototracks to write std::string inputPrototracks; - /// Which measurement collection to write. + /// Spacepoint collection std::string inputSpacepoints; - /// Which cluster collection to write (optional) + /// Output directory std::string outputDir; /// Number of decimal digits for floating point precision in output. int outputPrecision = std::numeric_limits::max_digits10; diff --git a/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp b/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp index 0c03d75ac07..78e9d5ed498 100644 --- a/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp +++ b/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp @@ -1,6 +1,6 @@ // This file is part of the Acts project. // -// Copyright (C) 2021 CERN for the benefit 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 From bbf1d4c9361704fa78ecf11120bae14632c193b1 Mon Sep 17 00:00:00 2001 From: Benjamin Huth Date: Wed, 18 Oct 2023 16:11:59 +0200 Subject: [PATCH 3/3] clang-tidy --- Examples/Io/Csv/src/CsvProtoTrackWriter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp b/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp index 78e9d5ed498..0f7b1b0adfd 100644 --- a/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp +++ b/Examples/Io/Csv/src/CsvProtoTrackWriter.cpp @@ -54,7 +54,7 @@ ActsExamples::ProcessCode ActsExamples::CsvProtoTrackWriter::writeT( for (auto trackId = 0ul; trackId < tracks.size(); ++trackId) { for (Index measurmentId : tracks[trackId]) { const auto spr = findSpacePointForIndex(measurmentId, spacepoints); - if (not spr) { + if (spr == nullptr) { ACTS_WARNING("Could not convert index " << measurmentId << " to spacepoint"); continue;