diff --git a/DDG4/plugins/Geant4TVUserParticleHandler.cpp b/DDG4/plugins/Geant4TVUserParticleHandler.cpp index daf8c3402..f7a9ac099 100644 --- a/DDG4/plugins/Geant4TVUserParticleHandler.cpp +++ b/DDG4/plugins/Geant4TVUserParticleHandler.cpp @@ -14,19 +14,20 @@ /** \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 namespace dd4hep { @@ -35,21 +36,22 @@ namespace dd4hep { namespace sim { /// Rejects to keep particles, which are created outside a tracking cylinder. - /** Geant4TCUserParticleHandler + /** Geant4TVUserParticleHandler * * TC stands for TrackingCylinder ;-) * * @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,26 @@ namespace dd4hep { // //==================================================================== // Framework include files -//#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); + declareProperty("TrackingVolume",m_trackingVolume=Volume()); } /// 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 +112,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 +153,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..a2c901aa2 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,15 @@ def setupUserParticleHandler(self, part, kernel, DDG4): part.adopt(user) + elif self.userParticleHandler == "Geant4TVUserParticleHandler": + user = DDG4.Action(kernel, "%s/UserParticleHandler" % self.userParticleHandler) + try: + user.TrackingVolume = DDG4.Kernel().detectorDescription().trackingVolume() + # TODO: better error message, suggestions welcome ;) + except: + logger.error("tracking_volume missing in detector model.") + exit(1) + + part.adopt(user) + return