From d70e358c808a35f315eb5d77cf02f08e98045b60 Mon Sep 17 00:00:00 2001 From: Leonhard Reichenbach Date: Wed, 24 Jul 2024 16:27:30 +0200 Subject: [PATCH] add Geant4TVUserParticleHandler --- DDG4/plugins/Geant4TVUserParticleHandler.cpp | 57 +++++++++----------- DDG4/python/DDSim/Helper/ParticleHandler.py | 10 +++- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/DDG4/plugins/Geant4TVUserParticleHandler.cpp b/DDG4/plugins/Geant4TVUserParticleHandler.cpp index daf8c3402..35b6a2165 100644 --- a/DDG4/plugins/Geant4TVUserParticleHandler.cpp +++ b/DDG4/plugins/Geant4TVUserParticleHandler.cpp @@ -14,18 +14,19 @@ /** \addtogroup Geant4Action * @{ - \package Geant4TCUserParticleHandler + \package Geant4TVUserParticleHandler * \brief Rejects to keep particles, which are created outside a tracking cylinder. * * @} */ -#ifndef DD4HEP_DDG4_GEANT4TCUSERPARTICLEHANDLER_H -#define DD4HEP_DDG4_GEANT4TCUSERPARTICLEHANDLER_H +#ifndef DD4HEP_DDG4_GEANT4TVUSERPARTICLEHANDLER_H +#define DD4HEP_DDG4_GEANT4TVUSERPARTICLEHANDLER_H // Framework include files #include +#include #include /// Namespace for the AIDA detector description toolkit @@ -34,22 +35,23 @@ namespace dd4hep { /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit namespace sim { - /// Rejects to keep particles, which are created outside a tracking cylinder. - /** Geant4TCUserParticleHandler + /// Rejects to keep particles, which are created outside a tracking volume. + /** Geant4TVUserParticleHandler * - * TC stands for TrackingCylinder ;-) + * TV stands for TrackingVolume ;-) * * @author M.Frank * @version 1.0 */ - class Geant4TCUserParticleHandler : public Geant4UserParticleHandler { - double m_zTrackerMin, m_zTrackerMax, m_rTracker; + class Geant4TVUserParticleHandler : public Geant4UserParticleHandler { + Volume m_trackingVolume; + public: /// Standard constructor - Geant4TCUserParticleHandler(Geant4Context* context, const std::string& nam); + Geant4TVUserParticleHandler(Geant4Context* context, const std::string& nam); /// Default destructor - virtual ~Geant4TCUserParticleHandler() {} + virtual ~Geant4TVUserParticleHandler() {} /// Post-track action callback /** Allow the user to force the particle handling in the post track action @@ -69,7 +71,7 @@ namespace dd4hep { } // End namespace sim } // End namespace dd4hep -#endif // DD4HEP_DDG4_GEANT4TCUSERPARTICLEHANDLER_H +#endif // DD4HEP_DDG4_GEANT4TVUSERPARTICLEHANDLER_H //==================================================================== // AIDA Detector description implementation @@ -79,32 +81,27 @@ namespace dd4hep { // //==================================================================== // Framework include files -//#include -#include +//#include #include +#include +#include using namespace dd4hep::sim; -DECLARE_GEANT4ACTION(Geant4TCUserParticleHandler) +DECLARE_GEANT4ACTION(Geant4TVUserParticleHandler) /// Standard constructor -Geant4TCUserParticleHandler::Geant4TCUserParticleHandler(Geant4Context* ctxt, const std::string& nam) +Geant4TVUserParticleHandler::Geant4TVUserParticleHandler(Geant4Context* ctxt, const std::string& nam) : Geant4UserParticleHandler(ctxt,nam) { - declareProperty("TrackingVolume_Zmin",m_zTrackerMin=-1e100); - declareProperty("TrackingVolume_Zmax",m_zTrackerMax=1e100); - declareProperty("TrackingVolume_Rmax",m_rTracker=1e100); + m_trackingVolume = ctxt->kernel().detectorDescription().trackingVolume(); } /// Post-track action callback -void Geant4TCUserParticleHandler::end(const G4Track* /* track */, Particle& p) { +void Geant4TVUserParticleHandler::end(const G4Track* /* track */, Particle& p) { - double r_prod = std::sqrt(p.vsx*p.vsx + p.vsy*p.vsy); - double z_prod = p.vsz; - bool starts_in_trk_vol = ( r_prod <= m_rTracker - && z_prod >= (m_zTrackerMin == -1e100? -m_zTrackerMax : m_zTrackerMin) - && z_prod <= m_zTrackerMax - ) ; + std::array start_point = {p.vsx, p.vsy, p.vsz}; + bool starts_in_trk_vol = m_trackingVolume.ptr()->Contains(start_point.data()); dd4hep::detail::ReferenceBitMask reason(p.reason); @@ -116,12 +113,8 @@ void Geant4TCUserParticleHandler::end(const G4Track* /* track */, Particle& p) return; } - double r_end = std::sqrt(p.vex*p.vex + p.vey*p.vey); - double z_end = p.vez; - bool ends_in_trk_vol = ( r_end <= m_rTracker - && z_end >= (m_zTrackerMin == -1e100? -m_zTrackerMax : m_zTrackerMin) - && z_end <= m_zTrackerMax - ) ; + std::array end_point = {p.vex, p.vey, p.vez}; + bool ends_in_trk_vol = m_trackingVolume.ptr()->Contains(end_point.data()); // created and ended in calo but not primary particle // @@ -161,7 +154,7 @@ void Geant4TCUserParticleHandler::end(const G4Track* /* track */, Particle& p) } /// Post-event action callback -void Geant4TCUserParticleHandler::end(const G4Event* /* event */) { +void Geant4TVUserParticleHandler::end(const G4Event* /* event */) { } diff --git a/DDG4/python/DDSim/Helper/ParticleHandler.py b/DDG4/python/DDSim/Helper/ParticleHandler.py index 147211dab..fdbb474a2 100644 --- a/DDG4/python/DDSim/Helper/ParticleHandler.py +++ b/DDG4/python/DDSim/Helper/ParticleHandler.py @@ -122,7 +122,7 @@ def setupUserParticleHandler(self, part, kernel, DDG4): if not self.userParticleHandler: return - if self.userParticleHandler not in ["Geant4TCUserParticleHandler"]: + if self.userParticleHandler not in ["Geant4TCUserParticleHandler", "Geant4TVUserParticleHandler"]: logger.error("unknown UserParticleHandler: %r" % self.userParticleHandler) exit(1) @@ -153,4 +153,12 @@ def setupUserParticleHandler(self, part, kernel, DDG4): part.adopt(user) + elif self.userParticleHandler == "Geant4TVUserParticleHandler": + if not kernel.detectorDescription().trackingVolume().isValid(): + logger.error("Geant4TVUserParticleHandler requested but no tracking_volume defined in the XML") + exit(1) + + user = DDG4.Action(kernel, "%s/UserParticleHandler" % self.userParticleHandler) + part.adopt(user) + return