Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:55

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 
0014 // Framework include files
0015 #include "MyTrackerHit.h"
0016 #include <DDG4/Geant4SensDetAction.inl>
0017 #include <DDG4/Geant4ParticleInformation.h>
0018 #include <DDG4/Factories.h>
0019 
0020 
0021 namespace SomeExperiment {
0022 
0023   class MyTrackerSD {
0024   public:
0025     typedef MyTrackerHit Hit;
0026     // If we need special data to personalize the action, be put it here
0027     bool   haveCellID = true;
0028     int    mumDeposits = 0;
0029     double integratedDeposit = 0;
0030   };
0031 }
0032 
0033 /// Namespace for the AIDA detector description toolkit
0034 namespace dd4hep {
0035 
0036   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0037   namespace sim   {
0038 
0039     using namespace SomeExperiment;
0040     
0041     // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0042     //               Geant4SensitiveAction<MyTrackerSD>
0043     // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0044     /** \addtogroup Geant4SDActionPlugin
0045      *
0046      * @{
0047      * \package MyTrackerSDAction
0048      * \brief Sensitive detector meant for tracking detectors, will produce one hit per step
0049      *
0050      * @}
0051      */
0052     template <>
0053     Geant4SensitiveAction<MyTrackerSD>::Geant4SensitiveAction(Geant4Context* ctxt,
0054                                                               const std::string& nam,
0055                                                               DetElement det,
0056                                                               Detector& description_ref)
0057       : Geant4Sensitive(ctxt,nam,det,description_ref), m_collectionName(), m_collectionID(0)
0058     {
0059       declareProperty("HaveCellID",     m_userData.haveCellID = true);
0060       declareProperty("ReadoutName",    m_readoutName);
0061       declareProperty("CollectionName", m_collectionName);
0062       initialize();
0063       InstanceCount::increment(this);
0064     }
0065     
0066     /// Define collections created by this sensitivie action object
0067     template <> void Geant4SensitiveAction<MyTrackerSD>::defineCollections()    {
0068       m_collectionID = declareReadoutFilteredCollection<MyTrackerSD::Hit>();
0069     }
0070 
0071     /// Method for generating hit(s) using the information of G4Step object.
0072     template <> bool Geant4SensitiveAction<MyTrackerSD>::process(const G4Step* step,G4TouchableHistory* /*hist*/ ) {
0073       Geant4StepHandler h(step);
0074       Position  prePos    = h.prePos();
0075       Position  postPos   = h.postPos();
0076       Position  direction = postPos - prePos;
0077       Position  pos       = mean_direction(prePos,postPos);
0078       Direction mom       = 0.5 * (h.preMom() + h.postMom());
0079       double    hit_len   = direction.R();
0080       double    depo      = h.deposit();
0081       double    tim       = h.track->GetGlobalTime();
0082       // Somehow extract here the physics you want
0083       MyTrackerSD::Hit* hit = 
0084         new MyTrackerSD::Hit(h.trkID(), h.trkPdgID(), depo, tim, hit_len, pos, mom);
0085       Geant4HitData::MonteCarloContrib contrib = Geant4HitData::extractContribution(step);
0086       hit->cellID        = this->m_userData.haveCellID ? cellID(step) : 0;
0087       hit->step_length   = hit_len;
0088       hit->prePos        = prePos;
0089       hit->postPos       = postPos;
0090       collection(m_collectionID)->add(hit);
0091       mark(h.track);
0092       if ( this->m_userData.haveCellID && 0 == hit->cellID )  {
0093         hit->cellID = volumeID(step);
0094         except("+++ Invalid CELL ID for hit!");
0095       }
0096       printP1("Hit with deposit:%f  Pos:%f %f %f ID=%016X",
0097               depo, pos.X(), pos.Y(), pos.Z(), (void*)hit->cellID);
0098       Geant4TouchableHandler handler(step);
0099       print("    Geant4 path:%s", handler.path().c_str());
0100 
0101       // Do something with my personal data (can be also something more clever ;-):
0102       m_userData.integratedDeposit += contrib.deposit;
0103       ++m_userData.mumDeposits;
0104 
0105       /// Let's play with the Geant4TrackInformation
0106       /// See issue https://github.com/AIDASoft/DD4hep/issues/1073
0107       if ( nullptr == h.track->GetUserInformation() )   {
0108         auto data = std::make_unique<ParticleUserData>();
0109         data->absolute_momentum = h.track->GetMomentum().mag();
0110         h.track->SetUserInformation(new Geant4ParticleInformation(std::move(data)));
0111       }
0112       return true;
0113     }
0114 
0115   }
0116 }
0117 
0118 //--- Factory declaration
0119 namespace dd4hep { namespace sim {
0120     typedef Geant4SensitiveAction<MyTrackerSD> MyTrackerSDAction;
0121   }}
0122 DECLARE_GEANT4SENSITIVE(MyTrackerSDAction)