gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
slhaea_helpers.cpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
24 
26 #include "gambit/Utils/version.hpp"
28 
29 namespace Gambit
30 {
33  {
34  SLHAstruct slhaea;
35  std::ifstream ifs(slha.c_str());
36  if (!ifs.good())
37  {
38  std::ostringstream err;
39  err << "ERROR: SLHA file " << slha << " not found.";
40  utils_error().raise(LOCAL_INFO,err.str());
41  }
42  ifs >> slhaea;
43  ifs.close();
44  return slhaea;
45  }
46 
48  double SLHAea_get(const SLHAstruct& slha, const str& block, const int index)
49  {
50  double output = 0.0;
51  try
52  {
53  output = SLHAea::to<double>(slha.at(block).at(index).at(1));
54  }
55  catch (const std::out_of_range& e)
56  {
57  std::ostringstream errmsg;
58  errmsg << "Error accessing data at index " << index << " of block " << block
59  << ". Please check that the SLHAea object was properly filled." << std::endl
60  << "(Received out_of_range error from SLHAea class with message: " << e.what() << ")";
61  utils_error().raise(LOCAL_INFO,errmsg.str());
62  }
63  return output;
64  }
65 
67  double SLHAea_get(const SLHAstruct& slha, const str& block, const int index, const double defvalue)
68  {
69  double output;
70  try
71  {
72  output = SLHAea::to<double>(slha.at(block).at(index).at(1));
73  }
74  catch (const std::out_of_range& e)
75  {
76  std::ostringstream warn;
77  warn << "Warning! No entry found at index "<<index<<" of block "<<block<<". Using default value: "<<defvalue<< std::endl;
78  utils_warning().raise(LOCAL_INFO,warn.str());
79  output = defvalue;
80  }
81  return output;
82  }
83 
85  void SLHAea_add_block(SLHAstruct& slha, const str& name, const double scale)
86  {
87  if(scale==-1)
88  {
89  slha[name][""] << "BLOCK" << name;
90  }
91  else
92  {
93  slha[name][""] << "BLOCK" << name << "Q=" << scale;
94  }
95  }
96 
97  bool SLHAea_block_exists(SLHAstruct& slha, const str& block)
98  {
99  // Check if block exists
100  bool found = false;
101  if(slha.find(block) != slha.end()) found = true;
102  return found;
103  }
104 
105  bool SLHAea_check_block(SLHAstruct& slha, const str& block)
106  {
107  bool exists;
108  if(SLHAea_block_exists(slha,block))
109  {
110  exists = true;
111  }
112  else
113  {
114  slha[block][""] << "BLOCK" << block;
115  exists = false; // Didn't exist, but now it does.
116  }
117  return exists;
118  }
119 
121  // TODO: Ben: I just found this, and I can't say I understand the logic related to "overwrite". It also makes
122  // overloading for two indices very difficult, so I'm going to delete it.
123  bool SLHAea_check_block(SLHAstruct& slha, const str& block, const int index) /*, const bool overwrite)*/
124  {
125  bool found;
126  // Check if block exists and create it if it doesn't
127  SLHAea_check_block(slha, block);
128  // Check for existing entry
129  std::stringstream i;
130  i<<index;
131  SLHAea::Block::key_type key(1);
132  key[0] = i.str();
133  if( slha[block].find(key) != slha[block].end())
134  {
135  found = true;
136  }
137  else
138  {
139  found = false;
140  }
141  return found;
142  }
143 
144  bool SLHAea_check_block(SLHAstruct& slha, const str& block, const int index1, const int index2) /*, const bool overwrite)*/
145  {
146  bool found;
147  // Check if block exists and create it if it doesn't
148  SLHAea_check_block(slha, block);
149  // Check for existing entry
150  std::stringstream i,j;
151  i<<index1; j<<index2;
152  SLHAea::Block::key_type key(2);
153  key[0] = i.str();
154  key[1] = j.str();
155  if( slha[block].find(key) != slha[block].end() )
156  {
157  found = true;
158  }
159  else
160  {
161  found = false;
162  }
163  return found;
164  }
165 
166 
168  void SLHAea_delete_block(SLHAstruct& slha, const std::string& block)
169  {
170  auto it = slha.find(block);
171  if(it!=slha.end()) slha.erase(it);
172  }
173 
174  void SLHAea_add_GAMBIT_SPINFO(SLHAstruct& slha /*modify*/)
175  {
176  // For now we don't try to track where the data originally came from, we just label
177  // it as GAMBIT-produced.
178  std::ostringstream progname;
179  if(not SLHAea_check_block(slha, "SPINFO", 1))
180  {
181  SLHAea_add(slha, "SPINFO", 1, "GAMBIT", "Program");
182  SLHAea_add(slha, "SPINFO", 2, gambit_version(), "Version number");
183  }
184  }
185 
188  void SLHAea_add(SLHAstruct& slha /*modify*/, const str& block, const int index,
189  const double value, const str& comment, const bool overwrite)
190  {
191  if (SLHAea_check_block(slha, block, index) and not overwrite) return;
192  SLHAea_overwrite_block(slha, block, index, value, (comment == "" ? "" : "# " + comment));
193  }
194 
195  // string version
196  void SLHAea_add(SLHAstruct& slha /*modify*/, const str& block, const int index,
197  const str& value, const str& comment, const bool overwrite)
198  {
199  if (SLHAea_check_block(slha, block, index, overwrite)) return;
200  SLHAea_overwrite_block(slha, block, index, value, (comment == "" ? "" : "# " + comment));
201  }
202 
203  // int version
204  void SLHAea_add(SLHAstruct& slha /*modify*/, const str& block, const int index,
205  const int value, const str& comment, const bool overwrite)
206  {
207  if (SLHAea_check_block(slha, block, index, overwrite)) return;
208  SLHAea_overwrite_block(slha, block, index, value, (comment == "" ? "" : "# " + comment));
209  }
210 
211  // two index version
212  void SLHAea_add(SLHAstruct& slha /*modify*/, const str& block, const int index1, const int index2,
213  const double& value, const str& comment, const bool overwrite)
214  {
215  if (SLHAea_check_block(slha, block, index1, index2) and not overwrite) return;
216  SLHAea_overwrite_block(slha, block, index1, index2, value, (comment == "" ? "" : "# " + comment));
217  }
218 
220 
221 }
EXPORT_SYMBOLS error & utils_error()
Utility errors.
void SLHAea_add_block(SLHAstruct &, const str &name, const double scale=-1)
Add a new block to an SLHAea object, with or without a scale.
#define LOCAL_INFO
Definition: local_info.hpp:34
SMslha_SLHAstruct SLHAstruct read_SLHA(str slha)
Read an SLHA file in to an SLHAea object with some error-checking.
void SLHAea_add_GAMBIT_SPINFO(SLHAstruct &slha)
Write the SPINFO block with GAMBIT name and version number.
void SLHAea_add(SLHAstruct &slha, const str &block, const int index, const double value, const str &comment="", const bool overwrite=false)
Add an entry to an SLHAea object (if overwrite=false, only if it doesn&#39;t already exist) ...
SLHAea::Coll SLHAstruct
Less confusing name for SLHAea container class.
Nicer alias for SLHAea container class, and some convenient helper functions that add or retrieve the...
EXPORT_SYMBOLS warning & utils_warning()
Utility warnings.
bool SLHAea_block_exists(SLHAstruct &slha, const str &block)
Check if a block exists in an SLHAea object.
void SLHAea_overwrite_block(SLHAstruct &slha, const str &block, int index, T value, const str &comment)
Check if a line exists in an SLHAea block, then overwrite it if it does. Otherwise add the line...
str & gambit_version()
Statically construct a string containing the full GAMBIT version information and return a reference t...
Definition: version.cpp:32
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
void SLHAea_delete_block(SLHAstruct &slha, const std::string &block)
Delete an entire block from an SLHAea object, if it exists (actually just the first block matching th...
Exception objects required for standalone compilation.
double SLHAea_get(const SLHAstruct &slha, const str &block, const int index)
Get an entry from an SLHAea object as a double, with some error checking.
Version numbering.
TODO: see if we can use this one:
Definition: Analysis.hpp:33
bool SLHAea_check_block(SLHAstruct &slha, const str &block)
Check if a block exists in an SLHAea object, add it if not.