ComPWA
Common Partial-Wave-Analysis Framework
FitResult.cpp
Go to the documentation of this file.
1 
2 // Copyright (c) 2015, 2017 The ComPWA Team.
3 // This file is part of the ComPWA framework, check
4 // https://github.com/ComPWA/ComPWA/license.txt for details.
5 
6 #include "Core/FitResult.hpp"
7 
8 #include <iomanip>
9 
10 #include "Core/Logging.hpp"
11 #include "Core/TableFormatter.hpp"
12 #include "Core/Utils.hpp"
13 
14 #include <boost/archive/xml_iarchive.hpp>
15 #include <boost/archive/xml_oarchive.hpp>
16 #include <boost/serialization/nvp.hpp>
17 #include <boost/serialization/vector.hpp>
18 namespace ComPWA {
19 
21  std::stringstream ss;
22  ss << std::setprecision(10);
23  ss << p.Value;
24  ss << std::setprecision(5);
25  if (p.IsFixed) {
26  ss << " (fixed)";
27  } else if (p.Error.first == p.Error.second)
28  ss << " +- " << p.Error.first;
29  else {
30  ss << " [" << p.Error.first << ", " << p.Error.second << "]";
31  }
32  return ss.str();
33 }
34 
35 std::ostream &operator<<(std::ostream &os, const FitResult &Result) {
36  auto OldPrecision(os.precision());
37 
39 
40  os << "\n--------------FIT RESULT INFOS----------------\n";
41  // ----------------- general info -----------------
42  os << "initial estimator value: " << Result.InitialEstimatorValue << "\n";
43  os << "final estimator value: " << Result.FinalEstimatorValue << "\n";
44  os << "duration of fit (in seconds): " << Result.FitDuration.count() << "\n";
45 
46  // ----------------- fit parameters -----------------
47  TableFormatter FitParametersFormatter(&os);
48  // Column width for parameter with symmetric error
49  size_t ParErrorWidth = 30;
50 
51  // Any parameter with asymmetric errors?
52  for (auto x : Result.FinalParameters) {
53  if (x.Error.first != x.Error.second) {
54  ParErrorWidth = 40;
55  break;
56  }
57  }
58 
59  FitParametersFormatter.addColumn("Nr");
60  FitParametersFormatter.addColumn("Name", 30);
61  FitParametersFormatter.addColumn("Initial Value", 15);
62  FitParametersFormatter.addColumn("Final Value", ParErrorWidth);
63 
64  FitParametersFormatter.header();
65 
66  os << std::setprecision(10);
67  size_t parameterId = 0;
68  for (auto p : Result.FinalParameters) {
69  auto res = std::find_if(Result.InitialParameters.begin(),
70  Result.InitialParameters.end(),
71  [&p](const ComPWA::FitParameter<double> &x) {
72  return p.Name == x.Name;
73  });
74 
75  // Is parameter an angle?
76  bool IsAngle(false);
77  if (p.Name.find("phase") != std::string::npos)
78  IsAngle = true;
79 
80  // Print parameter name
81  FitParametersFormatter << parameterId << p.Name;
82  parameterId++;
83 
84  // Print initial values
85  if (res != Result.InitialParameters.end()) {
86  if (IsAngle)
87  FitParametersFormatter << ComPWA::Utils::shiftAngle(res->Value);
88  else
89  FitParametersFormatter << res->Value;
90  } else {
91  LOG(WARNING)
92  << "FitResult operator<<(): could not find initial parameter. "
93  "FitResult corrupted, skipping initial value";
94  FitParametersFormatter << " "; // print blank into that table cell
95  }
96 
97  // Print final value
98  if (IsAngle)
99  p.Value = ComPWA::Utils::shiftAngle(p.Value);
100  FitParametersFormatter << makeFitParameterString(p);
101  }
102 
103  FitParametersFormatter.footer();
104 
105  os << std::setprecision(5);
106  // ----------------- covariance matrix -----------------
107  size_t NRows(Result.CovarianceMatrix.size());
108  if (0 < NRows) {
109  bool CovarianceValid(true);
110  for (auto x : Result.CovarianceMatrix) {
111  if (x.size() != NRows) {
112  CovarianceValid = false;
113  LOG(WARNING)
114  << "FitResult operator<<(): Covariance is not a square matrix!";
115  break;
116  }
117  }
118  if (CovarianceValid) {
119  TableFormatter CovarianceFormatter(&os);
120 
121  // Create table structure first
122  CovarianceFormatter.addColumn(" ", 17); // add empty first column
123  // add columns first
124  for (auto p : Result.FinalParameters) {
125  if (p.IsFixed)
126  continue;
127  CovarianceFormatter.addColumn(p.Name, 17);
128  }
129 
130  // Fill table
131  unsigned int n = 0;
132  CovarianceFormatter.header();
133  for (auto p : Result.FinalParameters) {
134  if (p.IsFixed)
135  continue;
136 
137  CovarianceFormatter << p.Name;
138  for (auto val : Result.CovarianceMatrix.at(n)) {
139  CovarianceFormatter << val;
140  }
141  n++;
142  }
143  CovarianceFormatter.footer();
144  }
145  }
146  os << std::setprecision(OldPrecision); // reset os precision
147 
148  return os;
149 }
150 
151 void FitResult::write(std::string filename) const {
152  std::ofstream ofs(filename);
153  boost::archive::xml_oarchive oa(ofs);
154  oa << boost::serialization::make_nvp("FitResult", *this);
155 }
156 
157 FitResult load(std::string filename) {
158  FitResult Result;
159  std::ifstream ifs(filename);
160  assert(ifs.good());
161  boost::archive::xml_iarchive ia(ifs);
162  ia >> boost::serialization::make_nvp("FitResult", Result);
163  return Result;
164 }
165 
167  ComPWA::FitResult Result) {
168  auto Params = Intens.getParameters();
169  std::vector<double> ParameterValues;
170  for (auto p : Params) {
171  auto found = std::find_if(Result.FinalParameters.begin(),
172  Result.FinalParameters.end(),
173  [&p](auto const &x) { return p.Name == x.Name; });
174  if (Result.FinalParameters.end() == found) {
175  LOG(ERROR) << "initializeWithFitResult(): Could not find parameter "
176  << p.Name
177  << " in fit result! Intensity is not fully initialized!"
178  << "\nPlease make sure the supplied fit result "
179  "is compatible with this Intensity.";
180  } else {
181  ParameterValues.push_back(found->Value);
182  }
183  }
184  Intens.updateParametersFrom(ParameterValues);
185 }
186 
187 } // namespace ComPWA
virtual void updateParametersFrom(const std::vector< double > &)=0
It is important to input the vector in the same length and order as defined in the getParameters() me...
std::pair< T, T > Error
std::vector< std::vector< double > > CovarianceMatrix
Definition: FitResult.hpp:29
std::ostream & operator<<(std::ostream &os, const Event &ev)
Definition: Event.cpp:11
void initializeWithFitResult(ComPWA::Intensity &Intens, ComPWA::FitResult Result)
Definition: FitResult.cpp:166
double shiftAngle(double value)
Definition: Utils.hpp:27
std::chrono::seconds FitDuration
Definition: FitResult.hpp:27
double InitialEstimatorValue
Definition: FitResult.hpp:25
double FinalEstimatorValue
Definition: FitResult.hpp:26
Data structure which resembles a general fit result.
Definition: FitResult.hpp:19
FitParameterList FinalParameters
Definition: FitResult.hpp:21
FitParameterList InitialParameters
Definition: FitResult.hpp:20
void write(std::string filename) const
Definition: FitResult.cpp:151
FitResult load(std::string filename)
Definition: FitResult.cpp:157
std::string makeFitParameterString(ComPWA::FitParameter< double > p)
Definition: FitResult.cpp:20
virtual std::vector< Parameter > getParameters() const =0
virtual void addColumn(std::string title, unsigned int fixlength=999)
Interface template for a general Function of the form OutputType Function(InputTypes) The concept clo...
Definition: Function.hpp:24