Minior Changes on new structure, exported elastic cross section to seperate file.

Please enter the commit message for your changes. Lines starting
This commit is contained in:
Christian
2019-05-02 13:29:21 +02:00
parent e07c03c888
commit 649c278084
33 changed files with 631 additions and 595 deletions
+168
View File
@@ -0,0 +1,168 @@
//
// read_system.h
// mc_electron
//
// Created by Christian Reiling on 30.04.19.
// Copyright © 2019 Christian Reiling. All rights reserved.
//
#ifndef read_system_h
#define read_system_h
#include <cmath>
#include <string>
#include <sstream>
#include <fstream>
inline double getParameterValue(std::string lineFromFile) {
std::stringstream ss;
ss.str(lineFromFile);
std::string nameDummy;
double parValue = NAN;
if ( !(ss>>nameDummy && ss>>parValue) ) {
std::stringstream sserr;
sserr<<"isParameterLine: Could not read line.\n";
throw MyException(sserr.str());
}
return (parValue);
}
inline bool isParameterLine(std::string lineFromFile, const std::string &parName) {
std::stringstream ss;
ss.str(lineFromFile);
std::string parNameTemp;
if ( !(ss>>parNameTemp) ) {
std::stringstream sserr;
sserr<<"isParameterLine: Could not read line.\n";
throw MyException(sserr.str());
}
if (parNameTemp == parName) {
return (true);
} else {
return (false);
}
}
inline bool isFaulty(std::string lineFromFile) {
std::stringstream ss;
ss.str(lineFromFile);
std::string nameDummy;
double valueDummy;
if (ss>>nameDummy && ss>>valueDummy) {
return (false);
} else {
return (true);
}
}
inline bool checkIgnoreLine(const std::string &lineFromFile) { // true if line is empty or starts with #
if (lineFromFile[0] =='#') {
return true;
} else if ( lineFromFile.empty() ) {
return true;
} else {
return false;
}
}
inline void replaceDelimiter(std::string &lineFromFile) { // replaces delimiter (';'',''=') with whitespace
replace(lineFromFile.begin(), lineFromFile.end(), ';', ' ');
replace(lineFromFile.begin(), lineFromFile.end(), ',', ' ');
replace(lineFromFile.begin(), lineFromFile.end(), '=', ' ');
}
inline void removeLeadingWhites(std::string &lineFromFile) {
static const std::string WHITESPACE = " \n\r\t\f\v";
size_t start = lineFromFile.find_first_not_of(WHITESPACE);
lineFromFile = (start == std::string::npos) ? "" : lineFromFile.substr(start);
}
inline double findParameter(std::ifstream *fileStream, const std::string &parName) {
std::string lineFromFile;
double parValue = NAN;
while (getline(*fileStream, lineFromFile)) {
removeLeadingWhites(lineFromFile);
replaceDelimiter(lineFromFile);
if ( checkIgnoreLine(lineFromFile) ) {
continue;
}
if ( isFaulty(lineFromFile) ) {
std::cout<<"Line in parameter file faulty.\n\tline:\t"<<lineFromFile<<"\n";
} else if ( isParameterLine(lineFromFile, parName) ) {
parValue = getParameterValue(lineFromFile);
break;
}
}
return (parValue);
}
inline std::string makePath (const std::string &path) { // append '/' if necessary to path.
if (path.back() != '/') {
return (path + '/');
} else {
return (path);
}
}
inline std::string getEnv (const std::string &envName) { // string version of C function getenv.
char * val = getenv( envName.c_str() );
return val == NULL ? std::string("") : std::string(val);
}
inline std::string getParameterLoadFilePath() {
std::string parpath = getEnv("parpath");
if (parpath == "") {
throw MyException("Parameter::getParameter: Environment variable parpath not defined.");
}
std::string parfile = getEnv("parfile");
if (parfile == "") {
throw MyException("Parameter::getParameter: Environment variable parfile not defined.");
}
parpath = makePath(parpath);
return (parpath + parfile);
}
inline double getParameter(const std::string &parName) {
std::string loadFilePath = getParameterLoadFilePath();
std::ifstream fileStream(loadFilePath);
if (!fileStream.good()) {
std::stringstream ss;
ss<<"Parameter::getParameter: Could not open file.\n loadFilePath:\t"<<loadFilePath<<"\n";
throw MyException(ss.str());
}
double parValue = findParameter(&fileStream, parName);
if ( isnan(parValue) ) {
std::stringstream ss;
ss<<"Parameter::getParameter: Parameter not found in file.\n parName:\t"<<parName<<"\n loadFilePath: \t"<<loadFilePath;
throw MyException(ss.str());
} else {
return (parValue);
}
}
inline std::string getTimeString() {
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%Y%m%d",timeinfo);
std::string str(buffer);
return(str);
}
inline std::string getHomebase() {
std::string Homebase = getEnv("HOMEBASE");
Homebase = makePath(Homebase);
return (Homebase);
}
inline std::string getDataPath() {
return (getHomebase() + "src/mc_electron/data/");
}
#endif /* read_system_h */