Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:26

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_GEANT4HITDUMPACTION_H
0014 #define DD4HEP_DDG4_GEANT4HITDUMPACTION_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 Geant4HitDumpAction : public Geant4EventAction {
0040     public:
0041       typedef std::vector<std::string> CollectionNames;
0042       /// Property: collection names to be dumped 
0043       CollectionNames m_containers;
0044 
0045       /// Dump single container of hits
0046       void dumpCollection(G4VHitsCollection* hc);
0047       
0048     public:
0049       /// Standard constructor
0050       Geant4HitDumpAction(Geant4Context* context, const std::string& nam);
0051       /// Default destructor
0052       virtual ~Geant4HitDumpAction();
0053       /// Geant4EventAction interface: Begin-of-event callback
0054       virtual void begin(const G4Event* event)  override;
0055       /// Geant4EventAction interface: End-of-event callback
0056       virtual void end(const G4Event* event)  override;
0057     };
0058 
0059   }    // End namespace sim
0060 }      // End namespace dd4hep
0061 
0062 #endif /* DD4HEP_DDG4_GEANT4HITDUMPACTION_H */
0063 
0064 //====================================================================
0065 //  AIDA Detector description implementation 
0066 //--------------------------------------------------------------------
0067 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0068 // All rights reserved.
0069 //
0070 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0071 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0072 //
0073 // Author     : M.Frank
0074 //
0075 //====================================================================
0076 
0077 // Framework include files
0078 #include <DD4hep/InstanceCount.h>
0079 #include <DDG4/Geant4DataDump.h>
0080 #include <DDG4/Geant4HitCollection.h>
0081 
0082 // Geant 4 includes
0083 #include <G4HCofThisEvent.hh>
0084 #include <G4Event.hh>
0085 
0086 using namespace dd4hep::sim;
0087 
0088 /// Standard constructor
0089 Geant4HitDumpAction::Geant4HitDumpAction(Geant4Context* ctxt, const std::string& nam)
0090   : Geant4EventAction(ctxt, nam), m_containers{"*"}
0091 {
0092   m_needsControl = true;
0093   declareProperty("Collections",m_containers);
0094   InstanceCount::increment(this);
0095 }
0096 
0097 /// Default destructor
0098 Geant4HitDumpAction::~Geant4HitDumpAction() {
0099   InstanceCount::decrement(this);
0100 }
0101 
0102 /// Geant4EventAction interface: Begin-of-event callback
0103 void Geant4HitDumpAction::begin(const G4Event* /* event */)   {
0104 }
0105 
0106 /// Dump single container of hits
0107 void Geant4HitDumpAction::dumpCollection(G4VHitsCollection* collection)  {
0108   Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
0109   std::string nam = collection->GetName();
0110   if ( coll )    {
0111     Geant4DataDump::CalorimeterHits cal_hits;
0112     Geant4DataDump::TrackerHits     trk_hits;
0113     Geant4DataDump dump(name());
0114     size_t nhits = coll->GetSize();
0115     for(size_t i=0; i<nhits; ++i)   {
0116       Geant4HitData* h = coll->hit(i);
0117       Geant4Tracker::Hit* trk_hit = dynamic_cast<Geant4Tracker::Hit*>(h);
0118       if ( 0 != trk_hit )   {
0119         trk_hits.emplace_back(trk_hit);
0120       }
0121       Geant4Calorimeter::Hit* cal_hit = dynamic_cast<Geant4Calorimeter::Hit*>(h);
0122       if ( 0 != cal_hit )   {
0123         cal_hits.emplace_back(cal_hit);
0124       }
0125     }
0126     if ( !trk_hits.empty() )
0127       dump.print(ALWAYS,nam,&trk_hits);
0128     if ( !cal_hits.empty() )
0129       dump.print(ALWAYS,nam,&cal_hits);
0130   }
0131 }
0132 
0133 /// Geant4EventAction interface: End-of-event callback
0134 void Geant4HitDumpAction::end(const G4Event* event)    {
0135   G4HCofThisEvent* hce = event->GetHCofThisEvent();
0136   if ( hce )  {
0137     int nCol = hce->GetNumberOfCollections();
0138     bool all = !m_containers.empty() && m_containers[0] == "*";
0139     for (int i = 0; i < nCol; ++i) {
0140       G4VHitsCollection* hc = hce->GetHC(i);
0141       if ( all || find(m_containers.begin(),m_containers.end(),hc->GetName()) != m_containers.end() )
0142         dumpCollection(hc);
0143     }
0144     return;
0145   }
0146   warning("+++ [Event:%d] The value of G4HCofThisEvent is NULL.",event->GetEventID());
0147 }
0148 
0149 #include <DDG4/Factories.h>
0150 DECLARE_GEANT4ACTION(Geant4HitDumpAction)