DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Geant4HitTruthHandler.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_GEANT4HITTRUTHHANDLER_H
14 #define DD4HEP_DDG4_GEANT4HITTRUTHHANDLER_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 
45  public:
47  Geant4HitTruthHandler(Geant4Context* context, const std::string& nam);
49  virtual ~Geant4HitTruthHandler();
51  virtual void begin(const G4Event* event) override;
53  virtual void end(const G4Event* event) override;
54  };
55 
56  } // End namespace sim
57 } // End namespace dd4hep
58 
59 #endif /* DD4HEP_DDG4_GEANT4HITTRUTHHANDLER_H */
60 
61 //====================================================================
62 // AIDA Detector description implementation
63 //--------------------------------------------------------------------
64 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
65 // All rights reserved.
66 //
67 // For the licensing terms see $DD4hepINSTALL/LICENSE.
68 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
69 //
70 // Author : M.Frank
71 //
72 //====================================================================
73 
74 // Framework include files
75 #include <DD4hep/InstanceCount.h>
76 #include <DDG4/Geant4DataDump.h>
78 
79 // Geant 4 includes
80 #include <G4HCofThisEvent.hh>
81 #include <G4Event.hh>
82 
83 using namespace dd4hep::sim;
84 
87  : Geant4EventAction(ctxt, nam)
88 {
89  m_needsControl = true;
91 }
92 
96 }
97 
99 void Geant4HitTruthHandler::begin(const G4Event* /* event */) {
100 }
101 
104  Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
105  if ( coll ) {
106  size_t nhits = coll->GetSize();
107  for(size_t i=0; i<nhits; ++i) {
108  Geant4HitData* h = coll->hit(i);
109  Geant4Tracker::Hit* trk_hit = dynamic_cast<Geant4Tracker::Hit*>(h);
110  if ( 0 != trk_hit ) {
111  if ( truth ) {
112  Geant4HitData::Contribution& t = trk_hit->truth;
113  int trackID = t.trackID;
114  t.trackID = truth->particleID(trackID);
115  }
116  }
117  Geant4Calorimeter::Hit* cal_hit = dynamic_cast<Geant4Calorimeter::Hit*>(h);
118  if ( 0 != cal_hit ) {
119  if ( truth ) {
120  Geant4HitData::Contributions& c = cal_hit->truth;
121  for(Geant4HitData::Contributions::iterator j=c.begin(); j!=c.end(); ++j) {
123  int trackID = t.trackID;
124  t.trackID = truth->particleID(trackID);
125  }
126  }
127  }
128  }
129  }
130 }
131 
133 void Geant4HitTruthHandler::end(const G4Event* event) {
134  G4HCofThisEvent* hce = event->GetHCofThisEvent();
135  if ( hce ) {
136  int nCol = hce->GetNumberOfCollections();
138  if ( truth && !truth->isValid() ) {
139  truth = 0;
140  printout(WARNING,name(),"+++ [Event:%d] No valid MC truth info present. "
141  "Is a Particle handler installed ?",event->GetEventID());
142  }
143  for (int i = 0; i < nCol; ++i) {
144  G4VHitsCollection* hc = hce->GetHC(i);
145  handleCollection(truth, hc);
146  }
147  return;
148  }
149  warning("+++ [Event:%d] The value of G4HCofThisEvent is NULL. No collections saved!",
150  event->GetEventID());
151 }
152 
153 #include <DDG4/Factories.h>
155 
dd4hep::sim::Geant4HitTruthHandler::~Geant4HitTruthHandler
virtual ~Geant4HitTruthHandler()
Default destructor.
Definition: Geant4HitTruthHandler.cpp:94
dd4hep::sim::Geant4Action::m_needsControl
bool m_needsControl
Default property: Flag to create control instance.
Definition: Geant4Action.h:123
dd4hep::sim::Geant4HitCollection::GetSize
virtual size_t GetSize() const override
Access the collection size.
Definition: Geant4HitCollection.h:321
Geant4HitCollection.h
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
dd4hep::sim::Geant4Context::event
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
Definition: Geant4Context.cpp:84
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
dd4hep::sim::Geant4HitTruthHandler::begin
virtual void begin(const G4Event *event) override
Geant4EventAction interface: Begin-of-event callback.
Definition: Geant4HitTruthHandler.cpp:99
Geant4DataDump.h
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::Geant4HitData::Contributions
std::vector< MonteCarloContrib > Contributions
Definition: Geant4Data.h:232
dd4hep::sim::Geant4EventAction
Concrete basic implementation of the Geant4 event action.
Definition: Geant4EventAction.h:53
dd4hep::sim::Geant4HitTruthHandler::CollectionNames
std::vector< std::string > CollectionNames
Definition: Geant4HitTruthHandler.cpp:41
dd4hep::sim::Geant4HitTruthHandler::end
virtual void end(const G4Event *event) override
Geant4EventAction interface: End-of-event callback.
Definition: Geant4HitTruthHandler.cpp:133
dd4hep::sim::Geant4Tracker::Hit
DDG4 tracker hit class used by the generic DDG4 tracker sensitive detector.
Definition: Geant4Data.h:263
dd4hep::sim::Geant4HitTruthHandler
Class to measure the energy of escaping tracks.
Definition: Geant4HitTruthHandler.cpp:39
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::Geant4Tracker::Hit::truth
Contribution truth
Monte Carlo / Geant4 information.
Definition: Geant4Data.h:276
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::Geant4ParticleMap::particleID
int particleID(int track, bool throw_if_not_found=true) const
Access the equivalent track id (shortcut to the usage of TrackEquivalents)
Definition: Geant4Particle.cpp:541
dd4hep::sim::Geant4HitData::MonteCarloContrib::trackID
int trackID
Geant 4 Track identifier.
Definition: Geant4Data.h:141
Factories.h
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::Geant4Calorimeter::Hit::truth
Contributions truth
Hit contributions by individual particles.
Definition: Geant4Data.h:330
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4HitData::MonteCarloContrib
Utility class describing the monte carlo contribution of a given particle to a hit.
Definition: Geant4Data.h:138
dd4hep::sim::Geant4HitTruthHandler::handleCollection
void handleCollection(Geant4ParticleMap *truth, G4VHitsCollection *hc)
Dump single container of hits.
Definition: Geant4HitTruthHandler.cpp:103
InstanceCount.h
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::Geant4HitTruthHandler::Geant4HitTruthHandler
Geant4HitTruthHandler(Geant4Context *context, const std::string &nam)
Standard constructor.
Definition: Geant4HitTruthHandler.cpp:86
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