Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-29 07:35:40

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
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 // Geant4 header files
0022 #include <G4Track.hh>
0023 #include <G4VSensitiveDetector.hh>
0024 
0025 // C/C++ include files
0026 #include <cstdint>
0027 
0028 /// Namespace for the AIDA detector description toolkit
0029 namespace dd4hep {
0030 
0031   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0032   namespace sim {
0033 
0034     /// Geant4ParticleHandler user extension action to modify the particle reason mask
0035     /**
0036      * This action marks the 'reason' bitmask of a Geant4Particle with a given bitmask
0037      * if a sub-detector created a hit. Multiple subdetectors can be set to mask
0038      * different values by the options <action>.Masks = {'det1': 4, 'det2': 1024, ... }
0039      *
0040      * Please note:
0041      *
0042      * This is a simple example of a user supplied Geant4UserParticleHandler
0043      * Do not artificially complicate it. Enhanced functionality can also be achieved
0044      * by chaining other sub-classes of Geant4UserParticleHandler instances.
0045      *
0046      *
0047      * @author  M.Frank
0048      * @version 1.0
0049      */       
0050     class Geant4ParticleMaskAction : public Geant4UserParticleHandler  {
0051     protected:
0052       /// Map of Subdetector names to trigger setting of the mask : Bit mask to be set to particle 
0053       std::map<std::string, int32_t>  m_nameMasks;
0054       /// Map of Subdetector types to trigger setting of the mask : Bit mask to be set to particle 
0055       std::map<std::string, int32_t>  m_typeMasks;
0056       
0057     public:
0058       /// Standard constructor
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       /// User overload to handle particle settings when processing the track in the Geant4ParticleHandler.
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     }; // End class Geant4ParticleMaskAction
0105   }    // End namespace sim
0106 }      // End namespace dd4hep
0107 
0108 #include <DDG4/Geant4Particle.h>
0109 #include <DDG4/Factories.h>
0110 using namespace dd4hep::sim;
0111 DECLARE_GEANT4ACTION(Geant4ParticleMaskAction)