Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-03-13 08:20:07

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 #ifndef DD4HEP_DDG4_GEANT4HITTRUTHHANDLER_H
0014 #define DD4HEP_DDG4_GEANT4HITTRUTHHANDLER_H
0015 
0016 // Framework include files
0017 #include <DDG4/Geant4EventAction.h>
0018 
0019 // Forward declarations
0020 class G4VHitsCollection;
0021 
0022 /// Namespace for the AIDA detector description toolkit
0023 namespace dd4hep {
0024 
0025   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0026   namespace sim {
0027 
0028     // Forward declarations
0029     class Geant4ParticleMap;
0030     
0031     /// Class to measure the energy of escaping tracks
0032     /** Class to measure the energy of escaping tracks of a detector using Geant 4
0033      * Measure escaping energy....
0034      *
0035      *  \author  M.Frank
0036      *  \version 1.0
0037      *  \ingroup DD4HEP_SIMULATION
0038      */
0039     class Geant4HitTruthHandler : public Geant4EventAction {
0040     public:
0041       typedef std::vector<std::string> CollectionNames;
0042       /// Dump single container of hits
0043       void handleCollection(Geant4ParticleMap* truth, G4VHitsCollection* hc);
0044       
0045     public:
0046       /// Standard constructor
0047       Geant4HitTruthHandler(Geant4Context* context, const std::string& nam);
0048       /// Default destructor
0049       virtual ~Geant4HitTruthHandler();
0050       /// Geant4EventAction interface: Begin-of-event callback
0051       virtual void begin(const G4Event* event)  override;
0052       /// Geant4EventAction interface: End-of-event callback
0053       virtual void end(const G4Event* event)  override;
0054     };
0055 
0056   }    // End namespace sim
0057 }      // End namespace dd4hep
0058 
0059 #endif /* DD4HEP_DDG4_GEANT4HITTRUTHHANDLER_H */
0060 
0061 //====================================================================
0062 //  AIDA Detector description implementation 
0063 //--------------------------------------------------------------------
0064 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0065 // All rights reserved.
0066 //
0067 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0068 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0069 //
0070 // Author     : M.Frank
0071 //
0072 //====================================================================
0073 
0074 // Framework include files
0075 #include <DD4hep/InstanceCount.h>
0076 #include <DDG4/Geant4DataDump.h>
0077 #include <DDG4/Geant4HitCollection.h>
0078 
0079 // Geant 4 includes
0080 #include <G4HCofThisEvent.hh>
0081 #include <G4Event.hh>
0082 
0083 using namespace dd4hep::sim;
0084 
0085 /// Standard constructor
0086 Geant4HitTruthHandler::Geant4HitTruthHandler(Geant4Context* ctxt, const std::string& nam)
0087   : Geant4EventAction(ctxt, nam)
0088 {
0089   m_needsControl = true;
0090   InstanceCount::increment(this);
0091 }
0092 
0093 /// Default destructor
0094 Geant4HitTruthHandler::~Geant4HitTruthHandler() {
0095   InstanceCount::decrement(this);
0096 }
0097 
0098 /// Geant4EventAction interface: Begin-of-event callback
0099 void Geant4HitTruthHandler::begin(const G4Event* /* event */)   {
0100 }
0101 
0102 /// Dump single container of hits
0103 void Geant4HitTruthHandler::handleCollection(Geant4ParticleMap* truth, G4VHitsCollection* collection)  {
0104   Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
0105   if ( coll )    {
0106     size_t nhits = coll->GetSize();
0107     for(size_t i=0; i<nhits; ++i)   {
0108       Geant4HitData* h = coll->hit(i);
0109       Geant4Tracker::Hit* trk_hit = dynamic_cast<Geant4Tracker::Hit*>(h);
0110       if ( 0 != trk_hit )   {
0111         if ( truth )  {
0112           Geant4HitData::Contribution& t = trk_hit->truth;
0113           int trackID = t.trackID;
0114           t.trackID = truth->particleID(trackID);
0115         }
0116       }
0117       Geant4Calorimeter::Hit* cal_hit = dynamic_cast<Geant4Calorimeter::Hit*>(h);
0118       if ( 0 != cal_hit )   {
0119         if ( truth )  {
0120           Geant4HitData::Contributions& c = cal_hit->truth;
0121           for(Geant4HitData::Contributions::iterator j=c.begin(); j!=c.end(); ++j)  {
0122             Geant4HitData::Contribution& t = *j;
0123             int trackID = t.trackID;
0124             t.trackID = truth->particleID(trackID);
0125           }
0126         }
0127       }
0128     }
0129   }
0130 }
0131 
0132 /// Geant4EventAction interface: End-of-event callback
0133 void Geant4HitTruthHandler::end(const G4Event* event)    {
0134   G4HCofThisEvent* hce = event->GetHCofThisEvent();
0135   if ( hce )  {
0136     int nCol = hce->GetNumberOfCollections();
0137     Geant4ParticleMap* truth = context()->event().extension<Geant4ParticleMap>(false);
0138     if ( truth && !truth->isValid() )  {
0139       truth = 0;
0140       printout(WARNING,name(),"+++ [Event:%d] No valid MC truth info present. "
0141                "Is a Particle handler installed ?",event->GetEventID());
0142     }
0143     for (int i = 0; i < nCol; ++i) {
0144       G4VHitsCollection* hc = hce->GetHC(i);
0145       handleCollection(truth, hc);
0146     }
0147     return;
0148   }
0149   warning("+++ [Event:%d] The value of G4HCofThisEvent is NULL. No collections saved!",
0150           event->GetEventID());
0151 }
0152 
0153 #include <DDG4/Factories.h>
0154 DECLARE_GEANT4ACTION(Geant4HitTruthHandler)
0155