DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Geant4OutputAction.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
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::Simulation;
26 using namespace DD4hep;
27 using namespace std;
28 
31  : Geant4EventAction(ctxt, nam), m_truth(0)
32 {
34  declareProperty("Output", m_output);
35  declareProperty("HandleErrorsAsFatal", m_errorFatal=true);
36  // Need to instantiate run action to configure fibers
37  ctxt->runAction();
38 }
39 
43 }
44 
48  thread_ctxt->runAction().callAtBegin(this, &Geant4OutputAction::beginRun);
49  thread_ctxt->runAction().callAtEnd(this, &Geant4OutputAction::endRun);
50 }
51 
53 void Geant4OutputAction::begin(const G4Event* /* event */) {
54 }
55 
57 void Geant4OutputAction::end(const G4Event* evt) {
58  OutputContext < G4Event > ctxt(evt);
59  G4HCofThisEvent* hce = evt->GetHCofThisEvent();
60  if ( hce ) {
61  int nCol = hce->GetNumberOfCollections();
62  try {
64  if ( m_truth && !m_truth->isValid() ) {
65  m_truth = 0;
66  printout(WARNING,name(),"+++ [Event:%d] No valid MC truth info present. "
67  "Is a Particle handler installed ?",evt->GetEventID());
68  }
69  try {
70  saveEvent(ctxt);
71  for (int i = 0; i < nCol; ++i) {
72  G4VHitsCollection* hc = hce->GetHC(i);
73  saveCollection(ctxt, hc);
74  }
75  }
76  catch(const exception& e) {
77  printout(ERROR,name(),"+++ [Event:%d] Exception while saving event:%s",
78  evt->GetEventID(),e.what());
79  if ( m_errorFatal ) throw;
80  }
81  catch(...) {
82  printout(ERROR,name(),"+++ [Event:%d] UNKNWON Exception while saving event",
83  evt->GetEventID());
84  if ( m_errorFatal ) throw;
85  }
86  commit(ctxt);
87  }
88  catch(const exception& e) {
89  printout(ERROR,name(),"+++ [Event:%d] Exception while saving event:%s",
90  evt->GetEventID(),e.what());
91  if ( m_errorFatal ) throw;
92  }
93  catch(...) {
94  printout(ERROR,name(),"+++ [Event:%d] UNKNWON Exception while saving event",
95  evt->GetEventID());
96  if ( m_errorFatal ) throw;
97  }
98  m_truth = 0;
99  return;
100  }
101  printout(WARNING,"Geant4OutputAction",
102  "+++ The value of G4HCofThisEvent is NULL. No collections saved!");
103 }
104 
107 }
108 
110 void Geant4OutputAction::beginRun(const G4Run* /* run */) {
111 }
112 
114 void Geant4OutputAction::endRun(const G4Run* /* run */) {
115 }
116 
118 void Geant4OutputAction::saveRun(const G4Run* /* run */) {
119 }
120 
123 }
124 
127 }
128 
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
virtual void saveRun(const G4Run *run)
Callback to store the Geant4 event.
virtual void configureFiber(Geant4Context *ctxt)
Set or update client for the use in a new thread fiber.
virtual void end(const G4Event *event)
End-of-event callback.
void callAtEnd(Q *p, void(T::*f)(const G4Run *))
Register end-of-run callback. Types Q and T must be polymorph!
virtual void saveEvent(OutputContext< G4Event > &ctxt)
Callback to store the Geant4 event.
virtual void begin(const G4Event *event)
begin-of-event callback
static void decrement(T *)
Decrement count according to type information.
virtual void commit(OutputContext< G4Event > &ctxt)
Commit data at end of filling procedure.
Geant4OutputAction(Geant4Context *c, const std::string &nam)
Standard constructor.
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:40
return e
Definition: Volumes.cpp:297
Concrete basic implementation of the Geant4 event action.
virtual ~Geant4OutputAction()
Default destructor.
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:261
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:271
Geant4RunActionSequence & runAction() const
Access to the main run action sequence from the kernel object.
virtual void configureFiber(Geant4Context *thread_context)
Set or update client for the use in a new thread fiber.
Geant4ParticleMap * m_truth
Reference to MC truth object.
Data structure to map particles produced during the generation and the simulation.
void callAtBegin(Q *p, void(T::*f)(const G4Run *))
Register begin-of-run callback. Types Q and T must be polymorph!
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
virtual void beginRun(const G4Run *run)
Callback to initialize storing the Geant4 information.
virtual void saveCollection(OutputContext< G4Event > &ctxt, G4VHitsCollection *collection)
Callback to store each Geant4 hit collection.
virtual void endRun(const G4Run *run)
Callback to store the Geant4 run information.
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
Generic context to extend user, run and event information.
bool isValid() const
Check if the particle map was ever filled (ie. some particle handler was present) ...
bool m_errorFatal
Property: "HandleErrorsAsFatal" Handle errors as fatal and rethrow eventual exceptions.
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Definition: Geant4Action.h:358
std::string m_output
Property: "Output" output destination.