Back to home page

EIC code displayed by LXR

 
 

    


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

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 /** \addtogroup Geant4GeneratorAction
0015  *
0016  @{
0017    \package Geant4InputAction
0018  * \brief Basic geant4 event reader class. This interface/base-class must be implemented by concrete readers.
0019  *
0020  *
0021 @}
0022  */
0023 
0024 #ifndef DDG4_GEANT4INPUTACTION_H
0025 #define DDG4_GEANT4INPUTACTION_H
0026 
0027 // Framework include files
0028 #include <DDG4/Geant4Vertex.h>
0029 #include <DDG4/Geant4Particle.h>
0030 #include <DDG4/Geant4GeneratorAction.h>
0031 #include <Parsers/Parsers.h>
0032 
0033 // C/C++ include files
0034 #include <memory>
0035 #include <set>
0036 #include <vector>
0037 
0038 // Forward declarations
0039 class G4Event;
0040 class G4Run;
0041 
0042 /// Namespace for the AIDA detector description toolkit
0043 namespace dd4hep  {
0044 
0045   /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
0046   namespace sim  {
0047     
0048     class Geant4InputAction;
0049 
0050     /// Basic geant4 event reader class. This interface/base-class must be implemented by concrete readers.
0051     /**
0052      * Base class to read input files containing simulation data.
0053      *
0054      *  \author  P.Kostka (main author)
0055      *  \author  M.Frank  (code reshuffeling into new DDG4 scheme)
0056      *  \author  R.Ete    (added context from input action)
0057      *  \version 1.0
0058      *  \ingroup DD4HEP_SIMULATION
0059      */
0060     class Geant4EventReader  {
0061 
0062     public:
0063       typedef Geant4Vertex   Vertex;
0064       typedef Geant4Particle Particle;
0065       typedef std::vector<Particle*> Particles;
0066       typedef std::vector<Vertex*> Vertices;
0067       /// Status codes of the event reader object. Anything with NOT low-bit set is an error.
0068       enum EventReaderStatus {
0069         EVENT_READER_ERROR=0,
0070         EVENT_READER_OK=1,
0071         EVENT_READER_NO_DIRECT=2,
0072         EVENT_READER_NO_PRIMARIES=4,
0073         EVENT_READER_NO_FACTORY=6,
0074         EVENT_READER_IO_ERROR=8,
0075         EVENT_READER_EOF=10
0076       };
0077     protected:
0078       /// File name to be opened and read
0079       std::string m_name;
0080       /// Flag if direct event access is supported. To be explicitly set by subclass constructors
0081       bool m_directAccess  { false };
0082       /// Current event number
0083       int  m_currEvent     { 0 };
0084       /// The input action context
0085       Geant4InputAction *m_inputAction   { nullptr };
0086 
0087       /// transform the string parameter value into the type of parameter
0088       /**
0089        * removes parameter from the parameters map
0090        */
0091       template <typename T>
0092       void _getParameterValue( std::map< std::string, std::string > & parameters,
0093                                std::string const& parameterName,
0094                                T& parameter, T defaultValue ) {
0095 
0096         if( parameters.find( parameterName ) != parameters.end() ) {
0097           dd4hep::Parsers::parse( parameter, parameters.at( parameterName ) );
0098           parameters.erase( parameterName );
0099         } else {
0100           parameter = std::move(defaultValue);
0101         }
0102       }
0103 
0104     public:
0105       /// Initializing constructor
0106       Geant4EventReader(const std::string& nam);
0107       /// Default destructor
0108       virtual ~Geant4EventReader();
0109       /// Get the context (from the input action)
0110       Geant4Context* context() const;
0111       /// Set the input action
0112       void setInputAction(Geant4InputAction* action);
0113       /// File name
0114       const std::string& name()  const   {  return m_name;         }
0115       /// Flag if direct event access (by event sequence number) is supported (Default: false)
0116       bool hasDirectAccess() const       {  return m_directAccess; }
0117       /// return current Event Number
0118       int currentEventNumber() const     {  return m_currEvent;    }
0119       /// Move to the indicated event number.
0120       /** For pure sequential access, the default implementation
0121        *  will skip events one by one.
0122        *  For technologies supporting direct event access the default
0123        *  implementation will be empty.
0124        *
0125        *  @return
0126        */
0127       virtual EventReaderStatus moveToEvent(int event_number);
0128       /// Skip event. To be implemented for sequential sources
0129       virtual EventReaderStatus skipEvent();
0130       /// Read an event and fill a vector of MCParticles.
0131       /** The additional argument
0132        */
0133       virtual EventReaderStatus readParticles(int event_number, 
0134                                               Vertices&  vertices,
0135                                               Particles& particles) = 0;
0136 
0137       /// pass parameters to the event reader object
0138       virtual EventReaderStatus setParameters( std::map< std::string, std::string > & ) {return EVENT_READER_OK; }
0139 
0140       /// make sure that all parameters have been processed, otherwise throw exceptions
0141       virtual void checkParameters( std::map< std::string, std::string >& );
0142 
0143       /// Register Run Parameters
0144       virtual void registerRunParameters() {}
0145     };
0146 
0147     /// Generic input action capable of using the Geant4EventReader class.
0148     /**
0149      * Concrete implementation of the Geant4 generator action base class
0150      * populating Geant4 primaries from Geant4 and HepStd files.
0151      *
0152      *  \author  P.Kostka (main author)
0153      *  \author  M.Frank  (code reshuffeling into new DDG4 scheme)
0154      *  \version 1.0
0155      *  \ingroup DD4HEP_SIMULATION
0156      */
0157     class Geant4InputAction : public Geant4GeneratorAction {
0158 
0159     public:
0160       typedef Geant4Vertex   Vertex;
0161       typedef Geant4Particle Particle;
0162       typedef std::vector<Particle*> Particles;
0163       typedef std::vector<Vertex*> Vertices;
0164     protected:
0165       /// Property: input file
0166       std::string         m_input;
0167       /// Property: SYNCEVT
0168       int                 m_firstEvent;
0169       /// Property; interaction mask
0170       int                 m_mask;
0171       /// Property: Momentum downscaler for debugging
0172       double              m_momScale;
0173       /// Event reader object
0174       Geant4EventReader*  m_reader;
0175       /// current event number without initially skipped events
0176       int m_currentEventNumber;
0177       /// Flag to call abortEvent in case of failure (default: true)
0178       bool m_abort;
0179       /// Property: named parameters to configure file readers or input actions
0180       std::map< std::string, std::string> m_parameters;
0181 
0182       /// Property: set of alternative decay statuses that MC generators might use for unstable particles
0183       std::set<int> m_alternativeDecayStatuses = {};
0184 
0185       /// Perform some actions before the run starts, like opening the event inputs
0186       void beginRun(const G4Run*);
0187 
0188       /// Create the input reader
0189       void createReader();
0190     public:
0191       /// Read an event and return a LCCollectionVec of MCParticles.
0192       int readParticles(int event_number,
0193                         Vertices&  vertices,
0194                         Particles& particles);
0195       using PropertyMask = dd4hep::detail::ReferenceBitMask<int>;
0196       /// Convert the generator status into a common set of generator status bits
0197       void setGeneratorStatus(int generatorStatus, PropertyMask& status);
0198 
0199       /// helper to report Geant4 exceptions
0200       std::string issue(int i) const;
0201 
0202       /// Standard constructor
0203       Geant4InputAction(Geant4Context* context, const std::string& name);
0204       /// Default destructor
0205       virtual ~Geant4InputAction();
0206       /// Create particle vector
0207       Particles* new_particles() const { return new Particles; }
0208       /// Callback to generate primary particles
0209       virtual void operator()(G4Event* event)   override;
0210     };
0211   }     /* End namespace sim   */
0212 }       /* End namespace dd4hep */
0213 #endif // DDG4_GEANT4INPUTACTION_H