ComPWA
Common Partial-Wave-Analysis Framework
FitParameter.hpp
Go to the documentation of this file.
1 #ifndef CORE_FITPARAMETER_HPP_
2 #define CORE_FITPARAMETER_HPP_
3 
4 #include "Core/Function.hpp"
5 #include "Core/Logging.hpp"
6 
7 #include <ostream>
8 #include <string>
9 #include <vector>
10 
11 #include <boost/serialization/nvp.hpp>
12 #include <boost/serialization/utility.hpp>
13 
14 namespace ComPWA {
15 
16 template <typename T> struct FitParameter {
17  FitParameter() = default;
18  FitParameter(std::string name, T val, bool isfixed = true)
19  : Value(val), Name(name), Bounds(0.0, 0.0), HasBounds(false),
20  IsFixed(isfixed) {}
21  FitParameter(std::string name, T val, T min, T max, bool isfixed = true)
22  : Value(val), Name(name), Bounds(min, max), HasBounds(true),
23  IsFixed(isfixed) {}
24  T Value;
25  std::pair<T, T> Error;
26  std::string Name = "";
27  std::pair<T, T> Bounds;
28  bool HasBounds = false;
29  bool IsFixed = true;
30 
31  friend std::ostream &operator<<(std::ostream &os,
32  const FitParameter<double> &x) {
33  os << x.Name << ": " << x.Value;
34  if (x.Error.first != 0.0 || x.Error.second != 0.0) {
35  if (x.Error.first == x.Error.second) {
36  os << " +- " << x.Error.second;
37  } else {
38  os << " + " << x.Error.second << " - " << x.Error.first;
39  }
40  if (x.HasBounds) {
41  os << " Bounds: [" << x.Bounds.first << ", " << x.Bounds.second << "]";
42  }
43  }
44  if (x.IsFixed)
45  os << " (fixed)";
46  return os;
47  }
48 };
49 
50 using FitParameterList = std::vector<FitParameter<double>>;
51 
52 inline bool isValid(const FitParameterList &FitParameters,
53  const std::vector<ComPWA::Parameter> &EstimatorParameters) {
54  // validate FitParameterList
55  auto ActualParametersIt = EstimatorParameters.begin();
56  for (auto const &Par : FitParameters) {
57  if (ActualParametersIt == EstimatorParameters.end()) {
58  LOG(ERROR) << "Less fit parameters given then actual parameters in the "
59  "function!";
60  return false;
61  }
62 
63  if (Par.Name != ActualParametersIt->Name) {
64  LOG(ERROR) << "Wrong ordering of fit parameters!";
65  return false;
66  }
67  ++ActualParametersIt;
68  }
69  return true;
70 }
71 
72 } // namespace ComPWA
73 
74 namespace boost {
75 namespace serialization {
76 
77 template <class Archive>
78 void serialize(Archive &ar, ComPWA::FitParameter<double> &FitParameter,
79  const unsigned int version) {
80  ar &BOOST_SERIALIZATION_NVP(FitParameter.Name);
81  ar &BOOST_SERIALIZATION_NVP(FitParameter.Value);
82  ar &BOOST_SERIALIZATION_NVP(FitParameter.Error);
83  ar &BOOST_SERIALIZATION_NVP(FitParameter.Bounds);
84  ar &BOOST_SERIALIZATION_NVP(FitParameter.HasBounds);
85  ar &BOOST_SERIALIZATION_NVP(FitParameter.IsFixed);
86 }
87 
88 } // namespace serialization
89 } // namespace boost
90 
91 #endif
friend std::ostream & operator<<(std::ostream &os, const FitParameter< double > &x)
std::pair< T, T > Error
Support for serialization of std::shared_ptr (and other types) is added in boost 1.56 .
bool isValid(const FitParameterList &FitParameters, const std::vector< ComPWA::Parameter > &EstimatorParameters)
std::vector< FitParameter< double > > FitParameterList
FitParameter(std::string name, T val, T min, T max, bool isfixed=true)
std::pair< T, T > Bounds
FitParameter(std::string name, T val, bool isfixed=true)