DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4SensDet.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Printout.h>
16 #include <DD4hep/Primitives.h>
17 #include <DD4hep/InstanceCount.h>
18 
19 #include <DDG4/Geant4Kernel.h>
20 #include <DDG4/Geant4Context.h>
21 #include <DDG4/Geant4FastSimSpot.h>
24 
25 // Geant4 include files
26 #include <G4Run.hh>
27 #include <G4Event.hh>
28 #include <G4Version.hh>
29 #include <G4TouchableHistory.hh>
30 #include <G4VSensitiveDetector.hh>
31 #include <G4VGFlashSensitiveDetector.hh>
32 #if G4VERSION_NUMBER < 1070
35 public:
37  virtual ~G4VFastSimSensitiveDetector() = default;
39  virtual G4bool ProcessHits(const G4FastHit* hit,
40  const G4FastTrack* track,
41  G4TouchableHistory* hist) = 0;
42 };
43 #else
44 #include <G4VFastSimSensitiveDetector.hh>
45 #endif
46 
48 namespace dd4hep {
49 
51  namespace sim {
52 
54 
59  template <typename T> class RefCountedSequence {
60  public:
65  RefCountedSequence(T* seq) { _aquire(seq); }
67  virtual ~RefCountedSequence() { _release(); }
68  void _aquire(T* s) {
70  m_sequence = s;
71  m_sequence->addRef();
72  }
73  void _release() {
74  detail::releasePtr(m_sequence);
76  }
77  };
78 
80 
85  class Geant4SensDet : virtual public G4VSensitiveDetector,
86  virtual public G4VGFlashSensitiveDetector,
87  virtual public G4VFastSimSensitiveDetector,
88  virtual public G4VSDFilter,
89  virtual public Geant4ActionSD,
90  virtual public RefCountedSequence<Geant4SensDetActionSequence>
91  {
92  protected:
95  public:
97  Geant4SensDet(const std::string& nam, Detector& description)
98  : G4VSensitiveDetector(nam),
99  G4VGFlashSensitiveDetector(),
101  G4VSDFilter(nam),
102  Geant4Action(0,nam),
103  Geant4ActionSD(nam),
105  {
106  Geant4Kernel& master = Geant4Kernel::instance(description);
107  Geant4Kernel& kernel = master.worker(Geant4Kernel::thread_self());
108  m_sensitive = description.sensitiveDetector(nam);
109  m_context = kernel.workerContext();
110  m_outputLevel = kernel.getOutputLevel(nam);
111  _aquire(kernel.sensitiveAction(nam));
114  this->G4VSensitiveDetector::SetFilter(this);
115  }
116 
118  virtual ~Geant4SensDet() = default;
120  inline G4String GetName() const
121  { return this->G4VSensitiveDetector::SensitiveDetectorName; }
123  virtual std::string path() const override final
124  { return this->G4VSensitiveDetector::GetPathName(); }
126  virtual std::string fullPath() const override final
127  { return this->G4VSensitiveDetector::GetFullPathName(); }
129  virtual bool isActive() const override
130  { return this->G4VSensitiveDetector::isActive(); }
132  virtual G4int GetCollectionID(G4int i) override final
133  { return this->G4VSensitiveDetector::GetCollectionID(i); }
135  virtual G4VReadOutGeometry* readoutGeometry() const override
136  { return this->G4VSensitiveDetector::GetROgeometry(); }
138  virtual SensitiveDetector sensitiveDetector() const override final
139  { return m_sensitive; }
141  virtual const std::string& sensitiveType() const override final
142  { return m_sequence->sensitiveType(); }
144  virtual G4bool Accept(const G4Step* step) const override final
145  { return m_sequence->accept(step); }
147  virtual void Initialize(G4HCofThisEvent* hce) override final
148  { m_sequence->begin(hce); }
150  virtual void EndOfEvent(G4HCofThisEvent* hce) override final
151  { m_sequence->end(hce); }
153  virtual G4bool ProcessHits(G4Step* step,
154  G4TouchableHistory* hist) override final
155  { return m_sequence->process(step,hist); }
157  virtual G4bool ProcessHits(G4GFlashSpot* sp,
158  G4TouchableHistory* hist) override final
159  {
160  const GFlashEnergySpot* esp = sp->GetEnergySpot();
161  G4FastHit hit(esp->GetPosition(), esp->GetEnergy());
162  Geant4FastSimSpot spot(&hit, sp->GetOriginatorTrack(), (sp->GetTouchableHandle())());
163  return m_sequence->processFastSim(&spot, hist);
164  }
166  virtual G4bool ProcessHits(const G4FastHit* hit,
167  const G4FastTrack* track,
168  G4TouchableHistory* hist) override final
169  {
170  Geant4FastSimSpot spot(hit, track, hist);
171  return m_sequence->processFastSim(&spot, hist);
172  }
174  virtual void clear() override
175  { m_sequence->clear(); }
177  virtual size_t defineCollection(const std::string& coll) override {
178  if ( coll.empty() ) {
179  except("Geant4Sensitive: No collection defined for %s [Invalid name]",c_name());
180  }
181  collectionName.emplace_back(coll);
182  return collectionName.size()-1;
183  }
184 
185  };
186  } // End namespace sim
187 } // End namespace dd4hep
188 
189 
190 #include <DDG4/Factories.h>
191 
195 
dd4hep::sim::Geant4SensDet::defineCollection
virtual size_t defineCollection(const std::string &coll) override
Initialize the usage of a hit collection. Returns the collection identifier.
Definition: Geant4SensDet.cpp:177
dd4hep::sim::Geant4SensDet::path
virtual std::string path() const override final
G4VSensitiveDetector internals: Access to the detector path name.
Definition: Geant4SensDet.cpp:123
dd4hep::sim::RefCountedSequence::m_sequence
T * m_sequence
Definition: Geant4SensDet.cpp:61
Geant4tracker
dd4hep::sim::Geant4SensDet Geant4tracker
Definition: Geant4SensDet.cpp:193
Geant4HitCollection.h
G4VFastSimSensitiveDetector
Lower versions of Geant4 do not provide G4VFastSimSensitiveDetector.
Definition: Geant4SensDet.cpp:34
dd4hep::SensitiveDetector
Handle class to hold the information of a sensitive detector.
Definition: DetElement.h:44
dd4hep::sim::Geant4SensDetActionSequence::processFastSim
virtual bool processFastSim(const Geant4FastSimSpot *spot, G4TouchableHistory *history)
GFLASH/FastSim interface: Method for generating hit(s) using the information of the fast simulation s...
Definition: Geant4SensDetAction.cpp:451
dd4hep::sim::Geant4ActionContainer::sensitiveAction
Geant4SensDetActionSequence * sensitiveAction(const std::string &name)
Access to the sensitive detector action from the actioncontainer object.
Definition: Geant4ActionContainer.cpp:139
dd4hep::sim::Geant4SensDetActionSequence::clear
virtual void clear()
G4VSensitiveDetector interface: Method invoked if the event was aborted.
Definition: Geant4SensDetAction.cpp:490
dd4hep::sim::Geant4Action::m_context
Geant4Context * m_context
Reference to the Geant4 context.
Definition: Geant4Action.h:116
dd4hep::sim::RefCountedSequence::RefCountedSequence
RefCountedSequence()
Default constructor.
Definition: Geant4SensDet.cpp:63
Geant4SensDetAction.h
dd4hep::sim::Geant4SensDet::readoutGeometry
virtual G4VReadOutGeometry * readoutGeometry() const override
Access to the readout geometry of the sensitive detector.
Definition: Geant4SensDet.cpp:135
dd4hep::sim::Geant4SensDetActionSequence::sensitiveType
virtual const std::string & sensitiveType() const
Access to the sensitive type of the detector.
Definition: Geant4SensDetAction.h:365
dd4hep::sim::Geant4Kernel
Class, which allows all Geant4Action derivatives to access the DDG4 kernel structures.
Definition: Geant4Kernel.h:64
dd4hep::sim::RefCountedSequence::RefCountedSequence
RefCountedSequence(T *seq)
Initializing constructor.
Definition: Geant4SensDet.cpp:65
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4SensDet::isActive
virtual bool isActive() const override
Is the detector active?
Definition: Geant4SensDet.cpp:129
dd4hep::sim::Geant4Action::m_outputLevel
int m_outputLevel
Default property: Output level.
Definition: Geant4Action.h:121
dd4hep::sim::Geant4SensDet::clear
virtual void clear() override
G4VSensitiveDetector interface: Method invoked if the event was aborted.
Definition: Geant4SensDet.cpp:174
dd4hep::sim::Geant4ActionContainer::workerContext
Geant4Context * workerContext()
Thread's Geant4 execution context.
Definition: Geant4ActionContainer.cpp:65
dd4hep::sim::Geant4SensDetActionSequence::defineCollections
std::size_t defineCollections(Geant4ActionSD *sens_det)
Called at construction time of the sensitive detector to declare all hit collections.
Geant4FastSimSpot.h
dd4hep::sim::Geant4FastSimSpot
Spot definition for fast simulation and GFlash.
Definition: Geant4FastSimSpot.h:71
G4VFastSimSensitiveDetector::ProcessHits
virtual G4bool ProcessHits(const G4FastHit *hit, const G4FastTrack *track, G4TouchableHistory *hist)=0
Geant4 Fast simulation interface.
dd4hep::sim::Geant4Kernel::worker
Geant4Kernel & worker(unsigned long thread_identifier, bool create_if=false)
Access worker instance by its identifier.
Definition: Geant4Kernel.cpp:230
G4FastHit
Definition: Geant4FastSimSpot.h:27
dd4hep::sim::Geant4SensDet::fullPath
virtual std::string fullPath() const override final
G4VSensitiveDetector internals: Access to the detector path name.
Definition: Geant4SensDet.cpp:126
dd4hep::sim::Geant4Action::except
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
Definition: Geant4Action.cpp:256
dd4hep::sim::Geant4Kernel::getOutputLevel
PrintLevel getOutputLevel(const std::string object) const
Retrieve the global output level of a named object.
Definition: Geant4Kernel.cpp:308
dd4hep::sim::RefCountedSequence::_release
void _release()
Definition: Geant4SensDet.cpp:73
dd4hep::sim::Geant4SensDet::~Geant4SensDet
virtual ~Geant4SensDet()=default
Destructor.
DECLARE_GEANT4SENSITIVEDETECTOR
#define DECLARE_GEANT4SENSITIVEDETECTOR(id)
Definition: Factories.h:185
G4VSensitiveDetector
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:59
dd4hep::sim::Geant4SensDet::sensitiveType
virtual const std::string & sensitiveType() const override final
Access to the sensitive type of the detector.
Definition: Geant4SensDet.cpp:141
dd4hep::Detector::sensitiveDetector
virtual SensitiveDetector sensitiveDetector(const std::string &name) const =0
Retrieve a sensitive detector by its name from the detector description.
dd4hep::sim::Geant4SensDetActionSequence::end
virtual void end(G4HCofThisEvent *hce)
G4VSensitiveDetector interface: Method invoked at the end of each event.
Definition: Geant4SensDetAction.cpp:478
dd4hep::sim::RefCountedSequence::~RefCountedSequence
virtual ~RefCountedSequence()
Default destructor.
Definition: Geant4SensDet.cpp:67
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4SensDet::Initialize
virtual void Initialize(G4HCofThisEvent *hce) override final
Method invoked at the begining of each event.
Definition: Geant4SensDet.cpp:147
dd4hep::sim::Geant4SensDetActionSequence::accept
bool accept(const G4Step *step) const
Callback before hit processing starts. Invoke all filters.
Definition: Geant4SensDetAction.cpp:426
dd4hep::sim::Geant4SensDet::Geant4SensDet
Geant4SensDet(const std::string &nam, Detector &description)
Constructor. The detector element is identified by the name.
Definition: Geant4SensDet.cpp:97
dd4hep::sim::Geant4Action
Default base class for all Geant 4 actions and derivates thereof.
Definition: Geant4Action.h:113
dd4hep::sim::Geant4Kernel::thread_self
static unsigned long int thread_self()
Access thread identifier.
Definition: Geant4Kernel.cpp:211
dd4hep::sim::Geant4SensDet::sensitiveDetector
virtual SensitiveDetector sensitiveDetector() const override final
Access to the Detector sensitive detector handle.
Definition: Geant4SensDet.cpp:138
dd4hep::sim::Geant4SensDetActionSequence::updateContext
virtual void updateContext(Geant4Context *ctxt) override
Set or update client context.
Definition: Geant4SensDetAction.cpp:342
dd4hep::sim::Geant4SensDet::m_sensitive
SensitiveDetector m_sensitive
Access to the geant4 sensitive detector handle.
Definition: Geant4SensDet.cpp:94
dd4hep::sim::Geant4SensDet::ProcessHits
virtual G4bool ProcessHits(G4GFlashSpot *sp, G4TouchableHistory *hist) override final
GFLASH interface.
Definition: Geant4SensDet.cpp:157
dd4hep::sim::Geant4ActionSD
Interface class to access properties of the underlying Geant4 sensitive detector structure.
Definition: Geant4SensDetAction.h:61
dd4hep::sim::RefCountedSequence::_aquire
void _aquire(T *s)
Definition: Geant4SensDet.cpp:68
Factories.h
Primitives.h
Geant4SensDet
dd4hep::sim::Geant4SensDet Geant4SensDet
Definition: Geant4SensDet.cpp:192
dd4hep::sim::Geant4Kernel::instance
static Geant4Kernel & instance(Detector &description)
Instance accessor.
Definition: Geant4Kernel.cpp:163
dd4hep::sim::Geant4Action::c_name
const char * c_name() const
Access name of the action.
Definition: Geant4Action.h:284
dd4hep::sim::Geant4SensDetActionSequence
The sequencer to host Geant4 sensitive actions called if particles interact with sensitive elements.
Definition: Geant4SensDetAction.h:317
dd4hep::sim::Geant4SensDet::EndOfEvent
virtual void EndOfEvent(G4HCofThisEvent *hce) override final
Method invoked at the end of each event.
Definition: Geant4SensDet.cpp:150
Geant4calorimeter
dd4hep::sim::Geant4SensDet Geant4calorimeter
Definition: Geant4SensDet.cpp:194
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4SensDet::Accept
virtual G4bool Accept(const G4Step *step) const override final
Callback if the sequence should be accepted or filtered off.
Definition: Geant4SensDet.cpp:144
dd4hep::sim::Geant4SensDet::GetName
G4String GetName() const
Overload to avoid ambiguity between G4VSensitiveDetector and G4VSDFilter.
Definition: Geant4SensDet.cpp:120
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
Geant4Kernel.h
dd4hep::sim::RefCountedSequence
Private helper to support sequence reference counting.
Definition: Geant4SensDet.cpp:59
dd4hep::sim::Geant4SensDetActionSequence::begin
virtual void begin(G4HCofThisEvent *hce)
G4VSensitiveDetector interface: Method invoked at the begining of each event.
Definition: Geant4SensDetAction.cpp:465
InstanceCount.h
dd4hep::sim::Geant4SensDet
Concrete implementation of the G4VSensitiveDetector calling the registered sequence object.
Definition: Geant4SensDet.cpp:91
dd4hep::sim::Geant4SensDet::ProcessHits
virtual G4bool ProcessHits(G4Step *step, G4TouchableHistory *hist) override final
Method for generating hit(s) using the information of G4Step object.
Definition: Geant4SensDet.cpp:153
G4VFastSimSensitiveDetector::G4VFastSimSensitiveDetector
G4VFastSimSensitiveDetector()=default
G4VSDFilter
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:55
Printout.h
Geant4Context.h
G4VFastSimSensitiveDetector::~G4VFastSimSensitiveDetector
virtual ~G4VFastSimSensitiveDetector()=default
dd4hep::sim::Geant4SensDet::GetCollectionID
virtual G4int GetCollectionID(G4int i) override final
This is a utility method which returns the hits collection ID.
Definition: Geant4SensDet.cpp:132
dd4hep::sim::Geant4SensDetActionSequence::process
virtual bool process(const G4Step *step, G4TouchableHistory *history)
G4VSensitiveDetector interface: Method for generating hit(s) using the information of G4Step object.
Definition: Geant4SensDetAction.cpp:440
dd4hep::sim::Geant4SensDet::ProcessHits
virtual G4bool ProcessHits(const G4FastHit *hit, const G4FastTrack *track, G4TouchableHistory *hist) override final
Geant4 Fast simulation interface.
Definition: Geant4SensDet.cpp:166