Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:13

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // Author: Mathieu Karamitros
0027 
0028 // The code is developed in the framework of the ESA AO7146
0029 //
0030 // We would be very happy hearing from you, send us your feedback! :)
0031 //
0032 // In order for Geant4-DNA to be maintained and still open-source,
0033 // article citations are crucial. 
0034 // If you use Geant4-DNA chemistry and you publish papers about your software, 
0035 // in addition to the general paper on Geant4-DNA:
0036 //
0037 // Int. J. Model. Simul. Sci. Comput. 1 (2010) 157–178
0038 //
0039 // we would be very happy if you could please also cite the following
0040 // reference papers on chemistry:
0041 //
0042 // J. Comput. Phys. 274 (2014) 841-882
0043 // Prog. Nucl. Sci. Tec. 2 (2011) 503-508 
0044 
0045 #ifndef G4TRACKSTATE_HH_
0046 #define G4TRACKSTATE_HH_
0047 
0048 #include <map>
0049 #include "G4memory.hh"
0050 
0051 //------------------------------------------------------------------------------
0052 
0053 class G4VTrackStateID
0054 {
0055 protected:
0056   static int fgLastID;
0057   static int Create();
0058   
0059   G4VTrackStateID() {}
0060   virtual ~G4VTrackStateID() = default;
0061 };
0062 
0063 //------------------------------------------------------------------------------
0064 
0065 template<class T>
0066 class G4TrackStateID: public G4VTrackStateID
0067 {
0068 public:
0069   static int GetID() { return fID; }
0070 
0071 private:
0072   static const int fID;
0073   
0074   G4TrackStateID() {}
0075   ~G4TrackStateID() override = default;
0076 };
0077 
0078 template<class T>
0079 const int G4TrackStateID<T>::fID (G4VTrackStateID::Create());
0080 
0081 //------------------------------------------------------------------------------
0082 
0083 class G4VTrackState
0084 {
0085 public:
0086   G4VTrackState() = default;
0087   virtual ~G4VTrackState() = default;
0088   virtual int GetID() = 0;
0089 };
0090 
0091 //------------------------------------------------------------------------------
0092 
0093 using G4VTrackStateHandle = std::shared_ptr<G4VTrackState>;
0094 
0095 //------------------------------------------------------------------------------
0096 //!
0097 template<class T>
0098 class G4TrackStateDependent;
0099 
0100 template<class T>
0101 class G4TrackStateBase : public G4VTrackState
0102 {
0103 public:
0104   ~G4TrackStateBase() override = default;
0105 
0106   int GetID() override {
0107     return G4TrackStateID<T>::GetID();
0108   }
0109 
0110   static int ID() {
0111     return G4TrackStateID<T>::GetID();
0112   }
0113 
0114 protected:
0115   G4TrackStateBase() : G4VTrackState() {}
0116 };
0117 
0118 //------------------------------------------------------------------------------
0119 
0120 template<class T>
0121 class G4TrackState : public G4TrackStateBase<T>
0122 {
0123   /*
0124   // friend T; // works in c++11
0125   */
0126   friend class G4TrackStateDependent<T>; //!
0127 
0128 public:
0129   virtual ~G4TrackState() = default;
0130 
0131   static int ID() {
0132     return G4TrackStateID<T>::GetID();
0133   }
0134 
0135   G4TrackState() : G4TrackStateBase<T>() {}
0136 };
0137 
0138 //------------------------------------------------------------------------------
0139 
0140 
0141 class G4TrackStateManager
0142 {
0143   std::map<int, G4VTrackStateHandle> fTrackStates;
0144   std::map<void*, G4VTrackStateHandle> fMultipleTrackStates;
0145   
0146 public:
0147   
0148   void SetTrackState(void* adress, G4VTrackStateHandle state)
0149   {
0150     fMultipleTrackStates[adress] = state;
0151   }
0152   
0153   G4VTrackStateHandle GetTrackState(void* adress) const
0154   {
0155     auto it =
0156       fMultipleTrackStates.find(adress);
0157     if (it == fMultipleTrackStates.end())
0158     {
0159       return G4VTrackStateHandle();
0160     }
0161     return it->second;
0162   }
0163   
0164   template<class T>
0165   G4VTrackStateHandle GetTrackState(T* adress) const
0166   {
0167     auto it =
0168       fMultipleTrackStates.find((void*)adress);
0169     if (it == fMultipleTrackStates.end())
0170     {
0171       return G4VTrackStateHandle();
0172     }
0173     return it->second;
0174   }
0175 
0176   void SetTrackState(G4VTrackStateHandle state)
0177   {
0178     fTrackStates[state->GetID()] = state;
0179   }
0180 
0181   template<typename T>
0182   G4VTrackStateHandle GetTrackState() const
0183   {
0184     auto it =
0185       fTrackStates.find(G4TrackStateID<T>::GetID());
0186     if (it == fTrackStates.end())
0187     {
0188       return G4VTrackStateHandle();
0189     }
0190     return it->second;
0191   }
0192 };
0193 
0194 //------------------------------------------------------------------------------
0195 
0196 class G4VTrackStateDependent
0197 {
0198 public:
0199   G4VTrackStateDependent() = default;
0200   virtual ~G4VTrackStateDependent() = default;
0201   
0202   virtual void NewTrackState() = 0;
0203   virtual void LoadTrackState(G4TrackStateManager&) = 0;
0204   virtual void SaveTrackState(G4TrackStateManager&) = 0;
0205   virtual G4VTrackStateHandle GetTrackState() const = 0;
0206   virtual G4VTrackStateHandle PopTrackState() = 0;
0207   virtual void ResetTrackState() = 0;
0208 };
0209 
0210 #define G4TrackStateHandle(T) G4shared_ptr<G4TrackState<T> >
0211 
0212 template<class OriginalType>
0213 G4shared_ptr<G4VTrackState>
0214 ConvertToAbstractTrackState(G4shared_ptr<G4TrackState<OriginalType> > state)
0215 {
0216 
0217   G4shared_ptr<G4VTrackState> output =
0218     G4dynamic_pointer_cast<G4VTrackState>(state);
0219   return output;
0220 }
0221 
0222 template<class FinalType>
0223 G4shared_ptr<G4TrackState<FinalType> >
0224 ConvertToConcreteTrackState(G4VTrackStateHandle state)
0225 {
0226 
0227   G4shared_ptr<G4TrackState<FinalType> > output =
0228     G4dynamic_pointer_cast<G4TrackState<FinalType>>(state);
0229   return output;
0230 }
0231 
0232 //------------------------------------------------------------------------------
0233 //!
0234 template<class T>
0235 class G4TrackStateDependent : public G4VTrackStateDependent
0236 {
0237 public:
0238   using ClassType = T;
0239   using StateType = G4TrackState<T>;
0240   using StateTypeHandle = std::shared_ptr<StateType>;
0241 
0242   ~G4TrackStateDependent() override = default;
0243 
0244   virtual void SetTrackState(G4shared_ptr<StateType> state)
0245   {
0246     fpTrackState = state;
0247   }
0248 
0249   G4VTrackStateHandle PopTrackState() override
0250   {
0251     G4VTrackStateHandle output =
0252       G4dynamic_pointer_cast<G4VTrackState>(fpTrackState);
0253     fpTrackState.reset();
0254     return output;
0255   }
0256 
0257   G4VTrackStateHandle GetTrackState() const override
0258   {
0259     G4VTrackStateHandle output =
0260       G4dynamic_pointer_cast<G4VTrackState>(fpTrackState);
0261     return output;
0262   }
0263 
0264   virtual StateTypeHandle GetConcreteTrackState() const
0265   {
0266     return fpTrackState;
0267   }
0268 
0269   void LoadTrackState(G4TrackStateManager& manager) override
0270   {
0271     fpTrackState =
0272       ConvertToConcreteTrackState<ClassType>(manager.GetTrackState(this));
0273     if (fpTrackState == nullptr)
0274     {
0275       NewTrackState();
0276       SaveTrackState(manager);
0277     }
0278   }
0279 
0280   void SaveTrackState(G4TrackStateManager& manager) override
0281   {
0282     manager.SetTrackState(this, ConvertToAbstractTrackState(fpTrackState));
0283   }
0284 
0285   void NewTrackState() override
0286   {
0287     fpTrackState = StateTypeHandle(new StateType());
0288   }
0289 
0290   virtual StateTypeHandle CreateTrackState() const
0291   {
0292     return StateTypeHandle(new StateType());
0293   }
0294 
0295   void ResetTrackState() override
0296   {
0297     fpTrackState.reset();
0298   }
0299 
0300 protected:
0301   G4TrackStateDependent()
0302     : G4VTrackStateDependent()
0303   {}
0304 
0305   StateTypeHandle fpTrackState;
0306 };
0307 
0308 //------------------------------------------------------------------------------
0309 
0310 #if __cplusplus > 199711L
0311 #define RegisterTrackState(CLASS,STATE) \
0312   template<> \
0313   class G4TrackState<CLASS> : public G4TrackStateBase<CLASS>, \
0314   public CLASS::STATE \
0315   { \
0316     friend class G4TrackStateDependent<CLASS>;  \
0317     using CLASS::STATE::STATE; \
0318   public: \
0319     typedef CLASS::STATE State; \
0320     G4TrackState() : G4TrackStateBase<CLASS>(), CLASS::STATE(){}\
0321     virtual ~G4TrackState(){}\
0322     virtual int GetID()\
0323     {\
0324       return  G4TrackStateID<CLASS>::GetID();\
0325     }\
0326     static int ID()\
0327     {\
0328       return  G4TrackStateID<CLASS>::GetID();\
0329     }\
0330   protected:\
0331   };
0332 #else
0333 #define RegisterTrackState(CLASS,STATE) \
0334   template<> \
0335   class G4TrackState<CLASS> : public G4TrackStateBase<CLASS>, \
0336   public CLASS::STATE \
0337   { \
0338     friend class G4TrackStateDependent<CLASS>;  \
0339   public: \
0340     typedef CLASS::STATE State; \
0341     G4TrackState() : G4TrackStateBase<CLASS>(), CLASS::STATE(){}\
0342     virtual ~G4TrackState(){}\
0343     virtual int GetID()\
0344     {\
0345       return  G4TrackStateID<CLASS>::GetID();\
0346     }\
0347     static int ID()\
0348     {\
0349       return  G4TrackStateID<CLASS>::GetID();\
0350     }\
0351     protected:\
0352   };
0353 #endif
0354 
0355 #endif /* G4TRACKSTATE_HH_ */