Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4EnvironmentUtils.hh File Reference
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <sstream>
#include <string>
#include "G4Exception.hh"
#include "G4ExceptionSeverity.hh"
#include "G4String.hh"
#include "G4ios.hh"

Go to the source code of this file.

Classes

class  G4EnvSettings
 

Functions

template<typename _Tp >
_Tp G4GetEnv (const std::string &env_id, _Tp _default=_Tp())
 
template<>
G4bool G4GetEnv (const std::string &env_id, bool _default)
 
template<typename _Tp >
_Tp G4GetEnv (const std::string &env_id, _Tp _default, const std::string &msg)
 
G4String G4GetDataEnv (const std::string &env_id, const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
 
const char * G4FindDataDir (const char *)
 
void G4PrintEnv (std::ostream &os=G4cout)
 

Function Documentation

◆ G4FindDataDir()

const char * G4FindDataDir ( const char * name)

Definition at line 94 of file G4FindDataDir.cc.

95{
96#if defined(G4MULTITHREADED)
97 static std::mutex mutex;
98 std::lock_guard<std::mutex> lock(mutex);
99#endif
100
101 /* If environment variable is set for this dataset, use it */
102 if (const char *datadir = std::getenv(name))
103 return datadir;
104
105 /* If we know which directory/version to search for, try to find it */
106 if (const char *dataset = G4GetDataDir(name)) {
107 /* If GEANT4_DATA_DIR environment variable is set, use it and don't search further */
108 if (const char *basedir = std::getenv("GEANT4_DATA_DIR")) {
109 if (is_directory(basedir)) return G4FindDataDir(name, basedir, dataset);
110
111 G4Exception("G4FindDataDir", "Invalid GEANT4_DATA_DIR", JustWarning, "The GEANT4_DATA_DIR environment variable points to an invalid directory.\n"
112 "Will try fallback locations now. Correct the variable to disable this behaviour.");
113 }
114
115 /* If GEANT4_DATA_DIR environment variable is not set, search in default system paths */
116 for (const auto prefix : system_paths)
117 if (const auto datadir = G4FindDataDir(name, prefix, dataset))
118 return datadir;
119 }
120
121 return nullptr;
122}
const char * G4FindDataDir(const char *)
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)

Referenced by G4LENDBertiniGammaElectroNuclearBuilder::Build(), G4NeutronHPCapture::BuildPhysicsTable(), G4ParticleHPElastic::BuildPhysicsTable(), G4ParticleHPFission::BuildPhysicsTable(), G4ParticleHPJENDLHEData::BuildPhysicsTable(), G4ParticleHPThermalScatteringData::BuildPhysicsTable(), G4VLEPTSModel::BuildPhysicsTable(), G4IonStoppingData::BuildPhysicsVector(), G4IonStoppingData::BuildPhysicsVector(), G4INCL::NNbarToAnnihilationChannel::fillFinalState(), G4ecpssrBaseKxsModel::G4ecpssrBaseKxsModel(), G4ecpssrBaseLixsModel::G4ecpssrBaseLixsModel(), G4FindDataDir(), G4FissLib::G4FissLib(), G4LevelReader::G4LevelReader(), G4ParticleHPInelasticData::G4ParticleHPInelasticData(), G4PhotoElectricAngularGeneratorPolarized::G4PhotoElectricAngularGeneratorPolarized(), G4RadioactiveDecay::G4RadioactiveDecay(), G4NuclideTable::GenerateNuclide(), G4BoldyshevTripletModel::Initialise(), G4DNABornExcitationModel2::Initialise(), G4DNABornIonisationModel1::Initialise(), G4DNABornIonisationModel2::Initialise(), G4DNAChampionElasticModel::Initialise(), G4DNACPA100ElasticModel::Initialise(), G4DNACPA100ExcitationModel::Initialise(), G4DNACPA100IonisationModel::Initialise(), G4DNAELSEPAElasticModel::Initialise(), G4DNAEmfietzoglouIonisationModel::Initialise(), G4DNAIonElasticModel::Initialise(), G4DNARelativisticIonisationModel::Initialise(), G4DNASancheExcitationModel::Initialise(), G4JAEAElasticScatteringModel::Initialise(), G4JAEAPolarizedElasticScatteringModel::Initialise(), G4LivermoreBremsstrahlungModel::Initialise(), G4LivermoreNuclearGammaConversionModel::Initialise(), G4LivermorePolarizedComptonModel::Initialise(), G4LivermorePolarizedGammaConversionModel::Initialise(), G4LivermorePolarizedRayleighModel::Initialise(), G4LowEPComptonModel::Initialise(), G4LowEPPolarizedComptonModel::Initialise(), G4MicroElecElasticModel::Initialise(), G4MicroElecElasticModel_new::Initialise(), G4MicroElecInelasticModel::Initialise(), G4MicroElecInelasticModel_new::Initialise(), G4ANuElNucleusCcModel::InitialiseModel(), G4ANuElNucleusNcModel::InitialiseModel(), G4ANuMuNucleusCcModel::InitialiseModel(), G4ANuMuNucleusNcModel::InitialiseModel(), G4ANuTauNucleusCcModel::InitialiseModel(), G4ANuTauNucleusNcModel::InitialiseModel(), G4NuElNucleusCcModel::InitialiseModel(), G4NuElNucleusNcModel::InitialiseModel(), G4NuMuNucleusCcModel::InitialiseModel(), G4NuMuNucleusNcModel::InitialiseModel(), G4NuTauNucleusCcModel::InitialiseModel(), G4NuTauNucleusNcModel::InitialiseModel(), G4INCL::HFB::initialize(), G4DNARelativisticIonisationModel::LoadAtomicStates(), G4AugerData::LoadData(), G4FluoData::LoadData(), G4ShellData::LoadData(), G4VCrossSectionHandler::LoadData(), G4VCrossSectionHandler::LoadNonLogData(), G4FissionProductYieldDist::MakeDirectoryName(), G4INCL::PbarAtrestEntryChannel::makeMesonStar(), G4INCL::INCL::processEvent(), G4OpticalSurface::ReadCompressedFile(), G4AblaDataFile::readData(), G4OpticalSurface::ReadDichroicFile(), G4DNACPA100IonisationModel::ReadDiffCSFile(), and G4MicroElecMaterialStructure::ReadMaterialFile().

◆ G4GetDataEnv()

G4String G4GetDataEnv ( const std::string & env_id,
const char * originOfException,
const char * exceptionCode,
G4ExceptionSeverity severity,
const char * description )
inline

Definition at line 198 of file G4EnvironmentUtils.hh.

203{
204 char* env_var = std::getenv(env_id.c_str());
205 if(env_var != nullptr)
206 {
207 std::string str_var = std::string(env_var);
208 std::istringstream iss(str_var);
209 G4String var = "";
210 iss >> var;
211 // record value defined by environment
213 return var;
214 }
215
216 // issue an exception
217 G4Exception(originOfException, exceptionCode, severity, description);
218
219 // return default initialized
220 return "";
221}
void insert(const std::string &env_id, _Tp val)
static G4EnvSettings * GetInstance()

◆ G4GetEnv() [1/3]

template<typename _Tp >
_Tp G4GetEnv ( const std::string & env_id,
_Tp _default,
const std::string & msg )

Definition at line 167 of file G4EnvironmentUtils.hh.

168{
169 char* env_var = std::getenv(env_id.c_str());
170 if(env_var)
171 {
172 std::string str_var = std::string(env_var);
173 std::istringstream iss(str_var);
174 _Tp var = _Tp();
175 iss >> var;
176 G4cout << "Environment variable \"" << env_id << "\" enabled with "
177 << "value == " << var << ". " << msg << G4endl;
178 // record value defined by environment
179 G4EnvSettings::GetInstance()->insert<_Tp>(env_id, var);
180 return var;
181 }
182 // record default value
183 G4EnvSettings::GetInstance()->insert<_Tp>(env_id, _default);
184
185 // return default if not specified in environment
186 return _default;
187}
#define G4endl
Definition G4ios.hh:67
G4GLOB_DLL std::ostream G4cout

◆ G4GetEnv() [2/3]

template<typename _Tp >
_Tp G4GetEnv ( const std::string & env_id,
_Tp _default = _Tp() )

Definition at line 116 of file G4EnvironmentUtils.hh.

117{
118 char* env_var = std::getenv(env_id.c_str());
119 if(env_var)
120 {
121 std::string str_var = std::string(env_var);
122 std::istringstream iss(str_var);
123 _Tp var = _Tp();
124 iss >> var;
125 // record value defined by environment
126 G4EnvSettings::GetInstance()->insert<_Tp>(env_id, var);
127 return var;
128 }
129 // record default value
130 G4EnvSettings::GetInstance()->insert<_Tp>(env_id, _default);
131
132 // return default if not specified in environment
133 return _default;
134}

Referenced by G4TaskRunManager::ComputeNumberOfTasks(), G4RunManagerFactory::CreateRunManager(), G4RunManagerKernel::G4RunManagerKernel(), G4RunManagerKernel::G4RunManagerKernel(), G4TaskRunManager::G4TaskRunManager(), and G4GDMLRead::Read().

◆ G4GetEnv() [3/3]

template<>
G4bool G4GetEnv ( const std::string & env_id,
bool _default )
inline

Definition at line 143 of file G4EnvironmentUtils.hh.

144{
145 char* env_var = std::getenv(env_id.c_str());
146 if(env_var != nullptr)
147 {
148 // record value defined by environment
149 G4EnvSettings::GetInstance()->insert<bool>(env_id, true);
150 return true;
151 }
152 // record default value
153 G4EnvSettings::GetInstance()->insert<bool>(env_id, false);
154
155 // return default if not specified in environment
156 return _default;
157}

◆ G4PrintEnv()

void G4PrintEnv ( std::ostream & os = G4cout)
inline

Definition at line 228 of file G4EnvironmentUtils.hh.

229{
231}