File indexing completed on 2025-01-30 09:17:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef DD4HEP_DDG4_GEANT4HITDUMPACTION_H
0014 #define DD4HEP_DDG4_GEANT4HITDUMPACTION_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 Geant4HitDumpAction : public Geant4EventAction {
0040 public:
0041 typedef std::vector<std::string> CollectionNames;
0042
0043 CollectionNames m_containers;
0044
0045
0046 void dumpCollection(G4VHitsCollection* hc);
0047
0048 public:
0049
0050 Geant4HitDumpAction(Geant4Context* context, const std::string& nam);
0051
0052 virtual ~Geant4HitDumpAction();
0053
0054 virtual void begin(const G4Event* event) override;
0055
0056 virtual void end(const G4Event* event) override;
0057 };
0058
0059 }
0060 }
0061
0062 #endif
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078 #include <DD4hep/InstanceCount.h>
0079 #include <DDG4/Geant4DataDump.h>
0080 #include <DDG4/Geant4HitCollection.h>
0081
0082
0083 #include <G4HCofThisEvent.hh>
0084 #include <G4Event.hh>
0085
0086 using namespace dd4hep::sim;
0087
0088
0089 Geant4HitDumpAction::Geant4HitDumpAction(Geant4Context* ctxt, const std::string& nam)
0090 : Geant4EventAction(ctxt, nam), m_containers{"*"}
0091 {
0092 m_needsControl = true;
0093 declareProperty("Collections",m_containers);
0094 InstanceCount::increment(this);
0095 }
0096
0097
0098 Geant4HitDumpAction::~Geant4HitDumpAction() {
0099 InstanceCount::decrement(this);
0100 }
0101
0102
0103 void Geant4HitDumpAction::begin(const G4Event* ) {
0104 }
0105
0106
0107 void Geant4HitDumpAction::dumpCollection(G4VHitsCollection* collection) {
0108 Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
0109 std::string nam = collection->GetName();
0110 if ( coll ) {
0111 Geant4DataDump::CalorimeterHits cal_hits;
0112 Geant4DataDump::TrackerHits trk_hits;
0113 Geant4DataDump dump(name());
0114 size_t nhits = coll->GetSize();
0115 for(size_t i=0; i<nhits; ++i) {
0116 Geant4HitData* h = coll->hit(i);
0117 Geant4Tracker::Hit* trk_hit = dynamic_cast<Geant4Tracker::Hit*>(h);
0118 if ( 0 != trk_hit ) {
0119 trk_hits.emplace_back(trk_hit);
0120 }
0121 Geant4Calorimeter::Hit* cal_hit = dynamic_cast<Geant4Calorimeter::Hit*>(h);
0122 if ( 0 != cal_hit ) {
0123 cal_hits.emplace_back(cal_hit);
0124 }
0125 }
0126 if ( !trk_hits.empty() )
0127 dump.print(ALWAYS,nam,&trk_hits);
0128 if ( !cal_hits.empty() )
0129 dump.print(ALWAYS,nam,&cal_hits);
0130 }
0131 }
0132
0133
0134 void Geant4HitDumpAction::end(const G4Event* event) {
0135 G4HCofThisEvent* hce = event->GetHCofThisEvent();
0136 if ( hce ) {
0137 int nCol = hce->GetNumberOfCollections();
0138 bool all = !m_containers.empty() && m_containers[0] == "*";
0139 for (int i = 0; i < nCol; ++i) {
0140 G4VHitsCollection* hc = hce->GetHC(i);
0141 if ( all || find(m_containers.begin(),m_containers.end(),hc->GetName()) != m_containers.end() )
0142 dumpCollection(hc);
0143 }
0144 return;
0145 }
0146 warning("+++ [Event:%d] The value of G4HCofThisEvent is NULL.",event->GetEventID());
0147 }
0148
0149 #include <DDG4/Factories.h>
0150 DECLARE_GEANT4ACTION(Geant4HitDumpAction)