Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:27

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/Printout.h>
0016 #include <DD4hep/Primitives.h>
0017 #include <DD4hep/InstanceCount.h>
0018 
0019 #include <DDG4/Geant4Kernel.h>
0020 #include <DDG4/Geant4Context.h>
0021 #include <DDG4/Geant4FastSimSpot.h>
0022 #include <DDG4/Geant4HitCollection.h>
0023 #include <DDG4/Geant4SensDetAction.h>
0024 
0025 // Geant4 include files
0026 #include <G4Run.hh>
0027 #include <G4Event.hh>
0028 #include <G4Version.hh>
0029 #include <G4TouchableHistory.hh>
0030 #include <G4VSensitiveDetector.hh>
0031 #include <G4VGFlashSensitiveDetector.hh>
0032 #if G4VERSION_NUMBER < 1070
0033 /// Lower versions of Geant4 do not provide G4VFastSimSensitiveDetector
0034 class G4VFastSimSensitiveDetector  {
0035 public:
0036   G4VFastSimSensitiveDetector() = default;
0037   virtual ~G4VFastSimSensitiveDetector() = default;
0038   /// Geant4 Fast simulation interface
0039   virtual G4bool ProcessHits(const G4FastHit* hit,
0040                  const G4FastTrack* track,
0041                  G4TouchableHistory* hist) = 0;
0042 };
0043 #else
0044 #include <G4VFastSimSensitiveDetector.hh>
0045 #endif
0046 
0047 /// Namespace for the AIDA detector description toolkit
0048 namespace dd4hep {
0049 
0050   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0051   namespace sim {
0052 
0053     /// Private helper to support sequence reference counting
0054     /**
0055      *  \author  M.Frank
0056      *  \version 1.0
0057      *  \ingroup DD4HEP_SIMULATION
0058      */
0059     template <typename T> class RefCountedSequence  {
0060     public:
0061       T* m_sequence;
0062       /// Default constructor
0063       RefCountedSequence() : m_sequence(0) {                 }
0064       /// Initializing constructor
0065       RefCountedSequence(T* seq)           {   _aquire(seq); }
0066       /// Default destructor
0067       virtual ~RefCountedSequence()        { _release();     }
0068       void _aquire(T* s)  {
0069         InstanceCount::increment(this);
0070         m_sequence = s;
0071         m_sequence->addRef();
0072       }
0073       void _release()  {
0074         detail::releasePtr(m_sequence);
0075         InstanceCount::decrement(this);
0076       }
0077     };
0078 
0079     /// Concrete implementation of the G4VSensitiveDetector calling the registered sequence object
0080     /**
0081      *  \author  M.Frank
0082      *  \version 1.0
0083      *  \ingroup DD4HEP_SIMULATION
0084      */
0085     class Geant4SensDet : virtual public G4VSensitiveDetector,
0086               virtual public G4VGFlashSensitiveDetector,
0087               virtual public G4VFastSimSensitiveDetector,
0088                           virtual public G4VSDFilter,
0089                           virtual public Geant4ActionSD,
0090                           virtual public RefCountedSequence<Geant4SensDetActionSequence>
0091     {
0092     protected:
0093       /// Access to the geant4 sensitive detector handle
0094       SensitiveDetector m_sensitive;
0095     public:
0096       /// Constructor. The detector element is identified by the name
0097       Geant4SensDet(const std::string& nam, Detector& description)
0098         : G4VSensitiveDetector(nam),
0099       G4VGFlashSensitiveDetector(),
0100       G4VFastSimSensitiveDetector(),
0101       G4VSDFilter(nam),
0102           Geant4Action(0,nam),
0103       Geant4ActionSD(nam),
0104           RefCountedSequence<Geant4SensDetActionSequence>()
0105       {
0106         Geant4Kernel& master = Geant4Kernel::instance(description);
0107         Geant4Kernel& kernel = master.worker(Geant4Kernel::thread_self());
0108         m_sensitive   = description.sensitiveDetector(nam);
0109         m_context     = kernel.workerContext();
0110         m_outputLevel = kernel.getOutputLevel(nam);
0111         _aquire(kernel.sensitiveAction(nam));
0112         m_sequence->defineCollections(this);
0113         m_sequence->updateContext(m_context);
0114         this->G4VSensitiveDetector::SetFilter(this);
0115       }
0116 
0117       /// Destructor
0118       virtual ~Geant4SensDet() = default;
0119       /// Overload to avoid ambiguity between G4VSensitiveDetector and G4VSDFilter
0120       inline G4String GetName() const
0121       {  return this->G4VSensitiveDetector::SensitiveDetectorName;      }
0122       /// G4VSensitiveDetector internals: Access to the detector path name
0123       virtual std::string path()  const  override  final
0124       {  return this->G4VSensitiveDetector::GetPathName();              }
0125       /// G4VSensitiveDetector internals: Access to the detector path name
0126       virtual std::string fullPath()  const  override  final
0127       {  return this->G4VSensitiveDetector::GetFullPathName();          }
0128       /// Is the detector active?
0129       virtual bool isActive() const  override
0130       {  return this->G4VSensitiveDetector::isActive();                 }
0131       /// This is a utility method which returns the hits collection ID
0132       virtual G4int GetCollectionID(G4int i)  override  final
0133       {  return this->G4VSensitiveDetector::GetCollectionID(i);         }
0134       /// Access to the readout geometry of the sensitive detector
0135       virtual G4VReadOutGeometry* readoutGeometry() const  override
0136       {  return this->G4VSensitiveDetector::GetROgeometry();            }
0137       /// Access to the Detector sensitive detector handle
0138       virtual SensitiveDetector sensitiveDetector() const  override  final
0139       {  return m_sensitive;                                            }
0140       /// Access to the sensitive type of the detector
0141       virtual const std::string& sensitiveType() const  override  final
0142       {  return m_sequence->sensitiveType();                            }
0143       /// Callback if the sequence should be accepted or filtered off
0144       virtual G4bool Accept(const G4Step* step) const  override  final
0145       {  return m_sequence->accept(step);                               }
0146       /// Method invoked at the begining of each event.
0147       virtual void Initialize(G4HCofThisEvent* hce)  override  final
0148       {  m_sequence->begin(hce);                                        }
0149       /// Method invoked at the end of each event.
0150       virtual void EndOfEvent(G4HCofThisEvent* hce)  override  final
0151       {  m_sequence->end(hce);                                          }
0152       /// Method for generating hit(s) using the information of G4Step object.
0153       virtual G4bool ProcessHits(G4Step* step,
0154                  G4TouchableHistory* hist)   override  final
0155       {  return m_sequence->process(step,hist);                         }
0156       /// GFLASH interface
0157       virtual G4bool ProcessHits(G4GFlashSpot* sp,
0158                  G4TouchableHistory* hist)   override final
0159       {
0160     const GFlashEnergySpot* esp = sp->GetEnergySpot();
0161     G4FastHit         hit(esp->GetPosition(), esp->GetEnergy());
0162     Geant4FastSimSpot spot(&hit, sp->GetOriginatorTrack(), (sp->GetTouchableHandle())());
0163     return m_sequence->processFastSim(&spot, hist);
0164       }
0165       /// Geant4 Fast simulation interface
0166       virtual G4bool ProcessHits(const G4FastHit* hit,
0167                  const G4FastTrack* track,
0168                  G4TouchableHistory* hist)   override final
0169       {
0170     Geant4FastSimSpot spot(hit, track, hist);
0171     return m_sequence->processFastSim(&spot, hist);
0172       }
0173       /// G4VSensitiveDetector interface: Method invoked if the event was aborted.
0174       virtual void clear()  override
0175       {  m_sequence->clear();                                           }
0176       /// Initialize the usage of a hit collection. Returns the collection identifier
0177       virtual size_t defineCollection(const std::string& coll)   override  {
0178         if ( coll.empty() ) {
0179           except("Geant4Sensitive: No collection defined for %s [Invalid name]",c_name());
0180         }
0181         collectionName.emplace_back(coll);
0182         return collectionName.size()-1;
0183       }
0184 
0185     };
0186   }    // End namespace sim
0187 }      // End namespace dd4hep
0188 
0189 
0190 #include <DDG4/Factories.h>
0191 
0192 typedef dd4hep::sim::Geant4SensDet Geant4SensDet;
0193 typedef dd4hep::sim::Geant4SensDet Geant4tracker;
0194 typedef dd4hep::sim::Geant4SensDet Geant4calorimeter;
0195 
0196 DECLARE_GEANT4SENSITIVEDETECTOR(Geant4SensDet)
0197 DECLARE_GEANT4SENSITIVEDETECTOR(Geant4tracker)
0198 DECLARE_GEANT4SENSITIVEDETECTOR(Geant4calorimeter)