Skip to content

Commit

Permalink
Implement digitization configurator for detray geometries
Browse files Browse the repository at this point in the history
  • Loading branch information
niermann999 committed Oct 13, 2023
1 parent 7a4aca5 commit f67290c
Show file tree
Hide file tree
Showing 9 changed files with 517 additions and 1 deletion.
5 changes: 4 additions & 1 deletion examples/io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
# Mozilla Public License Version 2.0

traccc_add_executable( create_binaries "create_binaries.cpp"
LINK_LIBRARIES vecmem::core traccc::core traccc::io traccc::options)
LINK_LIBRARIES vecmem::core traccc::core traccc::io traccc::options)

traccc_add_executable( create_digitization_config "create_digitization_config.cpp"
LINK_LIBRARIES vecmem::core traccc::core traccc::io traccc::options detray::io)
114 changes: 114 additions & 0 deletions examples/io/create_digitization_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2023 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

// Project include(s).
#include "traccc/io/digitization_configurator.hpp"
#include "traccc/io/read_digitization_config.hpp"
#include "traccc/io/write_digitization_config.hpp"
#include "traccc/options/handle_argument_errors.hpp"

// Detray include(s)
#include "detray/core/detector.hpp"
#include "detray/geometry/surface.hpp"
#include "detray/io/common/detector_reader.hpp"

// VecMem include(s).
#include <vecmem/memory/host_memory_resource.hpp>

// Boost
#include <boost/program_options.hpp>

// System include(s)
#include <sstream>
#include <stdexcept>
#include <string>

namespace po = boost::program_options;

/// @brief create a detailed digitization file for the given detector
///
/// Takes a human editable digitization "meta"-configuration file and assigns
/// the given configuration to the specified surfaces by visitng the geometry.
/// For every discovered surface in a digitization domain, the respective
/// digitization configuration is written into the output json file.
///
/// @param digi_meta_config_file file that contains the editable "meta"-config
/// @param reader_cfg detray detector reader config, specifying the geometry
int create_digitization_config(
const std::string& digi_meta_config_file,
const detray::io::detector_reader_config& reader_cfg) {

vecmem::host_memory_resource host_mr;

const auto [det, names] =
detray::io::read_detector<detray::detector<>>(host_mr, reader_cfg);

std::cout << "Running digitization configurator for \"" << names.at(0)
<< "\"..." << std::endl;

const traccc::digitization_config digi_meta_cfg =
traccc::io::read_digitization_config(digi_meta_config_file);

traccc::io::digitization_configurator digi_configurator{digi_meta_cfg};

// Visit all detector surfaces to generate their digitization configuration
for (const auto& sf_desc : det.surface_lookup()) {
digi_configurator(detray::surface{det, sf_desc});
}

std::string outfile_name = names.at(0) + "-geometric-config.json";
traccc::io::write_digitization_config(outfile_name,
digi_configurator.output_digi_cfg);

std::cout << "Done. Written config to: " << outfile_name << std::endl;

return EXIT_SUCCESS;
}

// The main routine
//
int main(int argc, char* argv[]) {
// Options parsing
po::options_description desc("\ntraccc digitization configurator");

desc.add_options()("help", "produce help message")(
"geometry_file", po::value<std::string>(),
"detray geometry input file")("digi_meta_config_file",
po::value<std::string>(),
"digitization meta-configuration file");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);

traccc::handle_argument_errors(vm, desc);

// Configs to be filled
detray::io::detector_reader_config reader_cfg{};

// Input files
if (vm.count("geometry_file")) {
reader_cfg.add_file(vm["geometry_file"].as<std::string>());
} else {
std::stringstream err_stream{};
err_stream << "Please specify a geometry input file!\n\n" << desc;

throw std::invalid_argument(err_stream.str());
}
std::string digi_meta_config_file;
if (vm.count("geometry_file")) {
digi_meta_config_file = vm["digi_meta_config_file"].as<std::string>();
} else {
std::stringstream err_stream{};
err_stream << "Please specify a digitization meta-configuration input"
<< " file!\n\n"
<< desc;

throw std::invalid_argument(err_stream.str());
}

return create_digitization_config(digi_meta_config_file, reader_cfg);
}
4 changes: 4 additions & 0 deletions io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ find_package( OpenMP COMPONENTS CXX )
traccc_add_library( traccc_io io TYPE SHARED
# Public headers
"include/traccc/io/digitization_config.hpp"
"include/traccc/io/digitization_configurator.hpp"
"include/traccc/io/digitization_configurator.ipp"
"include/traccc/io/read.hpp"
"include/traccc/io/read_cells.hpp"
"include/traccc/io/read_digitization_config.hpp"
Expand All @@ -26,6 +28,7 @@ traccc_add_library( traccc_io io TYPE SHARED
"include/traccc/io/event_map2.hpp"
"include/traccc/io/demonstrator_edm.hpp"
"include/traccc/io/mapper.hpp"
"include/traccc/io/write_digitization_config.hpp"
"include/traccc/io/write.hpp"
"include/traccc/io/utils.hpp"
"include/traccc/io/details/read_surfaces.hpp"
Expand All @@ -49,6 +52,7 @@ traccc_add_library( traccc_io io TYPE SHARED
"src/utils.cpp"
"src/read_binary.hpp"
"src/write_binary.hpp"
"src/write_digitization_config.cpp"
"src/details/read_surfaces.cpp"
"src/csv/surface.hpp"
"src/csv/make_surface_reader.hpp"
Expand Down
3 changes: 3 additions & 0 deletions io/include/traccc/io/digitization_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace traccc {

/// Type describing the digitization configuration of a detector module
struct module_digitization_config {
/// Measurement indices
std::vector<unsigned int> indices{};
/// Module segmentation description
Acts::BinUtility segmentation;
};

Expand Down
90 changes: 90 additions & 0 deletions io/include/traccc/io/digitization_configurator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2023 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/

#pragma once

// Local include(s).
#include "traccc/io/data_format.hpp"

// Project include(s).
#include "traccc/io/digitization_config.hpp"

// Detray include(s).
#include "detray/geometry/barcode.hpp"
#include "detray/geometry/surface.hpp"

// System include(s).
#include <string_view>

namespace Acts {
class BinUtility;
}

namespace traccc::io {

/// Helper configurator that takes a simplified (per volume, per extra bit)
/// input digitization file and creates a full fletched per module
/// digitization configuration file.
///
/// It acts as a visitor and then builds a fully developed digitization file
/// for the geometric digitization, filling in the correct dimensions and
/// number of bins.
///
/// The simplified file is assumed to have just one bin for the geometric
/// digitization, which is then used to calculate the number of bins with
/// respect to the bounds range.
struct digitization_configurator {
/// Simplified input components for digitization (meta-configuration)
digitization_config input_digi_cfg;

/// Final collection of output components
std::vector<
std::pair<detray::geometry::barcode, module_digitization_config>>
output_digi_cfg;

/// Needs an input configuration
digitization_configurator() = delete;

/// Construct from an input configuration @param cfg and initializes an
/// empty output configuration
digitization_configurator(digitization_config cfg)
: input_digi_cfg{cfg}, output_digi_cfg{} {}

/// The visitor call for the geometry
///
/// @param surface is the surfaces that is visited
///
/// Takes the @c input_digi_cfg and adds an appropriate entry into the
/// @c output_digi_cfg for the given surface
template <class detector_t>
void operator()(const detray::surface<detector_t> &surface);

struct segmentation_configurator {

/// Create the segmentation for a specific surface from the
/// given configuration in @param input_segmentation.
template <typename mask_t>
void fill_output_segmentation(
const mask_t &mask, const Acts::BinUtility &input_segmentation,
Acts::BinUtility &output_segmentation) const;

/// Visitor for the surface mask
template <typename mask_group_t, typename index_t>
inline void operator()(const mask_group_t &mask_group,
const index_t &index,
const Acts::BinUtility &input_segmentation,
Acts::BinUtility &output_segmentation) const {

fill_output_segmentation(mask_group[index], input_segmentation,
output_segmentation);
}
};
};

} // namespace traccc::io

#include "traccc/io/digitization_configurator.ipp"
Loading

0 comments on commit f67290c

Please sign in to comment.