ComPWA
Common Partial-Wave-Analysis Framework
ParameterList.hpp
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 
9 
10 #ifndef _PARAMETERLIST_HPP_
11 #define _PARAMETERLIST_HPP_
12 
13 #include <iostream>
14 #include <map>
15 #include <memory>
16 #include <sstream>
17 #include <string>
18 #include <vector>
19 
20 #include <boost/serialization/shared_ptr.hpp>
21 #include <boost/serialization/vector.hpp>
22 
23 #include "Core/Exceptions.hpp"
26 #include "Core/Logging.hpp"
27 
28 namespace ComPWA {
29 namespace Data {
30 struct DataSet;
31 }
32 namespace FunctionTree {
38 public:
40  ParameterList(const ComPWA::Data::DataSet &DataSample);
43  ParameterList(const ParameterList &in) = default;
44 
48  void DeepCopy(const ParameterList &in);
49 
50  virtual ~ParameterList(){};
51 
52  virtual std::size_t numParameters() const;
53 
54  std::shared_ptr<FitParameter>
55  addUniqueParameter(std::shared_ptr<FitParameter> par);
56 
57  virtual void addParameter(std::shared_ptr<Parameter> par);
58 
59  virtual void addParameter(std::shared_ptr<FitParameter> par);
60 
61  virtual void addParameters(std::vector<std::shared_ptr<Parameter>> pars);
62 
63  virtual std::size_t numValues() const;
64 
65  virtual void addValue(std::shared_ptr<Parameter> value);
66 
67  virtual void addValues(std::vector<std::shared_ptr<Parameter>> values);
68 
69  // Parameter
70  virtual std::shared_ptr<FitParameter> doubleParameter(size_t i) const {
71  return FitParameters.at(i);
72  };
73 
74  virtual std::vector<std::shared_ptr<FitParameter>> &doubleParameters() {
75  return FitParameters;
76  };
77 
78  virtual const std::vector<std::shared_ptr<FitParameter>> &
79  doubleParameters() const {
80  return FitParameters;
81  };
82 
83  // Value
84  // Single sized values
85  virtual std::shared_ptr<Value<int>> intValue(size_t i) {
86  return IntValues.at(i);
87  };
88 
89  virtual std::vector<std::shared_ptr<Value<int>>> &intValues() {
90  return IntValues;
91  };
92 
93  virtual const std::vector<std::shared_ptr<Value<int>>> &intValues() const {
94  return IntValues;
95  };
96 
97  virtual std::shared_ptr<Value<double>> doubleValue(size_t i) const {
98  return DoubleValues.at(i);
99  };
100 
101  virtual std::vector<std::shared_ptr<Value<double>>> &doubleValues() {
102  return DoubleValues;
103  };
104 
105  virtual const std::vector<std::shared_ptr<Value<double>>> &
106  doubleValues() const {
107  return DoubleValues;
108  };
109 
110  virtual std::shared_ptr<Value<std::complex<double>>>
111  complexValue(size_t i) const {
112  return ComplexValues.at(i);
113  };
114 
115  virtual std::vector<std::shared_ptr<Value<std::complex<double>>>> &
117  return ComplexValues;
118  };
119 
120  virtual const std::vector<std::shared_ptr<Value<std::complex<double>>>> &
121  complexValues() const {
122  return ComplexValues;
123  };
124 
125  virtual std::shared_ptr<Value<std::vector<int>>> mIntValue(size_t i) const {
126  return MultiIntValues.at(i);
127  };
128 
129  virtual std::vector<std::shared_ptr<Value<std::vector<int>>>> &mIntValues() {
130  return MultiIntValues;
131  };
132 
133  virtual const std::vector<std::shared_ptr<Value<std::vector<int>>>> &
134  mIntValues() const {
135  return MultiIntValues;
136  };
137 
138  virtual std::shared_ptr<Value<std::vector<double>>>
139  mDoubleValue(size_t i) const {
140  return MultiDoubleValues.at(i);
141  };
142 
143  virtual std::vector<std::shared_ptr<Value<std::vector<double>>>> &
145  return MultiDoubleValues;
146  };
147 
148  virtual const std::vector<std::shared_ptr<Value<std::vector<double>>>> &
149  mDoubleValues() const {
150  return MultiDoubleValues;
151  };
152 
153  virtual std::shared_ptr<Value<std::vector<std::complex<double>>>>
154  mComplexValue(size_t i) const {
155  return MultiComplexValues.at(i);
156  };
157 
158  virtual std::vector<std::shared_ptr<Value<std::vector<std::complex<double>>>>>
160  return MultiComplexValues;
161  };
162 
163  virtual const std::vector<
164  std::shared_ptr<Value<std::vector<std::complex<double>>>>> &
165  mComplexValues() const {
166  return MultiComplexValues;
167  };
168 
169  friend std::ostream &operator<<(std::ostream &out, const ParameterList &b) {
170  return out << b.to_str();
171  }
172 
174  virtual std::string to_str() const;
175 
176 protected:
177  std::vector<std::shared_ptr<Value<int>>> IntValues;
178 
179  std::vector<std::shared_ptr<Value<double>>> DoubleValues;
180 
181  std::vector<std::shared_ptr<Value<std::complex<double>>>> ComplexValues;
182 
183  std::vector<std::shared_ptr<Value<std::vector<int>>>> MultiIntValues;
184 
185  std::vector<std::shared_ptr<Value<std::vector<double>>>> MultiDoubleValues;
186 
187  std::vector<std::shared_ptr<Value<std::vector<std::complex<double>>>>>
189 
190  std::vector<std::shared_ptr<FitParameter>> FitParameters;
191 
192 private:
193  friend class boost::serialization::access;
194  template <class archive>
195  void serialize(archive &ar, const unsigned int version) {
196  using namespace boost::serialization;
197  // currently only FitParameters can be serialized
198  ar &make_nvp("FitParameters", FitParameters);
199  }
200 };
201 
205 inline std::shared_ptr<FitParameter> FindParameter(std::string name,
206  const ParameterList &v) {
207  auto it =
208  std::find_if(v.doubleParameters().begin(), v.doubleParameters().end(),
209  [name](const std::shared_ptr<FitParameter> &s) {
210  return s->name() == name;
211  });
212  if (it == v.doubleParameters().end())
213  throw BadParameter("FindParameter() | Parameter not in list!");
214  return *it;
215 }
216 
220 inline std::shared_ptr<FitParameter>
221 FindParameter(std::string name, std::vector<std::shared_ptr<FitParameter>> &v) {
222  auto it = std::find_if(v.begin(), v.end(),
223  [name](const std::shared_ptr<FitParameter> &s) {
224  return s->name() == name;
225  });
226  if (it == v.end())
227  throw BadParameter("FindParameter() | Parameter not in list!");
228  return *it;
229 }
230 
231 inline std::shared_ptr<Value<std::vector<double>>>
232 findMDoubleValue(const std::string &name, const ParameterList &list) {
233  auto it = std::find_if(
234  list.mDoubleValues().begin(), list.mDoubleValues().end(),
235  [name](const std::shared_ptr<Value<std::vector<double>>> &s) {
236  return s->name() == name;
237  });
238  if (it == list.mDoubleValues().end())
239  throw BadParameter("FindParameter() | Parameter not in list!");
240  return *it;
241 }
242 
243 } // namespace FunctionTree
244 } // namespace ComPWA
245 
249 #include <boost/version.hpp>
250 #if (BOOST_VERSION < 105600)
251 #include <boost/serialization/split_free.hpp>
252 #include <boost/unordered_map.hpp>
253 #include <typeinfo>
254 
255 //--- Wrapper for std::shared_ptr<T> ------------------------------------------
256 namespace boost {
257 namespace serialization {
258 
259 template <class Archive, class Type>
260 void save(Archive &archive, const std::shared_ptr<Type> &value,
261  const unsigned int version) {
262  Type *data = value.get();
263  archive << make_nvp("shared_ptr", data);
264 }
265 
266 template <class Archive, class Type>
267 void load(Archive &archive, std::shared_ptr<Type> &value,
268  const unsigned int version) {
269  Type *data;
270  archive >> make_nvp("shared_ptr", data);
271  // archive >>data;
272 
273  typedef std::weak_ptr<Type> WeakPtr;
274  static boost::unordered_map<void *, WeakPtr> hash;
275 
276  if (hash[data].expired()) {
277  value = std::shared_ptr<Type>(data);
278  hash[data] = value;
279  } else
280  value = hash[data].lock();
281 }
282 
283 template <class Archive, class Type>
284 inline void serialize(Archive &archive, std::shared_ptr<Type> &value,
285  const unsigned int version) {
286  split_free(archive, value, version);
287 }
288 
289 } // namespace serialization
290 } // namespace boost
291 #endif // END serialization work-a-round
292 
293 #endif
std::vector< std::shared_ptr< Value< std::vector< double > > > > MultiDoubleValues
virtual std::shared_ptr< Value< int > > intValue(size_t i)
Parameter not existing.
Definition: Exceptions.hpp:62
void serialize(archive &ar, const unsigned int version)
Support for serialization of std::shared_ptr (and other types) is added in boost 1.56 .
virtual const std::vector< std::shared_ptr< Value< std::complex< double > > > > & complexValues() const
Implementations of Parameter for various data types.
Template implementation of Parameter for simple values.
virtual const std::vector< std::shared_ptr< Value< std::vector< int > > > > & mIntValues() const
std::shared_ptr< Value< std::vector< double > > > findMDoubleValue(const std::string &name, const ParameterList &list)
ComPWA exceptions.
std::vector< std::shared_ptr< Value< std::vector< std::complex< double > > > > > MultiComplexValues
virtual const std::vector< std::shared_ptr< Value< std::vector< double > > > > & mDoubleValues() const
std::vector< std::shared_ptr< Value< double > > > DoubleValues
virtual std::vector< std::shared_ptr< Value< std::vector< int > > > > & mIntValues()
std::vector< std::shared_ptr< Value< int > > > IntValues
virtual std::shared_ptr< Value< std::vector< std::complex< double > > > > mComplexValue(size_t i) const
virtual std::vector< std::shared_ptr< Value< std::vector< double > > > > & mDoubleValues()
virtual const std::vector< std::shared_ptr< Value< std::vector< std::complex< double > > > > > & mComplexValues() const
virtual std::vector< std::shared_ptr< Value< std::complex< double > > > > & complexValues()
virtual const std::vector< std::shared_ptr< FitParameter > > & doubleParameters() const
virtual std::string to_str() const
A public function returning a string with parameter information.
virtual const std::vector< std::shared_ptr< Value< int > > > & intValues() const
virtual const std::vector< std::shared_ptr< Value< double > > > & doubleValues() const
friend std::ostream & operator<<(std::ostream &out, const ParameterList &b)
virtual std::shared_ptr< FitParameter > doubleParameter(size_t i) const
virtual std::vector< std::shared_ptr< Value< std::vector< std::complex< double > > > > > & mComplexValues()
virtual std::vector< std::shared_ptr< Value< double > > > & doubleValues()
FitResult load(std::string filename)
Definition: FitResult.cpp:157
virtual std::vector< std::shared_ptr< FitParameter > > & doubleParameters()
virtual std::vector< std::shared_ptr< Value< int > > > & intValues()
std::vector< std::shared_ptr< Value< std::complex< double > > > > ComplexValues
std::vector< std::shared_ptr< Value< std::vector< int > > > > MultiIntValues
virtual std::shared_ptr< Value< std::vector< int > > > mIntValue(size_t i) const
virtual std::shared_ptr< Value< std::complex< double > > > complexValue(size_t i) const
std::shared_ptr< FitParameter > FindParameter(std::string name, std::vector< std::shared_ptr< FitParameter >> &v)
Search list for a FitParameter with name.
std::vector< std::shared_ptr< FitParameter > > FitParameters
This class provides a list of parameters and values of different types.
virtual std::shared_ptr< Value< std::vector< double > > > mDoubleValue(size_t i) const
virtual std::shared_ptr< Value< double > > doubleValue(size_t i) const