Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:50:31

0001 #pragma once
0002 
0003 #include <string>
0004 #include <sstream>
0005 #include <cassert>
0006 
0007 #include "G4Version.hh"
0008 #include "G4MaterialPropertiesTable.hh"
0009 
0010 #include "U4MaterialPropertyVector.h"
0011 #include "NPFold.h"
0012 
0013 
0014 
0015 struct U4MaterialPropertiesTable
0016 {
0017     static std::string Detail(const G4MaterialPropertiesTable* mpt );  
0018     static std::string DescMaterialPropertyNames(const G4MaterialPropertiesTable* mpt); 
0019     static std::string DescPropertyMap(const G4MaterialPropertiesTable* mpt ); 
0020     static std::string DescConstPropertyMap(const G4MaterialPropertiesTable* mpt ); 
0021 
0022     static std::string Desc(const G4MaterialPropertiesTable* mpt );  
0023     static void GetProperties(std::vector<std::string>& keys, std::vector<G4MaterialPropertyVector*>& props, const G4MaterialPropertiesTable* mpt ); 
0024     static std::string DescProperties(const std::vector<std::string>& keys, const std::vector<G4MaterialPropertyVector*>& props ) ; 
0025 
0026     static NPFold* MakeFold( const G4MaterialPropertiesTable* mpt ); 
0027     static G4MaterialPropertiesTable* Create(std::vector<std::string>& keys, std::vector<double>& vals ); 
0028     static G4MaterialPropertiesTable* Create(const char* key, double val ); 
0029 }; 
0030 
0031 /**
0032 U4MaterialPropertiesTable::DescMaterialPropertyNames
0033 ------------------------------------------------------
0034 
0035 Returns all names, what are these G4 developers smoking ?
0036 
0037 **/
0038 inline std::string U4MaterialPropertiesTable::DescMaterialPropertyNames(const G4MaterialPropertiesTable* mpt )
0039 {
0040     std::vector<G4String> names = mpt->GetMaterialPropertyNames(); 
0041     std::stringstream ss ; 
0042     ss <<  "DescMaterialPropertyNames" ; 
0043     ss << " names " << names.size() ; 
0044     for(unsigned i=0 ; i < names.size() ; i++) ss << names[i] << " "  ; 
0045     std::string s = ss.str(); 
0046     return s ; 
0047 }
0048 
0049 inline std::string U4MaterialPropertiesTable::DescPropertyMap(const G4MaterialPropertiesTable* mpt )
0050 {
0051     std::stringstream ss ; 
0052     ss <<  "DescPropertyMap " ; 
0053 #if G4VERSION_NUMBER < 1100
0054     typedef std::map<G4int, G4MaterialPropertyVector*> MIV ; 
0055     const MIV* miv = mpt->GetPropertyMap(); 
0056     ss << " miv.size " << miv->size() ; 
0057 
0058     std::vector<G4String> names = mpt->GetMaterialPropertyNames(); 
0059 
0060     std::vector<int> ii ; 
0061     std::vector<G4MaterialPropertyVector*> vv ; // HUH: this vector getting overwritten somehow ? 
0062 
0063     const int N = 50 ;  
0064     G4MaterialPropertyVector* qq[N] ;  
0065     for(int i=0 ; i < N ; i++) qq[i] = nullptr ; 
0066 
0067     ss << " v0 [ " ; 
0068     for(MIV::const_iterator iv=miv->begin() ; iv != miv->end() ; iv++) 
0069     {
0070         G4int i = iv->first ;  
0071         G4MaterialPropertyVector* v = iv->second ; 
0072         ii.push_back(i) ;
0073         vv.push_back(v) ;
0074         if( i < N ) qq[i] = iv->second ;  
0075         ss << v << " " ;  
0076     }
0077     ss << "]" ; 
0078 
0079     unsigned nii = ii.size(); 
0080     ss << " i [" ; 
0081     for(unsigned i=0 ; i < nii ; i++ ) ss << ii[i] << ( i < nii - 1 ? " " : "" ) ; 
0082     ss << "]" ; 
0083 
0084     ss << " n [" ; 
0085     for(unsigned i=0 ; i < nii ; i++ ) ss << names[ii[i]] << ( i < nii - 1 ? " " : "" ) ; 
0086     ss << "]" ; 
0087 
0088     ss << " v [" ; 
0089     for(unsigned i=0 ; i < nii ; i++ ) 
0090     {
0091         int idx = ii[i] ; 
0092         G4MaterialPropertyVector* q = idx < N ? qq[idx] : nullptr  ; 
0093         //G4MaterialPropertyVector* v = vv[idx] ; 
0094         ss <<  q  << ( i < nii - 1 ? " " : "" ) ;  
0095     }
0096 
0097     ss << " vl [" ; 
0098     for(unsigned i=0 ; i < nii ; i++ ) 
0099     {
0100         int idx = ii[i] ; 
0101         //G4MaterialPropertyVector* v = vv[idx] ; 
0102         G4MaterialPropertyVector* q = idx < N ? qq[idx] : nullptr  ; 
0103         size_t vl = q ? q->GetVectorLength() : 0 ; 
0104         ss <<  vl  << ( i < nii - 1 ? " " : "" ) ;  
0105     }
0106     ss << "]" ; 
0107 #else
0108     ss << " NOT IMPLEMENTED YET FOR G4VERSION 1100+ " ; 
0109 #endif
0110     std::string s = ss.str(); 
0111     return s ; 
0112 }
0113 inline std::string U4MaterialPropertiesTable::DescConstPropertyMap(const G4MaterialPropertiesTable* mpt )
0114 {
0115     std::stringstream ss ; 
0116     ss <<  "DescConstPropertyMap " ; 
0117 #if G4VERSION_NUMBER < 1100
0118     typedef std::map<G4int, G4double> MIF ; 
0119     const MIF* mif = mpt->GetConstPropertyMap() ; 
0120     ss << " mif.size " << mif->size() ; 
0121 #else
0122     ss << " NOT IMPLEMENTED YET FOR G4VERSION 1100+ " ; 
0123 #endif
0124     std::string s = ss.str(); 
0125     return s ; 
0126 }    
0127 
0128 inline std::string U4MaterialPropertiesTable::Detail(const G4MaterialPropertiesTable* mpt )
0129 {
0130     std::stringstream ss ; 
0131     ss << "U4MaterialPropertiesTable::Desc" << std::endl ; 
0132     //ss << DescMaterialPropertyNames(mpt) << std::endl ; 
0133     ss << DescPropertyMap(mpt) << std::endl ; 
0134     ss << DescConstPropertyMap(mpt) << std::endl ; 
0135     std::string s = ss.str(); 
0136     return s ; 
0137 }
0138 
0139 
0140 
0141 
0142 
0143 inline std::string U4MaterialPropertiesTable::Desc(const G4MaterialPropertiesTable* mpt )
0144 {
0145     std::vector<std::string> keys ; 
0146     std::vector<G4MaterialPropertyVector*> props ; 
0147     GetProperties(keys, props, mpt); 
0148     return DescProperties(keys, props) ;  
0149 }
0150 
0151 
0152 /**
0153 U4MaterialPropertiesTable::GetProperties
0154 ------------------------------------------
0155 
0156 This aims to provide an API that does not change with Geant4 version. 
0157 
0158 Geant4 reference:
0159 
0160 * https://geant4.kek.jp/lxr/source/materials/include/G4MaterialPropertiesTable.hh
0161 * https://geant4.kek.jp/lxr/source/materials/src/G4MaterialPropertiesTable.cc
0162 
0163 **/
0164 
0165 inline void U4MaterialPropertiesTable::GetProperties(
0166       std::vector<std::string>& keys, 
0167       std::vector<G4MaterialPropertyVector*>& props, 
0168       const G4MaterialPropertiesTable* mpt )
0169 {
0170     std::vector<G4String> names = mpt->GetMaterialPropertyNames(); 
0171 
0172 #if G4VERSION_NUMBER < 1100
0173     typedef std::map<G4int, G4MaterialPropertyVector*> MIV ; 
0174     const MIV* miv = mpt->GetPropertyMap();   // <-- METHOD REMOVED IN G4 1100 
0175 
0176     for(MIV::const_iterator iv=miv->begin() ; iv != miv->end() ; iv++) 
0177     {
0178         G4int i = iv->first ;  
0179         G4MaterialPropertyVector* v = iv->second ; 
0180         const char* key = names[i].c_str();   
0181 
0182         keys.push_back(key); 
0183         props.push_back(v) ; 
0184     }
0185 #else
0186     for(unsigned i = 0 ; i < names.size() ; i++)
0187     {
0188         const char* key = names[i].c_str() ; 
0189         G4MaterialPropertyVector* v = mpt->GetProperty(key) ; 
0190         if( v != nullptr ) 
0191         {
0192       keys.push_back(key);
0193       props.push_back(v);  
0194         }
0195     }
0196     // My reading of code suggests that the vector obtained from "props = mpt->GetProperties();"  
0197     // will usually have lots of nullptr and will have a different length to the names vector
0198     // unless absolutely all the properties are defined. 
0199     // That is different behaviour to < 1100  above, so the 
0200     // existance of properties is checked before adding to the vectors.
0201 #endif
0202     assert( props.size() == keys.size() ); 
0203 
0204 }
0205 
0206 inline std::string U4MaterialPropertiesTable::DescProperties(
0207     const std::vector<std::string>& keys, 
0208     const std::vector<G4MaterialPropertyVector*>& props ) 
0209 {
0210     assert( keys.size() == props.size() ); 
0211     unsigned num_prop = keys.size(); 
0212 
0213     std::stringstream ss ; 
0214 
0215     ss << "U4MaterialPropertiesTable::DescProperties" 
0216        << " num_prop " << num_prop
0217        << std::endl 
0218        ; 
0219 
0220     for(unsigned i=0 ; i < num_prop ; i++) 
0221     {
0222         const char* k = keys[i].c_str() ; 
0223         const G4MaterialPropertyVector* v = props[i] ; 
0224         ss
0225             << " i " << std::setw(2) << i 
0226             << " k " << std::setw(20) << ( k ? k : "-" )
0227             << " v " << std::setw(5) << ( v ? v->GetVectorLength() : -1 ) 
0228             << std::endl 
0229             ; 
0230     }
0231     std::string str = ss.str(); 
0232     return str ; 
0233 }
0234 
0235 inline NPFold* U4MaterialPropertiesTable::MakeFold( const G4MaterialPropertiesTable* mpt ) // static 
0236 {
0237     std::vector<std::string> keys ; 
0238     std::vector<G4MaterialPropertyVector*> props ; 
0239 
0240     GetProperties(keys, props, mpt); 
0241 
0242     assert( keys.size() == props.size() ); 
0243     unsigned num_prop = props.size() ; 
0244 
0245     NPFold* fold = new NPFold ; 
0246     for(unsigned i=0 ; i < num_prop ; i++)
0247     {
0248         const char* k = keys[i].c_str() ;
0249         const G4MaterialPropertyVector* v = props[i] ;
0250         NP* a = U4MaterialPropertyVector::ConvertToArray( v ); 
0251 
0252         fold->add( k, a ); 
0253     }
0254     return fold ; 
0255 }
0256 
0257 inline G4MaterialPropertiesTable* U4MaterialPropertiesTable::Create( 
0258     std::vector<std::string>& keys, 
0259     std::vector<double>& vals )
0260 {
0261     G4MaterialPropertiesTable* mpt = new G4MaterialPropertiesTable ; 
0262     assert( keys.size() == vals.size() ) ; 
0263     int num = int(keys.size());  
0264     for(int i=0 ; i < num ; i++)
0265     {
0266         const char* key = keys[i].c_str(); 
0267         double val = vals[i] ; 
0268         G4MaterialPropertyVector* opv = U4MaterialPropertyVector::Make_V(val) ;   
0269         mpt->AddProperty(key, opv ); 
0270     }
0271     return mpt ; 
0272 }
0273 
0274 inline G4MaterialPropertiesTable* U4MaterialPropertiesTable::Create(const char* key, double val )
0275 {
0276     G4MaterialPropertiesTable* mpt = new G4MaterialPropertiesTable ; 
0277     G4MaterialPropertyVector* opv = U4MaterialPropertyVector::Make_V(val) ;   
0278     mpt->AddProperty(key, opv ); 
0279     return mpt ; 
0280 }
0281 
0282