DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4OutputAction.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/InstanceCount.h>
17 #include <DDG4/Geant4Particle.h>
18 #include <DDG4/Geant4RunAction.h>
20 
21 // Geant 4 includes
22 #include <G4HCofThisEvent.hh>
23 #include <G4Event.hh>
24 
25 using namespace dd4hep::sim;
26 
29  : Geant4EventAction(ctxt, nam)
30 {
32  declareProperty("Output", m_output);
33  declareProperty("HandleErrorsAsFatal", m_errorFatal=true);
34  // Need to instantiate run action to configure fibers
35  ctxt->runAction();
36 }
37 
41 }
42 
46  thread_ctxt->runAction().callAtBegin(this, &Geant4OutputAction::beginRun);
47  thread_ctxt->runAction().callAtEnd(this, &Geant4OutputAction::endRun);
48 }
49 
51 void Geant4OutputAction::begin(const G4Event* /* event */) {
52 }
53 
55 void Geant4OutputAction::end(const G4Event* evt) {
56  OutputContext < G4Event > ctxt(evt);
57  G4HCofThisEvent* hce = evt->GetHCofThisEvent();
58  if ( hce ) {
59  int nCol = hce->GetNumberOfCollections();
60  try {
62  if ( m_truth && !m_truth->isValid() ) {
63  m_truth = 0;
64  printout(WARNING,name(),"+++ [Event:%d] No valid MC truth info present. "
65  "Is a Particle handler installed ?",evt->GetEventID());
66  }
67  try {
68  saveEvent(ctxt);
69  for (int i = 0; i < nCol; ++i) {
70  G4VHitsCollection* hc = hce->GetHC(i);
71  saveCollection(ctxt, hc);
72  }
73  }
74  catch(const std::exception& e) {
75  printout(ERROR,name(),"+++ [Event:%d] Exception while saving event:%s",
76  evt->GetEventID(),e.what());
77  if ( m_errorFatal ) throw;
78  }
79  catch(...) {
80  printout(ERROR,name(),"+++ [Event:%d] UNKNWON Exception while saving event",
81  evt->GetEventID());
82  if ( m_errorFatal ) throw;
83  }
84  commit(ctxt);
85  }
86  catch(const std::exception& e) {
87  printout(ERROR,name(),"+++ [Event:%d] Exception while saving event:%s",
88  evt->GetEventID(),e.what());
89  if ( m_errorFatal ) throw;
90  }
91  catch(...) {
92  printout(ERROR,name(),"+++ [Event:%d] UNKNWON Exception while saving event",
93  evt->GetEventID());
94  if ( m_errorFatal ) throw;
95  }
96  m_truth = 0;
97  return;
98  }
99  printout(WARNING,"Geant4OutputAction",
100  "+++ The value of G4HCofThisEvent is NULL. No collections saved!");
101 }
102 
105 }
106 
108 void Geant4OutputAction::beginRun(const G4Run* /* run */) {
109 }
110 
112 void Geant4OutputAction::endRun(const G4Run* /* run */) {
113 }
114 
116 void Geant4OutputAction::saveRun(const G4Run* /* run */) {
117 }
118 
121 }
122 
125 }
126 
dd4hep::sim::Geant4Context::runAction
Geant4RunActionSequence & runAction() const
Access to the main run action sequence from the kernel object.
Definition: Geant4Context.cpp:113
dd4hep::sim::Geant4OutputAction::Geant4OutputAction
Geant4OutputAction()=delete
Inhibit default constructor.
dd4hep::sim::Geant4OutputAction::endRun
virtual void endRun(const G4Run *run)
Callback to store the Geant4 run information.
Definition: Geant4OutputAction.cpp:112
dd4hep::sim::Geant4OutputAction::saveRun
virtual void saveRun(const G4Run *run)
Callback to store the Geant4 event.
Definition: Geant4OutputAction.cpp:116
dd4hep::sim::Geant4Action::configureFiber
virtual void configureFiber(Geant4Context *thread_context)
Set or update client for the use in a new thread fiber.
Definition: Geant4Action.cpp:140
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
Geant4RunAction.h
dd4hep::sim::Geant4RunActionSequence::callAtBegin
void callAtBegin(Q *p, void(T::*f)(const G4Run *))
Register begin-of-run callback. Types Q and T must be polymorph!
Definition: Geant4RunAction.h:144
G4VHitsCollection
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:47
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4Context::event
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
Definition: Geant4Context.cpp:84
dd4hep::sim::Geant4RunActionSequence::callAtEnd
void callAtEnd(Q *p, void(T::*f)(const G4Run *))
Register end-of-run callback. Types Q and T must be polymorph!
Definition: Geant4RunAction.h:149
dd4hep::sim::Geant4OutputAction::OutputContext
Helper class for thread savety.
Definition: Geant4OutputAction.h:43
dd4hep::sim::Geant4ParticleMap
Data structure to map particles produced during the generation and the simulation.
Definition: Geant4Particle.h:337
dd4hep::sim::Geant4Event::extension
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
Definition: Geant4Context.h:151
dd4hep::sim::Geant4EventAction
Concrete basic implementation of the Geant4 event action.
Definition: Geant4EventAction.h:53
Geant4OutputAction.h
dd4hep::sim::Geant4OutputAction::commit
virtual void commit(OutputContext< G4Event > &ctxt)
Commit data at end of filling procedure.
Definition: Geant4OutputAction.cpp:104
dd4hep::sim::Geant4OutputAction::m_truth
Geant4ParticleMap * m_truth
Reference to MC truth object.
Definition: Geant4OutputAction.h:60
dd4hep::sim::Geant4Action::declareProperty
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:366
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4OutputAction::beginRun
virtual void beginRun(const G4Run *run)
Callback to initialize storing the Geant4 information.
Definition: Geant4OutputAction.cpp:108
dd4hep::sim::Geant4OutputAction::saveEvent
virtual void saveEvent(OutputContext< G4Event > &ctxt)
Callback to store the Geant4 event.
Definition: Geant4OutputAction.cpp:120
dd4hep::sim::Geant4Action::name
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:280
dd4hep::sim::Geant4OutputAction::end
virtual void end(const G4Event *event) override
End-of-event callback.
Definition: Geant4OutputAction.cpp:55
dd4hep::sim::Geant4OutputAction::begin
virtual void begin(const G4Event *event) override
begin-of-event callback
Definition: Geant4OutputAction.cpp:51
dd4hep::sim::Geant4OutputAction::m_output
std::string m_output
Property: "Output" output destination.
Definition: Geant4OutputAction.h:56
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: EDM4hepFileReader.cpp:41
dd4hep::sim::Geant4OutputAction::~Geant4OutputAction
virtual ~Geant4OutputAction()
Default destructor.
Definition: Geant4OutputAction.cpp:39
Geant4Particle.h
InstanceCount.h
dd4hep::sim::Geant4OutputAction::saveCollection
virtual void saveCollection(OutputContext< G4Event > &ctxt, G4VHitsCollection *collection)
Callback to store each Geant4 hit collection.
Definition: Geant4OutputAction.cpp:124
dd4hep::sim::Geant4ParticleMap::isValid
bool isValid() const
Check if the particle map was ever filled (ie. some particle handler was present)
Definition: Geant4Particle.cpp:536
dd4hep::sim::Geant4OutputAction::m_errorFatal
bool m_errorFatal
Property: "HandleErrorsAsFatal" Handle errors as fatal and rethrow eventual exceptions.
Definition: Geant4OutputAction.h:58
Printout.h
dd4hep::sim::Geant4OutputAction::configureFiber
virtual void configureFiber(Geant4Context *ctxt) override
Set or update client for the use in a new thread fiber.
Definition: Geant4OutputAction.cpp:44
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270