6 #include <momemta/SLHAReader.h> 10 std::vector<std::string> parse_line_elements(
const std::string& line,
char delimiter=
' ') {
11 std::vector<std::string> elements;
13 std::stringstream linestr(line);
14 while (getline(linestr, temp, delimiter)) {
15 if (temp == std::string(&delimiter, 1))
17 elements.push_back(temp);
23 using std::runtime_error::runtime_error;
26 Block::Block(
const std::string& name ) { _name = name; }
28 void Block::set_name(
const std::string& name) { _name = name; }
30 std::string Block::get_name()
const {
return _name; }
32 int Block::get_indices()
const {
return _indices; }
34 void Block::set_entry(
const std::vector<int>& indices,
double value) {
36 _indices = indices.size();
37 else if (indices.size() != _indices)
40 _entries[indices] = value;
43 double Block::get_entry(
const std::vector<int>& indices,
double def_val)
const {
44 auto it = _entries.find(indices);
45 if (it == _entries.end()) {
52 Reader::Reader(
const std::string& file_name ) {
53 if (!file_name.empty())
54 read_slha_file(file_name);
57 void Reader::read_slha_file(
const std::string& file_name) {
58 std::ifstream param_card(file_name.c_str(), std::ifstream::in);
59 if (!param_card.is_open())
62 std::string block(
"");
66 while (std::getline(param_card, line)) {
68 std::transform(line.begin(), line.end(), line.begin(), ::tolower);
70 line = std::regex_replace(line, std::regex(
"#.*"),
"");
72 line = std::regex_replace(line, std::regex(
"^ *| *$"),
"");
73 line = std::regex_replace(line, std::regex(
" +"),
" ");
79 if (line.find(
"block ") != line.npos) {
80 line = line.substr(6);
82 size_t space_pos = line.find(
' ');
83 if (space_pos != line.npos)
84 line = line.substr(0, space_pos);
89 if (line.find(
"decay ") == 0) {
90 line = line.substr(6);
92 auto elements = parse_line_elements(line);
93 if (elements.size() == 2) {
94 int pdg_id = std::stoi(elements[0]);
95 double value = std::stod(elements[1]);
96 set_block_entry(
"decay", pdg_id, value);
104 auto elements = parse_line_elements(line);
106 if (elements.size() == 3) {
107 int id1 = std::stoi(elements[0]);
108 int id2 = std::stoi(elements[1]);
109 double value = std::stod(elements[2]);
110 set_block_entry(block, { id1, id2 }, value);
113 }
else if (elements.size() == 2) {
115 int id = std::stoi(elements[0]);
116 double value = std::stod(elements[1]);
117 set_block_entry(block, {
id }, value);
121 throw invalid_card_error(
"Wrong format for entry in block " + block +
"; line: " + line);
126 if (_blocks.size() == 0)
130 }
catch(std::exception &e) {
138 double Reader::get_block_entry(
const std::string& block_name,
const std::vector<int>& indices,
double def_val)
const {
139 auto it = _blocks.find(block_name);
140 if (it == _blocks.end()) {
144 return it->second.get_entry(indices, def_val);
147 double Reader::get_block_entry(
const std::string& block_name,
int index,
double def_val)
const {
148 std::vector<int> indices { index };
149 return get_block_entry(block_name, indices, def_val);
152 void Reader::set_block_entry(
const std::string& block_name,
const std::vector<int>& indices,
double value) {
153 auto it = _blocks.find(block_name);
154 if (it == _blocks.end()) {
155 Block block(block_name);
156 _blocks[block_name] = block;
157 it = _blocks.find(block_name);
159 it->second.set_entry(indices, value);
162 void Reader::set_block_entry(
const std::string& block_name,
int index,
double value) {
163 std::vector<int> indices { index };
164 set_block_entry(block_name, indices, value);