From 4ef5b479914498f11653fdc37e29c40924cfcab0 Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Tue, 19 Aug 2025 14:35:04 -0400 Subject: [PATCH 1/2] feat: Add DetectorVersionRunAction plugin - Created DetectorVersionRunAction plugin to store epic_version in run parameters - Updated CMakeLists.txt to include the new plugin --- src/plugins/CMakeLists.txt | 23 +++++++++ .../include/npdet/DetectorVersionRunAction.h | 41 +++++++++++++++ src/plugins/src/DetectorVersionRunAction.cxx | 51 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 src/plugins/include/npdet/DetectorVersionRunAction.h create mode 100644 src/plugins/src/DetectorVersionRunAction.cxx diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt index 6216894..8081078 100644 --- a/src/plugins/CMakeLists.txt +++ b/src/plugins/CMakeLists.txt @@ -1,14 +1,37 @@ cmake_minimum_required(VERSION 3.12 FATAL_ERROR) +# Build the plugin library dd4hep_add_plugin(NPDetPlugins SOURCES src/EICInteractionVertexBoost.cxx src/EICInteractionVertexSmear.cxx src/OpticalPhotonEfficiencyStackingAction.cxx + src/DetectorVersionRunAction.cxx INCLUDES $ USES DD4hep::DDCore DD4hep::DDG4 ) +# Create components file template +file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/libNPDetPlugins.components.in +"v2::libNPDetPlugins.so:EICInteractionVertexBoost +v2::libNPDetPlugins.so:EICInteractionVertexSmear +v2::libNPDetPlugins.so:OpticalPhotonEfficiencyStackingAction +v2::libNPDetPlugins.so:DetectorVersionRunAction") + +# Copy components file to lib directory +add_custom_command(TARGET NPDetPlugins POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/lib + COMMAND ${CMAKE_COMMAND} -E copy + ${CMAKE_CURRENT_BINARY_DIR}/libNPDetPlugins.components.in + ${CMAKE_BINARY_DIR}/lib/libNPDetPlugins.components + COMMENT "Installing components file" +) + +# Install the components file +install(FILES ${CMAKE_BINARY_DIR}/lib/libNPDetPlugins.components + DESTINATION lib +) + install(TARGETS NPDetPlugins EXPORT NPDetTargets LIBRARY DESTINATION lib diff --git a/src/plugins/include/npdet/DetectorVersionRunAction.h b/src/plugins/include/npdet/DetectorVersionRunAction.h new file mode 100644 index 0000000..b6b250d --- /dev/null +++ b/src/plugins/include/npdet/DetectorVersionRunAction.h @@ -0,0 +1,41 @@ +#ifndef NPDET_DETECTORVERSIONRUNACTION_H +#define NPDET_DETECTORVERSIONRUNACTION_H + +#include "DDG4/Geant4Action.h" +#include "DDG4/Geant4RunAction.h" +#include "DD4hep/Detector.h" +#include "DDG4/RunParameters.h" + +// Geant4 includes +#include "G4Run.hh" + +/// Namespace for the npsim toolkit +namespace npdet { + +/// DD4hep RunAction plugin for storing detector version information +/** + * This class implements a Geant4 RunAction plugin that stores the detector + * version information from DD4hep constants into run parameters. + */ +class DetectorVersionRunAction : public dd4hep::sim::Geant4RunAction { +public: + /// Constructor + /** + * @param context The context object for the action + * @param name The name of the action + */ + DetectorVersionRunAction(dd4hep::sim::Geant4Context* context, const std::string& name); + + /// Destructor + virtual ~DetectorVersionRunAction() = default; + + /// Begin-of-run callback + virtual void begin(const G4Run* run) override; + + /// End-of-run callback + virtual void end(const G4Run* run) override; +}; + +} // namespace npdet + +#endif // NPDET_DETECTORVERSIONRUNACTION_H \ No newline at end of file diff --git a/src/plugins/src/DetectorVersionRunAction.cxx b/src/plugins/src/DetectorVersionRunAction.cxx new file mode 100644 index 0000000..8dc611f --- /dev/null +++ b/src/plugins/src/DetectorVersionRunAction.cxx @@ -0,0 +1,51 @@ +#include "npdet/DetectorVersionRunAction.h" + +// DD4hep includes +#include "DD4hep/Printout.h" +#include "DDG4/Geant4RunAction.h" +#include "DDG4/Factories.h" +#include "DDG4/Geant4Kernel.h" +#include "DD4hep/Detector.h" + +// Geant4 includes +#include "G4Run.hh" + +using namespace dd4hep::sim; + +// Macro to suppress unused parameter warnings +#define UNUSED(x) (void)(x) + +namespace npdet { + +DetectorVersionRunAction::DetectorVersionRunAction(Geant4Context* context, const std::string& name) + : Geant4RunAction(context, name) { +} + +void DetectorVersionRunAction::begin(const G4Run* run) { + UNUSED(run); + // Get the DD4hep detector description + auto& detector = context()->kernel().detectorDescription(); + + try { + // Try to get the epic_version constant + double epic_version = detector.constantAsDouble("epic_version"); + + // Store the epic_version in the action's properties + this->property("epic_version").set(epic_version); + dd4hep::printout(dd4hep::INFO, "DetectorVersionRunAction", + "Stored epic_version = %f in action properties", epic_version); + + } catch (const std::exception& e) { + dd4hep::printout(dd4hep::WARNING, "DetectorVersionRunAction", + "Failed to get epic_version constant: %s", e.what()); + } +} + +void DetectorVersionRunAction::end(const G4Run* run) { + UNUSED(run); +} + +} // namespace npdet + +// Factory declaration for the RunAction +DECLARE_GEANT4ACTION_NS(npdet, DetectorVersionRunAction) From d37add9b9235e4aab39c61fc70df984d53e5ca5f Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Tue, 19 Aug 2025 15:41:56 -0400 Subject: [PATCH 2/2] refactor: Use [[maybe_unused]] instead of UNUSED macro - Replace custom UNUSED macro with standard C++ attribute - Update both header and implementation files - Remove macro definition --- src/plugins/include/npdet/DetectorVersionRunAction.h | 4 ++-- src/plugins/src/DetectorVersionRunAction.cxx | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/plugins/include/npdet/DetectorVersionRunAction.h b/src/plugins/include/npdet/DetectorVersionRunAction.h index b6b250d..3d157fd 100644 --- a/src/plugins/include/npdet/DetectorVersionRunAction.h +++ b/src/plugins/include/npdet/DetectorVersionRunAction.h @@ -30,10 +30,10 @@ class DetectorVersionRunAction : public dd4hep::sim::Geant4RunAction { virtual ~DetectorVersionRunAction() = default; /// Begin-of-run callback - virtual void begin(const G4Run* run) override; + virtual void begin([[maybe_unused]] const G4Run* run) override; /// End-of-run callback - virtual void end(const G4Run* run) override; + virtual void end([[maybe_unused]] const G4Run* run) override; }; } // namespace npdet diff --git a/src/plugins/src/DetectorVersionRunAction.cxx b/src/plugins/src/DetectorVersionRunAction.cxx index 8dc611f..70f18f8 100644 --- a/src/plugins/src/DetectorVersionRunAction.cxx +++ b/src/plugins/src/DetectorVersionRunAction.cxx @@ -12,17 +12,13 @@ using namespace dd4hep::sim; -// Macro to suppress unused parameter warnings -#define UNUSED(x) (void)(x) - namespace npdet { DetectorVersionRunAction::DetectorVersionRunAction(Geant4Context* context, const std::string& name) : Geant4RunAction(context, name) { } -void DetectorVersionRunAction::begin(const G4Run* run) { - UNUSED(run); +void DetectorVersionRunAction::begin([[maybe_unused]] const G4Run* run) { // Get the DD4hep detector description auto& detector = context()->kernel().detectorDescription(); @@ -41,8 +37,7 @@ void DetectorVersionRunAction::begin(const G4Run* run) { } } -void DetectorVersionRunAction::end(const G4Run* run) { - UNUSED(run); +void DetectorVersionRunAction::end([[maybe_unused]] const G4Run* run) { } } // namespace npdet