File indexing completed on 2025-03-13 08:20:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_DDG4_GEANT4HITTRUTHHANDLER_H
0014 #define DD4HEP_DDG4_GEANT4HITTRUTHHANDLER_H
0015
0016
0017 #include <DDG4/Geant4EventAction.h>
0018
0019
0020 class G4VHitsCollection;
0021
0022
0023 namespace dd4hep {
0024
0025
0026 namespace sim {
0027
0028
0029 class Geant4ParticleMap;
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 class Geant4HitTruthHandler : public Geant4EventAction {
0040 public:
0041 typedef std::vector<std::string> CollectionNames;
0042
0043 void handleCollection(Geant4ParticleMap* truth, G4VHitsCollection* hc);
0044
0045 public:
0046
0047 Geant4HitTruthHandler(Geant4Context* context, const std::string& nam);
0048
0049 virtual ~Geant4HitTruthHandler();
0050
0051 virtual void begin(const G4Event* event) override;
0052
0053 virtual void end(const G4Event* event) override;
0054 };
0055
0056 }
0057 }
0058
0059 #endif
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075 #include <DD4hep/InstanceCount.h>
0076 #include <DDG4/Geant4DataDump.h>
0077 #include <DDG4/Geant4HitCollection.h>
0078
0079
0080 #include <G4HCofThisEvent.hh>
0081 #include <G4Event.hh>
0082
0083 using namespace dd4hep::sim;
0084
0085
0086 Geant4HitTruthHandler::Geant4HitTruthHandler(Geant4Context* ctxt, const std::string& nam)
0087 : Geant4EventAction(ctxt, nam)
0088 {
0089 m_needsControl = true;
0090 InstanceCount::increment(this);
0091 }
0092
0093
0094 Geant4HitTruthHandler::~Geant4HitTruthHandler() {
0095 InstanceCount::decrement(this);
0096 }
0097
0098
0099 void Geant4HitTruthHandler::begin(const G4Event* ) {
0100 }
0101
0102
0103 void Geant4HitTruthHandler::handleCollection(Geant4ParticleMap* truth, G4VHitsCollection* collection) {
0104 Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
0105 if ( coll ) {
0106 size_t nhits = coll->GetSize();
0107 for(size_t i=0; i<nhits; ++i) {
0108 Geant4HitData* h = coll->hit(i);
0109 Geant4Tracker::Hit* trk_hit = dynamic_cast<Geant4Tracker::Hit*>(h);
0110 if ( 0 != trk_hit ) {
0111 if ( truth ) {
0112 Geant4HitData::Contribution& t = trk_hit->truth;
0113 int trackID = t.trackID;
0114 t.trackID = truth->particleID(trackID);
0115 }
0116 }
0117 Geant4Calorimeter::Hit* cal_hit = dynamic_cast<Geant4Calorimeter::Hit*>(h);
0118 if ( 0 != cal_hit ) {
0119 if ( truth ) {
0120 Geant4HitData::Contributions& c = cal_hit->truth;
0121 for(Geant4HitData::Contributions::iterator j=c.begin(); j!=c.end(); ++j) {
0122 Geant4HitData::Contribution& t = *j;
0123 int trackID = t.trackID;
0124 t.trackID = truth->particleID(trackID);
0125 }
0126 }
0127 }
0128 }
0129 }
0130 }
0131
0132
0133 void Geant4HitTruthHandler::end(const G4Event* event) {
0134 G4HCofThisEvent* hce = event->GetHCofThisEvent();
0135 if ( hce ) {
0136 int nCol = hce->GetNumberOfCollections();
0137 Geant4ParticleMap* truth = context()->event().extension<Geant4ParticleMap>(false);
0138 if ( truth && !truth->isValid() ) {
0139 truth = 0;
0140 printout(WARNING,name(),"+++ [Event:%d] No valid MC truth info present. "
0141 "Is a Particle handler installed ?",event->GetEventID());
0142 }
0143 for (int i = 0; i < nCol; ++i) {
0144 G4VHitsCollection* hc = hce->GetHC(i);
0145 handleCollection(truth, hc);
0146 }
0147 return;
0148 }
0149 warning("+++ [Event:%d] The value of G4HCofThisEvent is NULL. No collections saved!",
0150 event->GetEventID());
0151 }
0152
0153 #include <DDG4/Factories.h>
0154 DECLARE_GEANT4ACTION(Geant4HitTruthHandler)
0155