File indexing completed on 2026-09-24 08:23:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
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
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
0034 class G4VFastSimSensitiveDetector {
0035 public:
0036 G4VFastSimSensitiveDetector() = default;
0037 virtual ~G4VFastSimSensitiveDetector() = default;
0038
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
0048 namespace dd4hep {
0049
0050
0051 namespace sim {
0052
0053
0054
0055
0056
0057
0058
0059 template <typename T> class RefCountedSequence {
0060 public:
0061 T* m_sequence;
0062
0063 RefCountedSequence() : m_sequence(0) { }
0064
0065 RefCountedSequence(T* seq) { _aquire(seq); }
0066
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
0080
0081
0082
0083
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
0094 SensitiveDetector m_sensitive;
0095 public:
0096
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
0118 virtual ~Geant4SensDet() = default;
0119
0120 inline G4String GetName() const
0121 { return this->G4VSensitiveDetector::SensitiveDetectorName; }
0122
0123 virtual std::string path() const override final
0124 { return this->G4VSensitiveDetector::GetPathName(); }
0125
0126 virtual std::string fullPath() const override final
0127 { return this->G4VSensitiveDetector::GetFullPathName(); }
0128
0129 virtual bool isActive() const override
0130 { return this->G4VSensitiveDetector::isActive(); }
0131
0132 virtual G4int GetCollectionID(G4int i) override final
0133 { return this->G4VSensitiveDetector::GetCollectionID(i); }
0134
0135 virtual G4VReadOutGeometry* readoutGeometry() const override
0136 { return this->G4VSensitiveDetector::GetROgeometry(); }
0137
0138 virtual SensitiveDetector sensitiveDetector() const override final
0139 { return m_sensitive; }
0140
0141 virtual const std::string& sensitiveType() const override final
0142 { return m_sequence->sensitiveType(); }
0143
0144 virtual Geant4SensDetActionSequence* sequence() const override final
0145 { return m_sequence; }
0146
0147 virtual G4bool Accept(const G4Step* step) const override final
0148 { return m_sequence->accept(step); }
0149
0150 virtual void Initialize(G4HCofThisEvent* hce) override final
0151 { m_sequence->begin(hce); }
0152
0153 virtual void EndOfEvent(G4HCofThisEvent* hce) override final
0154 { m_sequence->end(hce); }
0155
0156 virtual G4bool ProcessHits(G4Step* step,
0157 G4TouchableHistory* hist) override final
0158 { return m_sequence->process(step,hist); }
0159
0160 virtual G4bool ProcessHits(G4GFlashSpot* sp,
0161 G4TouchableHistory* hist) override final
0162 {
0163 const GFlashEnergySpot* esp = sp->GetEnergySpot();
0164 G4FastHit hit(esp->GetPosition(), esp->GetEnergy());
0165 Geant4FastSimSpot spot(&hit, sp->GetOriginatorTrack(), (sp->GetTouchableHandle())());
0166 return m_sequence->processFastSim(&spot, hist);
0167 }
0168
0169 virtual G4bool ProcessHits(const G4FastHit* hit,
0170 const G4FastTrack* track,
0171 G4TouchableHistory* hist) override final
0172 {
0173 Geant4FastSimSpot spot(hit, track, hist);
0174 return m_sequence->processFastSim(&spot, hist);
0175 }
0176
0177 virtual void clear() override
0178 { m_sequence->clear(); }
0179
0180 virtual size_t defineCollection(const std::string& coll) override {
0181 if ( coll.empty() ) {
0182 except("Geant4Sensitive: No collection defined for %s [Invalid name]",c_name());
0183 }
0184 collectionName.emplace_back(coll);
0185 return collectionName.size()-1;
0186 }
0187
0188 };
0189 }
0190 }
0191
0192
0193 #include <DDG4/Factories.h>
0194
0195 typedef dd4hep::sim::Geant4SensDet Geant4SensDet;
0196 typedef dd4hep::sim::Geant4SensDet Geant4tracker;
0197 typedef dd4hep::sim::Geant4SensDet Geant4calorimeter;
0198
0199 DECLARE_GEANT4SENSITIVEDETECTOR(Geant4SensDet)
0200 DECLARE_GEANT4SENSITIVEDETECTOR(Geant4tracker)
0201 DECLARE_GEANT4SENSITIVEDETECTOR(Geant4calorimeter)