InputParser Class Reference

A class for reading input parameters and output directives. More...

#include <InputParser.h>

Collaboration diagram for InputParser:
[legend]

Detailed Description

A class for reading input parameters and output directives.

Author
HEPfit Collaboration

The InputParser parses the SomeModel.conf file for the name of the model, ModelParameters, Observables, * Observables2D, ModelParaVsObs and CorrelatedGaussianObservables. The observables (or set of observables) are parsed for their inclusion in the Monte Carlo run.

Definition at line 49 of file InputParser.h.

Public Member Functions

void addCustomObservableType (const std::string name, boost::function< Observable *() > funct)
 
ObservableCreateObservableType (const std::string &name) const
 
StandardModelgetModel () const
 A get method to access the pointer to the object of the StandardModel class. More...
 
ModelFactorygetModelFactory () const
 
const ThObsFactorygetObsFactory () const
 
 InputParser (ModelFactory &ModF, ThObsFactory &ObsF)
 Constructor. More...
 
 InputParser (const InputParser &orig)
 The copy constructor. More...
 
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. More...
 
void setModel (StandardModel *myModel_i)
 
virtual ~InputParser ()
 The default destructor. More...
 

Private Attributes

std::map< std::string, boost::tuple< bool, std::string, int > > checkDuplicateParameter
 
std::map< std::string, boost::function< Observable *()> > customObservableTypeMap
 
std::string filename
 
std::string filepath
 
bool IsEOF
 
std::string line
 
int lineNo
 
std::string modeldefinedinfile
 
bool modelset
 
std::string modname
 A string to store the model name in. More...
 
StandardModelmyModel
 Pointer to an object of type StandardModel. More...
 
ModelFactorymyModelFactory
 Pointer to an object of type ModelFactory. More...
 
ThObsFactorymyObsFactory
 Reference to an object of type ThObsFactory. More...
 
int rank
 
boost::char_separator< char > * sep
 
boost::tokenizer< boost::char_separator< char > > * tok
 

Constructor & Destructor Documentation

InputParser::InputParser ( ModelFactory ModF,
ThObsFactory ObsF 
)

Constructor.

Definition at line 15 of file InputParser.cpp.

15  : myModelFactory(ModF), myObsFactory(ObsF), filename(""), rank(0)
16 {
17  modelset = 0;
18 }
ThObsFactory & myObsFactory
Reference to an object of type ThObsFactory.
Definition: InputParser.h:135
std::string filename
Definition: InputParser.h:144
ModelFactory & myModelFactory
Pointer to an object of type ModelFactory.
Definition: InputParser.h:134
InputParser::InputParser ( const InputParser orig)

The copy constructor.

Parameters
[in]origa reference to an object of type InputParser()

Definition at line 20 of file InputParser.cpp.

21 {
22  myModel = new StandardModel(*orig.myModel);
23 }
ThObsFactory & myObsFactory
Reference to an object of type ThObsFactory.
Definition: InputParser.h:135
A model class for the Standard Model.
StandardModel * myModel
Pointer to an object of type StandardModel.
Definition: InputParser.h:133
std::string filename
Definition: InputParser.h:144
ModelFactory & myModelFactory
Pointer to an object of type ModelFactory.
Definition: InputParser.h:134
My own Model.
Definition: myModel.h:17
InputParser::~InputParser ( )
virtual

The default destructor.

Definition at line 25 of file InputParser.cpp.

26 {}

Member Function Documentation

void InputParser::addCustomObservableType ( const std::string  name,
boost::function< Observable *() >  funct 
)

Definition at line 213 of file InputParser.cpp.

214 {
215  customObservableTypeMap[name] = funct;
216 }
std::map< std::string, boost::function< Observable *()> > customObservableTypeMap
Definition: InputParser.h:141
Observable * InputParser::CreateObservableType ( const std::string &  name) const

Definition at line 218 of file InputParser.cpp.

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 }
std::map< std::string, boost::function< Observable *()> > customObservableTypeMap
Definition: InputParser.h:141
StandardModel* InputParser::getModel ( ) const
inline

A get method to access the pointer to the object of the StandardModel class.

Returns
myModel a pointer to the object of the StandardModel class

Definition at line 110 of file InputParser.h.

111  {
112  return myModel;
113  }
StandardModel * myModel
Pointer to an object of type StandardModel.
Definition: InputParser.h:133
ModelFactory& InputParser::getModelFactory ( ) const
inline

Definition at line 120 of file InputParser.h.

120  {
121  return myModelFactory;
122  }
ModelFactory & myModelFactory
Pointer to an object of type ModelFactory.
Definition: InputParser.h:134
const ThObsFactory& InputParser::getObsFactory ( ) const
inline

Definition at line 115 of file InputParser.h.

116  {
117  return myObsFactory;
118  }
ThObsFactory & myObsFactory
Reference to an object of type ThObsFactory.
Definition: InputParser.h:135
std::string InputParser::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.

Parameters
[in]Observablesa vector of Observables
[in]ifilethe file being read
[in]began iterator over words in a line separated by a specific separator character Responsible for parsing the SomeModel.conf file.

This method parses the SomeModel.conf file for all input instructions. The algorithm is as follows:

  • Search for the predefined names of the available models, initialize the necessary model class and build a ThFactory for that model to define the observables.
  • Search and read the ModelParameter list.
  • Search and read the CorrelatedGaussianParameters.
  • Search and read the Observable list using the ParseObservable() method.
  • Search and read the Observable2D list using the ParseObservable() method.
  • Search and read the CorrelatedGaussianObservables.
  • Return the name of the model initialized.
    Parameters
    [in]filenamename of the SomeModel.conf file passed as an argument to the executable
    [in]rankthe rank of the process in an MPI run (set to 0 for a serial run)
    [out]ModelParsthe vector of model parameters
    [out]Observablesthe vector of observables
    [out]Observables2Dthe vector of observable pairs
    [out]CGOthe vector of correlated Gaussian observables
    [out]CGPthe vector of correlated Gaussian parameters
    Returns
    modname the name of the model initialized

Definition at line 28 of file InputParser.cpp.

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 }
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
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.
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
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
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
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.
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
void InputParser::setModel ( StandardModel myModel_i)
inline

Definition at line 128 of file InputParser.h.

128  {
129  myModel = myModel_i;
130  }
My own Model.
Definition: myModel.h:17

Member Data Documentation

std::map<std::string, boost::tuple<bool, std::string, int> > InputParser::checkDuplicateParameter
private

Definition at line 138 of file InputParser.h.

std::map<std::string, boost::function<Observable* ()> > InputParser::customObservableTypeMap
private

Definition at line 141 of file InputParser.h.

std::string InputParser::filename
private

Definition at line 144 of file InputParser.h.

std::string InputParser::filepath
private

Definition at line 146 of file InputParser.h.

bool InputParser::IsEOF
private

Definition at line 150 of file InputParser.h.

std::string InputParser::line
private

Definition at line 149 of file InputParser.h.

int InputParser::lineNo
private

Definition at line 143 of file InputParser.h.

std::string InputParser::modeldefinedinfile
private

Definition at line 139 of file InputParser.h.

bool InputParser::modelset
private

Definition at line 137 of file InputParser.h.

std::string InputParser::modname
private

A string to store the model name in.

Definition at line 136 of file InputParser.h.

StandardModel* InputParser::myModel
private

Pointer to an object of type StandardModel.

Definition at line 133 of file InputParser.h.

ModelFactory& InputParser::myModelFactory
private

Pointer to an object of type ModelFactory.

Definition at line 134 of file InputParser.h.

ThObsFactory& InputParser::myObsFactory
private

Reference to an object of type ThObsFactory.

Definition at line 135 of file InputParser.h.

int InputParser::rank
private

Definition at line 145 of file InputParser.h.

boost::char_separator<char>* InputParser::sep
private

Definition at line 148 of file InputParser.h.

boost::tokenizer<boost::char_separator<char> >* InputParser::tok
private

Definition at line 147 of file InputParser.h.


The documentation for this class was generated from the following files: