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 
16 InputParser::InputParser(ModelFactory& ModF, ThObsFactory& ObsF) : myModelFactory(ModF), myObsFactory(ObsF), filename(""), rank(0)
17 {
18  modelset = 0;
19  myModel = NULL;
20 }
21 
22 InputParser::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 
35 std::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  lineNo++;
58  if (*line.rbegin() == '\r') line.erase(line.length() - 1); // for CR+LF
59  if (line.empty() || line.find_first_not_of(' ') == std::string::npos || line.at(0) == '#')
60  continue;
61  boost::tokenizer<boost::char_separator<char> > *tok = new boost::tokenizer<boost::char_separator<char> >(line, *sep);
62  boost::tokenizer<boost::char_separator<char> >::iterator beg = tok->begin();
63 
64  if (modelset == 0) {
65  modname = *beg;
69  if (myModel->IsModelInitialized()) {
70  if (rank == 0) std::cout << "\nModel Initialized: " << modname << "\n" << std::endl;
72  } else if (rank == 0)
73  throw std::runtime_error("\nERROR: " + modname + " not initialized successfully.\n");
74  modelset = 1;
75  continue;
76  } else if (modelset == 1 && beg->compare(myModel->getModelName()) == 0) {
77  continue;
78  }
79 
80  std::string type = *beg;
81  ++beg;
82  if (type.compare("ModelParameter") == 0) {
83 
84  if (std::distance(tok->begin(), tok->end()) < 5) {
85  if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename + ".\n");
86  else sleep(2);
87  } else {
88  ModelParameter tmpMP;
89  beg = tmpMP.ParseModelParameter(beg);
90 
91  if (checkDuplicateParameter[tmpMP.getname()].get<0>()) {
92  if(rank == 0) throw std::runtime_error("\nERROR: ModelParameter " + tmpMP.getname() + " appears more than once ...!! \n" +
93  "1st Occurrence: Line No:" + boost::lexical_cast<std::string>(checkDuplicateParameter[tmpMP.getname()].get<2>()) +
94  " in file " + checkDuplicateParameter[tmpMP.getname()].get<1>() + ".\n"
95  "2nd Occurrence: Line No:" + boost::lexical_cast<std::string>(lineNo) + " in file " + filename + ".\n");
96  else sleep (2);
97  }
98 
99  if (beg != tok->end())
100  if (rank == 0) std::cout << "WARNING: unread information in parameter " << tmpMP.getname() << std::endl;
101  checkDuplicateParameter[tmpMP.getname()] = boost::make_tuple(true, filename, lineNo);
102 
103  ModelPars.push_back(tmpMP);
104  }
105 
106  } else if (type.compare("CorrelatedGaussianParameters") == 0) {
107 
109  lineNo = tmpCGP.ParseCGP(ModelPars, filename, ifile, beg, lineNo, rank);
110  IsEOF = tmpCGP.isEOF();
111  CGP.push_back(tmpCGP);
112 
113  } else if (type.compare("Observable") == 0 || type.compare("BinnedObservable") == 0 || type.compare("FunctionObservable") == 0 || type.compare("AsyGausObservable") == 0) {
114 
115  Observable * tmpObs = new Observable();
116  beg = tmpObs->ParseObservable(type, tok, beg, filepath, filename, rank);
117  tmpObs->setTho(myObsFactory.CreateThMethod(tmpObs->getThname(), *myModel));
118  Observables.push_back(tmpObs);
119 
120  } else if (type.compare("Observable2D") == 0) {
121 
122  Observable2D tmpObs2D;
123  lineNo = tmpObs2D.ParseObservable2D(type, tok, beg, filename, ifile, lineNo, rank);
125  if (!IsEOF) IsEOF = tmpObs2D.isEOF();
126  Observables2D.push_back(tmpObs2D);
127 
128  } else if (type.compare("HiggsObservable") == 0) {
129 
130  Observable * tmphObs = new Observable();
131  beg = tmphObs->ParseObservable(type, tok, beg, filepath, filename, rank);
132  tmphObs->setTho(myObsFactory.CreateThMethod(tmphObs->getThname(), *myModel));
133  HiggsObservable * tmpho = new HiggsObservable(*tmphObs);
134  beg = tmpho->ParseHiggsObservable(beg, myObsFactory, myModel, rank);
135  Observables.push_back(tmpho);
136  ++beg;
137  if (beg != tok->end() && rank == 0) std::cout << "WARNING: unread information in HiggsObservable " << tmpho->getName() << std::endl;
138 
139  } else if (type.compare("CorrelatedHiggsObservables") == 0) {
140 
141  Observable * tmphObs = new Observable();
142  beg = tmphObs->ParseObservable(type, tok, beg, filepath, filename, rank);
143  tmphObs->setTho(myObsFactory.CreateThMethod(tmphObs->getThname(), *myModel));
144  HiggsObservable * tmpho = new HiggsObservable(*tmphObs);
145  tmpho->setIsCorrelated(true);
146  beg = tmpho->ParseHiggsObservable(beg, myObsFactory, myModel, rank);
147  Observables.push_back(tmpho);
148  ++beg;
149  if (beg != tok->end() && rank == 0) std::cout << "WARNING: unread information in HiggsObservable " << tmpho->getName() << std::endl;
150 
151  } else if (type.compare("CorrelatedGaussianObservables") == 0) {
152 
154  lineNo = tmpCGO.ParseCGO(Observables, ifile, beg, filename, myObsFactory, myModel, lineNo, rank);
155  IsEOF = tmpCGO.isEOF();
156  if (tmpCGO.getObs().size() > 1) CGO.push_back(tmpCGO);
157 
158  } else if (type.compare("ObservablesWithCovarianceInverse") == 0) {
159 
161  tmpCGO.setCovarianceFromConfig(true);
162  lineNo = tmpCGO.ParseCGO(Observables, ifile, beg, filename, myObsFactory, myModel, lineNo, rank);
163  IsEOF = tmpCGO.isEOF();
164  if (tmpCGO.getObs().size() > 1) CGO.push_back(tmpCGO);
165 
166  } else if (type.compare("CorrelatedObservables") == 0) {
167 
169  tmpCO.setIsPrediction(true);
170  lineNo = tmpCO.ParseCGO(Observables, ifile, beg, filename, myObsFactory, myModel, lineNo, rank);
171  IsEOF = tmpCO.isEOF();
172  CGO.push_back(tmpCO);
173 
174  } else if (type.compare("CustomObservable") == 0) {
175 
176  if (std::distance(tok->begin(), tok->end()) < 2) {
177  if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename + ".\n");
178  else sleep(2);
179  }
180  std::string customObsName = *beg;
181  beg++;
182  if (customObservableTypeMap.find(customObsName) == customObservableTypeMap.end()) {
183  if (rank == 0) throw std::runtime_error("\nERROR: No Observable Type defined for " + customObsName + "\n");
184  else sleep(2);
185  }
186  Observable * tmpcustomObs = CreateObservableType(customObsName);
187  tmpcustomObs->setObsType(customObsName);
188  beg = tmpcustomObs->ParseObservable(type, tok, beg, filepath, filename, rank);
189  tmpcustomObs->setTho(myObsFactory.CreateThMethod(tmpcustomObs->getThname(), *myModel));
190  Observables.push_back(tmpcustomObs);
191 
192  } else if (type.compare("ModelFlag") == 0) {
193  if (std::distance(tok->begin(), tok->end()) < 3) {
194  if(rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename);
195  else sleep (2);
196  }
197  std::string flagname = *beg;
198  ++beg;
199  if (boost::iequals(*beg, "true") || boost::iequals(*beg, "false")) {
200  /* Boolean flags */
201  bool value_bool;
202  if (boost::iequals(*beg, "true"))
203  value_bool = 1;
204  else
205  value_bool = 0;
206  if (!myModel->setFlag(flagname, value_bool)) {
207  if(rank == 0) throw std::runtime_error("ERROR: setFlag error for " + flagname);
208  else sleep (2);
209  }
210  else if (rank == 0) std::cout << "set flag " << flagname << "=" << *beg << std::endl;
211  } else {
212  /* String flags */
213  std::string value_str = *beg;
214  if (!myModel->setFlagStr(flagname, value_str)) {
215  if(rank == 0) throw std::runtime_error("ERROR: setFlag error for " + flagname);
216  else sleep (2);
217  } else if (rank == 0) std::cout << "set flag " << flagname << "=" << value_str << std::endl;
218  }
219  ++beg;
220  if (beg != tok->end() && rank == 0) std::cout << "WARNING: unread information in Flag " << flagname << std::endl;
221  } else if (type.compare("IncludeFile") == 0) {
222  std::string oldfilepath = filepath;
223  std::string oldfilename = filename;
224  int oldlineNo = lineNo;
225  std::string IncludeFileName = filepath + *beg;
226  if (rank == 0) std::cout << "Including File: " + IncludeFileName << std::endl;
227  ReadParameters(IncludeFileName, rank, ModelPars, Observables, Observables2D, CGO, CGP);
228  filepath = oldfilepath;
229  filename = oldfilename;
230  lineNo = oldlineNo;
231  IsEOF = false;
232  ++beg;
233  } else {
234  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");
235  else sleep(2);
236  }
237  delete tok;
238  tok = NULL;
239  } while (!IsEOF);
240 
241  if (modelset == 0 && rank == 0)
242  throw std::runtime_error("ERROR: Incorrect or missing model name in the model configuration file.\n");
243  if (!myModel->CheckFlags() && rank == 0)
244  throw std::runtime_error("ERROR: incompatible flag(s)\n");
245  delete sep;
246  sep = NULL;
247  return (modname);
248 }
249 
250 void InputParser::addCustomObservableType(const std::string name, boost::function<Observable*() > funct)
251 {
252  customObservableTypeMap[name] = funct;
253 }
254 
255 Observable * InputParser::CreateObservableType(const std::string& name) const
256 {
257  if (customObservableTypeMap.find(name) == customObservableTypeMap.end()) {
258  if (rank ==0) throw std::runtime_error("ERROR: No observable defined for " + name + " so it cannot be created");
259  else sleep(0);
260  }
261  return (customObservableTypeMap.at(name)());
262 }
CorrelatedGaussianObservables::setCovarianceFromConfig
void setCovarianceFromConfig(bool setInvCov)
A method to specify whether the inverse covariance is being set from the config file.
Definition: CorrelatedGaussianObservables.h:169
ModelFactory
A class for.
Definition: ModelFactory.h:25
InputParser::rank
int rank
Definition: InputParser.h:144
Observable2D::ParseObservable2D
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)
Definition: Observable2D.cpp:160
InputParser::~InputParser
virtual ~InputParser()
The default destructor.
Definition: InputParser.cpp:27
InputParser.h
HiggsObservable
A class for Higgs experimental analyses.
Definition: HiggsObservable.h:28
Observable2D::getThname2
std::string getThname2() const
A get method to access the thname of the second observable as defined in ThFactory class.
Definition: Observable2D.h:155
CorrelatedGaussianObservables::setIsPrediction
void setIsPrediction(bool IsPrediction_i)
A method to set a set of CGO to be predicted.
Definition: CorrelatedGaussianObservables.h:142
CorrelatedGaussianParameters
A class for correlated Gaussian parameters.
Definition: CorrelatedGaussianParameters.h:23
Observable::setTho
void setTho(ThObservable *tho_i)
A set method to fix the pointer to object of type ThObservable.
Definition: Observable.h:413
InputParser::InputParser
InputParser(ModelFactory &ModF, ThObsFactory &ObsF)
Constructor.
Definition: InputParser.cpp:16
CorrelatedGaussianObservables
A class for correlated Gaussian observables.
Definition: CorrelatedGaussianObservables.h:32
Observable::ParseObservable
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
HiggsObservable::setIsCorrelated
void setIsCorrelated(bool correlated)
Definition: HiggsObservable.h:89
InputParser
A class for reading input parameters and output directives.
Definition: InputParser.h:48
InputParser::CreateObservableType
Observable * CreateObservableType(const std::string &name) const
Definition: InputParser.cpp:255
InputParser::filename
std::string filename
Definition: InputParser.h:143
StandardModel
A model class for the Standard Model.
Definition: StandardModel.h:474
CorrelatedGaussianObservables::getObs
std::vector< Observable > getObs() const
A get method to access the vector of observables that are defined in one correlated Gaussian observab...
Definition: CorrelatedGaussianObservables.h:78
ModelFactory.h
InputParser::myModel
StandardModel * myModel
Pointer to an object of type StandardModel.
Definition: InputParser.h:132
Observable::getThname
std::string getThname() const
A get method to access the thname of the observable as defined in ThFactory class.
Definition: Observable.h:368
myModel
My own Model.
Definition: Doxygen/examples-src/myModel/src/myModel.h:17
CorrelatedGaussianParameters::ParseCGP
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.
Definition: CorrelatedGaussianParameters.cpp:129
ModelParameter::ParseModelParameter
boost::tokenizer< boost::char_separator< char > >::iterator & ParseModelParameter(boost::tokenizer< boost::char_separator< char > >::iterator &beg)
Parser for model parameters.
Definition: ModelParameter.cpp:39
myModel::InitializeModel
virtual bool InitializeModel()
Definition: Doxygen/examples-src/myModel/src/myModel.cpp:31
InputParser::ReadParameters
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
ThObsFactory
A class for.
Definition: ThObsFactory.h:26
InputParser::customObservableTypeMap
std::map< std::string, boost::function< Observable *()> > customObservableTypeMap
Definition: InputParser.h:140
ModelParameter::getname
std::string getname() const
A get method to get the name of each parameter.
Definition: ModelParameter.h:69
InputParser::myObsFactory
ThObsFactory & myObsFactory
Reference to an object of type ThObsFactory.
Definition: InputParser.h:134
InputParser::checkDuplicateParameter
std::map< std::string, boost::tuple< bool, std::string, int > > checkDuplicateParameter
Definition: InputParser.h:137
myModel::setFlag
virtual bool setFlag(const std::string name, const bool value)
Definition: Doxygen/examples-src/myModel/src/myModel.cpp:105
InputParser::myModelFactory
ModelFactory & myModelFactory
Pointer to an object of type ModelFactory.
Definition: InputParser.h:133
Observable
A class for observables.
Definition: Observable.h:28
StandardModel::setFlagStr
virtual bool setFlagStr(const std::string name, const std::string value)
A method to set a flag of StandardModel.
Definition: StandardModel.cpp:418
Model::IsModelInitialized
bool IsModelInitialized() const
A method to check if the model is initialized.
Definition: Model.h:136
InputParser::lineNo
int lineNo
Definition: InputParser.h:142
ModelParameter
A class for model parameters.
Definition: ModelParameter.h:28
Model::setModelName
void setModelName(const std::string name)
A method to set the name of the model.
Definition: Model.h:50
Observable2D
A class for analyzing observables pairwise.
Definition: Observable2D.h:24
CorrelatedGaussianObservables::isEOF
bool isEOF()
A method to check if the end of file has been reached.
Definition: CorrelatedGaussianObservables.h:133
CorrelatedGaussianObservables::ParseCGO
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.
Definition: CorrelatedGaussianObservables.cpp:84
InputParser::line
std::string line
Definition: InputParser.h:146
InputParser::IsEOF
bool IsEOF
Definition: InputParser.h:147
InputParser::addCustomObservableType
void addCustomObservableType(const std::string name, boost::function< Observable *() > funct)
Definition: InputParser.cpp:250
ModelFactory::CreateModel
StandardModel * CreateModel(const std::string &ModelName)
Definition: ModelFactory.cpp:74
InputParser::filepath
std::string filepath
Definition: InputParser.h:145
Observable2D::isEOF
bool isEOF()
A method to check if the end of file has been reached.
Definition: Observable2D.h:285
Model::getModelName
std::string getModelName() const
A method to fetch the name of the model.
Definition: Model.h:59
Observable::setObsType
void setObsType(std::string &obsType_s)
A set method to set the Observable type.
Definition: Observable.h:395
InputParser::modelset
bool modelset
Definition: InputParser.h:136
HiggsObservable::ParseHiggsObservable
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
Definition: HiggsObservable.cpp:197
Observable::getName
std::string getName() const
A get method to access the name of the observable.
Definition: Observable.h:323
CorrelatedGaussianParameters::isEOF
bool isEOF()
A method to check if the end of file has been reached.
Definition: CorrelatedGaussianParameters.h:129
StandardModel::CheckFlags
virtual bool CheckFlags() const
A method to check the sanity of the set of model flags.
Definition: StandardModel.cpp:449
InputParser::modeldefinedinfile
std::string modeldefinedinfile
Definition: InputParser.h:138
ThObsFactory::CreateThMethod
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...
Definition: ThObsFactory.cpp:4860
InputParser::modname
std::string modname
A string to store the model name in.
Definition: InputParser.h:135
Observable2D::setTho1Tho2
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