Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:53

0001 #pragma once
0002 /**
0003 STrackInfo.h (formerly U4TrackInfo.h)
0004 ========================================
0005 
0006 Formerly uses templated STrackInfo but as
0007 doing dynamic cast on such a type is not possible
0008 that is dangerous as must rely on no other track
0009 info subclasses being in use.
0010 
0011 Required methods for spho::
0012 
0013    std::string desc() const ;
0014    spho spho::Placeholder() ;
0015    spho spho::Fabricate(int id) ;
0016 
0017 
0018 Users::
0019 
0020     epsilon:opticks blyth$ opticks-fl STrackInfo
0021     ./sysrap/CMakeLists.txt
0022     ./sysrap/STrackInfo.h
0023     ./sysrap/SFastSimOpticalModel.hh
0024 
0025     ./u4/U4Track.h
0026 
0027     ./u4/tests/U4TrackTest.cc
0028     ./u4/tests/U4TrackInfoTest.cc
0029 
0030     ./u4/U4Recorder.cc
0031          vital part of U4Recorder::PreUserTrackingAction_Optical PostUserTrackingAction_Optical
0032 
0033     ./u4/InstrumentedG4OpBoundaryProcess.cc
0034          not fully impl, seems informational only
0035 
0036     ./u4/U4.cc
0037          setting photon labels at generation
0038 
0039 
0040 ::
0041 
0042     epsilon:PMTFastSim blyth$ grep STrackInfo.h *.*
0043     junoPMTOpticalModel.cc:#include "STrackInfo.h"
0044     junoPMTOpticalModel.rst:* instead replaced with passing the FastSim status via trackinfo with sysrap/STrackInfo.h
0045     junoPMTOpticalModelSimple.cc:#include "STrackInfo.h"
0046     epsilon:PMTFastSim blyth$
0047 
0048 
0049 **/
0050 
0051 #include <string>
0052 #include "G4Track.hh"
0053 #include "G4VUserTrackInformation.hh"
0054 
0055 #include "spho.h"
0056 
0057 struct STrackInfo : public G4VUserTrackInformation
0058 {
0059     spho label  ;
0060 
0061     STrackInfo(const spho& label);
0062     std::string desc() const ;
0063 
0064     static STrackInfo* GetTrackInfo_UNDEFINED(const G4Track* track);
0065     static STrackInfo* GetTrackInfo(          const G4Track* track);
0066     static bool Exists(const G4Track* track);
0067     static spho  Get(   const G4Track* track);   // by value
0068     static spho* GetRef(const G4Track* track);   // by reference, allowing inplace changes
0069     static std::string Desc(const G4Track* track);
0070 
0071     static void Set(G4Track* track, const spho& label );
0072 };
0073 
0074 inline STrackInfo::STrackInfo(const spho& _label )
0075     :
0076     G4VUserTrackInformation("STrackInfo"),
0077     label(_label)
0078 {
0079 }
0080 
0081 inline std::string STrackInfo::desc() const
0082 {
0083     std::stringstream ss ;
0084     ss << *pType << " " << label.desc() ;
0085     std::string s = ss.str();
0086     return s ;
0087 }
0088 
0089 /**
0090 STrackInfo::GetTrackInfo_UNDEFINED
0091 -----------------------------------
0092 
0093 With U4PhotonInfo the ancestor of STrackInfo was using dynamic_cast
0094 without issue. After moving to the templated STrackInfo the
0095 dynamic cast always giving nullptr. So switched to static_cast.
0096 
0097 **/
0098 
0099 inline STrackInfo* STrackInfo::GetTrackInfo_UNDEFINED(const G4Track* track) // static, label by value
0100 {
0101     G4VUserTrackInformation* ui = track->GetUserInformation() ;
0102     STrackInfo* trackinfo = ui ? static_cast<STrackInfo*>(ui) : nullptr ;
0103     return trackinfo ;
0104 }
0105 
0106 inline STrackInfo* STrackInfo::GetTrackInfo(const G4Track* track) // static, label by value
0107 {
0108     G4VUserTrackInformation* ui = track->GetUserInformation() ;
0109     STrackInfo* trackinfo = ui ? dynamic_cast<STrackInfo*>(ui) : nullptr ;
0110     return trackinfo ;
0111 }
0112 
0113 
0114 
0115 
0116 inline bool STrackInfo::Exists(const G4Track* track) // static
0117 {
0118     STrackInfo* trackinfo = GetTrackInfo(track);
0119     return trackinfo != nullptr ;
0120 }
0121 
0122 inline spho STrackInfo::Get(const G4Track* track) // static, label by value
0123 {
0124     STrackInfo* trackinfo = GetTrackInfo(track);
0125     return trackinfo ? trackinfo->label : spho::Placeholder() ;
0126 }
0127 
0128 inline spho* STrackInfo::GetRef(const G4Track* track) // static, label reference
0129 {
0130     STrackInfo* trackinfo = GetTrackInfo(track);
0131     return trackinfo ? &(trackinfo->label) : nullptr ;
0132 }
0133 
0134 inline std::string STrackInfo::Desc(const G4Track* track)
0135 {
0136     G4VUserTrackInformation* ui = track->GetUserInformation() ;
0137 
0138     STrackInfo* trackinfo = GetTrackInfo(track);
0139     STrackInfo* trackinfo_static = GetTrackInfo_UNDEFINED(track);
0140 
0141     std::stringstream ss ;
0142     ss << "STrackInfo::Desc"
0143        << std::endl
0144        << " track " << track
0145        << " track.GetUserInformation " << ui
0146        << std::endl
0147        << " trackinfo " << trackinfo
0148        << " trackinfo_static " << trackinfo_static
0149        << std::endl
0150        << " trackinfo.desc " << ( trackinfo ? trackinfo->desc() : "-" )
0151        << std::endl
0152        << " trackinfo_static.desc " << ( trackinfo_static ? trackinfo_static->desc() : "-" )
0153        ;
0154     std::string s = ss.str();
0155     return s ;
0156 }
0157 
0158 
0159 inline void STrackInfo::Set(G4Track* track, const spho& _label )  // static
0160 {
0161     spho* label = GetRef(track);
0162     if(label == nullptr)
0163     {
0164         track->SetUserInformation(new STrackInfo(_label));
0165     }
0166     else
0167     {
0168         *label = _label ;
0169     }
0170 }
0171