a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
InputParser.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 HEPfit Collaboration
3 *
4 *
5 * For the licensing terms see doc/COPYING.
6 */
7
8#include "InputParser.h"
9#include "ModelFactory.h"
10#include <stdexcept>
11#include <boost/lexical_cast.hpp>
12#include <boost/algorithm/string/predicate.hpp>
13#include <iostream>
14#include <boost/tokenizer.hpp>
15
16InputParser::InputParser(ModelFactory& ModF, ThObsFactory& ObsF) : myModelFactory(ModF), myObsFactory(ObsF), filename(""), rank(0)
17{
18 modelset = false;
19 myModel = NULL;
20}
21
22InputParser::InputParser(const InputParser& orig) : myModelFactory(orig.myModelFactory), myObsFactory(orig.myObsFactory), filename(""), rank(0)
23{
24 myModel = new StandardModel(*orig.myModel);
25}
26
28{
29 if (myModel != NULL) {
30 delete myModel;
31 myModel = NULL;
32 }
33}
34
35std::string InputParser::ReadParameters(const std::string filename_i,
36 const int rank_i,
37 std::vector<ModelParameter>& ModelPars,
38 boost::ptr_vector<Observable>& Observables,
39 std::vector<Observable2D>& Observables2D,
40 std::vector<CorrelatedGaussianObservables>& CGO,
41 std::vector<CorrelatedGaussianParameters>& CGP)
42{
43 filename = filename_i;
44 rank = rank_i;
45 lineNo = 0;
46 std::ifstream ifile(filename.c_str());
47 if (!ifile.is_open()) {
48 if(rank == 0) throw std::runtime_error("\nERROR: " + filename + " does not exist. Make sure to specify a valid model configuration file.\n");
49 else sleep (2);
50 }
51
52 if (filename.find("\\/") == std::string::npos) filepath = filename.substr(0, filename.find_last_of("\\/") + 1);
53 IsEOF = false;
54 boost::char_separator<char> * sep = new boost::char_separator<char>(" \t");
55 do {
56 IsEOF = getline(ifile, line).eof();
57 if (IsEOF)
58 continue;
59 lineNo++;
60
61 if (!line.empty() && *line.rbegin() == '\r') line.erase(line.length() - 1); // for CR+LF
62 if (line.empty() || line.find_first_not_of(' ') == std::string::npos || line.at(0) == '#')
63 continue;
64
65 boost::tokenizer<boost::char_separator<char> > *tok = new boost::tokenizer<boost::char_separator<char> >(line, *sep);
66 boost::tokenizer<boost::char_separator<char> >::iterator beg = tok->begin();
67
68 if (modelset == 0) {
69 modname = *beg;
74 if (rank == 0) std::cout << "\nModel Initialized: " << modname << "\n" << std::endl;
76 } else if (rank == 0)
77 throw std::runtime_error("\nERROR: " + modname + " not initialized successfully.\n");
78 modelset = 1;
79 continue;
80 } else if (modelset == 1 && beg->compare(myModel->getModelName()) == 0) {
81 continue;
82 }
83
84 std::string type = *beg;
85 ++beg;
86 if (type.compare("ModelParameter") == 0) {
87
88 if (std::distance(tok->begin(), tok->end()) < 5) {
89 if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename + ".\n");
90 else sleep(2);
91 } else {
92 ModelParameter tmpMP;
93 beg = tmpMP.ParseModelParameter(beg);
94 if (checkDuplicateParameter[tmpMP.getname()].get<0>()) {
95 if(rank == 0) throw std::runtime_error("\nERROR: ModelParameter " + tmpMP.getname() + " appears more than once ...!! \n" +
96 "1st Occurrence: Line No:" + boost::lexical_cast<std::string>(checkDuplicateParameter[tmpMP.getname()].get<2>()) +
97 " in file " + checkDuplicateParameter[tmpMP.getname()].get<1>() + ".\n"
98 "2nd Occurrence: Line No:" + boost::lexical_cast<std::string>(lineNo) + " in file " + filename + ".\n");
99 else sleep (2);
100 }
101
102 if (tmpMP.getname().compare("lambdaNP") == 0 && (tmpMP.geterrf() > 0. || tmpMP.geterrg() > 0.))
103 if(rank == 0) throw std::runtime_error("\nERROR: ModelParameter " + tmpMP.getname() + " cannot float in the MonteCarlo run ");
104
105 if (beg != tok->end())
106 if (rank == 0) std::cout << "WARNING: unread information in parameter " << tmpMP.getname() << std::endl;
107 checkDuplicateParameter[tmpMP.getname()] = boost::make_tuple(true, filename, lineNo);
108
109 ModelPars.push_back(tmpMP);
110 }
111
112 } else if (type.compare("CorrelatedGaussianParameters") == 0) {
113
115 lineNo = tmpCGP.ParseCGP(ModelPars, filename, ifile, beg, lineNo, rank);
116 IsEOF = tmpCGP.isEOF();
117 CGP.push_back(tmpCGP);
118
119 } else if (type.compare("Observable") == 0 || type.compare("BinnedObservable") == 0 || type.compare("FunctionObservable") == 0 || type.compare("AsyGausObservable") == 0) {
120
121 Observable * tmpObs = new Observable();
122 beg = tmpObs->ParseObservable(type, tok, beg, filepath, filename, rank);
123 tmpObs->setTho(myObsFactory.CreateThMethod(tmpObs->getThname(), *myModel));
124 Observables.push_back(tmpObs);
125
126 } else if (type.compare("Observable2D") == 0) {
127
128 Observable2D tmpObs2D;
129 lineNo = tmpObs2D.ParseObservable2D(type, tok, beg, filename, ifile, lineNo, rank);
131 if (!IsEOF) IsEOF = tmpObs2D.isEOF();
132 Observables2D.push_back(tmpObs2D);
133
134 } else if (type.compare("HiggsObservable") == 0) {
135
136 Observable * tmphObs = new Observable();
137 beg = tmphObs->ParseObservable(type, tok, beg, filepath, filename, rank);
138 tmphObs->setTho(myObsFactory.CreateThMethod(tmphObs->getThname(), *myModel));
139 HiggsObservable * tmpho = new HiggsObservable(*tmphObs);
140 beg = tmpho->ParseHiggsObservable(beg, myObsFactory, myModel, rank);
141 Observables.push_back(tmpho);
142 ++beg;
143 if (beg != tok->end() && rank == 0) std::cout << "WARNING: unread information in HiggsObservable " << tmpho->getName() << std::endl;
144
145 } else if (type.compare("CorrelatedHiggsObservables") == 0) {
146
147 Observable * tmphObs = new Observable();
148 beg = tmphObs->ParseObservable(type, tok, beg, filepath, filename, rank);
149 tmphObs->setTho(myObsFactory.CreateThMethod(tmphObs->getThname(), *myModel));
150 HiggsObservable * tmpho = new HiggsObservable(*tmphObs);
151 tmpho->setIsCorrelated(true);
152 beg = tmpho->ParseHiggsObservable(beg, myObsFactory, myModel, rank);
153 Observables.push_back(tmpho);
154 ++beg;
155 if (beg != tok->end() && rank == 0) std::cout << "WARNING: unread information in HiggsObservable " << tmpho->getName() << std::endl;
156
157 } else if (type.compare("CorrelatedGaussianObservables") == 0) {
158
160 lineNo = tmpCGO.ParseCGO(Observables, ifile, beg, filename, myObsFactory, myModel, lineNo, rank);
161 IsEOF = tmpCGO.isEOF();
162 if (tmpCGO.getObs().size() > 1) CGO.push_back(tmpCGO);
163
164 } else if (type.compare("ObservablesWithCovarianceInverse") == 0) {
165
167 tmpCGO.setCovarianceFromConfig(true);
168 lineNo = tmpCGO.ParseCGO(Observables, ifile, beg, filename, myObsFactory, myModel, lineNo, rank);
169 IsEOF = tmpCGO.isEOF();
170 if (tmpCGO.getObs().size() > 1) CGO.push_back(tmpCGO);
171
172 } else if (type.compare("CorrelatedObservables") == 0) {
173
175 tmpCO.setIsPrediction(true);
176 lineNo = tmpCO.ParseCGO(Observables, ifile, beg, filename, myObsFactory, myModel, lineNo, rank);
177 IsEOF = tmpCO.isEOF();
178 CGO.push_back(tmpCO);
179
180 } else if (type.compare("CustomObservable") == 0) {
181
182 if (std::distance(tok->begin(), tok->end()) < 2) {
183 if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename + ".\n");
184 else sleep(2);
185 }
186 std::string customObsName = *beg;
187 beg++;
188 if (customObservableTypeMap.find(customObsName) == customObservableTypeMap.end()) {
189 if (rank == 0) throw std::runtime_error("\nERROR: No Observable Type defined for " + customObsName + "\n");
190 else sleep(2);
191 }
192 Observable * tmpcustomObs = CreateObservableType(customObsName);
193 tmpcustomObs->setObsType(customObsName);
194 beg = tmpcustomObs->ParseObservable(type, tok, beg, filepath, filename, rank);
195 tmpcustomObs->setTho(myObsFactory.CreateThMethod(tmpcustomObs->getThname(), *myModel));
196 Observables.push_back(tmpcustomObs);
197
198 } else if (type.compare("ModelFlag") == 0) {
199 if (std::distance(tok->begin(), tok->end()) < 3) {
200 if(rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename);
201 else sleep (2);
202 }
203 std::string flagname = *beg;
204 ++beg;
205 if (boost::iequals(*beg, "true") || boost::iequals(*beg, "false")) {
206 /* Boolean flags */
207 bool value_bool;
208 if (boost::iequals(*beg, "true"))
209 value_bool = 1;
210 else
211 value_bool = 0;
212 if (!myModel->setFlag(flagname, value_bool)) {
213 if(rank == 0) throw std::runtime_error("ERROR: setFlag error for " + flagname);
214 else sleep (2);
215 }
216 else if (rank == 0) std::cout << "set flag " << flagname << "=" << *beg << std::endl;
217 } else {
218 /* String flags */
219 std::string value_str = *beg;
220 if (!myModel->setFlagStr(flagname, value_str)) {
221 if(rank == 0) throw std::runtime_error("ERROR: setFlag error for " + flagname);
222 else sleep (2);
223 } else if (rank == 0) std::cout << "set flag " << flagname << "=" << value_str << std::endl;
224 }
225 ++beg;
226 if (beg != tok->end() && rank == 0) std::cout << "WARNING: unread information in Flag " << flagname << std::endl;
227 } else if (type.compare("IncludeFile") == 0) {
228 std::string oldfilepath = filepath;
229 std::string oldfilename = filename;
230 int oldlineNo = lineNo;
231 std::string IncludeFileName = filepath + *beg;
232 if (rank == 0) std::cout << "Including File: " + IncludeFileName << std::endl;
233 ReadParameters(IncludeFileName, rank, ModelPars, Observables, Observables2D, CGO, CGP);
234 filepath = oldfilepath;
235 filename = oldfilename;
236 lineNo = oldlineNo;
237 IsEOF = false;
238 ++beg;
239 } else {
240 if (rank == 0) throw std::runtime_error("\nERROR: wrong keyword " + type + " in file " + filename + " line no. " + boost::lexical_cast<std::string>(lineNo) + ". Make sure to specify a valid model configuration file.\n");
241 else sleep(2);
242 }
243 delete tok;
244 tok = NULL;
245 } while (!IsEOF);
246
247 if (modelset == 0 && rank == 0)
248 throw std::runtime_error("ERROR: Incorrect or missing model name in the model configuration file.\n");
249 if (!myModel->CheckFlags() && rank == 0)
250 throw std::runtime_error("ERROR: incompatible flag(s)\n");
251 delete sep;
252 sep = NULL;
253 return (modname);
254}
255
256void InputParser::addCustomObservableType(const std::string name, boost::function<Observable*() > funct)
257{
258 customObservableTypeMap[name] = funct;
259}
260
261Observable * InputParser::CreateObservableType(const std::string& name) const
262{
263 if (customObservableTypeMap.find(name) == customObservableTypeMap.end()) {
264 if (rank ==0) throw std::runtime_error("ERROR: No observable defined for " + name + " so it cannot be created");
265 else sleep(0);
266 }
267 return (customObservableTypeMap.at(name)());
268}
A class for correlated Gaussian observables.
bool isEOF()
A method to check if the end of file has been reached.
void setIsPrediction(bool IsPrediction_i)
A method to set a set of CGO to be predicted.
int ParseCGO(boost::ptr_vector< Observable > &Observables, std::ifstream &ifile, boost::tokenizer< boost::char_separator< char > >::iterator &beg, std::string &infilename, ThObsFactory &myObsFactory, StandardModel *myModel, int lineNo, int rank)
The parser for CorrelatedGaussianObservables.
std::vector< Observable > getObs() const
A get method to access the vector of observables that are defined in one correlated Gaussian observab...
void setCovarianceFromConfig(bool setInvCov)
A method to specify whether the inverse covariance is being set from the config file.
A class for correlated Gaussian parameters.
bool isEOF()
A method to check if the end of file has been reached.
int ParseCGP(std::vector< ModelParameter > &ModPars, std::string &filename, std::ifstream &ifile, boost::tokenizer< boost::char_separator< char > >::iterator &beg, int lineNo, int rank)
The parser for CorrelatedGaussianParameters.
A class for Higgs experimental analyses.
void setIsCorrelated(bool correlated)
boost::tokenizer< boost::char_separator< char > >::iterator & ParseHiggsObservable(boost::tokenizer< boost::char_separator< char > >::iterator &beg, ThObsFactory &myObsFactory, StandardModel *myModel, int rank)
the parser for HiggsObservables
A class for reading input parameters and output directives.
Definition: InputParser.h:48
std::string modname
A string to store the model name in.
Definition: InputParser.h:135
std::map< std::string, boost::function< Observable *()> > customObservableTypeMap
Definition: InputParser.h:140
ThObsFactory & myObsFactory
Reference to an object of type ThObsFactory.
Definition: InputParser.h:134
InputParser(ModelFactory &ModF, ThObsFactory &ObsF)
Constructor.
Definition: InputParser.cpp:16
Observable * CreateObservableType(const std::string &name) const
std::string filename
Definition: InputParser.h:143
StandardModel * myModel
Pointer to an object of type StandardModel.
Definition: InputParser.h:132
std::string modeldefinedinfile
Definition: InputParser.h:138
virtual ~InputParser()
The default destructor.
Definition: InputParser.cpp:27
std::string line
Definition: InputParser.h:146
void addCustomObservableType(const std::string name, boost::function< Observable *() > funct)
std::map< std::string, boost::tuple< bool, std::string, int > > checkDuplicateParameter
Definition: InputParser.h:137
ModelFactory & myModelFactory
Pointer to an object of type ModelFactory.
Definition: InputParser.h:133
std::string filepath
Definition: InputParser.h:145
std::string ReadParameters(const std::string filename_i, const int rank, std::vector< ModelParameter > &ModelPars, boost::ptr_vector< Observable > &Observables, std::vector< Observable2D > &Observables2D, std::vector< CorrelatedGaussianObservables > &CGO, std::vector< CorrelatedGaussianParameters > &CGP)
The member that parses the Observable2D directives from SomeModel.conf file.
Definition: InputParser.cpp:35
A class for.
Definition: ModelFactory.h:25
StandardModel * CreateModel(const std::string &ModelName)
bool IsModelInitialized() const
A method to check if the model is initialized.
Definition: Model.h:136
void setModelName(const std::string name)
A method to set the name of the model.
Definition: Model.h:50
std::string getModelName() const
A method to fetch the name of the model.
Definition: Model.h:59
A class for model parameters.
boost::tokenizer< boost::char_separator< char > >::iterator & ParseModelParameter(boost::tokenizer< boost::char_separator< char > >::iterator &beg)
Parser for model parameters.
std::string getname() const
A get method to get the name of each parameter.
double geterrg() const
A get method to get the gaussian error.
double geterrf() const
A get method to get the flat error.
A class for analyzing observables pairwise.
Definition: Observable2D.h:24
int ParseObservable2D(std::string &type, boost::tokenizer< boost::char_separator< char > > *tok, boost::tokenizer< boost::char_separator< char > >::iterator &beg, std::string &infilename, std::ifstream &ifile, int lineNo, int rank)
bool isEOF()
A method to check if the end of file has been reached.
Definition: Observable2D.h:285
std::string getThname2() const
A get method to access the thname of the second observable as defined in ThFactory class.
Definition: Observable2D.h:155
void setTho1Tho2(ThObservable *tho1_i, ThObservable *tho2_i)
A set method to fix the pointer to object of type ThObservable class for the second observable.
Definition: Observable2D.h:196
A class for observables.
Definition: Observable.h:28
void setTho(ThObservable *tho_i)
A set method to fix the pointer to object of type ThObservable.
Definition: Observable.h:413
std::string getThname() const
A get method to access the thname of the observable as defined in ThFactory class.
Definition: Observable.h:368
boost::tokenizer< boost::char_separator< char > >::iterator & ParseObservable(std::string &type, boost::tokenizer< boost::char_separator< char > > *tok, boost::tokenizer< boost::char_separator< char > >::iterator &beg, std::string &filepath, std::string &infilename, int rank)
The parser for Observables.
Definition: Observable.cpp:202
std::string getName() const
A get method to access the name of the observable.
Definition: Observable.h:323
void setObsType(std::string &obsType_s)
A set method to set the Observable type.
Definition: Observable.h:395
A model class for the Standard Model.
virtual bool CheckFlags() const
A method to check the sanity of the set of model flags.
virtual bool setFlagStr(const std::string name, const std::string value)
A method to set a flag of StandardModel.
virtual bool setFlag(const std::string name, const bool value)
A method to set a flag of StandardModel.
virtual bool InitializeModel()
A method to initialize the model.
A class for.
Definition: ThObsFactory.h:26
ThObservable * CreateThMethod(const std::string &name, StandardModel &model) const
This method checks for the existence of an observable of a specific name in the map thobs and returns...