File indexing completed on 2025-01-30 09:17:33
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Printout.h>
0016 #include <DD4hep/InstanceCount.h>
0017 #include <DDG4/Geant4Particle.h>
0018 #include <DDG4/Geant4RunAction.h>
0019 #include <DDG4/Geant4OutputAction.h>
0020
0021
0022 #include <G4HCofThisEvent.hh>
0023 #include <G4Event.hh>
0024
0025 using namespace dd4hep::sim;
0026
0027
0028 Geant4OutputAction::Geant4OutputAction(Geant4Context* ctxt, const std::string& nam)
0029 : Geant4EventAction(ctxt, nam)
0030 {
0031 InstanceCount::increment(this);
0032 declareProperty("Output", m_output);
0033 declareProperty("HandleErrorsAsFatal", m_errorFatal=true);
0034
0035 ctxt->runAction();
0036 }
0037
0038
0039 Geant4OutputAction::~Geant4OutputAction() {
0040 InstanceCount::decrement(this);
0041 }
0042
0043
0044 void Geant4OutputAction::configureFiber(Geant4Context* thread_ctxt) {
0045 Geant4EventAction::configureFiber(thread_ctxt);
0046 thread_ctxt->runAction().callAtBegin(this, &Geant4OutputAction::beginRun);
0047 thread_ctxt->runAction().callAtEnd(this, &Geant4OutputAction::endRun);
0048 }
0049
0050
0051 void Geant4OutputAction::begin(const G4Event* ) {
0052 }
0053
0054
0055 void Geant4OutputAction::end(const G4Event* evt) {
0056 OutputContext < G4Event > ctxt(evt);
0057 G4HCofThisEvent* hce = evt->GetHCofThisEvent();
0058 if ( hce ) {
0059 int nCol = hce->GetNumberOfCollections();
0060 try {
0061 m_truth = context()->event().extension<Geant4ParticleMap>(false);
0062 if ( m_truth && !m_truth->isValid() ) {
0063 m_truth = 0;
0064 printout(WARNING,name(),"+++ [Event:%d] No valid MC truth info present. "
0065 "Is a Particle handler installed ?",evt->GetEventID());
0066 }
0067 try {
0068 saveEvent(ctxt);
0069 for (int i = 0; i < nCol; ++i) {
0070 G4VHitsCollection* hc = hce->GetHC(i);
0071 saveCollection(ctxt, hc);
0072 }
0073 }
0074 catch(const std::exception& e) {
0075 printout(ERROR,name(),"+++ [Event:%d] Exception while saving event:%s",
0076 evt->GetEventID(),e.what());
0077 if ( m_errorFatal ) throw;
0078 }
0079 catch(...) {
0080 printout(ERROR,name(),"+++ [Event:%d] UNKNWON Exception while saving event",
0081 evt->GetEventID());
0082 if ( m_errorFatal ) throw;
0083 }
0084 commit(ctxt);
0085 }
0086 catch(const std::exception& e) {
0087 printout(ERROR,name(),"+++ [Event:%d] Exception while saving event:%s",
0088 evt->GetEventID(),e.what());
0089 if ( m_errorFatal ) throw;
0090 }
0091 catch(...) {
0092 printout(ERROR,name(),"+++ [Event:%d] UNKNWON Exception while saving event",
0093 evt->GetEventID());
0094 if ( m_errorFatal ) throw;
0095 }
0096 m_truth = 0;
0097 return;
0098 }
0099 printout(WARNING,"Geant4OutputAction",
0100 "+++ The value of G4HCofThisEvent is NULL. No collections saved!");
0101 }
0102
0103
0104 void Geant4OutputAction::commit(OutputContext<G4Event>& ) {
0105 }
0106
0107
0108 void Geant4OutputAction::beginRun(const G4Run* ) {
0109 }
0110
0111
0112 void Geant4OutputAction::endRun(const G4Run* ) {
0113 }
0114
0115
0116 void Geant4OutputAction::saveRun(const G4Run* ) {
0117 }
0118
0119
0120 void Geant4OutputAction::saveEvent(OutputContext<G4Event>& ) {
0121 }
0122
0123
0124 void Geant4OutputAction::saveCollection(OutputContext<G4Event>& , G4VHitsCollection* ) {
0125 }
0126