ComPWA
Common Partial-Wave-Analysis Framework
ParameterList.cpp
Go to the documentation of this file.
1 // Copyright (c) 2013, 2017 The ComPWA Team.
2 // This file is part of the ComPWA framework, check
3 // https://github.com/ComPWA/ComPWA/license.txt for details.
4 
5 #include <iostream>
6 #include <map>
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 #include "Core/Exceptions.hpp"
12 #include "Data/DataSet.hpp"
13 #include "FitParameter.hpp"
14 
15 #include "ParameterList.hpp"
16 
17 namespace ComPWA {
18 namespace FunctionTree {
19 
21  // Add data vector to ParameterList
22  for (auto x : DataSample.Data)
23  addValue(MDouble(x.first, x.second));
24  // Adding weight at the end
25  addValue(MDouble("Weight", DataSample.Weights));
26 }
27 
29  IntValues.clear();
30  DoubleValues.clear();
31  ComplexValues.clear();
32  MultiIntValues.clear();
33  MultiDoubleValues.clear();
34  MultiComplexValues.clear();
35  FitParameters.clear();
36 
37  for (auto p : in.IntValues)
38  IntValues.push_back(std::make_shared<Value<int>>(*p));
39  for (auto p : in.DoubleValues)
40  DoubleValues.push_back(std::make_shared<Value<double>>(*p));
41  for (auto p : in.ComplexValues)
42  ComplexValues.push_back(std::make_shared<Value<std::complex<double>>>(*p));
43 
44  for (auto p : in.MultiIntValues)
45  MultiIntValues.push_back(std::make_shared<Value<std::vector<int>>>(*p));
46  for (auto p : in.MultiDoubleValues)
47  MultiDoubleValues.push_back(
48  std::make_shared<Value<std::vector<double>>>(*p));
49  for (auto p : in.MultiComplexValues)
50  MultiComplexValues.push_back(
51  std::make_shared<Value<std::vector<std::complex<double>>>>(*p));
52 
53  for (auto p : in.FitParameters)
54  FitParameters.push_back(
55  std::make_shared<ComPWA::FunctionTree::FitParameter>(*p));
56 }
57 
58 std::size_t ParameterList::numParameters() const {
59  return FitParameters.size();
60 }
61 
63  std::vector<std::shared_ptr<Parameter>> pars) {
64  for (auto i : pars)
65  addParameter(i);
66 }
67 
68 std::shared_ptr<FitParameter>
69 ParameterList::addUniqueParameter(std::shared_ptr<FitParameter> par) {
70  std::shared_ptr<FitParameter> tmp;
71  try {
72  tmp = FindParameter(par->name(), FitParameters);
73  } catch (std::exception &ex) {
74  tmp = par;
75  FitParameters.push_back(par);
76  }
77 
78  if (*tmp != *par)
79  throw BadParameter("ParameterList::addUniqueParameter() | FitParameter " +
80  par->name() +
81  " found in list but match is not identical!");
82 
83  return tmp;
84 }
85 
86 void ParameterList::addParameter(std::shared_ptr<FitParameter> par) {
87  FitParameters.push_back(
88  std::dynamic_pointer_cast<FunctionTree::FitParameter>(par));
89 }
90 
91 void ParameterList::addParameter(std::shared_ptr<Parameter> par) {
92  switch (par->type()) {
93  case ParType::DOUBLE: {
94  addParameter(std::dynamic_pointer_cast<FunctionTree::FitParameter>(par));
95  break;
96  }
97  default: { break; }
98  }
99 }
100 
101 std::size_t ParameterList::numValues() const {
102  return IntValues.size() + DoubleValues.size() + ComplexValues.size() +
103  MultiIntValues.size() + MultiDoubleValues.size() +
104  MultiComplexValues.size();
105 }
106 void ParameterList::addValues(std::vector<std::shared_ptr<Parameter>> values) {
107  for (auto i : values)
108  addValue(i);
109 }
110 
111 void ParameterList::addValue(std::shared_ptr<Parameter> par) {
112  switch (par->type()) {
113  case ParType::INTEGER: {
114  IntValues.push_back(std::dynamic_pointer_cast<Value<int>>(par));
115  break;
116  }
117  case ParType::DOUBLE: {
118  DoubleValues.push_back(std::dynamic_pointer_cast<Value<double>>(par));
119  break;
120  }
121  case ParType::COMPLEX: {
122  ComplexValues.push_back(
123  std::dynamic_pointer_cast<Value<std::complex<double>>>(par));
124  break;
125  }
126  case ParType::MINTEGER: {
127  MultiIntValues.push_back(
128  std::dynamic_pointer_cast<Value<std::vector<int>>>(par));
129  break;
130  }
131  case ParType::MDOUBLE: {
132  MultiDoubleValues.push_back(
133  std::dynamic_pointer_cast<Value<std::vector<double>>>(par));
134  break;
135  }
136  case ParType::MCOMPLEX: {
137  MultiComplexValues.push_back(
138  std::dynamic_pointer_cast<Value<std::vector<std::complex<double>>>>(
139  par));
140  break;
141  }
142  default: { break; }
143  }
144 }
145 
146 std::string ParameterList::to_str() const {
147  std::stringstream s;
148  if (IntValues.size()) {
149  s << "Integer values [" << IntValues.size() << "]:" << std::endl;
150  for (auto p : IntValues)
151  s << p->to_str() << std::endl;
152  }
153  if (DoubleValues.size()) {
154  s << "Double values [" << DoubleValues.size() << "]:" << std::endl;
155  for (auto p : DoubleValues)
156  s << p->to_str() << std::endl;
157  }
158  if (ComplexValues.size()) {
159  s << "Complex values [" << ComplexValues.size() << "]:" << std::endl;
160  for (auto p : ComplexValues)
161  s << p->to_str() << std::endl;
162  }
163  if (MultiIntValues.size()) {
164  s << "Multi integer values [" << MultiIntValues.size() << "]:" << std::endl;
165  for (auto p : MultiIntValues)
166  s << p->to_str() << std::endl;
167  }
168  if (MultiDoubleValues.size()) {
169  s << "Multi double values [" << MultiDoubleValues.size()
170  << "]:" << std::endl;
171  for (auto p : MultiDoubleValues)
172  s << p->to_str() << std::endl;
173  }
174  if (MultiComplexValues.size()) {
175  s << "Multi complex values [" << MultiComplexValues.size()
176  << "]:" << std::endl;
177  for (auto p : MultiComplexValues)
178  s << p->to_str() << std::endl;
179  }
180  if (FitParameters.size()) {
181  s << "Fit parameters [" << FitParameters.size() << "]:" << std::endl;
182  for (auto p : FitParameters)
183  s << p->to_str() << std::endl;
184  }
185  return s.str();
186 };
187 
188 } // namespace FunctionTree
189 } // namespace ComPWA
std::vector< std::shared_ptr< Value< std::vector< double > > > > MultiDoubleValues
virtual void addValue(std::shared_ptr< Parameter > value)
Parameter not existing.
Definition: Exceptions.hpp:62
void DeepCopy(const ParameterList &in)
Clear this parameter and deep-copy all parameters from in.
Implementations of Parameter for various data types.
virtual void addParameters(std::vector< std::shared_ptr< Parameter >> pars)
ComPWA::DataMap Data
Definition: DataSet.hpp:18
ComPWA exceptions.
virtual void addParameter(std::shared_ptr< Parameter > par)
std::vector< std::shared_ptr< Value< std::vector< std::complex< double > > > > > MultiComplexValues
virtual void addValues(std::vector< std::shared_ptr< Parameter >> values)
std::vector< double > Weights
Definition: DataSet.hpp:19
std::shared_ptr< Value< std::vector< double > > > MDouble(std::string name, size_t s, double el=0.)
Definition: Value.hpp:134
ParameterList class.
std::shared_ptr< FitParameter > addUniqueParameter(std::shared_ptr< FitParameter > par)
std::vector< std::shared_ptr< Value< double > > > DoubleValues
std::vector< std::shared_ptr< Value< int > > > IntValues
virtual std::size_t numValues() const
std::shared_ptr< FitParameter > FindParameter(std::string name, const ParameterList &v)
Search ParameterList for a FitParameter with name.
virtual std::size_t numParameters() const
virtual std::string to_str() const
A public function returning a string with parameter information.
std::vector< std::shared_ptr< Value< std::complex< double > > > > ComplexValues
std::vector< std::shared_ptr< Value< std::vector< int > > > > MultiIntValues
std::vector< std::shared_ptr< FitParameter > > FitParameters
This class provides a list of parameters and values of different types.