Skip to content

Commit 9a6ccfd

Browse files
benedikt-voelkelBenedikt Volkel
andauthored
Introduce TMCReplay and restructure repo (#22)
* Introduce TMCReplay and restructure repo [TMCReplay] Simulation engine based on the Virtual Monte Carlo (VMC) framework to exactly replay the steps as previously recorded from another run using the MCStepLogger. Cut and process settings can be varied to study the impact on the steps. That funcionality is currently not fully implemented but will be as the next step. The replay engine is built by default but the build can be switched off setting `-MCStepLogger_BUILD_TMCReplay=OFF" at configure time when `CMake` is invoked. In the easiest case it can be run using the installed executable `tmcreplay`. Use `tmcreplay --help` to see all options. Generally, the engine can be used as usual within any other simulation framework. In that case it might be used to evaluate the impact of different cut and process settings on hist or digits. [MCStepLogger] 3-momenta (px, py, pz) added to logging as that is necessary to replay simulation correctly [Repository restructuring] The original MCStepLogger (and the TMCReplay engine) were moved to corresponding sub-directories. Installation of libraries was unified through common CMake function. [Apply clang-format to cxx (previously not done)] * switch off test builds by default * Complete TVirtualMC interface implementations * implement * TVirtualMC::Gs* methods * TVirtualMC::{Material,Mixture,Medium} -> overall implementation follows https://github.com/vmc-project/vmc/blob/master/source/include/TGeoMCGeometry.h however, in this case with a cached pointer to TGeoManager instead of everytime obtaining it from gGeoManager * small updates of READMEs and executable source replay.cxx * update tracking and stepping logic * ignore additional secondaries secondaries pushed during hit creation (or anywhere during TVirtualMCApplication::Stepping) * take copy number into account when updating the TGeoManager/TNavigator state * Fix issues with running Replay stand-alone * add MCReplayGenericApplication to be interceptable * update replay test * Add energy cut option to replay executable Co-authored-by: Benedikt Volkel <[email protected]>
1 parent 7edc764 commit 9a6ccfd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3792
-510
lines changed

CMakeLists.txt

Lines changed: 43 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -12,86 +12,49 @@
1212
# @author Sandro Wenzel
1313
# @brief cmake setup for module Utilities/MCStepLogger
1414

15-
set(MODULE_NAME "MCStepLogger")
16-
1715
# Minimum version of CMake
18-
CMAKE_MINIMUM_REQUIRED(VERSION 3.11.0 FATAL_ERROR)
16+
CMAKE_MINIMUM_REQUIRED(VERSION 3.15.0 FATAL_ERROR)
1917

20-
project(${MODULE_NAME})
18+
project(MCStepLogger)
2119
set(CMAKE_MODULE_PATH
2220
${CMAKE_MODULE_PATH}
2321
${CMAKE_SOURCE_DIR}/cmake)
2422

23+
option(MCStepLogger_BUILD_MCReplay "Build MCReplay engine" ON)
2524
option(MCStepLogger_BUILD_TESTS "Control whether tests are built" OFF)
2625

2726
include(CTest)
2827

2928
# Install directories
3029
SET(INSTALL_BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin)
3130
SET(INSTALL_MACRO_DIR ${CMAKE_INSTALL_PREFIX}/macro)
32-
SET(INSTALL_INC_DIR ${CMAKE_INSTALL_PREFIX}/include/${MODULE_NAME})
31+
SET(INSTALL_INC_DIR ${CMAKE_INSTALL_PREFIX}/include)
3332
SET(INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib)
3433
SET(INSTALL_CMAKE_DIR ${CMAKE_INSTALL_PREFIX}/cmake)
3534

36-
# Source directories
37-
SET(IMP_SRC_DIR ${CMAKE_SOURCE_DIR}/src)
38-
SET(INC_SRC_DIR ${CMAKE_SOURCE_DIR}/include/${MODULE_NAME})
39-
SET(MACRO_SRC_DIR ${CMAKE_SOURCE_DIR}/macro)
40-
41-
# Library name
42-
SET(LIBRARY_NAME ${MODULE_NAME})
43-
# Executable name
44-
SET(EXECUTABLE_NAME mcStepAnalysis)
45-
46-
# Required source files to build the library.
47-
set(SRCS_CORE
48-
${IMP_SRC_DIR}/StepInfo.cxx
49-
)
50-
51-
set(SRCS_INTERCEPT
52-
${IMP_SRC_DIR}/MCStepInterceptor.cxx
53-
${IMP_SRC_DIR}/MCStepLoggerImpl.cxx
54-
)
55-
56-
set(SRCS_ANALYSIS
57-
${IMP_SRC_DIR}/MCAnalysis.cxx
58-
${IMP_SRC_DIR}/BasicMCAnalysis.cxx
59-
${IMP_SRC_DIR}/SimpleStepAnalysis.cxx
60-
${IMP_SRC_DIR}/MCAnalysisManager.cxx
61-
${IMP_SRC_DIR}/MCAnalysisFileWrapper.cxx
62-
${IMP_SRC_DIR}/MCAnalysisUtilities.cxx
63-
${IMP_SRC_DIR}/ROOTIOUtilities.cxx
64-
)
65-
66-
# Requried headers to build the library.
67-
set(HEADERS_CORE
68-
${INC_SRC_DIR}/StepInfo.h
69-
${INC_SRC_DIR}/MetaInfo.h
70-
)
71-
72-
set(HEADERS_INTERCEPT
73-
)
74-
75-
set(HEADERS_ANALYSIS
76-
${INC_SRC_DIR}/MCAnalysis.h
77-
${INC_SRC_DIR}/BasicMCAnalysis.h
78-
${INC_SRC_DIR}/SimpleStepAnalysis.h
79-
${INC_SRC_DIR}/MCAnalysisManager.h
80-
${INC_SRC_DIR}/MCAnalysisFileWrapper.h
81-
${INC_SRC_DIR}/MCAnalysisUtilities.h
82-
${INC_SRC_DIR}/ROOTIOUtilities.h
83-
)
84-
include_directories(include/)
85-
86-
# Required source for the executable
87-
set(EXE_SRCS
88-
${IMP_SRC_DIR}/analyseMCSteps.cxx
89-
)
90-
91-
# Macros to be copied
92-
set(MACROS
93-
${MACRO_SRC_DIR}/plotAnalysisHistograms.C
94-
)
35+
#################################
36+
# Whether or not to build tests #
37+
#################################
38+
set(HAS_TEST_DATA FALSE)
39+
set(REPO_NAME_TEST MCStepLoggerTestData)
40+
set(TEST_DATA_DIR)
41+
if(NOT MCStepLogger_BUILD_TESTS)
42+
message(WARNING "No tests are built.")
43+
else()
44+
# Could potentially use CMake's FetchContent module but seems to be overhead since we only need this single line
45+
execute_process(COMMAND git clone https://gitlab.cern.ch/bvolkel/${REPO_NAME_TEST}.git WORKING_DIRECTORY ${CMAKE_BINARY_DIR} RESULT_VARIABLE res)
46+
if(NOT res EQUAL "0")
47+
message(WARNING "Test data could not be fetched")
48+
else()
49+
set(HAS_TEST_DATA TRUE)
50+
set(TEST_DATA_DIR ${CMAKE_BINARY_DIR}/${REPO_NAME_TEST})
51+
endif(NOT res EQUAL "0")
52+
endif(NOT MCStepLogger_BUILD_TESTS)
53+
54+
55+
#################
56+
# Find packages #
57+
#################
9558

9659
########
9760
# ROOT #
@@ -107,17 +70,6 @@ include(${ROOT_USE_FILE})
10770
#########
10871
find_package(Boost COMPONENTS program_options chrono unit_test_framework REQUIRED)
10972

110-
# Generate ROOT dictionary
111-
SET(ROOT_DICT_LINKDEF_FILE ${IMP_SRC_DIR}/MCStepLoggerLinkDef.h)
112-
SET(ROOT_DICT_NAME "G__${MODULE_NAME}")
113-
114-
ROOT_GENERATE_DICTIONARY(${ROOT_DICT_NAME} ${HEADERS} LINKDEF ${ROOT_DICT_LINKDEF_FILE})
115-
# Files produced by the dictionary generation
116-
SET(ROOT_DICT_LIB_FILES
117-
"${PROJECT_BINARY_DIR}/lib${MODULE_NAME}_rdict.pcm"
118-
"${PROJECT_BINARY_DIR}/lib${MODULE_NAME}.rootmap"
119-
)
120-
12173
###############################
12274
# Determine CXX STD from ROOT #
12375
###############################
@@ -134,91 +86,21 @@ if (${POSITION} GREATER -1)
13486
endif()
13587
message(STATUS "Build with CXX STD ${CMAKE_CXX_STANDARD}")
13688

137-
# Build a library from the sources specified above together with generated ROOT
138-
# dictionary
139-
140-
####################
141-
# Add main library #
142-
####################
143-
set(lib_core "${MODULE_NAME}Core")
144-
145-
# Generate ROOT dictionary
146-
SET(ROOT_DICT_LINKDEF_FILE_CORE "${IMP_SRC_DIR}/${lib_core}LinkDef.h")
147-
SET(ROOT_DICT_NAME_CORE "G__${lib_core}")
148-
ROOT_GENERATE_DICTIONARY(${ROOT_DICT_NAME_CORE} ${HEADERS_CORE} LINKDEF ${ROOT_DICT_LINKDEF_FILE_CORE})
149-
# Files produced by the dictionary generation
150-
SET(ROOT_DICT_LIB_FILES_CORE
151-
"${PROJECT_BINARY_DIR}/lib${lib_core}_rdict.pcm"
152-
"${PROJECT_BINARY_DIR}/lib${lib_core}.rootmap")
153-
154-
add_library(${lib_core} SHARED ${SRCS_CORE} "${ROOT_DICT_NAME_CORE}.cxx")
155-
set(LIB_DEPS ROOT::Core ROOT::Hist ROOT::Graf ROOT::Gpad ROOT::Tree ROOT::VMC ROOT::RIO ROOT::Geom ROOT::EG)
156-
target_link_libraries(${lib_core} ${LIB_DEPS})
157-
set_target_properties(${lib_core} PROPERTIES INTERFACE_LINK_LIBRARIES "${LIB_DEPS}")
158-
target_include_directories(${lib_core} INTERFACE $<INSTALL_INTERFACE:include>)
159-
160-
161-
#########################
162-
# Add intercept library #
163-
#########################
164-
set(lib_intercept "${MODULE_NAME}Intercept")
165-
166-
# Generate ROOT dictionary
167-
SET(ROOT_DICT_LINKDEF_FILE_INTERCEPT "${IMP_SRC_DIR}/${lib_intercept}LinkDef.h")
168-
SET(ROOT_DICT_NAME_INTERCEPT "G__${lib_intercept}")
169-
ROOT_GENERATE_DICTIONARY(${ROOT_DICT_NAME_INTERCEPT} ${HEADERS_INTERCEPT} LINKDEF ${ROOT_DICT_LINKDEF_FILE_INTERCEPT})
170-
# Files produced by the dictionary generation
171-
SET(ROOT_DICT_LIB_FILES_INTERCEPT
172-
"${PROJECT_BINARY_DIR}/lib${lib_intercept}_rdict.pcm"
173-
"${PROJECT_BINARY_DIR}/lib${lib_intercept}.rootmap")
174-
175-
add_library(${lib_intercept} SHARED ${SRCS_INTERCEPT} "${ROOT_DICT_NAME_INTERCEPT}.cxx")
176-
target_link_libraries(${lib_intercept} ${lib_core})
177-
set_target_properties(${lib_intercept} PROPERTIES INTERFACE_LINK_LIBRARIES "${lib_core}")
178-
target_include_directories(${lib_intercept} INTERFACE $<INSTALL_INTERFACE:include>)
179-
180-
########################
181-
# Add analysis library #
182-
########################
183-
set(lib_analysis "${MODULE_NAME}Analysis")
184-
185-
# Generate ROOT dictionary
186-
SET(ROOT_DICT_LINKDEF_FILE_ANALYSIS "${IMP_SRC_DIR}/${lib_analysis}LinkDef.h")
187-
SET(ROOT_DICT_NAME_ANALYSIS "G__${lib_analysis}")
188-
ROOT_GENERATE_DICTIONARY(${ROOT_DICT_NAME_ANALYSIS} ${HEADERS_ANALYSIS} LINKDEF ${ROOT_DICT_LINKDEF_FILE_ANALYSIS})
189-
# Files produced by the dictionary generation
190-
SET(ROOT_DICT_LIB_FILES_ANALYSIS
191-
"${PROJECT_BINARY_DIR}/lib${lib_analysis}_rdict.pcm"
192-
"${PROJECT_BINARY_DIR}/lib${lib_analysis}.rootmap")
193-
194-
add_library(${lib_analysis} SHARED ${SRCS_ANALYSIS} "${ROOT_DICT_NAME_ANALYSIS}.cxx")
195-
target_link_libraries(${lib_analysis} ${lib_core})
196-
set_target_properties(${lib_analysis} PROPERTIES INTERFACE_LINK_LIBRARIES "${lib_core}")
197-
target_include_directories(${lib_analysis} INTERFACE $<INSTALL_INTERFACE:include>)
198-
199-
200-
# Install headers
201-
install(FILES ${HEADERS_CORE} ${HEADERS_INTERCEPT} ${HEADERS_ANALYSIS} DESTINATION ${INSTALL_INC_DIR})
202-
203-
# Install libraries (and add to export such that we can easily find its dependencies later)
204-
install(TARGETS ${lib_core} DESTINATION ${INSTALL_LIB_DIR} EXPORT ${CMAKE_PROJECT_NAME}Exports)
205-
install(TARGETS ${lib_intercept} DESTINATION ${INSTALL_LIB_DIR} EXPORT ${CMAKE_PROJECT_NAME}Exports)
206-
install(TARGETS ${lib_analysis} DESTINATION ${INSTALL_LIB_DIR} EXPORT ${CMAKE_PROJECT_NAME}Exports)
207-
208-
# Install the ROOT dictionary files
209-
install(FILES ${ROOT_DICT_LIB_FILES_CORE} DESTINATION ${INSTALL_LIB_DIR})
210-
install(FILES ${ROOT_DICT_LIB_FILES_INTERCEPT} DESTINATION ${INSTALL_LIB_DIR})
211-
install(FILES ${ROOT_DICT_LIB_FILES_ANALYSIS} DESTINATION ${INSTALL_LIB_DIR})
212-
213-
# Install executables (and add to export such that we can easily find its dependencies later)
214-
add_executable(${EXECUTABLE_NAME} ${EXE_SRCS})
215-
target_link_libraries(${EXECUTABLE_NAME} ${lib_core} ${lib_analysis} Boost::program_options Boost::chrono Boost::unit_test_framework)
216-
install(TARGETS ${EXECUTABLE_NAME} DESTINATION ${INSTALL_BIN_DIR} EXPORT ${CMAKE_PROJECT_NAME}Exports)
217-
218-
# Install macros
219-
install(FILES ${MACROS} DESTINATION ${INSTALL_MACRO_DIR})
220-
221-
# Install exports to find targets and dependencies later
89+
#######################
90+
# Do the installation #
91+
#######################
92+
include(MCStepLoggerUtils)
93+
set(CMAKE_INSTALL_LIBDIR ${INSTALL_LIB_DIR})
94+
set(CMAKE_INSTALL_INCLUDEDIR ${INSTALL_INC_DIR})
95+
96+
add_subdirectory(MCStepLogger)
97+
if(MCStepLogger_BUILD_MCReplay)
98+
add_subdirectory(MCReplay)
99+
endif()
100+
101+
##################################################
102+
# Do final global installation and configuration #
103+
##################################################
222104
install(EXPORT ${CMAKE_PROJECT_NAME}Exports FILE MCStepLoggerTargets.cmake DESTINATION ${INSTALL_CMAKE_DIR})
223105

224106
# Configure and install the config to be used by CMake of depending package
@@ -227,13 +109,4 @@ configure_file(
227109
"${PROJECT_BINARY_DIR}/MCStepLoggerConfig.cmake" @ONLY)
228110
install(FILES
229111
"${PROJECT_BINARY_DIR}/MCStepLoggerConfig.cmake"
230-
DESTINATION ${INSTALL_CMAKE_DIR})
231-
232-
###############
233-
# Build tests #
234-
###############
235-
if(NOT MCStepLogger_BUILD_TESTS)
236-
message(WARNING "No tests are built.")
237-
else()
238-
add_subdirectory(test)
239-
endif(NOT MCStepLogger_BUILD_TESTS)
112+
DESTINATION ${INSTALL_CMAKE_DIR})

MCReplay/CMakeLists.txt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
# All rights not expressly granted are reserved.
4+
#
5+
# This software is distributed under the terms of the GNU General Public
6+
# License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
#
8+
# In applying this license CERN does not waive the privileges and immunities
9+
# granted to it by virtue of its status as an Intergovernmental Organization
10+
# or submit itself to any jurisdiction.
11+
12+
# @author Benedikt Volkel
13+
# @brief cmake setup for TMCReplay based on VMC
14+
15+
set(MODULE_NAME "MCReplay")
16+
17+
################################################################################
18+
# Basic configurations
19+
################################################################################
20+
# Sources for built are here.
21+
set(CXX_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
22+
set(CXX_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
23+
24+
# Executables
25+
set(EXECUTABLE_NAME mcreplay)
26+
27+
################################################################################
28+
# Sources and headers
29+
################################################################################
30+
31+
set(SRCS
32+
${CXX_SOURCE_DIR}/MCReplayEngine.cxx
33+
${CXX_SOURCE_DIR}/MCReplayGenericApplication.cxx
34+
${CXX_SOURCE_DIR}/MCReplayGenericStack.cxx
35+
${CXX_SOURCE_DIR}/MCReplayEvGen.cxx
36+
)
37+
set(HEADERS
38+
${CXX_INCLUDE_DIR}/${MODULE_NAME}/MCReplayEngine.h
39+
${CXX_INCLUDE_DIR}/${MODULE_NAME}/MCReplayGenericApplication.h
40+
${CXX_INCLUDE_DIR}/${MODULE_NAME}/MCReplayGenericStack.h
41+
${CXX_INCLUDE_DIR}/${MODULE_NAME}/MCReplayEvGen.h
42+
${CXX_INCLUDE_DIR}/${MODULE_NAME}/MCReplayPhysics.h
43+
)
44+
45+
set(EXECUTABLE_SRCS
46+
${CXX_SOURCE_DIR}/replay.cxx
47+
)
48+
49+
mcsl_add_library(
50+
TARGETNAME Core
51+
BASENAME ${MODULE_NAME}
52+
DEPENDENCIES ROOT::Core ROOT::Hist ROOT::Graf ROOT::Gpad ROOT::Tree ROOT::VMC ROOT::RIO ROOT::Geom ROOT::EG MCStepLoggerCore MCStepLoggerAnalysis
53+
SOURCES ${SRCS}
54+
INCLUDE_DIRECTORIES ${CMAKE_CURRENT_LIST_DIR}/include
55+
LINKDEFDIR ${CMAKE_CURRENT_LIST_DIR}/src
56+
ROOT_DICTIONARY_HEADERS ${HEADERS})
57+
58+
59+
add_executable(${EXECUTABLE_NAME} ${EXECUTABLE_SRCS})
60+
target_link_libraries(${EXECUTABLE_NAME} MCReplayCore Boost::program_options)
61+
install(TARGETS ${EXECUTABLE_NAME} DESTINATION ${INSTALL_BIN_DIR} EXPORT ${CMAKE_PROJECT_NAME}Exports)
62+
63+
if(MCStepLogger_BUILD_TESTS AND HAS_TEST_DATA)
64+
add_subdirectory(test)
65+
endif()

MCReplay/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# TMCReplay
2+
3+
This is a pseudo-detector-simulation engine based on the [Virtual Monte Carlo (VMC)](https://vmc-project.github.io/) package. It takes logged steps recorded by the `MCStepLogger` to **replay** the entire simulation.
4+
5+
The main objective is to be able to provide an engine to study the impact of parameter variations with. Eventually, this can be used to optimise (full-)simulation parameters in view of enhancing their speed and efficiency.
6+
7+
The functionality is compiled into a separate library `libTMCReplay` which also allows to use the `libMCStepLoggerIntercept` which also allows to run the step logging against the `TMCReplay` engine.
8+
9+
## Setting parameters
10+
11+
The list of tunable parameters is foreseen to correspond to the possible settings implemented for [GEANT3_VMC]() and [GEANT4_VMC](https://github.com/vmc-project/geant4_vmc) and can be found [here](include/TMCReplay/Physics.h). The parameters can be set via the usual interfaces
12+
* `TVirtualMC::SetCut` and `TVirtualMC::SetProcess` to set global cut and process values
13+
* `TVirtualMC::Gstpar` to set parameters depending on a certain medium
14+
15+
**Please note** that the parameters can be set but at the moment nothing is actually applied. Therefore, the exact same steps would be reproduced regardless of the parameters. Of course, this is the next major development as it is one of the core objectives of this engine.
16+
17+
## Testing different parameter settings
18+
19+
**Note that this section becomes actually meaningful when the aforementioned functionality will have been implemented.** However, in the current state it can already be verified that the `TMCReplay` engine does what it is supposed to do.
20+
21+
### Running/testing on step level only (`TMCReplayDummyApplication` and `TMCReplayDummyStack`)
22+
23+
If only the impact on the steps is desired, all required functionality is already included here. Simply have a look at [this executable](src/replay.cxx). It makes use of the `TMCReplayDummyApplication` and `TMCReplayDummyStack` just to replay the steps. Once implemented, the impact of the parameters on the stepping can be evaluated with this tool alone. To do so, just run the `MCStepLogger` together with the replay, for instance as follows
24+
25+
```bash
26+
MCSTEPLOG_TTREE=1 LD_PRELOAD=$MCSTEPLOGGER_ROOT/lib/libMCStepLoggerIntercept.so tmcreplay
27+
```
28+
29+
That will pick up the step file `MCStepLoggerOutput.root`. If you gave that a different name (or in case also your contained tree has another name), you will find the following help message useful:
30+
31+
```bash
32+
> tmcreplay --help
33+
Replaying a previously recorded particle transport step-by-step. Mainly meant for checking and performance measurements/optimisation of the transport:
34+
--help show this help message and exit
35+
--stepfilename arg (=MCStepLoggerOutput.root)
36+
MCStepLogger filename
37+
--steptreename arg (=StepLoggerTree) treename inside file where to find step
38+
tree
39+
--geofilename arg (=o2sim_geometry.root)
40+
ROOT geometry filename
41+
--geokeyname arg (=FAIRGeom) key name inside geo file where to find
42+
geometry tree
43+
44+
```
45+
46+
As you can see the geometry is required by this executable as well since it has otherwise no idea what geometry was used for the original simulation.
47+
48+
### Running in another environment
49+
50+
In that case the geometry construction is assumed to implemented by that environment's implementation of `TVirtualMCApplication`. Also, no stack is constructed automatically and it has to be constructed and passed by the user.
51+
52+
Such a scenario is useful to evaluate the impact of parameter variation on hits or even digits.

0 commit comments

Comments
 (0)