ComPWA
Common Partial-Wave-Analysis Framework
Value.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 ParameterT_hpp
11 #define ParameterT_hpp
12 
13 #include "FitParameter.hpp"
14 #include <iterator>
15 namespace ComPWA {
16 namespace FunctionTree {
17 
18 template <class T>
19 std::ostream &operator<<(std::ostream &stream, const std::vector<T> &values) {
20  auto n = values.size();
21  if (n > 5)
22  n = 5; // Print first 5 elements
23  auto first = values.begin();
24  auto last = values.begin();
25  std::advance(last, n);
26  std::copy(first, last, std::ostream_iterator<T>(stream, ", "));
27  if (values.size() > n)
28  stream << "...";
29 
30  return stream;
31 }
32 
33 template <class T> class Value : public Parameter {
34 public:
35  Value(std::string name = "") : Parameter(name), Val(0) {
36  Type = typeName<T>();
37  }
38 
39  Value(T val) : Parameter(""), Val(val) { Type = typeName<T>(); }
40 
41  Value(std::string name, T val) : Parameter(name), Val(val) {
42  Type = typeName<T>();
43  }
44 
45  virtual T value() const { return Val; }
46 
49  virtual T &values() { return Val; }
50 
51  virtual void setValue(T inVal) {
52  Val = inVal;
53  notify();
54  };
55 
57  operator T() const { return Val; };
58 
59  const T &operator()() const { return Val; };
60 
61  T &operator()() { return Val; };
62 
64  virtual std::string to_str() const {
65  return Name + " [" + ParNames[Type] + "]";
66  };
67 
69  virtual std::string val_to_str() const {
70  std::stringstream stream;
71  stream << Val;
72  return stream.str();
73  };
74 
75 protected:
76  virtual std::string className() const {
77  return std::string(ParNames[(int)Type]);
78  }
79 
80  T Val;
81 };
82 
83 inline std::shared_ptr<Parameter> ValueFactory(ParType t,
84  std::string name = "") {
85  std::shared_ptr<Parameter> p;
86  switch (t) {
87  case ParType::MCOMPLEX: {
88  p = std::make_shared<Value<std::vector<std::complex<double>>>>(name);
89  break;
90  }
91  case ParType::MDOUBLE: {
92  p = std::make_shared<Value<std::vector<double>>>(name);
93  break;
94  }
95  case ParType::MINTEGER: {
96  p = std::make_shared<Value<std::vector<int>>>(name);
97  break;
98  }
99  case ParType::COMPLEX: {
100  p = std::make_shared<Value<std::complex<double>>>(name);
101  break;
102  }
103  case ParType::DOUBLE: {
104  p = std::make_shared<Value<double>>(name);
105  break;
106  }
107  case ParType::INTEGER: {
108  p = std::make_shared<Value<int>>(name);
109  break;
110  }
111  default: {
112  throw BadParameter("ValueFactory() | Parameter type " + std::to_string(t) +
113  " unknown!");
114  }
115  }
116  return p;
117 }
118 
119 inline std::shared_ptr<Value<std::vector<std::complex<double>>>>
120 MComplex(std::string name, size_t s,
121  std::complex<double> el = std::complex<double>(0., 0.)) {
122 
123  return std::make_shared<Value<std::vector<std::complex<double>>>>(
124  name, std::vector<std::complex<double>>(s, el));
125 }
126 
127 inline std::shared_ptr<Value<std::vector<std::complex<double>>>>
128 MComplex(std::string name, std::vector<std::complex<double>> v) {
129 
130  return std::make_shared<Value<std::vector<std::complex<double>>>>(name, v);
131 }
132 
133 inline std::shared_ptr<Value<std::vector<double>>>
134 MDouble(std::string name, size_t s, double el = 0.) {
135 
136  return std::make_shared<Value<std::vector<double>>>(
137  name, std::vector<double>(s, el));
138 }
139 
140 inline std::shared_ptr<Value<std::vector<double>>>
141 MDouble(std::string name, std::vector<double> v) {
142 
143  return std::make_shared<Value<std::vector<double>>>(name, v);
144 }
145 
146 inline std::shared_ptr<Value<std::vector<int>>>
147 MInteger(std::string name, size_t s, int el = 0.) {
148 
149  return std::make_shared<Value<std::vector<int>>>(name,
150  std::vector<int>(s, el));
151 }
152 
153 inline std::shared_ptr<Value<std::vector<int>>> MInteger(std::string name,
154  std::vector<int> v) {
155 
156  return std::make_shared<Value<std::vector<int>>>(name, v);
157 }
158 
159 } // namespace FunctionTree
160 } // namespace ComPWA
161 #endif
Parameter not existing.
Definition: Exceptions.hpp:62
Implementations of Parameter for various data types.
Base class for internal parameter.
Definition: Parameter.hpp:79
Value(std::string name="")
Definition: Value.hpp:35
virtual std::string name() const
Getter for name of object.
Definition: Parameter.hpp:88
virtual std::string val_to_str() const
A public function returning a string with parameter value.
Definition: Value.hpp:69
std::shared_ptr< Value< std::vector< double > > > MDouble(std::string name, size_t s, double el=0.)
Definition: Value.hpp:134
virtual void setValue(T inVal)
Definition: Value.hpp:51
std::string Name
Name of parameter.
Definition: Parameter.hpp:145
virtual std::string className() const
Getter for typename of object, to be defined by the actual implementation.
Definition: Value.hpp:76
Value(std::string name, T val)
Definition: Value.hpp:41
static const char *const ParNames[7]
Names of the parameter types, should be extended if an new parameter type is added.
Definition: Parameter.hpp:46
const T & operator()() const
Definition: Value.hpp:59
std::shared_ptr< Parameter > ValueFactory(ParType t, std::string name="")
Definition: Value.hpp:83
std::shared_ptr< Value< std::vector< int > > > MInteger(std::string name, size_t s, int el=0.)
Definition: Value.hpp:147
std::shared_ptr< Value< std::vector< std::complex< double > > > > MComplex(std::string name, size_t s, std::complex< double > el=std::complex< double >(0., 0.))
Definition: Value.hpp:120
virtual T value() const
Definition: Value.hpp:45
virtual std::string to_str() const
A public function returning a string with parameter information.
Definition: Value.hpp:64
ParType Type
Type of parameter (e.g. Double, Integer, ...)
Definition: Parameter.hpp:148
void notify()
Notify all observing TreeNodes that parameter changed.
Definition: Parameter.hpp:117
ParType
Enums for the type of the parameter, should be extended if an new parameter type is added...
Definition: Parameter.hpp:34
virtual T & values()
Reference on the value.
Definition: Value.hpp:49