File indexing completed on 2025-01-18 09:14:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include "MyTrackerHit.h"
0016 #include <DDG4/Geant4SensDetAction.inl>
0017 #include <DDG4/Geant4ParticleInformation.h>
0018 #include <DDG4/Factories.h>
0019
0020
0021 namespace SomeExperiment {
0022
0023 class MyTrackerSD {
0024 public:
0025 typedef MyTrackerHit Hit;
0026
0027 bool haveCellID = true;
0028 int mumDeposits = 0;
0029 double integratedDeposit = 0;
0030 };
0031 }
0032
0033
0034 namespace dd4hep {
0035
0036
0037 namespace sim {
0038
0039 using namespace SomeExperiment;
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 template <>
0053 Geant4SensitiveAction<MyTrackerSD>::Geant4SensitiveAction(Geant4Context* ctxt,
0054 const std::string& nam,
0055 DetElement det,
0056 Detector& description_ref)
0057 : Geant4Sensitive(ctxt,nam,det,description_ref), m_collectionName(), m_collectionID(0)
0058 {
0059 declareProperty("HaveCellID", m_userData.haveCellID = true);
0060 declareProperty("ReadoutName", m_readoutName);
0061 declareProperty("CollectionName", m_collectionName);
0062 initialize();
0063 InstanceCount::increment(this);
0064 }
0065
0066
0067 template <> void Geant4SensitiveAction<MyTrackerSD>::defineCollections() {
0068 m_collectionID = declareReadoutFilteredCollection<MyTrackerSD::Hit>();
0069 }
0070
0071
0072 template <> bool Geant4SensitiveAction<MyTrackerSD>::process(const G4Step* step,G4TouchableHistory* ) {
0073 Geant4StepHandler h(step);
0074 Position prePos = h.prePos();
0075 Position postPos = h.postPos();
0076 Position direction = postPos - prePos;
0077 Position pos = mean_direction(prePos,postPos);
0078 Direction mom = 0.5 * (h.preMom() + h.postMom());
0079 double hit_len = direction.R();
0080 double depo = h.deposit();
0081 double tim = h.track->GetGlobalTime();
0082
0083 MyTrackerSD::Hit* hit =
0084 new MyTrackerSD::Hit(h.trkID(), h.trkPdgID(), depo, tim, hit_len, pos, mom);
0085 Geant4HitData::MonteCarloContrib contrib = Geant4HitData::extractContribution(step);
0086 hit->cellID = this->m_userData.haveCellID ? cellID(step) : 0;
0087 hit->step_length = hit_len;
0088 hit->prePos = prePos;
0089 hit->postPos = postPos;
0090 collection(m_collectionID)->add(hit);
0091 mark(h.track);
0092 if ( this->m_userData.haveCellID && 0 == hit->cellID ) {
0093 hit->cellID = volumeID(step);
0094 except("+++ Invalid CELL ID for hit!");
0095 }
0096 printP1("Hit with deposit:%f Pos:%f %f %f ID=%016X",
0097 depo, pos.X(), pos.Y(), pos.Z(), (void*)hit->cellID);
0098 Geant4TouchableHandler handler(step);
0099 print(" Geant4 path:%s", handler.path().c_str());
0100
0101
0102 m_userData.integratedDeposit += contrib.deposit;
0103 ++m_userData.mumDeposits;
0104
0105
0106
0107 if ( nullptr == h.track->GetUserInformation() ) {
0108 auto data = std::make_unique<ParticleUserData>();
0109 data->absolute_momentum = h.track->GetMomentum().mag();
0110 h.track->SetUserInformation(new Geant4ParticleInformation(std::move(data)));
0111 }
0112 return true;
0113 }
0114
0115 }
0116 }
0117
0118
0119 namespace dd4hep { namespace sim {
0120 typedef Geant4SensitiveAction<MyTrackerSD> MyTrackerSDAction;
0121 }}
0122 DECLARE_GEANT4SENSITIVE(MyTrackerSDAction)