gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
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"
29 
30 namespace Gambit
31 {
34  {
35  SLHAstruct slhaea;
36  std::ifstream ifs(slha.c_str());
37  if (!ifs.good())
38  {
39  std::ostringstream err;
40  err << "ERROR: SLHA file " << slha << " not found.";
41  utils_error().raise(LOCAL_INFO,err.str());
42  }
43  ifs >> slhaea;
44  ifs.close();
45  return slhaea;
46  }
47 
49  double SLHAea_get(const SLHAstruct& slha, const str& block, const int index)
50  {
51  double output = 0.0;
52  try
53  {
54  output = SLHAea::to<double>(slha.at(block).at(index).at(1));
55  }
56  catch (const std::out_of_range& e)
57  {
58  std::ostringstream errmsg;
59  errmsg << "Error accessing data at index " << index << " of block " << block
60  << ". Please check that the SLHAea object was properly filled." << std::endl
61  << "(Received out_of_range error from SLHAea class with message: " << e.what() << ")";
62  utils_error().raise(LOCAL_INFO,errmsg.str());
63  }
64  return output;
65  }
66 
68  double SLHAea_get(const SLHAstruct& slha, const str& block, const int index, const double defvalue)
69  {
70  double output;
71  try
72  {
73  output = SLHAea::to<double>(slha.at(block).at(index).at(1));
74  }
75  catch (const std::out_of_range& e)
76  {
77  std::ostringstream warn;
78  warn << "Warning! No entry found at index "<<index<<" of block "<<block<<". Using default value: "<<defvalue<< std::endl;
79  utils_warning().raise(LOCAL_INFO,warn.str());
80  output = defvalue;
81  }
82  return output;
83  }
84 
86  void SLHAea_add_block(SLHAstruct& slha, const str& name, const double scale)
87  {
88  if(scale==-1)
89  {
90  slha[name][""] << "BLOCK" << name;
91  }
92  else
93  {
94  slha[name][""] << "BLOCK" << name << "Q=" << scale;
95  }
96  }
97 
98  bool SLHAea_block_exists(SLHAstruct& slha, const str& block)
99  {
100  // Check if block exists
101  bool found = false;
102  if(slha.find(block) != slha.end()) found = true;
103  return found;
104  }
105 
106  bool SLHAea_check_block(SLHAstruct& slha, const str& block)
107  {
108  bool exists;
109  if(SLHAea_block_exists(slha,block))
110  {
111  exists = true;
112  }
113  else
114  {
115  slha[block][""] << "BLOCK" << block;
116  exists = false; // Didn't exist, but now it does.
117  }
118  return exists;
119  }
120 
122  // TODO: Ben: I just found this, and I can't say I understand the logic related to "overwrite". It also makes
123  // overloading for two indices very difficult, so I'm going to delete it.
124  bool SLHAea_check_block(SLHAstruct& slha, const str& block, const int index) /*, const bool overwrite)*/
125  {
126  bool found;
127  // Check if block exists and create it if it doesn't
128  SLHAea_check_block(slha, block);
129  // Check for existing entry
130  std::stringstream i;
131  i<<index;
132  SLHAea::Block::key_type key(1);
133  key[0] = i.str();
134  if( slha[block].find(key) != slha[block].end())
135  {
136  found = true;
137  }
138  else
139  {
140  found = false;
141  }
142  return found;
143  }
144 
145  bool SLHAea_check_block(SLHAstruct& slha, const str& block, const int index1, const int index2) /*, const bool overwrite)*/
146  {
147  bool found;
148  // Check if block exists and create it if it doesn't
149  SLHAea_check_block(slha, block);
150  // Check for existing entry
151  std::stringstream i,j;
152  i<<index1; j<<index2;
153  SLHAea::Block::key_type key(2);
154  key[0] = i.str();
155  key[1] = j.str();
156  if( slha[block].find(key) != slha[block].end() )
157  {
158  found = true;
159  }
160  else
161  {
162  found = false;
163  }
164  return found;
165  }
166 
167 
168 
170  template <class T>
171  void SLHAea_overwrite_block(SLHAstruct& slha /*modify*/, const str& block, int index,
172  T value, const str& comment)
173  {
174  if(SLHAea_check_block(slha, block, index))
175  {
176  // entry exists already, delete it
177  slha.at(block).at(index).at(1);
178  auto& line = slha[block][index];
179  line.clear();
180  line << index << value << comment;
181  }
182  else
183  {
184  // Doesn't already exist, add it
185  slha[block][""] << index << value << comment;
186  }
187  }
188 
190  template <class T>
191  void SLHAea_overwrite_block(SLHAstruct& slha /*modify*/, const str& block, int index1, int index2,
192  T value, const str& comment)
193  {
194  //std::vector<int> indices = initVector<int>(index1, index2);
195  if(SLHAea_check_block(slha, block, index1, index2))
196  {
197  // entry exists already, delete it
198  //slha.at(block).at(indices).at(1); // Is this actually a valid way to use SLHAea? I don't see it in their documentation.
199  std::stringstream i,j;
200  i<<index1; j<<index2;
201  SLHAea::Block::key_type key(2);
202  key[0] = i.str();
203  key[1] = j.str();
204  auto& line = slha[block][key];
205  line.clear();
206  line << index1 << index2 << value << comment;
207  }
208  else
209  {
210  // Doesn't exist, add it
211  slha[block][""] << index1 << index2 << value << comment;
212  }
213  }
214 
216  void SLHAea_delete_block(SLHAstruct& slha, const std::string& block)
217  {
218  auto it = slha.find(block);
219  if(it!=slha.end()) slha.erase(it);
220  }
221 
222  void SLHAea_add_GAMBIT_SPINFO(SLHAstruct& slha /*modify*/)
223  {
224  // For now we don't try to track where the data originally came from, we just label
225  // it as GAMBIT-produced.
226  std::ostringstream progname;
227  if(not SLHAea_check_block(slha, "SPINFO", 1))
228  {
229  SLHAea_add(slha, "SPINFO", 1, "GAMBIT", "Program");
230  SLHAea_add(slha, "SPINFO", 2, gambit_version(), "Version number");
231  }
232  }
233 
236  void SLHAea_add(SLHAstruct& slha /*modify*/, const str& block, const int index,
237  const double value, const str& comment, const bool overwrite)
238  {
239  if (SLHAea_check_block(slha, block, index) and not overwrite) return;
240  SLHAea_overwrite_block(slha, block, index, value, (comment == "" ? "" : "# " + comment));
241  }
242 
243  // string version
244  void SLHAea_add(SLHAstruct& slha /*modify*/, const str& block, const int index,
245  const str& value, const str& comment, const bool overwrite)
246  {
247  if (SLHAea_check_block(slha, block, index, overwrite)) return;
248  SLHAea_overwrite_block(slha, block, index, value, (comment == "" ? "" : "# " + comment));
249  }
250 
251  // int version
252  void SLHAea_add(SLHAstruct& slha /*modify*/, const str& block, const int index,
253  const int value, const str& comment, const bool overwrite)
254  {
255  if (SLHAea_check_block(slha, block, index, overwrite)) return;
256  SLHAea_overwrite_block(slha, block, index, value, (comment == "" ? "" : "# " + comment));
257  }
258 
259  // two index version
260  void SLHAea_add(SLHAstruct& slha /*modify*/, const str& block, const int index1, const int index2,
261  const double& value, const str& comment, const bool overwrite)
262  {
263  if (SLHAea_check_block(slha, block, index1, index2) and not overwrite) return;
264  SLHAea_overwrite_block(slha, block, index1, index2, value, (comment == "" ? "" : "# " + comment));
265  }
266 
268 
270  void SLHAea_add_from_subspec(SLHAstruct& slha /*modify*/, const str local_info, const SubSpectrum& subspec,
271  const Par::Tags partype, const std::pair<int, int>& pdg_pair, const str& block, const str& comment,
272  const bool error_if_missing, const double rescale)
273  {
274  if(subspec.has(partype,pdg_pair))
275  {
276  SLHAea_overwrite_block(slha, block, pdg_pair.first, subspec.get(partype,pdg_pair)*rescale, (comment == "" ? "" : "# " + comment));
277  }
278  else if(error_if_missing)
279  {
280  std::ostringstream errmsg;
281  errmsg << "Error creating SLHAea output from SubSpectrum object! Required entry not found (paramtype="<<Par::toString.at(partype)
282  <<", pdg:context="<<pdg_pair.first<<":"<<pdg_pair.second<<")";
283  utils_error().raise(local_info,errmsg.str());
284  }
285  // else skip this entry
286  return;
287  }
288 
290  void SLHAea_add_from_subspec(SLHAstruct& slha /*modify*/, const str local_info, const SubSpectrum& subspec,
291  const Par::Tags partype, const str& name, const str& block, const int slha_index,
292  const str& comment, const bool error_if_missing, const double rescale)
293  {
294  if(subspec.has(partype,name))
295  {
296  SLHAea_overwrite_block(slha, block, slha_index, subspec.get(partype,name)*rescale, (comment == "" ? "" : "# " + comment));
297  }
298  else if(error_if_missing)
299  {
300  std::ostringstream errmsg;
301  errmsg << "Error creating SLHAea output from SubSpectrum object! Required entry not found (paramtype="<<Par::toString.at(partype)<<", name="<<name<<")";
302  utils_error().raise(local_info,errmsg.str());
303  }
304  // else skip this entry
305  return;
306  }
307 
309  void SLHAea_add_from_subspec(SLHAstruct& slha /*modify*/, const str local_info, const SubSpectrum& subspec,
310  const Par::Tags partype, const str& name, const int index1, const int index2, const str& block,
311  const int slha_index1, const int slha_index2, const str& comment, const bool error_if_missing, const double rescale)
312  {
313  if(subspec.has(partype,name,index1,index2))
314  {
315  SLHAea_overwrite_block(slha, block, slha_index1, slha_index2, subspec.get(partype,name,index1,index2)*rescale, (comment == "" ? "" : "# " + comment));
316  }
317  else if(error_if_missing)
318  {
319  std::ostringstream errmsg;
320  errmsg << "Error creating SLHAea output from SubSpectrum object! Required entry not found (paramtype="<<Par::toString.at(partype)<<", name="<<name<<", index1="<<index1<<", index2="<<index2;
321  utils_error().raise(local_info,errmsg.str());
322  }
323  // else skip this entry
324  return;
325  }
326 
327 
328 }
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 our without a scale.
#define LOCAL_INFO
Definition: local_info.hpp:34
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.
virtual double get(const Par::Tags, const str &, const SpecOverrideOptions=use_overrides, const SafeBool check_antiparticle=SafeBool(true)) const =0
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
Abstract class for accessing general spectrum information.
Virtual base class for interacting with spectrum generator output.
Definition: subspectrum.hpp:87
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.
void SLHAea_add_from_subspec(SLHAstruct &slha, const str local_info, const SubSpectrum &subspec, const Par::Tags partype, const std::pair< int, int > &pdg_pair, const str &block, const str &comment, const bool error_if_missing=true, const double rescale=1.0)
Add an entry from a subspectrum getter to an SLHAea object; SLHA index given by pdg code...
TODO: see if we can use this one:
Definition: Analysis.hpp:33
virtual bool has(const Par::Tags, const str &, const SpecOverrideOptions=use_overrides, const SafeBool check_antiparticle=SafeBool(true)) const =0
Getters/Setters etc.
bool SLHAea_check_block(SLHAstruct &slha, const str &block)
Check if a block exists in an SLHAea object, add it if not.