Skip to content

Commit

Permalink
Merge pull request #56 from ornlneutronimaging/tof_imaging
Browse files Browse the repository at this point in the history
Enable TOF imaging output
  • Loading branch information
KedoKudo authored Aug 26, 2024
2 parents e6409a5 + d1e8bbc commit bf23ec4
Show file tree
Hide file tree
Showing 27 changed files with 1,903 additions and 468 deletions.
2 changes: 0 additions & 2 deletions sophiread/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,3 @@ WhitespaceSensitiveMacros:
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...

67 changes: 48 additions & 19 deletions sophiread/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1,65 @@
# Sophiread CMakeLists.txt
cmake_minimum_required(VERSION 3.20)

execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/scripts/version.sh -s print
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE SOPHIREAD_VERSION)
OUTPUT_VARIABLE SOPHIREAD_VERSION
RESULT_VARIABLE VERSION_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT VERSION_RESULT EQUAL 0)
message(FATAL_ERROR "Failed to determine version")
endif()

project("Sophiread" VERSION ${SOPHIREAD_VERSION})

# set(CMAKE_BUILD_TYPE DEBUG)
# This is to avoid accidentally using Homebrew header/lib instead of the one from micromamba
if(DEFINED ENV{CONDA_PREFIX})
set(CMAKE_PREFIX_PATH $ENV{CONDA_PREFIX} ${CMAKE_PREFIX_PATH})
endif()

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_STANDARD 20)
# set(CMAKE_AUTOMOC ON) # for meta object compiler, needed for Qt5

# Dependencies
find_package(Eigen3 REQUIRED)
find_package(spdlog REQUIRED)
find_package(HDF5 REQUIRED COMPONENTS CXX)
find_package(GTest REQUIRED)
find_package(nlohmann_json 3.2.0 REQUIRED)
find_package(TBB REQUIRED)
find_package(TIFF REQUIRED)
find_package(spdlog 1.8.0 REQUIRED)
find_package(fmt 7.0.0 REQUIRED)
# find_package(Qt5 COMPONENTS Widgets REQUIRED)

include_directories(${HDF5_INCLUDE_DIRS})

link_directories(
$ENV{CONDA_PREFIX}/lib
)

# Testing setup
enable_testing()
include(GoogleTest)
file(COPY ${CMAKE_SOURCE_DIR}/resources/data DESTINATION ${CMAKE_BINARY_DIR})

# Add compiler flags
add_compile_options(
-O3
-std=c++20
-march=native
-ffast-math
-pthread
-Wall
-Wextra
# Copy resources
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/data
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/resources/data
${CMAKE_BINARY_DIR}/data
DEPENDS ${CMAKE_SOURCE_DIR}/resources/data
)
add_custom_target(copy_resources ALL DEPENDS ${CMAKE_BINARY_DIR}/data)

# Add compiler flags
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(
-march=native
-ffast-math
-pthread
-Wall
-Wextra
)
endif()

# Add Sophiread library
# NOTE: this will take care of the testing as well
Expand Down Expand Up @@ -65,6 +87,13 @@ if(DOXYGEN_FOUND)
COMMENT "Generating API documentation with Doxygen"
VERBATIM
)
if(APPLE)
set(OPEN_COMMAND open)
elseif(UNIX)
set(OPEN_COMMAND xdg-open)
elseif(WIN32)
set(OPEN_COMMAND start)
endif()
add_custom_command(TARGET docs POST_BUILD
COMMAND ${CMAKE_COMMAND} -E chdir
${CMAKE_BINARY_DIR}/docs/html
Expand Down
227 changes: 65 additions & 162 deletions sophiread/FastSophiread/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
# Config SophireadLib
include_directories(
include
$ENV{CONDA_PREFIX}/include
${EIGEN3_INCLUDE_DIR}
${HDF5_INCLUDE_DIRS}
)

link_directories(
$ENV{CONDA_PREFIX}/lib
)

# Config FastSophireadLib
set(SRC_FAST_FILES
src/abs.cpp
src/centroid.cpp
Expand All @@ -21,164 +10,78 @@ set(SRC_FAST_FILES

# ------------- SophireadLibFast -------------- #
add_library(FastSophiread ${SRC_FAST_FILES})

# ----------------- TESTS ----------------- #
# DiskIO Tests
add_executable(
SophireadTests_diskio
tests/test_disk_io.cpp
)
target_link_libraries(
SophireadTests_diskio
FastSophiread
tbb
spdlog::spdlog
GTest::GTest
GTest::Main
${HDF5_LIBRARIES}
)
gtest_discover_tests(SophireadTests_diskio)
# Hit Test
add_executable(
SophireadTests_hit
tests/test_hit.cpp
)
target_link_libraries(
SophireadTests_hit
FastSophiread
pthread
tbb
spdlog::spdlog
GTest::GTest
GTest::Main
)
gtest_discover_tests(SophireadTests_hit)
# TPX3 Test
add_executable(
SophireadTests_tpx3
tests/test_tpx3.cpp
)
target_link_libraries(
SophireadTests_tpx3
FastSophiread
pthread
tbb
spdlog::spdlog
GTest::GTest
GTest::Main
${HDF5_LIBRARIES}
)
gtest_discover_tests(SophireadTests_tpx3)
# ABS Test
add_executable(
SophireadTests_abs
tests/test_abs.cpp
)
target_link_libraries(
SophireadTests_abs
FastSophiread
pthread
tbb
spdlog::spdlog
GTest::GTest
GTest::Main
)
gtest_discover_tests(SophireadTests_abs)
# Centroid Test
add_executable(
SophireadTests_centroid
tests/test_centroid.cpp
)
target_link_libraries(
SophireadTests_centroid
FastSophiread
pthread
tbb
spdlog::spdlog
GTest::GTest
GTest::Main
)
gtest_discover_tests(SophireadTests_centroid)
# Fast Gaussian Test
add_executable(
SophireadTests_fastgaussian
tests/test_fastgaussian.cpp
target_include_directories(FastSophiread
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
$ENV{CONDA_PREFIX}/include
${EIGEN3_INCLUDE_DIR}
${HDF5_INCLUDE_DIRS}
)
target_link_libraries(
SophireadTests_fastgaussian
FastSophiread
pthread
tbb
spdlog::spdlog
GTest::GTest
GTest::Main
target_link_directories(FastSophiread
PRIVATE
$ENV{CONDA_PREFIX}/lib
)
gtest_discover_tests(SophireadTests_fastgaussian)

# ----------------- TESTS ----------------- #
function(add_sophiread_test test_name)
add_executable(${test_name} tests/test_${test_name}.cpp)
target_link_libraries(${test_name}
PRIVATE
FastSophiread
pthread
TBB::tbb
spdlog::spdlog
GTest::GTest
GTest::Main
${ARGN}
)
gtest_discover_tests(${test_name})
endfunction()
# Add tests
add_sophiread_test(disk_io ${HDF5_LIBRARIES})
add_sophiread_test(hit)
add_sophiread_test(tpx3 ${HDF5_LIBRARIES})
add_sophiread_test(abs)
add_sophiread_test(centroid)
add_sophiread_test(fastgaussian)


# ------------------ Benchmarks ------------------ #
# Raw2Hits Benchmark
add_executable(
SophireadBenchmarks_raw2hits
benchmarks/benchmark_raw2hits.cpp
)
target_link_libraries(
SophireadBenchmarks_raw2hits
FastSophiread
pthread
tbb
spdlog::spdlog
${HDF5_LIBRARIES}
)
add_custom_command(TARGET SophireadBenchmarks_raw2hits POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
${PROJECT_BINARY_DIR}/FastSophiread/SophireadBenchmarks_raw2hits
${PROJECT_BINARY_DIR}/FastSophireadBenchmarks_raw2hits.app
)
# Hits2Events Benchmark
add_executable(
SophireadBenchmarks_hits2events
benchmarks/benchmark_hits2events.cpp
)
target_link_libraries(
SophireadBenchmarks_hits2events
FastSophiread
pthread
tbb
spdlog::spdlog
)
add_custom_command(TARGET SophireadBenchmarks_hits2events POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
${PROJECT_BINARY_DIR}/FastSophiread/SophireadBenchmarks_hits2events
${PROJECT_BINARY_DIR}/FastSophireadBenchmarks_hits2events.app
)
# Raw2Events Benchmark
add_executable(
SophireadBenchmarks_raw2events
benchmarks/benchmark_raw2events.cpp
)
target_link_libraries(
SophireadBenchmarks_raw2events
FastSophiread
pthread
tbb
spdlog::spdlog
${HDF5_LIBRARIES}
)
add_custom_command(TARGET SophireadBenchmarks_raw2events POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
${PROJECT_BINARY_DIR}/FastSophiread/SophireadBenchmarks_raw2events
${PROJECT_BINARY_DIR}/FastSophireadBenchmarks_raw2events.app
)
# CLI
add_executable(
FastSophireadBenchmarksCLI
# Define a function to add benchmark targets
function(add_sophiread_benchmark NAME)
set(TARGET_NAME SophireadBenchmarks_${NAME})
add_executable(${TARGET_NAME} benchmarks/benchmark_${NAME}.cpp)

target_link_libraries(${TARGET_NAME}
PRIVATE
FastSophiread
pthread
TBB::tbb
spdlog::spdlog
${ARGN} # Additional libraries passed as arguments
)

add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
${PROJECT_BINARY_DIR}/FastSophiread/${TARGET_NAME}
${PROJECT_BINARY_DIR}/FastSophireadBenchmarks_${NAME}.app
)
endfunction()

# Add benchmarks
add_sophiread_benchmark(raw2hits ${HDF5_LIBRARIES})
add_sophiread_benchmark(hits2events)
add_sophiread_benchmark(raw2events ${HDF5_LIBRARIES})

# ----------------- CLI ----------------- #
add_executable(FastSophireadBenchmarksCLI
benchmarks/benchmark_mmap.cpp
)
target_link_libraries(
FastSophireadBenchmarksCLI
target_link_libraries(FastSophireadBenchmarksCLI
PRIVATE
FastSophiread
pthread
tbb
TBB::tbb
spdlog::spdlog
${HDF5_LIBRARIES}
)
Expand Down
8 changes: 4 additions & 4 deletions sophiread/FastSophiread/benchmarks/benchmark_mmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ void saveEventsToHDF5(const std::string out_file_name, const std::vector<Neutron
y_dataset.write(y.data(), float_type);
// -- write TOF_ns
std::vector<double> tof_ns(events.size());
std::transform(events.begin(), events.end(), tof_ns.begin(),
std::transform(events.cbegin(), events.cend(), tof_ns.begin(),
[](const Neutron &event) { return event.getTOF_ns(); });
H5::DataSet tof_ns_dataset = group.createDataSet("tof", float_type, dataspace);
tof_ns_dataset.write(tof_ns.data(), float_type);
// -- write Nhits
std::vector<int> nhits(events.size());
std::transform(events.begin(), events.end(), nhits.begin(),
std::transform(events.cbegin(), events.cend(), nhits.begin(),
[](const Neutron &event) { return event.getNHits(); });
H5::DataSet nhits_dataset = group.createDataSet("nHits", int_type, dataspace);
nhits_dataset.write(nhits.data(), int_type);
// -- write TOT
std::vector<double> tot(events.size());
std::transform(events.begin(), events.end(), tot.begin(), [](const Neutron &event) { return event.getTOT(); });
std::transform(events.cbegin(), events.cend(), tot.begin(), [](const Neutron &event) { return event.getTOT(); });
H5::DataSet tot_dataset = group.createDataSet("tot", float_type, dataspace);
tot_dataset.write(tot.data(), float_type);
// -- close file
Expand Down Expand Up @@ -260,7 +260,7 @@ int main(int argc, char* argv[])

spdlog::debug("@{:p}, {}", raw_data.map, raw_data.max);

if ( raw_data.map == NULL )
if ( raw_data.map == nullptr )
{
spdlog::error("Insufficient memory: {}", in_tpx3);
exit(EXIT_FAILURE);
Expand Down
Loading

0 comments on commit bf23ec4

Please sign in to comment.