a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
ComputeObservables.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 HEPfit Collaboration
3 *
4 *
5 * For the licensing terms see doc/COPYING.
6 */
7
8#include "Observable.h"
9#include "Observable2D.h"
11#include "ComputeObservables.h"
12#include <boost/algorithm/string/predicate.hpp>
13
15 const std::string& ModelConf_i, const int rank_i)
16: myInputParser(ModelF, ThObsF),
17 rank(rank_i)
18{
19
20 std::string ModelName = myInputParser.ReadParameters(ModelConf_i, rank, ModPars, Obs, Obs2D, CGO, CGP);
21 std::map<std::string, double> DP;
22 for (std::vector<ModelParameter>::iterator it = ModPars.begin(); it < ModPars.end(); it++) {
23 DP[it->getname()] = it->getave();
24 paraNames.push_back(it->getname());
25 }
26 DPars = DP;
27
28 for (boost::ptr_vector<Observable>::iterator it = Obs.begin(); it < Obs.end(); it++) {
29 DObs.insert(std::pair<std::string, double> (it->getThname() , 0.));
30 DThObs.insert(std::pair<std::string, ThObservable*> (it->getThname() , it->getTho()));
31 }
32 for (std::vector<CorrelatedGaussianObservables>::iterator it1 = CGO.begin(); it1 < CGO.end(); it1++) {
33 std::vector<Observable> ObsInCGO = it1->getObs();
34 for (std::vector<Observable>::iterator it2 = ObsInCGO.begin(); it2 < ObsInCGO.end(); it2++) {
35 DObs.insert(std::pair<std::string, double> (it2->getThname() , 0.));
36 DThObs.insert(std::pair<std::string, ThObservable*> (it2->getThname() , it2->getTho()));
37 }
38 }
39
41 if (!Mod->Init(DP)) {
43 std::cout << "\nPlease set the following parameters in the model configuration files:\n" << std::endl;
44 std::vector<std::string> missingParameters = myInputParser.getModel()->getmissingModelParameters(); // Store the result in a variable to avoid dangling pointer warning
45 for (std::vector<std::string>::iterator it = missingParameters.begin(); it != missingParameters.end(); it++) {
46 std::cout << "ModelParameter\t" << *it << std::endl;
47 }
48 std::cout << std::endl;
49 missingParameters.clear();
50 }
51 throw std::runtime_error("ERROR: Parameter(s) missing in model initialization.\n");
52 }
53
55 if (unknownParameters.size() > 0 && rank == 0) {
56 std::cout << "\n" << std::endl;
57 for (std::vector<std::string>::iterator it = unknownParameters.begin(); it != unknownParameters.end(); it++)
58 std::cout << "WARNING: unknown parameter " << *it << " not added." << std::endl;
59 }
60}
61
63 const std::string& ModelName_i, std::map<std::string, double> DPars_i, std::map<std::string, std::string> DFlags_i,
64 const int rank_i)
65: ModelName(ModelName_i),
66 myInputParser(ModelF, ThObsF),
67 rank(rank_i)
68{
69 for (std::map<std::string, double>::iterator it = DPars_i.begin(); it != DPars_i.end(); it++) {
70 paraNames.push_back(it->first);
71 }
75 if (Mod->IsModelInitialized()) {
76 if (rank == 0) std::cout << "\nModel Initialized: " << ModelName << std::endl;
77 } else {
78 throw std::runtime_error("\nERROR: " + ModelName + " not initialized successfully.\n");
79 }
80 setFlags(DFlags_i);
81 if (!Mod->Init(DPars_i)) {
83 std::cout << "\nPlease set the following parameters in the model configuration files:\n" << std::endl;
84 std::vector<std::string> missingParameters = myInputParser.getModel()->getmissingModelParameters(); // Store the result in a variable to avoid dangling pointer warning
85 for (std::vector<std::string>::iterator it = missingParameters.begin(); it != missingParameters.end(); it++) {
86 std::cout << "ModelParameter\t" << *it << std::endl;
87 }
88 std::cout << std::endl;
90 }
91 throw std::runtime_error("\nERROR: " + ModelName + " cannot be initialized.\n");
92 }
93
95 if (unknownParameters.size() > 0 && rank == 0) {
96 std::cout << "\n" << std::endl;
97 for (std::vector<std::string>::iterator it = unknownParameters.begin(); it != unknownParameters.end(); it++)
98 std::cout << "WARNING: unknown parameter " << *it << " not added to MCMC" << std::endl;
99 }
100}
101
103{
104 Obs.clear();
105 boost::ptr_vector<Observable>().swap(Obs);
106 Mod = NULL;
107}
108
109void ComputeObservables::setFlags(std::map<std::string, std::string> DFlags_i)
110{
111 for (std::map<std::string, std::string>::iterator it = DFlags_i.begin(); it != DFlags_i.end(); it++) {
112 if (!(boost::iequals(it->second, "true")) && !(boost::iequals(it->second, "false")) && !Mod->setFlagStr(it->first, it->second)) {
113 throw std::runtime_error("ERROR: setFlagStr error for " + it->first);
114 } else if (boost::iequals(it->second, "true") && !Mod->setFlag(it->first, 1)) {
115 throw std::runtime_error("ERROR: setFlag error for " + it->first);
116 } else if (boost::iequals(it->second, "false") && !Mod->setFlag(it->first, 0)) {
117 throw std::runtime_error("ERROR: setFlag error for " + it->first);
118 } else {
119 if (rank == 0) std::cout << "set flag " << it->first << " = " << it->second << std::endl;
120 }
121 }
122}
123
124std::map<std::string, double> ComputeObservables::compute(std::map<std::string, double> DP)
125{
126 if (DP != DPars)
127 for (std::map<std::string, double>::iterator it = DP.begin(); it != DP.end(); it++) {
128 if(!(std::find(paraNames.begin(), paraNames.end(), it->first) != paraNames.end()))
129 throw std::runtime_error("\nERROR: Incorrect parameter name " + it->first + " passed to ComputeObservable");
130 }
131 DPars = DP;
132 Mod->Update(DPars);
133 for (std::map<std::string, double>::iterator it = DObs.begin(); it != DObs.end(); it++) {
134 DObs[it->first] = DThObs[it->first]->computeThValue();
135 }
136 return(DObs);
137}
138
140{
141 if(DObs.find(ObsName) == DObs.end())
142 throw std::runtime_error("\nERROR: Observable cannot be removed since it has not been added.\n");
143 DObs.erase(ObsName);
144 if(DThObs.find(ObsName) == DThObs.end())
145 throw std::runtime_error("\nERROR: ThObservable cannot be removed since it has not been added.\n");
146 DThObs.erase(ObsName);
147}
148
149void ComputeObservables::AddObservable(std::string ObsName)
150{
151 DThObs[ObsName] = myInputParser.getObsFactory().CreateThMethod(ObsName, *Mod);
152 DObs.insert(std::pair<std::string, double> (ObsName , 0.));
153}
154
155void ComputeObservables::addCustomObservableType(const std::string name, boost::function<Observable*() > funct){
157}
std::string ModelName
Definition: Minimal.cpp:17
const int rank
< Rank of the MPI process. Set to 0 for serial run.
InputParser myInputParser
An object of the InputParser class.
std::vector< std::string > unknownParameters
StandardModel * Mod
Pointer to an object of the class StandardModel.
void setFlags(std::map< std::string, std::string > DFlags_i)
This method sets the necessary flag for the requested model.
std::string ModelName
Name of the Model to be used.
std::map< std::string, double > DObs
Map of the observables to be computed.
std::map< std::string, double > compute(std::map< std::string, double > DP)
The method used to compute observables.
std::vector< CorrelatedGaussianParameters > CGP
std::vector< ModelParameter > ModPars
std::vector< Observable2D > Obs2D
std::map< std::string, ThObservable * > DThObs
virtual ~ComputeObservables()
The default destructor.
std::vector< std::string > paraNames
The vector of allowed parameter names.
ComputeObservables(ModelFactory &ModelF, ThObsFactory &ThObsF, const std::string &ModelConf_i, const int rank_i=0)
Constructor.
boost::ptr_vector< Observable > Obs
void addCustomObservableType(const std::string name, boost::function< Observable *() > funct)
A method to add a user defined observable with a user defined likelihood.
void AddObservable(std::string ObsName)
A method to remove an observable from the list of observables.
std::map< std::string, double > DPars
Map of the parameters to be passed to Model.
void RemoveObservable(std::string ObsName)
A method to add an observable to the list of observables.
std::vector< CorrelatedGaussianObservables > CGO
const ThObsFactory & getObsFactory() const
Definition: InputParser.h:114
void addCustomObservableType(const std::string name, boost::function< Observable *() > funct)
StandardModel * getModel() const
A get method to access the pointer to the object of the StandardModel class.
Definition: InputParser.h:109
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
ModelFactory & getModelFactory() const
Definition: InputParser.h:119
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::vector< std::string > getmissingModelParameters()
Definition: Model.h:255
A class for observables.
Definition: Observable.h:28
std::vector< std::string > getUnknownParameters()
A method to get the vector of the parameters that have been specified in the configuration file but n...
Definition: QCD.h:469
virtual bool Init(const std::map< std::string, double > &DPars)
A method to initialize the model parameters.
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 Update(const std::map< std::string, double > &DPars)
The update method for 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...