gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
baseprinter.cpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
16 
17 // Gambit
20 
21 // Boost
22 #include <boost/preprocessor/seq/for_each.hpp>
23 #include <boost/preprocessor/seq/for_each_i.hpp>
24 //#include <boost/preprocessor/punctuation/comma_if.hpp>
25 
26 // Code!
27 namespace Gambit
28 {
29  namespace Printers
30  {
33  #define GETTYPEID(r,data,i,elem) \
34  template<> \
35  std::size_t getTypeID<elem>(void) {return i+1;}
37  #undef GETTYPEID
38 
40  void printAllTypeIDs(void)
41  {
42  std::cout << "Types known to getTypeID<T>() function:" << std::endl;
43  #define PRINTTYPEID(r,data,elem) \
44  std::cout << " Type: " << STRINGIFY(elem) << ", TypeID: " << getTypeID<elem>() << std::endl;
45  BOOST_PP_SEQ_FOR_EACH(PRINTTYPEID, _, PRINTABLE_TYPES)
46  #undef PRINTTYPEID
47  }
48 
50  bool BaseReader::retrieve_and_print(const std::string& in_label, const std::string& out_label, BaseBasePrinter& printer, const uint rank, const ulong pointID)
51  {
56  bool valid = false; // Switch to true if value is successfully retrieved for this point
57  #define TYPE_CASES(r,data,elem) \
58  if( get_type(in_label) == getTypeID<elem>()) \
59  { \
60  elem buffer; \
61  valid = retrieve(buffer, in_label, rank, pointID); \
62  if(valid) \
63  { \
64  printer.print(buffer, out_label, rank, pointID); \
65  } \
66  } else
67  BOOST_PP_SEQ_FOR_EACH(TYPE_CASES, _, PRINTABLE_TYPES)
68  #undef TYPE_CASES
69  {
70  std::ostringstream err;
71  err << "Did not recognise retrieved type for data label '"<<in_label<<"'! This may indicate a bug in the Reader class you are using, please report it.";
72  printer_error().raise(LOCAL_INFO,err.str());
73  }
74  return valid;
75  }
76 
81  bool parse_label_for_ModelParameters(const std::string& fulllabel, const std::string& modelname, std::string& out, std::string& labelroot, bool case_sensitive)
82  {
83  bool result = false;
84  std::istringstream iss(fulllabel);
85  std::string capability;
86  std::string rest;
87  iss >> capability;
88  iss >> rest;
89  if(!iss)
90  {
91  // Weren't two elements to the label, so this can't be a match
92  result = false; // failed to match
93  }
94  else
95  {
96  //capability is "#NormalDist_parameters", for example
97  capability.erase(0,1); // cut off the first character (hash, in all potentially matching cases)
98  if(Utils::startsWith(capability,modelname,case_sensitive))
99  {
100  // Cut off the modelname which matched
101  capability.erase(0,modelname.size());
102  if(Utils::startsWith(capability,"_parameters",case_sensitive))
103  {
104  // Still good so far, check 'rest', should be something like @NormalDist::primary_parameters::mu
105  rest.erase(0,1); // cut off the first character (@, in all potentially matching cases)
106  std::vector<str> split_rest = Utils::delimiterSplit(rest, "::");
107  if(Utils::iequals(split_rest[0],modelname,case_sensitive) and split_rest.size()==3)
108  {
109  // Ok! We have a match!
110  out = split_rest[2];
111  result = true;
112  // Get the rest of the full label
113  labelroot = fulllabel;
114  // Erase the "::parameter_name" part
115  labelroot.erase(fulllabel.size() - out.size() - 2, out.size()+2);
116  } else { result = false; }
117  } else { result = false; }
118  } else { result = false; }
119  }
120  return result;
121  }
122 
127  // example label:
128  // #MSSM_spectrum @SpecBit::get_MSSM_spectrum_as_map::~u_(6,6) Pole_Mixing
129  bool parse_label_for_spectrum_entry(const std::string& fulllabel, const std::string& req_capability, const std::string& req_module, const std::string& req_function, std::string& outname, std::string& outtag, std::string& labelroot, bool case_sensitive)
130  {
131  bool result = false;
132  std::istringstream iss(fulllabel);
133  std::string capability;
134  std::string rest;
135  iss >> capability;
136  iss >> rest;
137  iss >> outtag; // Last element is that spectrum 'tag', e.g. dimensionless, Pole_Mixing, etc.
138  if(!iss)
139  {
140  // Weren't three elements to the label, so this can't be a match
141  result = false; // failed to match
142  }
143  else
144  {
145  //capability is "#MSSM_spectrum", for example
146  capability.erase(0,1); // cut off the first character (hash, in all potentially matching cases)
147  if(Utils::iequals(capability,req_capability,case_sensitive))
148  {
149  // Check 'rest', should be something like @SpecBit::get_MSSM_spectrum_as_map::~u_(6,6)
150  rest.erase(0,1); // cut off the first character (@, in all potentially matching cases)
151  std::vector<str> split_rest = Utils::delimiterSplit(rest, "::");
152  if( Utils::iequals(split_rest[0],req_module,case_sensitive)
153  and Utils::iequals(split_rest[1],req_function,case_sensitive)
154  and split_rest.size()==3)
155  {
156  // Ok! We have a match!
157  outname = split_rest[2];
158  result = true;
159  // Get the rest of the full label
160  labelroot = fulllabel;
161  // Erase the "::entryname tag" part
162  std::size_t offset = outname.size() + outtag.size() + 3; // Names plus :: plus space
163  labelroot.erase(fulllabel.size() - offset, offset);
164  } else { result = false; }
165  } else { result = false; }
166  }
167  return result;
168  }
169 
170 
171  }
172 }
EXPORT_SYMBOLS error & printer_error()
Printer errors.
#define LOCAL_INFO
Definition: local_info.hpp:34
BOOST_PP_SEQ_FOR_EACH_I(GETTYPEID, _, PRINTABLE_TYPES) void printAllTypeIDs(void)
For debugging; print to stdout all the typeIDs for all types.
Definition: baseprinter.cpp:36
#define PRINTABLE_TYPES
General small utility functions.
EXPORT_SYMBOLS bool startsWith(const std::string &str, const std::string &prefix, bool case_sensitive=true)
Checks whether `str&#39; begins with `prefix&#39;.
EXPORT_SYMBOLS bool iequals(const std::string &a, const std::string &b, bool case_sensitive=false)
Perform a (possibly) case-insensitive string comparison.
Printers::BaseBasePrinter printer
Type of the printer objects.
Declaration and definition of printer base class.
void printAllTypeIDs(void)
For debugging; print to stdout all the typeIDs for all types.
unsigned long int ulong
EXPORT_SYMBOLS std::vector< str > delimiterSplit(str s, str delim)
Split a string into a vector of strings, using a delimiter, and removing any whitespace around the de...
#define PRINTTYPEID(r, data, elem)
#define GETTYPEID(r, data, i, elem)
Specialisations of type ID getters ID zero reserved for null ID.
Definition: baseprinter.cpp:33
bool parse_label_for_spectrum_entry(const std::string &fulllabel, const std::string &req_capability, const std::string &req_module, const std::string &req_function, std::string &outname, std::string &outtag, std::string &labelroot, bool case_sensitive=true)
Helper function for parsing Spectrum label strings.
TODO: see if we can use this one:
Definition: Analysis.hpp:33
bool parse_label_for_ModelParameters(const std::string &fulllabel, const std::string &modelname, std::string &out, std::string &rest, bool case_sensitive=true)
Helper function for the ModelParameters &#39;_retrieve&#39; functions Parses a printer label and checks if it...
Definition: baseprinter.cpp:81
bool retrieve_and_print(const std::string &in_label, const std::string &out_label, BaseBasePrinter &printer, const uint rank, const ulong pointID)
Retrieve and directly print data to new output.
Definition: baseprinter.cpp:50
#define TYPE_CASES(r, data, elem)