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 csv prototrack writer #2513

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions Examples/Io/Csv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <string>

namespace ActsExamples {

/// @class CsvProtoTrackWriter
///
class CsvProtoTrackWriter final : public WriterT<ProtoTrackContainer> {
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<float>::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<SimSpacePointContainer> m_inputSpacepoints{this,
"inputSpacepoints"};
};

} // namespace ActsExamples
10 changes: 10 additions & 0 deletions Examples/Io/Csv/src/CsvOutputData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#pragma once

#include <ActsExamples/EventData/Index.hpp>

#include <cstdint>

#include <dfe/dfe_namedtuple.hpp>
Expand Down Expand Up @@ -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
67 changes: 67 additions & 0 deletions Examples/Io/Csv/src/CsvProtoTrackWriter.cpp
Original file line number Diff line number Diff line change
@@ -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 <ios>
#include <optional>
#include <stdexcept>

#include <dfe/dfe_io_dsv.hpp>

#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<ProtoTrackData> 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;
}
5 changes: 5 additions & 0 deletions Examples/Python/src/Output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

Expand Down