a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
CorrelatedGaussianObservables.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 <stdexcept>
10 #include "ThObsFactory.h"
11 #include <fstream>
12 
14 {
15  name = name_i;
16  IsPrediction = false;
17  IsEOF = false;
18  covarianceFromConfig = false;
19  InvCov = NULL;
20 }
21 
23 {
24  name = "";
25  IsPrediction = false;
26  IsEOF = false;
27  covarianceFromConfig = false;
28  InvCov = NULL;
29 }
30 
32 {
33  Obs = orig.Obs;
34  name = orig.name;
35  if (orig.InvCov) InvCov = new gslpp::matrix<double>(*orig.InvCov);
36  else InvCov = NULL;
38  IsEOF = orig.IsEOF;
39  covarianceFromConfig = false;
40 }
41 
43 {
44 // Cov = new gslpp::matrix<double>(100, 100, 0.); /** Put in to prevent seg fault during error handling in InputParser **/
45  if (InvCov) delete(InvCov);
46 }
47 
49 {
50  Obs.push_back(Obs_i);
51 }
52 
54 {
55  unsigned int size = Obs.size();
56  if (Corr.size_i() != size || Corr.size_j() != size)
57  throw std::runtime_error("The size of the correlated observables in " + name + " does not match the size of the correlation matrix!");
58  InvCov = new gslpp::matrix<double>(size, size, 0.);
60  for (unsigned int i = 0; i < size; i++)
61  for (unsigned int j = 0; j < size; j++)
62  (*InvCov)(i, j) = Corr(i, j);
63  } else {
64  for (unsigned int i = 0; i < size; i++) {
65  for (unsigned int j = 0; j < size; j++) {
66  if (Corr(i, j) != Corr(j, i)) throw std::runtime_error("Invalid correlation matrix for " + name + ". The matrix is not symmetric: for elements [" + boost::lexical_cast<std::string>(i) + ", " + boost::lexical_cast<std::string>(j) + "]");
67  (*InvCov)(i, j) = Obs.at(i).getErrg() * Corr(i, j) * Obs.at(j).getErrg();
68  }
69  }
70  *InvCov = InvCov->inverse();
71  }
72 }
73 
75 {
76  gslpp::vector<double> x(Obs.size());
77 
78  for (unsigned int i = 0; i < Obs.size(); i++)
79  x(i) = Obs.at(i).computeTheoryValue() - Obs.at(i).getAve();
80 
81  return (-0.5 * x * ((*InvCov) * x));
82 }
83 
84 int CorrelatedGaussianObservables::ParseCGO(boost::ptr_vector<Observable>& Observables,
85  std::ifstream& ifile,
86  boost::tokenizer<boost::char_separator<char> >::iterator& beg,
87  std::string& infilename,
88  ThObsFactory& myObsFactory,
90  int lineNo,
91  int rank) {
92  if (infilename.find("\\/") == std::string::npos) filepath = infilename.substr(0, infilename.find_last_of("\\/") + 1);
93  boost::char_separator<char> sep(" \t");
94  name = *beg;
95  ++beg;
96  int size = atoi((*beg).c_str());
97 
98  int nlines = 0;
99  std::vector<bool> lines;
100  std::string line;
101  for (int i = 0; i < size; i++) {
102  IsEOF = getline(ifile, line).eof();
103  if (line.empty() || line.at(0) == '#') {
104  if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in CorrelatedGaussianObservables please! In file " + infilename + " at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n");
105  else sleep(2);
106  }
107  lineNo++;
108  boost::tokenizer<boost::char_separator<char> > tok(line, sep);
109  beg = tok.begin();
110  std::string type = *beg;
111  ++beg;
112  if (type.compare("Observable") != 0 && type.compare("BinnedObservable") != 0 && type.compare("FunctionObservable") != 0) {
113  if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", expecting an Observable or BinnedObservable or FunctionObservable type here...\n");
114  else sleep(2);
115  }
116  Observable * tmpObs = new Observable();
118  beg = tmpObs->ParseObservable(type, &tok, beg, filepath, infilename, rank);
119  tmpObs->setTho(myObsFactory.CreateThMethod(tmpObs->getThname(), *myModel));
120  if (!IsPrediction) {
121  if (tmpObs->isTMCMC()) {
122  AddObs(*tmpObs);
123  lines.push_back(true);
124  nlines++;
125  } else {
126  Observables.push_back(tmpObs);
127  lines.push_back(false);
128  }
129  } else {
130  if (tmpObs->isTMCMC()) {
131  if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", Observable/BinnedObservable cannot be set to MCMC in CorrelatedObservable. Use CorrelatedGaussianObservable instead.\n");
132  else sleep(2);
133  } else {
134  AddObs(*tmpObs);
135  nlines++;
136  }
137  if (tmpObs->getDistr().compare("weight") == 0 || tmpObs->getDistr().compare("file") == 0) {
138  if (rank == 0) throw std::runtime_error("ERROR: in line no." + boost::lexical_cast<std::string>(lineNo) + " of file " + infilename + ", Observable/BinnedObservable cannot be set to weight or file in CorrelatedObservable. Use CorrelatedGaussianObservable instead.\n");
139  else sleep(2);
140  }
141  }
142  }
143 
144  if (nlines > 1) {
145  if (!IsPrediction) {
147  int ni = 0;
148  for (int i = 0; i < size; i++) {
149  IsEOF = getline(ifile, line).eof();
150  if (line.empty() || line.at(0) == '#') {
151  if (rank == 0) throw std::runtime_error("ERROR: no comments or empty lines in CorrelatedGaussianObservables please! In file " + infilename + " at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n");
152  else sleep(2);
153  }
154  lineNo++;
155  if (lines.at(i)) {
156  boost::tokenizer<boost::char_separator<char> > mytok(line, sep);
157  beg = mytok.begin();
158  int nj = 0;
159  for (int j = 0; j < size; j++) {
160  if ((*beg).compare(0, 1, "0") == 0
161  || (*beg).compare(0, 1, "1") == 0
162  || (*beg).compare(0, 1, "-") == 0 || (covarianceFromConfig == true)) {
163  if (std::distance(mytok.begin(), mytok.end()) < size) {
164  if (rank == 0) throw std::runtime_error(("ERROR: Correlation matrix is of wrong size in Correlated Gaussian Observables: " + name + +" at line number:" + boost::lexical_cast<std::string>(lineNo) + ".\n").c_str());
165  else sleep(2);
166  }
167  if (lines.at(j)) {
168  myCorr(ni, nj) = atof((*beg).c_str());
169  nj++;
170  }
171  beg++;
172  } else {
173  if (rank == 0) throw std::runtime_error("ERROR: invalid correlation matrix for " + name + ". Check element (" + boost::lexical_cast<std::string>(ni + 1) + "," + boost::lexical_cast<std::string>(nj + 1) + ") in line number " + boost::lexical_cast<std::string>(lineNo) + " in file " + infilename + ".\n");
174  else sleep(2);
175  }
176  }
177  ni++;
178  }
179  }
180  ComputeCov(myCorr);
181  } else {
182  InvCov = new gslpp::matrix<double>(size, size, 0.);
183  }
184  } else {
185  if (rank == 0) std::cout << "\nWARNING: Correlated (Gaussian) Observable " << name.c_str() << " defined with less than two observables" << " in file " << infilename << ". The set is being marked as normal Observables." << std::endl;
186  if (getObs().size() == 1) Observables.push_back(new Observable(getObs(0)));
187  for (int i = 0; i < size; i++) {
188  IsEOF = getline(ifile, line).eof();
189  lineNo++;
190  }
191  }
192  return lineNo;
193 }
CorrelatedGaussianObservables::IsPrediction
bool IsPrediction
Flag to define a set of Correlated Observables to be predicted.
Definition: CorrelatedGaussianObservables.h:180
Observable::getDistr
std::string getDistr() const
A get method to access the name of the distribution of the observable.
Definition: Observable.h:140
gslpp::matrix< double >
A class for constructing and defining operations on real matrices.
Definition: gslpp_matrix_double.h:48
Observable::setTho
void setTho(ThObservable *tho_i)
A set method to fix the pointer to object of type ThObservable.
Definition: Observable.h:413
CorrelatedGaussianObservables
A class for correlated Gaussian observables.
Definition: CorrelatedGaussianObservables.h:32
CorrelatedGaussianObservables::name
std::string name
The name of the correlated Gaussian Observables set.
Definition: CorrelatedGaussianObservables.h:177
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
gslpp::matrix< double >::size_j
size_t size_j() const
Definition: gslpp_matrix_double.cpp:137
CorrelatedGaussianObservables::filepath
std::string filepath
The path to the config file being parsed.
Definition: CorrelatedGaussianObservables.h:178
gslpp::matrix
A base class for defining operations on matrices, both real and complex.
Definition: gslpp_matrix_base.h:21
CorrelatedGaussianObservables::~CorrelatedGaussianObservables
virtual ~CorrelatedGaussianObservables()
The default destructor.
Definition: CorrelatedGaussianObservables.cpp:42
StandardModel
A model class for the Standard Model.
Definition: StandardModel.h:477
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
CorrelatedGaussianObservables::CorrelatedGaussianObservables
CorrelatedGaussianObservables()
The default Constructor.
Definition: CorrelatedGaussianObservables.cpp:22
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
CorrelatedGaussianObservables::AddObs
void AddObs(Observable &Obs_i)
A method to add observables to the list of correlated Gaussian observables.
Definition: CorrelatedGaussianObservables.cpp:48
myModel
My own Model.
Definition: myModel.h:17
ThObsFactory.h
ThObsFactory
A class for.
Definition: ThObsFactory.h:26
Observable::isTMCMC
bool isTMCMC() const
A method to check if the observable is listed for MCMC.
Definition: Observable.h:341
gslpp::matrix< double >::inverse
matrix< double > inverse()
Definition: gslpp_matrix_double.cpp:178
CorrelatedGaussianObservables::covarianceFromConfig
bool covarianceFromConfig
Definition: CorrelatedGaussianObservables.h:181
CorrelatedGaussianObservables::IsEOF
bool IsEOF
A boolean which is true if the end of file is reached.
Definition: CorrelatedGaussianObservables.h:179
Observable
A class for observables.
Definition: Observable.h:28
CorrelatedGaussianObservables::InvCov
gslpp::matrix< double > * InvCov
The inverse covariance matrix.
Definition: CorrelatedGaussianObservables.h:176
CorrelatedGaussianObservables::computeWeight
virtual double computeWeight()
A method to compute the weight associated with the observable.
Definition: CorrelatedGaussianObservables.cpp:74
gslpp::matrix< double >::size_i
size_t size_i() const
Definition: gslpp_matrix_double.cpp:132
gslpp::vector< double >
A class for constructing and defining operations on real vectors.
Definition: gslpp_vector_double.h:33
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
CorrelatedGaussianObservables::ComputeCov
void ComputeCov(gslpp::matrix< double > Corr)
Computes the covariance matrix for the correlated Gaussian observables set.
Definition: CorrelatedGaussianObservables.cpp:53
Observable::setHasInverseCovariance
void setHasInverseCovariance(bool hasInverseCovariance)
A set method to state that the Observable is a part of ObservablesWithInverseCovariance.
Definition: Observable.h:464
CorrelatedGaussianObservables.h
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:4623
CorrelatedGaussianObservables::Obs
std::vector< Observable > Obs
A vector of observables whose correlation will be calculated.
Definition: CorrelatedGaussianObservables.h:175