Skip to content

Commit

Permalink
add Geant4TVUserParticleHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Zehvogel committed Jul 29, 2024
1 parent 4640e38 commit d70e358
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
57 changes: 25 additions & 32 deletions DDG4/plugins/Geant4TVUserParticleHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <DD4hep/Primitives.h>
#include <DD4hep/Volumes.h>
#include <DDG4/Geant4UserParticleHandler.h>

/// Namespace for the AIDA detector description toolkit
Expand All @@ -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
Expand All @@ -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
Expand All @@ -79,32 +81,27 @@ namespace dd4hep {
//
//====================================================================
// Framework include files
//#include <DDG4/Geant4TCUserParticleHandler.h>
#include <DDG4/Geant4Particle.h>
//#include <DDG4/Geant4TVUserParticleHandler.h>
#include <DDG4/Factories.h>
#include <DDG4/Geant4Particle.h>
#include <DDG4/Geant4Kernel.h>


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<double, 3> start_point = {p.vsx, p.vsy, p.vsz};
bool starts_in_trk_vol = m_trackingVolume.ptr()->Contains(start_point.data());

dd4hep::detail::ReferenceBitMask<int> reason(p.reason);

Expand All @@ -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<double, 3> 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
//
Expand Down Expand Up @@ -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 */) {

}

10 changes: 9 additions & 1 deletion DDG4/python/DDSim/Helper/ParticleHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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

0 comments on commit d70e358

Please sign in to comment.