Skip to content

Commit

Permalink
feat: add a spacepoints writer (#970)
Browse files Browse the repository at this point in the history
* add spacepoint writer

* add license

* rename files from X2Y to XtoY

* clang-format applied

* format fixed in one more spot

Co-authored-by: Paul Gessinger <hello@paulgessinger.com>
  • Loading branch information
xju2 and paulgessinger committed Nov 8, 2021
1 parent c8ef092 commit 4f28047
Show file tree
Hide file tree
Showing 12 changed files with 374 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Examples/Io/Csv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ add_library(
src/CsvSimHitWriter.cpp
src/CsvSpacePointReader.cpp
src/CsvTrackingGeometryWriter.cpp
src/CsvMultiTrajectoryWriter.cpp )
src/CsvMultiTrajectoryWriter.cpp
src/CsvSpacepointWriter.cpp)
target_include_directories(
ActsExamplesIoCsv
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 "Acts/Utilities/Helpers.hpp"
#include "ActsExamples/Digitization/DigitizationConfig.hpp"
#include "ActsExamples/EventData/Cluster.hpp"
#include "ActsExamples/EventData/Index.hpp"
#include "ActsExamples/EventData/Measurement.hpp"
#include "ActsExamples/EventData/SimHit.hpp"
#include "ActsExamples/EventData/SimSpacePoint.hpp"
#include "ActsExamples/Framework/WriterT.hpp"

#include <string>

namespace ActsExamples {

/// @class CsvSpacePointWriter
///
/// This writes one file per event containing information about the
/// spacepoints
///
/// event000000001-spacepoint.csv
/// event000000002-spacepoint.csv
/// ...
///
/// Intrinsically thread-safe as one file per event.
class CsvSpacepointWriter final : public WriterT<SimSpacePointContainer> {
public:
struct Config {
/// Which measurement collection to write.
std::string inputSpacepoints;
/// Where to place output files
std::string outputDir;
/// Number of decimal digits for floating point precision in output.
size_t outputPrecision = std::numeric_limits<float>::max_digits10;
};

/// Constructor with
/// @param config configuration struct
/// @param level logging level
CsvSpacepointWriter(const Config& config, Acts::Logging::Level level);

/// Virtual destructor
~CsvSpacepointWriter() final override;

/// End-of-run hook
ProcessCode endRun() final override;

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 spacepoints is the data to be written out
ProcessCode writeT(const AlgorithmContext& ctx,
const SimSpacePointContainer& spacepoints) final override;

private:
Config m_cfg;
};

} // namespace ActsExamples
7 changes: 7 additions & 0 deletions Examples/Io/Csv/src/CsvOutputData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,11 @@ struct SurfaceGridData {
nbins_loc1, min_loc1, max_loc1);
};

struct SpacepointData {
uint64_t measurement_id;
float x, y, z;
float var_r, var_z;
DFE_NAMEDTUPLE(SpacepointData, measurement_id, x, y, z, var_r, var_z);
};

} // namespace ActsExamples
56 changes: 56 additions & 0 deletions Examples/Io/Csv/src/CsvSpacepointWriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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/CsvSpacepointWriter.hpp"

#include "Acts/Definitions/Units.hpp"
#include "ActsExamples/EventData/SimSpacePoint.hpp"
#include "ActsExamples/Utilities/Paths.hpp"

#include <ios>
#include <optional>
#include <stdexcept>

#include <dfe/dfe_io_dsv.hpp>

#include "CsvOutputData.hpp"

ActsExamples::CsvSpacepointWriter::CsvSpacepointWriter(
const ActsExamples::CsvSpacepointWriter::Config& config,
Acts::Logging::Level level)
: WriterT(config.inputSpacepoints, "CsvSpacepointWriter", level),
m_cfg(config) {}

ActsExamples::CsvSpacepointWriter::~CsvSpacepointWriter() {}

ActsExamples::ProcessCode ActsExamples::CsvSpacepointWriter::endRun() {
// Write the tree
return ProcessCode::SUCCESS;
}

ActsExamples::ProcessCode ActsExamples::CsvSpacepointWriter::writeT(
const AlgorithmContext& ctx, const SimSpacePointContainer& spacepoints) {
// Open per-event file for all components
std::string pathSP =
perEventFilepath(m_cfg.outputDir, "spacepoint.csv", ctx.eventNumber);

dfe::NamedTupleCsvWriter<SpacepointData> writerSP(pathSP,
m_cfg.outputPrecision);

SpacepointData spData;
for (const auto& sp : spacepoints) {
spData.measurement_id = sp.measurementIndex();
spData.x = sp.x();
spData.y = sp.y();
spData.z = sp.z();
spData.var_r = sp.varianceR();
spData.var_z = sp.varianceZ();
writerSP.append(spData);
}
return ActsExamples::ProcessCode::SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "ActsExamples/Io/Csv/CsvOptionsReader.hpp"
#include "ActsExamples/Io/Csv/CsvParticleReader.hpp"
#include "ActsExamples/Io/Csv/CsvSimHitReader.hpp"
#include "ActsExamples/Options/CommonOptions.hpp"
#include "ActsExamples/TruthTracking/ParticleSmearing.hpp"
#include "ActsExamples/Utilities/Options.hpp"

Expand Down Expand Up @@ -73,3 +74,13 @@ ActsExamples::ParticleSmearing::Config setupParticleSmearing(
ActsExamples::Sequencer& sequencer,
std::shared_ptr<const ActsExamples::RandomNumbers> randomNumbers,
const std::string& inputParticles);

/// Setup reading measurements
///
/// @param vars The configuration variables
/// @param sequencer The framework sequencer
///
/// @return config for reading measurements
ActsExamples::CsvMeasurementReader::Config setupMeasurementReading(
const ActsExamples::Options::Variables& vars,
ActsExamples::Sequencer& sequencer);
19 changes: 19 additions & 0 deletions Examples/Run/Common/src/ReconstructionBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,22 @@ ActsExamples::ParticleSmearing::Config setupParticleSmearing(

return particleSmearingCfg;
}

ActsExamples::CsvMeasurementReader::Config setupMeasurementReading(
const ActsExamples::Options::Variables& vars,
ActsExamples::Sequencer& sequencer) {
using namespace ActsExamples;
// Read some standard options
auto logLevel = Options::readLogLevel(vars);

// Read particles (initial states) from CSV files
auto measurementsReader = Options::readCsvMeasurementReaderConfig(vars);
measurementsReader.outputMeasurements = "measurements";
measurementsReader.outputMeasurementSimHitsMap = "measurements2hits";
measurementsReader.outputSourceLinks = "source_links";
measurementsReader.outputClusters = "clusters";
sequencer.addReader(
std::make_shared<CsvMeasurementReader>(measurementsReader, logLevel));

return measurementsReader;
}
37 changes: 37 additions & 0 deletions Examples/Run/Reconstruction/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,43 @@ target_link_libraries(
ActsExamplesDetectorTGeo
)

# =================================================
# Convert measurements to spacepoints and write sp to csv
add_executable(
ActsExampleMeasurementsToSPTGeo
MeasurementsToSPTGeo.cpp
)
target_link_libraries(
ActsExampleMeasurementsToSPTGeo
PRIVATE
ActsExamplesFramework
ActsExamplesCommon
ActsExamplesIoCsv
ActsExamplesIoJson
ActsExamplesIoRoot
ActsExamplesDetectorTGeo
ActsExamplesMagneticField
ActsExamplesTrackFinding
ActsExamplesRecTracksCommon
)

add_executable(
ActsExampleMeasurementsToSPGeneric
MeasurementsToSPGeneric.cpp)
target_link_libraries(
ActsExampleMeasurementsToSPGeneric
PRIVATE
ActsExamplesFramework
ActsExamplesCommon
ActsExamplesIoCsv
ActsExamplesIoJson
ActsExamplesIoRoot
ActsExamplesDetectorGeneric
ActsExamplesMagneticField
ActsExamplesTrackFinding
ActsExamplesRecTracksCommon)
# =================================================

install(
TARGETS
ActsExampleTruthTracksGeneric
Expand Down
4 changes: 3 additions & 1 deletion Examples/Run/Reconstruction/Common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ add_library(
ActsExamplesRecTracksCommon SHARED
RecTruthTracks.cpp
RecCKFTracks.cpp
SeedingExample.cpp)
SeedingExample.cpp
MeasurementsToSpacepoints.cpp
)
target_include_directories(
ActsExamplesRecTracksCommon
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
Expand Down
117 changes: 117 additions & 0 deletions Examples/Run/Reconstruction/Common/MeasurementsToSpacepoints.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// 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 "Acts/Definitions/Units.hpp"
#include "ActsExamples/Detector/IBaseDetector.hpp"
#include "ActsExamples/Framework/RandomNumbers.hpp"
#include "ActsExamples/Framework/Sequencer.hpp"
#include "ActsExamples/Geometry/CommonGeometry.hpp"
#include "ActsExamples/Io/Csv/CsvOptionsReader.hpp"
#include "ActsExamples/Io/Csv/CsvParticleReader.hpp" // for evaluating performance
#include "ActsExamples/Io/Csv/CsvSimHitReader.hpp"
#include "ActsExamples/Io/Csv/CsvSpacepointWriter.hpp"
#include "ActsExamples/Io/Json/JsonDigitizationConfig.hpp" // to read digi config
#include "ActsExamples/MagneticField/MagneticFieldOptions.hpp"
#include "ActsExamples/Options/CommonOptions.hpp"
#include "ActsExamples/Reconstruction/ReconstructionBase.hpp"
#include "ActsExamples/TrackFinding/SpacePointMaker.hpp"
#include "ActsExamples/TruthTracking/TruthSeedSelector.hpp" // for evaluating performance
#include "ActsExamples/Utilities/Options.hpp"
#include "ActsExamples/Utilities/Paths.hpp"
#include <Acts/Utilities/Logger.hpp>

#include <iostream>

#include <boost/program_options.hpp>

using namespace Acts::UnitLiterals;
using namespace ActsExamples;

static std::unique_ptr<const Acts::Logger> m_logger;
const Acts::Logger& logger() {
return *m_logger;
}
int runMeasurementsToSP(int argc, char* argv[],
std::shared_ptr<ActsExamples::IBaseDetector> detector) {
// Setup and parse options
auto desc = Options::makeDefaultOptions();
Options::addSequencerOptions(desc);
Options::addRandomNumbersOptions(desc);
Options::addGeometryOptions(desc);
Options::addMaterialOptions(desc);
Options::addOutputOptions(desc, OutputFormat::Csv | OutputFormat::Root);
Options::addInputOptions(desc);
Options::addMagneticFieldOptions(desc);

// Add specific options for this geometry
// <TODO> make it as an argument.
// auto detector = std::make_shared<TGeoDetector>();
detector->addOptions(desc);

auto vm = Options::parse(desc, argc, argv);
if (vm.empty()) {
return EXIT_FAILURE;
}

Sequencer sequencer(Options::readSequencerConfig(vm));

// Now read the standard options
auto logLevel = Options::readLogLevel(vm);
auto outputDir = ensureWritableDirectory(vm["output-dir"].as<std::string>());

m_logger = Acts::getDefaultLogger("MeasurementsToSP", logLevel);
ACTS_INFO("after parsing input options");

// The geometry, material and decoration
// build the detector
auto geometry = Geometry::build(vm, *detector);
auto tGeometry = geometry.first;
auto contextDecorators = geometry.second;
auto randomNumbers =
std::make_shared<RandomNumbers>(Options::readRandomNumbersConfig(vm));

ACTS_INFO("after building geometry");

// Add the decorator to the sequencer
for (auto cdr : contextDecorators) {
sequencer.addContextDecorator(cdr);
}

ACTS_INFO("after adding context decorator");

// Setup the magnetic field
Options::setupMagneticFieldServices(vm, sequencer);
auto magneticField = Options::readMagneticField(vm);

ACTS_INFO("after setting magnetic field");

// Read the inputs
auto simHitReaderCfg = setupSimHitReading(vm, sequencer);
auto particleReader = setupParticleReading(vm, sequencer);
auto measurementsReader = setupMeasurementReading(vm, sequencer);

ACTS_INFO("after reading SimHits and particles");

// Now measurements --> SpacePoints
SpacePointMaker::Config spCfg;
spCfg.inputSourceLinks = measurementsReader.outputSourceLinks;
spCfg.inputMeasurements = measurementsReader.outputMeasurements;
spCfg.outputSpacePoints = "spacepoints";
spCfg.trackingGeometry = tGeometry;
spCfg.geometrySelection = {Acts::GeometryIdentifier().setVolume(0)};
sequencer.addAlgorithm(std::make_shared<SpacePointMaker>(spCfg, logLevel));

// // write out spacepoints...
CsvSpacepointWriter::Config spWriterCfg;
spWriterCfg.inputSpacepoints = spCfg.outputSpacePoints;
spWriterCfg.outputDir = outputDir;
sequencer.addWriter(
std::make_shared<CsvSpacepointWriter>(spWriterCfg, logLevel));

return sequencer.run();
}
17 changes: 17 additions & 0 deletions Examples/Run/Reconstruction/Common/MeasurementsToSpacepoints.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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 <memory>
namespace ActsExamples {
class IBaseDetector;
}

int runMeasurementsToSP(int argc, char* argv[],
std::shared_ptr<ActsExamples::IBaseDetector> detector);

0 comments on commit 4f28047

Please sign in to comment.