InputParser.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 HEPfit Collaboration
3  * All rights reserved.
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 
15 InputParser::InputParser(ModelFactory& ModF, ThObsFactory& ObsF) : myModelFactory(ModF), myObsFactory(ObsF), filename(""), rank(0)
16 {
17  modelset = 0;
18 }
19 
20 InputParser::InputParser(const InputParser& orig) : myModelFactory(orig.myModelFactory), myObsFactory(orig.myObsFactory), filename(""), rank(0)
21 {
22  myModel = new StandardModel(*orig.myModel);
23 }
24 
26 {}
27 
28 std::string InputParser::ReadParameters(const std::string filename_i,
29  const int rank_i,
30  std::vector<ModelParameter>& ModelPars,
31  boost::ptr_vector<Observable>& Observables,
32  std::vector<Observable2D>& Observables2D,
33  std::vector<CorrelatedGaussianObservables>& CGO,
34  std::vector<CorrelatedGaussianParameters>& CGP)
35 {
36  filename = filename_i;
37  rank = rank_i;
38  modname = "";
39  lineNo = 0;
40  std::ifstream ifile(filename.c_str());
41  if (!ifile.is_open()) {
42  if(rank == 0) throw std::runtime_error("\nERROR: " + filename + " does not exist. Make sure to specify a valid model configuration file.\n");
43  else sleep (2);
44  }
45 
46  if (filename.find("\\/") == std::string::npos) filepath = filename.substr(0, filename.find_last_of("\\/") + 1);
47  IsEOF = false;
48  do {
49  IsEOF = getline(ifile, line).eof();
50  lineNo++;
51  if (*line.rbegin() == '\r') line.erase(line.length() - 1); // for CR+LF
52  if (line.empty() || line.find_first_not_of(' ') == std::string::npos || line.at(0) == '#')
53  continue;
54  sep = new boost::char_separator<char>(" \t");
55  tok = new boost::tokenizer<boost::char_separator<char> >(line, *sep);
56  boost::tokenizer<boost::char_separator<char> >::iterator beg = tok->begin();
57 
58  if (modelset == 0) {
59  modname = *beg;
63  if (myModel->IsModelInitialized()) {
64  if (rank == 0) std::cout << "\nModel Initialized: " << modname << "\n" << std::endl;
66  } else if (rank == 0)
67  throw std::runtime_error("\nERROR: " + modname + " not initialized successfully.\n");
68  modelset = 1;
69  continue;
70  } else if (modelset == 1 && beg->compare(myModel->ModelName()) == 0) {
71  continue;
72  }
73 
74  std::string type = *beg;
75  ++beg;
76  if (type.compare("ModelParameter") == 0) {
77 
78  if (std::distance(tok->begin(), tok->end()) < 5) {
79  if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename + ".\n");
80  else sleep(2);
81  } else {
82  ModelParameter tmpMP;
83  beg = tmpMP.ParseModelParameter(beg);
84 
85  if (checkDuplicateParameter[tmpMP.getname()].get<0>()) {
86  if(rank == 0) throw std::runtime_error("\nERROR: ModelParameter " + tmpMP.getname() + " appears more than once ...!! \n" +
87  "1st Occurrence: Line No:" + boost::lexical_cast<std::string>(checkDuplicateParameter[tmpMP.getname()].get<2>()) +
88  " in file " + checkDuplicateParameter[tmpMP.getname()].get<1>() + ".\n"
89  "2nd Occurrence: Line No:" + boost::lexical_cast<std::string>(lineNo) + " in file " + filename + ".\n");
90  else sleep (2);
91  }
92 
93  if (beg != tok->end())
94  if (rank == 0) std::cout << "WARNING: unread information in parameter " << tmpMP.getname() << std::endl;
95  checkDuplicateParameter[tmpMP.getname()] = boost::make_tuple(true, filename, lineNo);
96 
97  ModelPars.push_back(tmpMP);
98  }
99 
100  } else if (type.compare("CorrelatedGaussianParameters") == 0) {
101 
103  lineNo = tmpCGP.ParseCGP(ModelPars, filename, ifile, beg, lineNo, rank);
104  IsEOF = tmpCGP.isEOF();
105  CGP.push_back(tmpCGP);
106 
107  } else if (type.compare("Observable") == 0 || type.compare("BinnedObservable") == 0 || type.compare("FunctionObservable") == 0) {
108 
109  Observable * tmpObs = new Observable();
110  beg = tmpObs->ParseObservable(type, tok, beg, filepath, filename, rank);
111  tmpObs->setTho(myObsFactory.CreateThMethod(tmpObs->getThname(), *myModel));
112  Observables.push_back(tmpObs);
113 
114  } else if (type.compare("Observable2D") == 0) {
115 
116  Observable2D tmpObs2D;
117  lineNo = tmpObs2D.ParseObservable2D(type, tok, beg, filename, ifile, lineNo, rank);
119  IsEOF = tmpObs2D.isEOF();
120  Observables2D.push_back(tmpObs2D);
121 
122  } else if (type.compare("HiggsObservable") == 0) {
123 
124  Observable * tmphObs = new Observable();
125  beg = tmphObs->ParseObservable(type, tok, beg, filepath, filename, rank);
126  tmphObs->setTho(myObsFactory.CreateThMethod(tmphObs->getThname(), *myModel));
127  HiggsObservable * tmpho = new HiggsObservable(*tmphObs);
128  beg = tmpho->ParseHiggsObservable(beg, myObsFactory, myModel, rank);
129  Observables.push_back(tmpho);
130  ++beg;
131  if (beg != tok->end() && rank == 0) std::cout << "WARNING: unread information in HiggsObservable " << tmpho->getName() << std::endl;
132 
133  } else if (type.compare("CorrelatedGaussianObservables") == 0) {
135  lineNo = tmpCGO.ParseCGO(Observables, ifile, beg, filename, myObsFactory, myModel, lineNo, rank);
136  IsEOF = tmpCGO.isEOF();
137  if (tmpCGO.getObs().size() > 1) CGO.push_back(tmpCGO);
138 
139  } else if (type.compare("CorrelatedObservables") == 0) {
141  tmpCO.setIsPrediction(true);
142  lineNo = tmpCO.ParseCGO(Observables, ifile, beg, filename, myObsFactory, myModel, lineNo, rank);
143  IsEOF = tmpCO.isEOF();
144  CGO.push_back(tmpCO);
145 
146  } else if (type.compare("CustomObservable") == 0) {
147  if (std::distance(tok->begin(), tok->end()) < 2) {
148  if (rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename + ".\n");
149  else sleep(2);
150  }
151  std::string customObsName = *beg;
152  beg++;
153  if (customObservableTypeMap.find(customObsName) == customObservableTypeMap.end()) {
154  if (rank == 0) throw std::runtime_error("\nERROR: No Observable Type defined for " + customObsName + "\n");
155  else sleep(2);
156  }
157  Observable * tmpcustomObs = CreateObservableType(customObsName);
158  tmpcustomObs->setObsType(customObsName);
159  beg = tmpcustomObs->ParseObservable(type, tok, beg, filepath, filename, rank);
160  tmpcustomObs->setTho(myObsFactory.CreateThMethod(tmpcustomObs->getThname(), *myModel));
161  Observables.push_back(tmpcustomObs);
162 
163  } else if (type.compare("ModelFlag") == 0) {
164  if (std::distance(tok->begin(), tok->end()) < 3) {
165  if(rank == 0) throw std::runtime_error("ERROR: lack of information on " + *beg + " in " + filename);
166  else sleep (2);
167  }
168  std::string flagname = *beg;
169  ++beg;
170  if (boost::iequals(*beg, "true") || boost::iequals(*beg, "false")) {
171  /* Boolean flags */
172  bool value_bool;
173  if (boost::iequals(*beg, "true"))
174  value_bool = 1;
175  else
176  value_bool = 0;
177  if (!myModel->setFlag(flagname, value_bool)) {
178  if(rank == 0) throw std::runtime_error("ERROR: setFlag error for " + flagname);
179  else sleep (2);
180  }
181  else if (rank == 0) std::cout << "set flag " << flagname << "=" << *beg << std::endl;
182  } else {
183  /* String flags */
184  std::string value_str = *beg;
185  if (!myModel->setFlagStr(flagname, value_str)) {
186  if(rank == 0) throw std::runtime_error("ERROR: setFlag error for " + flagname);
187  else sleep (2);
188  } else if (rank == 0) std::cout << "set flag " << flagname << "=" << value_str << std::endl;
189  }
190  ++beg;
191  if (beg != tok->end() && rank == 0) std::cout << "WARNING: unread information in Flag " << flagname << std::endl;
192  } else if (type.compare("IncludeFile") == 0) {
193  std::string IncludeFileName = filepath + *beg;
194  if (rank == 0) std::cout << "Including File: " + IncludeFileName << std::endl;
195  ReadParameters(IncludeFileName, rank, ModelPars, Observables, Observables2D, CGO, CGP);
196  IsEOF = false;
197  ++beg;
198  } else {
199  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");
200  else sleep(2);
201  }
202 
203  } while (!IsEOF);
204 
205  if (modelset == 0 && rank == 0)
206  throw std::runtime_error("ERROR: Incorrect or missing model name in the model configuration file.\n");
207  if (!myModel->CheckFlags() && rank == 0)
208  throw std::runtime_error("ERROR: incompatible flag(s)\n");
209 
210  return (modname);
211 }
212 
213 void InputParser::addCustomObservableType(const std::string name, boost::function<Observable*() > funct)
214 {
215  customObservableTypeMap[name] = funct;
216 }
217 
218 Observable * InputParser::CreateObservableType(const std::string& name) const
219 {
220  if (customObservableTypeMap.find(name) == customObservableTypeMap.end()) {
221  if (rank ==0) throw std::runtime_error("ERROR: No observable defined for " + name + " so it cannot be created");
222  else sleep(0);
223  }
224  return (customObservableTypeMap.at(name)());
225 }
virtual ~InputParser()
The default destructor.
Definition: InputParser.cpp:25
boost::char_separator< char > * sep
Definition: InputParser.h:148
ThObsFactory & myObsFactory
Reference to an object of type ThObsFactory.
Definition: InputParser.h:135
A class for correlated Gaussian parameters.
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)
std::vector< Observable > getObs() const
A get method to access the vector of observables that are defined in one correlated Gaussian observab...
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:28
virtual bool setFlagStr(const std::string name, const std::string value)
A method to set a flag of StandardModel.
StandardModel * CreateModel(const std::string &ModelName)
void setTho(ThObservable *tho_i)
A set method to fix the pointer to object of type ThObservable.
Definition: Observable.h:385
A class for.
Definition: ModelFactory.h:25
std::string getThname2() const
A get method to access the thname of the second observable as defined in ThFactory class...
Definition: Observable2D.h:155
bool isEOF()
A method to check if the end of file has been reached.
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.
A class for.
Definition: ThObsFactory.h:26
virtual bool CheckFlags() const
A method to check the sanity of the set of model flags.
virtual bool InitializeModel()
Definition: myModel.cpp:31
std::string line
Definition: InputParser.h:149
A class for analyzing observables pairwise.
Definition: Observable2D.h:24
bool isEOF()
A method to check if the end of file has been reached.
Definition: Observable2D.h:285
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:184
Observable * CreateObservableType(const std::string &name) const
A model class for the Standard Model.
std::string getThname() const
A get method to access the thname of the observable as defined in ThFactory class.
Definition: Observable.h:340
StandardModel * myModel
Pointer to an object of type StandardModel.
Definition: InputParser.h:133
void addCustomObservableType(const std::string name, boost::function< Observable *() > funct)
ThObservable * CreateThMethod(const std::string &name, const StandardModel &model) const
This method checks for the existence of an observable of a specific name in the map thobs and returns...
std::string filepath
Definition: InputParser.h:146
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 model parameters.
bool IsModelInitialized() const
A method to check if the model is initialized.
Definition: Model.h:133
std::string getName() const
A get method to access the name of the observable.
Definition: Observable.h:304
void setIsPrediction(bool IsPrediction_i)
A method to set a set of CGO to be predicted.
std::string filename
Definition: InputParser.h:144
boost::tokenizer< boost::char_separator< char > > * tok
Definition: InputParser.h:147
A class for observables.
Definition: Observable.h:28
A class for reading input parameters and output directives.
Definition: InputParser.h:49
void setModelName(const std::string name)
A method to set the name of the model.
Definition: Model.h:47
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
bool isEOF()
A method to check if the end of file has been reached.
ModelFactory & myModelFactory
Pointer to an object of type ModelFactory.
Definition: InputParser.h:134
A class for correlated Gaussian observables.
A class for Higgs experimental analyses.
std::string getname() const
A get method to get the name of each parameter.
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.
InputParser(ModelFactory &ModF, ThObsFactory &ObsF)
Constructor.
Definition: InputParser.cpp:15
boost::tokenizer< boost::char_separator< char > >::iterator & ParseModelParameter(boost::tokenizer< boost::char_separator< char > >::iterator &beg)
Parser for model parameters.
std::string modname
A string to store the model name in.
Definition: InputParser.h:136
virtual bool setFlag(const std::string name, const bool value)
Definition: myModel.cpp:105
std::string ModelName() const
A method to fetch the name of the model.
Definition: Model.h:56
std::map< std::string, boost::tuple< bool, std::string, int > > checkDuplicateParameter
Definition: InputParser.h:138
My own Model.
Definition: myModel.h:17
std::map< std::string, boost::function< Observable *()> > customObservableTypeMap
Definition: InputParser.h:141
void setObsType(std::string &obsType_s)
A set method to set the Observable type.
Definition: Observable.h:367
std::string modeldefinedinfile
Definition: InputParser.h:139