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

Truth Particle Update, main branch (2024.05.23.) #592

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ traccc_add_library( traccc_core core TYPE SHARED
"include/traccc/edm/cluster.hpp"
"include/traccc/edm/spacepoint.hpp"
"include/traccc/edm/measurement.hpp"
"include/traccc/edm/particle.hpp"
"include/traccc/edm/track_parameters.hpp"
"include/traccc/edm/container.hpp"
"include/traccc/edm/internal_spacepoint.hpp"
Expand Down
13 changes: 8 additions & 5 deletions core/include/traccc/edm/particle.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
* (c) 2022-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -10,20 +10,21 @@
// Local include(s).
#include "traccc/definitions/primitives.hpp"
#include "traccc/edm/container.hpp"
#include "traccc/edm/measurement.hpp"

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

namespace traccc {

// Definition of truth particle
/// Definition of a truth particle
struct particle {
uint64_t particle_id;
std::uint64_t particle_id;
int particle_type;
int process;
point3 pos;
point3 vertex;
scalar time;
vector3 mom;
vector3 momentum;
scalar mass;
scalar charge;
};
Expand All @@ -37,5 +38,7 @@ inline bool operator<(const particle& lhs, const particle& rhs) {

/// Declare all particle collection types
using particle_collection_types = collection_types<particle>;
/// Declare all particle container types
using particle_container_types = container_types<particle, measurement>;

} // namespace traccc
16 changes: 13 additions & 3 deletions io/include/traccc/io/read_measurements.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
* (c) 2022-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -16,6 +16,8 @@

// System include(s).
#include <cstddef>
#include <cstdint>
#include <map>
#include <string_view>

namespace traccc::io {
Expand All @@ -29,10 +31,14 @@ namespace traccc::io {
/// @param event The event ID to read in the cells for
/// @param directory The directory holding the cell data files
/// @param format The format of the cell data files (to read)
/// @param bardoce_map An object to perform barcode re-mapping with
/// (For Acts->Detray identifier re-mapping, if necessary)
///
void read_measurements(measurement_reader_output& out, std::size_t event,
std::string_view directory,
data_format format = data_format::csv);
data_format format = data_format::csv,
const std::map<std::uint64_t, detray::geometry::barcode>*
barcode_map = nullptr);

/// Read measurement data into memory
///
Expand All @@ -41,9 +47,13 @@ void read_measurements(measurement_reader_output& out, std::size_t event,
/// @param out A measurement & a cell_module (host) collections
/// @param filename The file to read the cell data from
/// @param format The format of the cell data files (to read)
/// @param bardoce_map An object to perform barcode re-mapping with
/// (For Acts->Detray identifier re-mapping, if necessary)
///
void read_measurements(measurement_reader_output& out,
std::string_view filename,
data_format format = data_format::csv);
data_format format = data_format::csv,
const std::map<std::uint64_t, detray::geometry::barcode>*
barcode_map = nullptr);

} // namespace traccc::io
72 changes: 55 additions & 17 deletions io/include/traccc/io/read_particles.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
* (c) 2022-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -13,42 +13,80 @@
// Project include(s).
#include "traccc/edm/particle.hpp"

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

// System include(s).
#include <cstddef>
#include <cstdint>
#include <map>
#include <string_view>

namespace traccc::io {

/// Read particle data into memory
/// Read basic truth particle data into memory
///
/// The file to read is selected according the naming conventions used in
/// our data.
///
/// @param particles A particle collection to fill
/// @param event The event ID to read in the cells for
/// @param directory The directory holding the particle data files
/// @param format The format of the particle data files (to read)
/// @param mr The memory resource to create the host collection with
/// @return A particle (host) collection
/// @param filename_postfix Postfix for the particle file name(s)
///
particle_collection_types::host read_particles(
std::size_t event, std::string_view directory,
data_format format = data_format::csv,
vecmem::memory_resource *mr = nullptr);
void read_particles(particle_collection_types::host &particles,
std::size_t event, std::string_view directory,
data_format format = data_format::csv,
std::string_view filename_postfix = "-particles_initial");

/// Read particle data into memory
/// Read basic truth particle data into memory
///
/// The file name is selected explicitly by the user.
///
/// @param particles A particle collection to fill
/// @param filename The file to read the particle data from
/// @param format The format of the particle data files (to read)
/// @param mr The memory resource to create the host collection with
/// @return A particle (host) collection
///
particle_collection_types::host read_particles(
std::string_view filename, data_format format = data_format::csv,
vecmem::memory_resource *mr = nullptr);
void read_particles(particle_collection_types::host &particles,
std::string_view filename,
data_format format = data_format::csv);

/// Read full truth particle data into memory
///
/// The file to read is selected according the naming conventions used in
/// our data.
///
/// @param particles A particle container to fill
/// @param event The event ID to read in the cells for
/// @param directory The directory holding the particle data files
/// @param format The format of the particle data files (to read)
/// @param bardoce_map An object to perform barcode re-mapping with
/// (For Acts->Detray identifier re-mapping, if necessary)
/// @param filename_postfix Postfix for the particle file name(s)
///
void read_particles(particle_container_types::host &particles,
std::size_t event, std::string_view directory,
data_format format = data_format::csv,
const std::map<std::uint64_t, detray::geometry::barcode>
*barcode_map = nullptr,
std::string_view filename_postfix = "-particles_initial");

/// Read full truth particle data into memory
///
/// The required file names are selected explicitly by the user.
///
/// @param particles A particle container to fill
/// @param particles_file The file to read the particle data from
/// @param hits_file The file to read the simulated hits from
/// @param measurements_file The file to read the "Acts measurements" from
/// @param hit_map_file The file to read the hit->measurement mapping from
/// @param format The format of the particle data files (to read)
/// @param bardoce_map An object to perform barcode re-mapping with
/// (For Acts->Detray identifier re-mapping, if necessary)
///
void read_particles(
particle_container_types::host &particles, std::string_view particles_file,
std::string_view hits_file, std::string_view measurements_file,
std::string_view hit_map_file, data_format format = data_format::csv,
const std::map<std::uint64_t, detray::geometry::barcode> *barcode_map =
nullptr);

} // namespace traccc::io
87 changes: 22 additions & 65 deletions io/src/csv/read_measurements.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
* (c) 2022-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -18,8 +18,10 @@

namespace traccc::io::csv {

void read_measurements(measurement_reader_output& out,
std::string_view filename, const bool do_sort) {
void read_measurements(
measurement_reader_output& out, std::string_view filename,
const bool do_sort,
const std::map<std::uint64_t, detray::geometry::barcode>* barcode_map) {

// Construct the measurement reader object.
auto reader = make_measurement_reader(filename);
Expand All @@ -28,22 +30,34 @@ void read_measurements(measurement_reader_output& out,
measurement_collection_types::host& result_measurements = out.measurements;
cell_module_collection_types::host& result_modules = out.modules;

std::map<geometry_id, unsigned int> m;
std::map<detray::geometry::barcode, unsigned int> m;

// Read the measurements from the input file.
csv::measurement iomeas;
while (reader.read(iomeas)) {

// Establish the "correct" geometry ID.
detray::geometry::barcode barcode{iomeas.geometry_id};
if (barcode_map != nullptr) {
auto it = barcode_map->find(iomeas.geometry_id);
if (it != barcode_map->end()) {
barcode = (*it).second;
} else {
throw std::runtime_error("Barcode not found for geometry ID " +
std::to_string(iomeas.geometry_id));
}
}

unsigned int link;
auto it = m.find(iomeas.geometry_id);
auto it = m.find(barcode);

if (it != m.end()) {
link = (*it).second;
} else {
link = result_modules.size();
m[iomeas.geometry_id] = link;
m[barcode] = link;
cell_module mod;
mod.surface_link = detray::geometry::barcode{iomeas.geometry_id};
mod.surface_link = barcode;
result_modules.push_back(mod);
}

Expand Down Expand Up @@ -76,7 +90,7 @@ void read_measurements(measurement_reader_output& out,
}

meas.subs.set_indices(indices);
meas.surface_link = detray::geometry::barcode{iomeas.geometry_id};
meas.surface_link = barcode;
meas.module_link = link;
// Keeps measurement_id for ambiguity resolution
meas.measurement_id = iomeas.measurement_id;
Expand All @@ -90,61 +104,4 @@ void read_measurements(measurement_reader_output& out,
}
}

measurement_container_types::host read_measurements_container(
std::string_view filename, vecmem::memory_resource* mr) {

// Construct the measurement reader object.
auto reader = make_measurement_reader(filename);

// Create the result container.
measurement_container_types::host result;
if (mr != nullptr) {
result = measurement_container_types::host{mr};
}

// Read the measurements from the input file.
csv::measurement iomeas;
while (reader.read(iomeas)) {

// Construct the module ID for the measurement.
cell_module module;
module.surface_link = detray::geometry::barcode{iomeas.geometry_id};

// Construct the measurement object.
const traccc::measurement meas{
point2{iomeas.local0, iomeas.local1},
variance2{iomeas.var_local0, iomeas.var_local1},
detray::geometry::barcode{iomeas.geometry_id}};

// Find the detector module that this measurement belongs to.
const measurement_container_types::host::header_vector& headers =
result.get_headers();
auto rit = std::find(headers.rbegin(), headers.rend(), module);

// Add the measurement to the correct place in the container.
if (rit == headers.rend()) {
if (mr != nullptr) {
result.push_back(
module,
measurement_container_types::host::item_vector::value_type(
{meas}, mr));
} else {
result.push_back(
module,
measurement_container_types::host::item_vector::value_type(
{meas}));
}
} else {
// The reverse iterator.base() returns the equivalent normal
// iterator shifted by 1, so that the (r)end and (r)begin iterators
// match consistently, due to the extra past-the-last element
auto idx = std::distance(headers.begin(), rit.base()) - 1;
result.at(idx).items.push_back(meas);
}
}

// Return the container.
return result;
}

} // namespace traccc::io::csv
10 changes: 8 additions & 2 deletions io/src/csv/read_measurements.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** TRACCC library, part of the ACTS project (R&D line)
*
* (c) 2022 CERN for the benefit of the ACTS project
* (c) 2022-2024 CERN for the benefit of the ACTS project
*
* Mozilla Public License Version 2.0
*/
Expand All @@ -12,6 +12,8 @@
#include "traccc/io/reader_edm.hpp"

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

namespace traccc::io::csv {
Expand All @@ -21,8 +23,12 @@ namespace traccc::io::csv {
/// @param out A measurement & a cell_module (host) collections
/// @param filename The file to read the measurement data from
/// @param do_sort Whether to sort the measurements or not
/// @param bardoce_map An object to perform barcode re-mapping with
/// (For Acts->Detray identifier re-mapping, if necessary)
///
void read_measurements(measurement_reader_output& out,
std::string_view filename, const bool do_sort = true);
std::string_view filename, const bool do_sort = true,
const std::map<std::uint64_t, detray::geometry::barcode>*
barcode_map = nullptr);

} // namespace traccc::io::csv
Loading
Loading