Skip to content

Commit 592231d

Browse files
authored
Spacepoint OBJ Writing, main branch (2024.05.05.) (#574)
* Introduced the traccc::data_format::obj data format. At the same time updated traccc::opts::output_data to be able to deal with the new format. * Introduced traccc::io::get_absolute_path. The function shall allow us to specify either relative or absolute path names for the input/output files and directories used by the traccc applications. Instead of being forced to have all files in the "one central data directory". * Introduced a trivial Wavefront OBJ writer for spacepoints. At the same time updated the traccc::io::write(...) functions to make use of traccc::io::get_absolute_path(...) for writing binary files as well. * Made it possible to write spacepoint OBJ files with traccc_seq_example.
1 parent 7d96c67 commit 592231d

File tree

11 files changed

+168
-29
lines changed

11 files changed

+168
-29
lines changed

examples/options/include/traccc/options/output_data.hpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
// System include(s).
1515
#include <string>
16+
#include <string_view>
1617

1718
namespace traccc::opts {
1819

@@ -30,8 +31,13 @@ class output_data : public interface {
3031

3132
/// @}
3233

33-
/// Constructor
34-
output_data();
34+
/// Constructor, with default arguments
35+
///
36+
/// @param format The data format for the output
37+
/// @param directory The directory for the output
38+
///
39+
output_data(traccc::data_format format = data_format::csv,
40+
std::string_view directory = "testing/");
3541

3642
/// Read/process the command line options
3743
///

examples/options/src/output_data.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ using data_format_type = std::string;
2222
/// Name of the data format option
2323
static const char* data_format_option = "output-data-format";
2424

25-
output_data::output_data() : interface("Output Data Options") {
25+
output_data::output_data(traccc::data_format f, std::string_view d)
26+
: interface("Output Data Options"), format(f), directory(d) {
2627

2728
m_desc.add_options()(data_format_option,
2829
po::value<data_format_type>()->default_value("csv"),
@@ -44,6 +45,8 @@ void output_data::read(const boost::program_options::variables_map& vm) {
4445
format = data_format::binary;
4546
} else if (input_format_string == "json") {
4647
format = data_format::json;
48+
} else if (input_format_string == "obj") {
49+
format = data_format::obj;
4750
} else {
4851
throw std::invalid_argument("Unknown input data format");
4952
}

examples/run/cpu/seq_example.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "traccc/io/read_digitization_config.hpp"
1111
#include "traccc/io/read_geometry.hpp"
1212
#include "traccc/io/utils.hpp"
13+
#include "traccc/io/write.hpp"
1314

1415
// algorithms
1516
#include "traccc/ambiguity_resolution/greedy_ambiguity_resolution_algorithm.hpp"
@@ -30,6 +31,7 @@
3031
#include "traccc/options/clusterization.hpp"
3132
#include "traccc/options/detector.hpp"
3233
#include "traccc/options/input_data.hpp"
34+
#include "traccc/options/output_data.hpp"
3335
#include "traccc/options/performance.hpp"
3436
#include "traccc/options/program_options.hpp"
3537
#include "traccc/options/track_finding.hpp"
@@ -57,6 +59,7 @@
5759
#include <memory>
5860

5961
int seq_run(const traccc::opts::input_data& input_opts,
62+
const traccc::opts::output_data& output_opts,
6063
const traccc::opts::detector& detector_opts,
6164
const traccc::opts::clusterization& /*clusterization_opts*/,
6265
const traccc::opts::track_seeding& seeding_opts,
@@ -227,6 +230,12 @@ int seq_run(const traccc::opts::input_data& input_opts,
227230
sf(vecmem::get_data(measurements_per_event),
228231
vecmem::get_data(modules_per_event));
229232
}
233+
if (output_opts.directory != "") {
234+
traccc::io::write(event, output_opts.directory,
235+
output_opts.format,
236+
vecmem::get_data(spacepoints_per_event),
237+
vecmem::get_data(modules_per_event));
238+
}
230239

231240
/*-----------------------
232241
Seeding algorithm
@@ -351,6 +360,7 @@ int main(int argc, char* argv[]) {
351360
// Program options.
352361
traccc::opts::detector detector_opts;
353362
traccc::opts::input_data input_opts;
363+
traccc::opts::output_data output_opts{traccc::data_format::obj, ""};
354364
traccc::opts::clusterization clusterization_opts;
355365
traccc::opts::track_seeding seeding_opts;
356366
traccc::opts::track_finding finding_opts;
@@ -359,13 +369,14 @@ int main(int argc, char* argv[]) {
359369
traccc::opts::performance performance_opts;
360370
traccc::opts::program_options program_opts{
361371
"Full Tracking Chain on the Host",
362-
{detector_opts, input_opts, clusterization_opts, seeding_opts,
363-
finding_opts, propagation_opts, resolution_opts, performance_opts},
372+
{detector_opts, input_opts, output_opts, clusterization_opts,
373+
seeding_opts, finding_opts, propagation_opts, resolution_opts,
374+
performance_opts},
364375
argc,
365376
argv};
366377

367378
// Run the application.
368-
return seq_run(input_opts, detector_opts, clusterization_opts, seeding_opts,
369-
finding_opts, propagation_opts, resolution_opts,
370-
performance_opts);
379+
return seq_run(input_opts, output_opts, detector_opts, clusterization_opts,
380+
seeding_opts, finding_opts, propagation_opts,
381+
resolution_opts, performance_opts);
371382
}

io/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ traccc_add_library( traccc_io io TYPE SHARED
7373
"src/csv/make_particle_reader.cpp"
7474
"src/csv/read_particles.hpp"
7575
"src/csv/read_particles.cpp"
76+
"src/obj/write_spacepoints.hpp"
77+
"src/obj/write_spacepoints.cpp"
7678
)
7779
target_link_libraries( traccc_io
7880
PUBLIC vecmem::core traccc::core ActsCore

io/include/traccc/io/data_format.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** TRACCC library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2022 CERN for the benefit of the ACTS project
3+
* (c) 2022-2024 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
@@ -14,9 +14,10 @@ namespace traccc {
1414

1515
/// Format for an input or output file
1616
enum data_format : int {
17-
csv = 0,
18-
binary = 1,
19-
json = 2,
17+
csv = 0, ///< Comma-separated values
18+
binary = 1, ///< Binary format
19+
json = 2, ///< JSON format
20+
obj = 3, ///< Wavefront OBJ format
2021
};
2122

2223
/// Printout helper for @c traccc::data_format

io/include/traccc/io/utils.hpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** TRACCC library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2021-2022 CERN for the benefit of the ACTS project
3+
* (c) 2021-2024 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
@@ -28,4 +28,15 @@ const std::string& data_directory();
2828
///
2929
std::string get_event_filename(std::size_t event, std::string_view suffix);
3030

31-
} // namespace traccc::io
31+
/// Get the absolute path to a file or directory
32+
///
33+
/// This function would just return the received path as-is if it is already
34+
/// an absolute path. Otherwise, it would prepend the traccc data directory
35+
/// to it.
36+
///
37+
/// @param path The path to get the absolute path for
38+
/// @return The absolute path to the file or directory
39+
///
40+
std::string get_absolute_path(std::string_view path);
41+
42+
} // namespace traccc::io

io/src/data_format.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** TRACCC library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2022 CERN for the benefit of the ACTS project
3+
* (c) 2022-2024 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
@@ -25,6 +25,9 @@ std::ostream& operator<<(std::ostream& out, data_format format) {
2525
case data_format::json:
2626
out << "json";
2727
break;
28+
case data_format::obj:
29+
out << "wavefront obj";
30+
break;
2831
default:
2932
out << "?!?unknown?!?";
3033
break;

io/src/obj/write_spacepoints.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/** TRACCC library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2024 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
// Local include(s).
9+
#include "write_spacepoints.hpp"
10+
11+
// System include(s).
12+
#include <fstream>
13+
14+
namespace traccc::io::obj {
15+
16+
void write_spacepoints(
17+
std::string_view filename,
18+
traccc::spacepoint_collection_types::const_view spacepoints_view) {
19+
20+
// Open the output file.
21+
std::ofstream file(filename.data());
22+
if (!file.is_open()) {
23+
throw std::runtime_error("Failed to open file: " +
24+
std::string(filename));
25+
}
26+
27+
// Create a device collection around the spacepoint view.
28+
traccc::spacepoint_collection_types::const_device spacepoints(
29+
spacepoints_view);
30+
31+
// Write the spacepoints.
32+
for (const traccc::spacepoint& sp : spacepoints) {
33+
file << "v " << sp.x() << " " << sp.y() << " " << sp.z() << "\n";
34+
}
35+
}
36+
37+
} // namespace traccc::io::obj

io/src/obj/write_spacepoints.hpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** TRACCC library, part of the ACTS project (R&D line)
2+
*
3+
* (c) 2024 CERN for the benefit of the ACTS project
4+
*
5+
* Mozilla Public License Version 2.0
6+
*/
7+
8+
#pragma once
9+
10+
// Project include(s).
11+
#include "traccc/edm/spacepoint.hpp"
12+
13+
// System include(s).
14+
#include <string_view>
15+
16+
namespace traccc::io::obj {
17+
18+
/// Write a spacepoint collection into a Wavefront OBJ file.
19+
///
20+
/// @param filename is the name of the output file
21+
/// @param spacepoints is the spacepoint collection to write
22+
///
23+
void write_spacepoints(
24+
std::string_view filename,
25+
traccc::spacepoint_collection_types::const_view spacepoints);
26+
27+
} // namespace traccc::io::obj

io/src/utils.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/** TRACCC library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2021-2022 CERN for the benefit of the ACTS project
3+
* (c) 2021-2024 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
@@ -10,6 +10,7 @@
1010

1111
// System include(s).
1212
#include <cstdlib>
13+
#include <filesystem>
1314
#include <iomanip>
1415
#include <sstream>
1516

@@ -41,4 +42,16 @@ std::string get_event_filename(std::size_t event, std::string_view suffix) {
4142
return stream.str();
4243
}
4344

45+
std::string get_absolute_path(std::string_view path) {
46+
47+
// Check if the path is already absolute.
48+
if (std::filesystem::path(path).is_absolute()) {
49+
return std::string{path};
50+
}
51+
52+
// Otherwise, prepend the data directory to the path.
53+
return std::filesystem::path(data_directory()) /
54+
std::filesystem::path(path);
55+
}
56+
4457
} // namespace traccc::io

io/src/write.cpp

+38-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
/** TRACCC library, part of the ACTS project (R&D line)
22
*
3-
* (c) 2022 CERN for the benefit of the ACTS project
3+
* (c) 2022-2024 CERN for the benefit of the ACTS project
44
*
55
* Mozilla Public License Version 2.0
66
*/
77

88
// Local include(s).
99
#include "traccc/io/write.hpp"
1010

11+
#include "obj/write_spacepoints.hpp"
1112
#include "traccc/io/utils.hpp"
1213
#include "write_binary.hpp"
1314

15+
// System include(s).
16+
#include <filesystem>
17+
#include <stdexcept>
18+
1419
namespace traccc::io {
1520

1621
void write(std::size_t event, std::string_view directory,
@@ -21,12 +26,16 @@ void write(std::size_t event, std::string_view directory,
2126
switch (format) {
2227
case data_format::binary:
2328
details::write_binary_collection(
24-
data_directory() + directory.data() +
25-
get_event_filename(event, "-cells.dat"),
29+
get_absolute_path((std::filesystem::path(directory) /
30+
std::filesystem::path(
31+
get_event_filename(event, "-cells.dat")))
32+
.native()),
2633
traccc::cell_collection_types::const_device{cells});
2734
details::write_binary_collection(
28-
data_directory() + directory.data() +
29-
get_event_filename(event, "-modules.dat"),
35+
get_absolute_path((std::filesystem::path(directory) /
36+
std::filesystem::path(get_event_filename(
37+
event, "-modules.dat")))
38+
.native()),
3039
traccc::cell_module_collection_types::const_device{modules});
3140
break;
3241
default:
@@ -42,14 +51,26 @@ void write(std::size_t event, std::string_view directory,
4251
switch (format) {
4352
case data_format::binary:
4453
details::write_binary_collection(
45-
data_directory() + directory.data() +
46-
get_event_filename(event, "-hits.dat"),
54+
get_absolute_path((std::filesystem::path(directory) /
55+
std::filesystem::path(
56+
get_event_filename(event, "-hits.dat")))
57+
.native()),
4758
traccc::spacepoint_collection_types::const_device{spacepoints});
4859
details::write_binary_collection(
49-
data_directory() + directory.data() +
50-
get_event_filename(event, "-modules.dat"),
60+
get_absolute_path((std::filesystem::path(directory) /
61+
std::filesystem::path(get_event_filename(
62+
event, "-modules.dat")))
63+
.native()),
5164
traccc::cell_module_collection_types::const_device{modules});
5265
break;
66+
case data_format::obj:
67+
obj::write_spacepoints(
68+
get_absolute_path((std::filesystem::path(directory) /
69+
std::filesystem::path(get_event_filename(
70+
event, "-spacepoints.obj")))
71+
.native()),
72+
spacepoints);
73+
break;
5374
default:
5475
throw std::invalid_argument("Unsupported data format");
5576
}
@@ -63,13 +84,17 @@ void write(std::size_t event, std::string_view directory,
6384
switch (format) {
6485
case data_format::binary:
6586
details::write_binary_collection(
66-
data_directory() + directory.data() +
67-
get_event_filename(event, "-measurements.dat"),
87+
get_absolute_path((std::filesystem::path(directory) /
88+
std::filesystem::path(get_event_filename(
89+
event, "-measurements.dat")))
90+
.native()),
6891
traccc::measurement_collection_types::const_device{
6992
measurements});
7093
details::write_binary_collection(
71-
data_directory() + directory.data() +
72-
get_event_filename(event, "-modules.dat"),
94+
get_absolute_path((std::filesystem::path(directory) /
95+
std::filesystem::path(get_event_filename(
96+
event, "-modules.dat")))
97+
.native()),
7398
traccc::cell_module_collection_types::const_device{modules});
7499
break;
75100
default:

0 commit comments

Comments
 (0)