Jpp 21.0.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JCompass/JModel.hh
Go to the documentation of this file.
1#ifndef __JCOMPASS_JMODEL__
2#define __JCOMPASS_JMODEL__
3
4#include <memory>
5
8#include "JLang/JException.hh"
9#include "JMath/JMath.hh"
10#include "JMath/JMathToolkit.hh"
11#include "JFit/JMEstimator.hh"
12
13#include "JCompass/JHit.hh"
14
15
16/**
17 * \author mdejong
18 */
19namespace JCOMPASS {}
20namespace JPP { using namespace JCOMPASS; }
21
22namespace JCOMPASS {
23
24 using JMATH::JMath;
29
30 /**
31 * Model.
32 */
33 struct JModel :
34 public JMath<JModel>
35 {
36 /**
37 * Default constructor.
38 */
40 Q0(JQuaternion3D::getIdentity()),
41 Q1(JQuaternion3D::getIdentity())
42 {}
43
44
45 /**
46 * Constructor.
47 *
48 * \param Q0 tilt
49 * \param Q1 twist
50 */
52 const JQuaternion3D& Q1) :
53 Q0(Q0),
54 Q1(Q1)
55 {}
56
57
58 /**
59 * Constructor.
60 *
61 * The data type corresponding to the hits should provide for the following policy methods.
62 * <pre>
63 * double getZ(); // get z-position
64 * JQuaternion3D getQuaternion(); // get quaternion
65 * </pre>
66 * Note that the input data should be ordered with increasing z-positions.
67 *
68 * \param __begin begin of data
69 * \param __end end of data
70 */
71 template<class T>
72 JModel(T __begin,
73 T __end) :
74 Q0(JQuaternion3D::getIdentity()),
75 Q1(JQuaternion3D::getIdentity())
76 {
77 using namespace std;
78 using namespace JPP;
79
80 const int N = distance(__begin, __end);
81
82 if (N >= NUMBER_OF_PARAMETERS) {
83
85
86 for (T q = __begin, p = q++; q != __end; ++p, ++q) {
87
88 const double dz = q->getZ() - p->getZ();
89
90 if (dz != 0.0) {
91
92 JQuaternion3D Q(p->getQuaternion());
93
94 Q.conjugate();
95 Q.mul(q->getQuaternion());
96 Q.pow(1.0 / dz);
97
98 buffer.push_back(Q);
99 }
100 }
101
102 Q1 = getAverage(buffer.begin(), buffer.end());
103 Q1 = JQuaternion3D::decomposition(Q1, JVector3Z_t).twist;
104
105 const double z1 = getAverage(make_array(__begin, __end, &JHit::getZ));
106
107 Q0 = getAverage(make_array(__begin, __end, &JHit::getQuaternion));
108 Q0 = pow(Q1, -z1) * Q0;
109
110 } else {
111 THROW(JValueOutOfRange, "JModel: Not enough data points " << N);
112 }
113 }
114
115
116 /**
117 * Add model.
118 *
119 * \param model model
120 * \return this model
121 */
122 JModel& add(const JModel& model)
123 {
124 Q0 *= model.Q0;
125 Q1 *= model.Q1;
126
127 Q0.normalise();
128 Q1.normalise();
129
130 return *this;
131 }
132
133
134 /**
135 * Subtract model.
136 *
137 * \param model model
138 * \return this model
139 */
140 JModel& sub(const JModel& model)
141 {
142 Q0 *= model.Q0.getConjugate();
143 Q1 *= model.Q1.getConjugate();
144
145 Q0.normalise();
146 Q1.normalise();
147
148 return *this;
149 }
150
151
152 /**
153 * Scale model.
154 *
155 * \param factor multiplication factor
156 * \return this model
157 */
158 JModel& mul(const double factor)
159 {
160 Q0.pow(factor);
161 Q1.pow(factor);
162
163 return *this;
164 }
165
166
167 /**
168 * Get quaternion at given z-position.
169 *
170 * \param z z-position.
171 * \return quaternion
172 */
173 JQuaternion3D operator()(const double z) const
174 {
175 using namespace JPP;
176
177 return pow(Q1, z) * Q0;
178 }
179
180 static const int NUMBER_OF_PARAMETERS = 4; //!< number of parameters of fit per quaternion
181
182 JQuaternion3D Q0; //!< tilt
183 JQuaternion3D Q1; //!< twist
184 };
185
186
187 /**
188 * Auxiliary data structure for chi2 evaluation.
189 */
190 struct JChi2 {
191 /**
192 * Constructor.
193 *
194 * \param type M-Estimator type
195 */
196 JChi2(const int type) :
197 estimator(getMEstimator(type))
198 {}
199
200
201 /**
202 * Fit function.
203 *
204 * \param model model
205 * \param hit hit
206 * \return chi2
207 */
208 inline double operator()(const JModel& model, const JHit& hit) const
209 {
210 return estimator->getRho(getAngle(model(hit.getZ()), hit.getQuaternion()) / hit.getSigma());
211 }
212
213 std::shared_ptr<JMEstimator> estimator; //!< M-Estimator function
214 };
215}
216
217#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Maximum likelihood estimator (M-estimators).
Binary methods for member methods.
Base class for data structures with artithmetic capabilities.
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Data structure for unit quaternion in three dimensions.
const JQuaternion3D & getQuaternion() const
Get quaternion.
JQuaternion3D & normalise()
Normalise quaternion.
JQuaternion3D & conjugate()
Conjugate quaternion.
JQuaternion3D & mul(const double factor)
Scale quaternion.
JQuaternion3D & pow(const double y)
Raise quaternion to given power.
Exception for accessing a value in a collection that is outside of its range.
Auxiliary classes and methods for orientation calibration based on compasses.
JMEstimator * getMEstimator(const int type)
Get M-Estimator.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for chi2 evaluation.
double operator()(const JModel &model, const JHit &hit) const
Fit function.
JChi2(const int type)
Constructor.
std::shared_ptr< JMEstimator > estimator
M-Estimator function.
double getSigma() const
Get resolution.
double getZ() const
Get z-position.
JModel & sub(const JModel &model)
Subtract model.
JModel & add(const JModel &model)
Add model.
JQuaternion3D operator()(const double z) const
Get quaternion at given z-position.
JQuaternion3D Q0
tilt
JModel(T __begin, T __end)
Constructor.
static const int NUMBER_OF_PARAMETERS
number of parameters of fit per quaternion
JQuaternion3D Q1
twist
JModel & mul(const double factor)
Scale model.
JModel()
Default constructor.
JModel(const JQuaternion3D &Q0, const JQuaternion3D &Q1)
Constructor.
Interface for maximum likelihood estimator (M-estimator).
Auxiliary data structure for decomposition of quaternion in twist and swing quaternions.
JQuaternion3D twist
rotation around parallel axis
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347