#include "MODEL/UFO/UFO_Param_Reader.H" #include "ATOOLS/Org/Data_Reader.H" #include "ATOOLS/Org/Exception.H" #include "ATOOLS/Org/Run_Parameter.H" #include "ATOOLS/Org/Scoped_Settings.H" #include #include #include #include using namespace UFO; using std::string; using std::vector; using std::stringstream; using ATOOLS::ToType; using ATOOLS::Data_Reader; using ATOOLS::Settings; using ATOOLS::rpa; UFO_Param_Reader::UFO_Param_Reader(const string& filepath) { // either read from UFO param card or from Settings Data_Reader reader(" ", ";", "#", "="); reader.AddWordSeparator("\t"); reader.AddLineSeparator("\n"); reader.SetIgnoreCase(true); if (filepath != "") { string filename(""), path(""); size_t pos(filepath.find_last_of("/")); if (pos!=string::npos){ path=filepath.substr(0,pos+1); filename=filepath.substr(pos+1); } else{ path=string(""); filename=filepath; } reader.SetInputPath(path); reader.SetInputFile(filename); reader.MatrixFromFile(m_lines); for (auto& line : m_lines) for (auto& word : line) Settings::GetMainSettings().ReplaceTags(word); } else { Settings& s = Settings::GetMainSettings(); reader.SetString(s["UFO_PARAMS"].SetDefault("").Get(), true); reader.MatrixFromString(m_lines); } } template Read_Type UFO_Param_Reader::GetEntry(const string& block, const unsigned int& n, const unsigned int& m, const Read_Type& def, const bool& err) { vector< vector >::const_iterator line = FindBlock(block); for(++line; line!=m_lines.end(); ++line){ if (line->empty()) continue; if (IgnoreCaseCompare((*line)[0],"block")) return NotFound(block,n,m,def,err); if (line->size() < 3) continue; if (ToType((*line)[0])==n && ToType((*line)[1])==m) return ToType((*line)[2]); } return NotFound(block,n,m,def,err); } template Read_Type UFO_Param_Reader::GetEntry(const string& block, const unsigned int& n, const Read_Type& def, const bool& err) { // widths in UFO param cards are handled differently for some reason if (IgnoreCaseCompare(block, "decay")) return GetWidth(n,def,err); vector< vector >::const_iterator line = FindBlock(block); for(++line; line!=m_lines.end(); ++line){ if (line->empty()) continue; if (IgnoreCaseCompare((*line)[0],"block")) return NotFound(block,n,def,err); if (line->size() < 2) continue; if (ToType((*line)[0]) == n) return ToType((*line)[1]); } return NotFound(block,n,def,err); } template Read_Type UFO_Param_Reader::GetWidth(const unsigned int& n, const Read_Type& def, const bool& err) { for(vector< vector >::const_iterator line = m_lines.begin(); line != m_lines.end(); ++line){ if (line->size() < 3) continue; if (IgnoreCaseCompare((*line)[0],"decay") && ToType((*line)[1]) == n ) return ToType((*line)[2]); } return NotFound(string("decay"),n,def,err); } vector< vector >::const_iterator UFO_Param_Reader::FindBlock(const string& block){ vector< vector >::const_iterator ret=m_lines.begin(); for(; ret!=m_lines.end(); ++ret){ if(ret->size()<2) continue; if(IgnoreCaseCompare((*ret)[1],block)) return ret; } THROW(fatal_error, "Block "+block+" not found"); // avoid compiler warnings concerning missing return statement return m_lines.end(); } bool UFO_Param_Reader::IgnoreCaseCompare(const std::string& a, const std::string& b){ if (a.size() != b.size())return false; for (string::const_iterator ia = a.begin(), ib = b.begin(); ia!=a.end(); ++ia, ++ib) if (tolower(*ia) != tolower(*ib)) return false; return true; } template Read_Type UFO_Param_Reader::NotFound(const string &block, const unsigned int& n, const unsigned int& m, const Read_Type& def, const bool& err) { stringstream message; message << ("Entry [") << n << "," << m << "] " << "in block " << block << " not found."; if (err) { THROW(fatal_error, message.str().c_str() ); } else msg_Error()< Read_Type UFO_Param_Reader::NotFound(const string &block, const unsigned int& n, const Read_Type& def, const bool& err) { stringstream message; message << ("Entry [") << n << "] " << "in block " << block << " not found."; if (err) { THROW(fatal_error, message.str().c_str() ); } else msg_Error()<