File indexing completed on 2026-05-29 07:35:40
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Primitives.h>
0016 #include <DD4hep/InstanceCount.h>
0017 #include <DDG4/Geant4Particle.h>
0018 #include <DDG4/Geant4SensDetAction.h>
0019 #include <DDG4/Geant4UserParticleHandler.h>
0020
0021
0022 #include <G4Track.hh>
0023 #include <G4VSensitiveDetector.hh>
0024
0025
0026 #include <cstdint>
0027
0028
0029 namespace dd4hep {
0030
0031
0032 namespace sim {
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 class Geant4ParticleMaskAction : public Geant4UserParticleHandler {
0051 protected:
0052
0053 std::map<std::string, int32_t> m_nameMasks;
0054
0055 std::map<std::string, int32_t> m_typeMasks;
0056
0057 public:
0058
0059 Geant4ParticleMaskAction(Geant4Context* ctxt, const std::string& nam)
0060 : Geant4UserParticleHandler(ctxt,nam)
0061 {
0062 declareProperty("DetectorNameMasks", m_nameMasks );
0063 declareProperty("DetectorTypeMasks", m_typeMasks );
0064 m_needsControl = true;
0065 }
0066
0067
0068 void mark_track(const G4Track* track, Particle* curr_track ) override final {
0069 G4LogicalVolume* vol = track->GetVolume()->GetLogicalVolume();
0070 G4VSensitiveDetector* g4 = vol->GetSensitiveDetector();
0071 Geant4ActionSD* sd = dynamic_cast<Geant4ActionSD*>(g4);
0072
0073 if( !sd ) {
0074 return;
0075 }
0076 Geant4SensDetActionSequence* sens_det = sd->sequence();
0077 if( sens_det ) {
0078 using PropertyMask = dd4hep::detail::ReferenceBitMask<int>;
0079 PropertyMask mask(curr_track->reason);
0080 if( !m_nameMasks.empty() ) {
0081 auto it = m_nameMasks.begin();
0082 auto nam = sens_det->name();
0083 for( ; it != m_nameMasks.end(); ++it ) {
0084 if( it->first == nam || it->first == "*" ) {
0085 print("+++ Detector: name: %s Masking track %d with mask: %08X",
0086 nam.c_str(), curr_track->id, it->second);
0087 mask.set(it->second);
0088 }
0089 }
0090 }
0091 if( !m_typeMasks.empty() ) {
0092 auto it = m_typeMasks.begin();
0093 auto nam = sens_det->sensitiveType();
0094 for( ; it != m_typeMasks.end(); ++it ) {
0095 if( it->first == nam || it->first == "*" ) {
0096 print("+++ Detector: type: %s Masking track %d with mask: %08X",
0097 nam.c_str(), curr_track->id, it->second);
0098 mask.set(it->second);
0099 }
0100 }
0101 }
0102 }
0103 }
0104 };
0105 }
0106 }
0107
0108 #include <DDG4/Geant4Particle.h>
0109 #include <DDG4/Factories.h>
0110 using namespace dd4hep::sim;
0111 DECLARE_GEANT4ACTION(Geant4ParticleMaskAction)