DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4HitDumpAction.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 #ifndef DD4HEP_DDG4_GEANT4HITDUMPACTION_H
14 #define DD4HEP_DDG4_GEANT4HITDUMPACTION_H
15 
16 // Framework include files
17 #include <DDG4/Geant4EventAction.h>
18 
19 // Forward declarations
20 class G4VHitsCollection;
21 
23 namespace dd4hep {
24 
26  namespace sim {
27 
28  // Forward declarations
29  class Geant4ParticleMap;
30 
32 
40  public:
41  typedef std::vector<std::string> CollectionNames;
44 
47 
48  public:
50  Geant4HitDumpAction(Geant4Context* context, const std::string& nam);
52  virtual ~Geant4HitDumpAction();
54  virtual void begin(const G4Event* event) override;
56  virtual void end(const G4Event* event) override;
57  };
58 
59  } // End namespace sim
60 } // End namespace dd4hep
61 
62 #endif /* DD4HEP_DDG4_GEANT4HITDUMPACTION_H */
63 
64 //====================================================================
65 // AIDA Detector description implementation
66 //--------------------------------------------------------------------
67 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
68 // All rights reserved.
69 //
70 // For the licensing terms see $DD4hepINSTALL/LICENSE.
71 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
72 //
73 // Author : M.Frank
74 //
75 //====================================================================
76 
77 // Framework include files
78 #include <DD4hep/InstanceCount.h>
79 #include <DDG4/Geant4DataDump.h>
81 
82 // Geant 4 includes
83 #include <G4HCofThisEvent.hh>
84 #include <G4Event.hh>
85 
86 using namespace dd4hep::sim;
87 
90  : Geant4EventAction(ctxt, nam), m_containers{"*"}
91 {
92  m_needsControl = true;
93  declareProperty("Collections",m_containers);
95 }
96 
100 }
101 
103 void Geant4HitDumpAction::begin(const G4Event* /* event */) {
104 }
105 
108  Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
109  std::string nam = collection->GetName();
110  if ( coll ) {
113  Geant4DataDump dump(name());
114  size_t nhits = coll->GetSize();
115  for(size_t i=0; i<nhits; ++i) {
116  Geant4HitData* h = coll->hit(i);
117  Geant4Tracker::Hit* trk_hit = dynamic_cast<Geant4Tracker::Hit*>(h);
118  if ( 0 != trk_hit ) {
119  trk_hits.emplace_back(trk_hit);
120  }
121  Geant4Calorimeter::Hit* cal_hit = dynamic_cast<Geant4Calorimeter::Hit*>(h);
122  if ( 0 != cal_hit ) {
123  cal_hits.emplace_back(cal_hit);
124  }
125  }
126  if ( !trk_hits.empty() )
127  dump.print(ALWAYS,nam,&trk_hits);
128  if ( !cal_hits.empty() )
129  dump.print(ALWAYS,nam,&cal_hits);
130  }
131 }
132 
134 void Geant4HitDumpAction::end(const G4Event* event) {
135  G4HCofThisEvent* hce = event->GetHCofThisEvent();
136  if ( hce ) {
137  int nCol = hce->GetNumberOfCollections();
138  bool all = !m_containers.empty() && m_containers[0] == "*";
139  for (int i = 0; i < nCol; ++i) {
140  G4VHitsCollection* hc = hce->GetHC(i);
141  if ( all || find(m_containers.begin(),m_containers.end(),hc->GetName()) != m_containers.end() )
142  dumpCollection(hc);
143  }
144  return;
145  }
146  warning("+++ [Event:%d] The value of G4HCofThisEvent is NULL.",event->GetEventID());
147 }
148 
149 #include <DDG4/Factories.h>
dd4hep::sim::Geant4HitDumpAction::Geant4HitDumpAction
Geant4HitDumpAction(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4HitDumpAction.cpp:89
dd4hep::sim::Geant4HitDumpAction::end
virtual void end(const G4Event *event) override
Geant4EventAction interface: End-of-event callback.
Definition: Geant4HitDumpAction.cpp:134
dd4hep::sim::Geant4HitCollection::GetSize
virtual size_t GetSize() const override
Access the collection size.
Definition: Geant4HitCollection.h:321
Geant4HitCollection.h
dd4hep::sim::Geant4HitDumpAction::~Geant4HitDumpAction
virtual ~Geant4HitDumpAction()
Default destructor.
Definition: Geant4HitDumpAction.cpp:98
dd4hep::sim::Geant4HitDumpAction::begin
virtual void begin(const G4Event *event) override
Geant4EventAction interface: Begin-of-event callback.
Definition: Geant4HitDumpAction.cpp:103
dd4hep::sim::Geant4DataDump
Class to dump the records of the intrinsic Geant4 event model.
Definition: Geant4DataDump.h:37
dd4hep::sim::Geant4HitCollection
Generic hit container class using Geant4HitWrapper objects.
Definition: Geant4HitCollection.h:201
Geant4EventAction.h
dd4hep::sim::Geant4HitCollection::hit
Geant4HitWrapper & hit(size_t which)
Access the hit wrapper.
Definition: Geant4HitCollection.h:325
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
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::Geant4Action::warning
void warning(const char *fmt,...) const
Support of warning messages.
Definition: Geant4Action.cpp:223
Geant4DataDump.h
dd4hep::sim::Geant4ParticleMap
Data structure to map particles produced during the generation and the simulation.
Definition: Geant4Particle.h:337
dd4hep::sim::Geant4DataDump::CalorimeterHits
std::vector< SimpleCalorimeter::Hit * > CalorimeterHits
Definition: Geant4DataDump.h:46
dd4hep::sim::Geant4EventAction
Concrete basic implementation of the Geant4 event action.
Definition: Geant4EventAction.h:53
dd4hep::sim::Geant4Tracker::Hit
DDG4 tracker hit class used by the generic DDG4 tracker sensitive detector.
Definition: Geant4Data.h:263
dd4hep::sim::Geant4DataDump::TrackerHits
std::vector< SimpleTracker::Hit * > TrackerHits
Definition: Geant4DataDump.h:43
dd4hep::sim::Geant4HitData
Base class for geant4 hit structures used by the default DDG4 sensitive detector implementations.
Definition: Geant4Data.h:110
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4Action::name
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:280
dd4hep::sim::Geant4Calorimeter::Hit
DDG4 calorimeter hit class used by the generic DDG4 calorimeter sensitive detector.
Definition: Geant4Data.h:323
dd4hep::sim::Geant4HitDumpAction::m_containers
CollectionNames m_containers
Property: collection names to be dumped.
Definition: Geant4HitDumpAction.cpp:43
dd4hep::sim::Geant4HitDumpAction::CollectionNames
std::vector< std::string > CollectionNames
Definition: Geant4HitDumpAction.cpp:41
Factories.h
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4HitDumpAction
Class to measure the energy of escaping tracks.
Definition: Geant4HitDumpAction.cpp:39
InstanceCount.h
dd4hep::sim::Geant4HitDumpAction::dumpCollection
void dumpCollection(G4VHitsCollection *hc)
Dump single container of hits.
Definition: Geant4HitDumpAction.cpp:107
dd4hep::sim::Geant4DataDump::print
void print(PrintLevel level, Geant4ParticleHandle p) const
Print a single particle to the output logging using the specified print level.
Definition: Geant4DataDump.cpp:30
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