GeneralBrokenLines V03-01-03
using EIGEN
example3.cpp
Go to the documentation of this file.
1/*
2 * example3.cpp
3 *
4 * Created on: Aug 24, 2011
5 * Author: kleinwrt
6 */
7
30#include <time.h>
31#include "GblUtilities.h"
32#include "GblTrajectory.h"
33
34using namespace gbl;
35using namespace Eigen;
36
37void example3() {
39
62 unsigned int nTry = 1000; //: number of tries
63 unsigned int nLayer = 10; //: number of detector layers
64 std::cout << " Gbltst-eigen $Id$ " << nTry << ", " << nLayer
65 << std::endl;
66
67 srand(4711);
68
69 clock_t startTime = clock();
70// track direction
71 double sinLambda = 0.3;
72 double cosLambda = sqrt(1.0 - sinLambda * sinLambda);
73 double sinPhi = 0.;
74 double cosPhi = sqrt(1.0 - sinPhi * sinPhi);
75// tDir = (cosLambda * cosPhi, cosLambda * sinPhi, sinLambda)
76// U = Z x T / |Z x T|, V = T x U
77 Matrix<double, 2, 3> uvDir;
78 uvDir(0, 0) = -sinPhi;
79 uvDir(0, 1) = cosPhi;
80 uvDir(0, 2) = 0.;
81 uvDir(1, 0) = -sinLambda * cosPhi;
82 uvDir(1, 1) = -sinLambda * sinPhi;
83 uvDir(1, 2) = cosLambda;
84// measurement resolution
85 Vector2d measErr;
86 measErr << 0.001, 0.001;
87 Vector2d measPrec; // (independent) precisions
88 measPrec << 1.0 / (measErr(0) * measErr(0)), 1.0 / (measErr(1) * measErr(1));
89 Matrix2d measInvCov; // inverse covariance matrix
90 measInvCov.setZero();
91 measInvCov(0, 0) = measPrec[0];
92 measInvCov(1, 1) = measPrec[1];
93// scattering error
94 Vector2d scatErr;
95 scatErr << 0.003, 0.003;
96 Vector2d scatPrec;
97 scatPrec << 1.0 / (scatErr(0) * scatErr(0)), 1.0 / (scatErr(1) * scatErr(1));
98// (RMS of) CurviLinear track parameters (Q/P, slopes, offsets)
99 Vector5d clPar;
100 Vector5d clErr;
101 clErr << 0.001, -0.1, 0.2, -0.15, 0.25;
102 Matrix5d clCov, clSeed;
103 unsigned int seedLabel = 99999;
104// additional parameters
105 Vector2d scatVar;
106 scatVar << 0., 0.; // measured scattering variance (in layer 4)
107
108 double bfac = 0.2998; // Bz*c for Bz=1
109 double step = 1.5 / cosLambda; // constant steps in RPhi
110
111 double Chi2Sum = 0.;
112 int NdfSum = 0;
113 double LostSum = 0.;
114 int numFit = 0;
115
116 for (unsigned int iTry = 1; iTry <= nTry; ++iTry) {
117 // curvilinear track parameters
118 for (unsigned int i = 0; i < 5; ++i) {
119 clPar[i] = clErr[i] * unrm();
120 }
121 clCov.setZero();
122 for (unsigned int i = 0; i < 5; ++i) {
123 clCov(i, i) = 1.0 * (clErr[i] * clErr[i]);
124 }
125// std::cout << " Try " << iTry << ":" << clPar << std::endl;
126// arclength
127 double s = 0.;
128 double sScat = 0.;
129 Matrix5d jacPointToPoint;
130 jacPointToPoint.setIdentity();
131// create list of points
132 std::vector<GblPoint> listOfPoints;
133 listOfPoints.reserve(2 * nLayer);
134
135 for (unsigned int iLayer = 0; iLayer < nLayer; ++iLayer) {
136// std::cout << " Layer " << iLayer << ", " << s << std::endl;
137// measurement directions
138 double sinStereo = (iLayer % 2 == 0) ? 0. : 0.1;
139 double cosStereo = sqrt(1.0 - sinStereo * sinStereo);
140 Matrix<double, 3, 2> mDirT;
141 mDirT.setZero();
142 mDirT(1, 0) = cosStereo;
143 mDirT(2, 0) = sinStereo;
144 mDirT(1, 1) = -sinStereo;
145 mDirT(2, 1) = cosStereo;
146// projection measurement to local (curvilinear uv) directions (duv/dm)
147 Matrix2d proM2l = uvDir * mDirT;
148// projection local (uv) to measurement directions (dm/duv)
149 Matrix2d proL2m = proM2l.inverse();
150 // point with (independent) measurements (in measurement system)
151 GblPoint pointMeas(jacPointToPoint);
152 // measurement - prediction in measurement system with error
153 Vector2d meas = proL2m * clPar.tail(2);
154 for (unsigned int i = 0; i < 2; ++i) {
155 meas[i] += measErr[i] * unrm();
156 }
157 pointMeas.addMeasurement(proL2m, meas, measPrec);
158 /* point with (correlated) measurements (in local system)
159 GblPoint point(jacPointToPoint);
160 // measurement - prediction in local system with error
161 Vector2d meas;
162 for (unsigned int i = 0; i < 2; ++i) {
163 meas[i] = measErr[i] * unrm3();
164 }
165 meas = proM2l * meas + clPar.tail<2>();
166 Matrix2d localInvCov = proL2m.transpose() * measInvCov * proL2m;
167 point.addMeasurement(meas, localInvCov); */
168
169 // additional local parameters?
170 if (iLayer > 4) {
171 //std::cout << " scat " << sScat << " " << s << std::endl;
172 Matrix2d addDer = proL2m * (s - sScat);
173 pointMeas.addLocals(addDer);
174 }
175
176// add point to trajectory
177 listOfPoints.push_back(pointMeas);
178 unsigned int iLabel = listOfPoints.size();
179 if (iLabel == seedLabel) {
180 clSeed = clCov.inverse();
181 }
182// propagate to scatterer
183 jacPointToPoint = gblSimpleJacobian(step, cosLambda, bfac);
184 clPar = jacPointToPoint * clPar;
185 clCov = jacPointToPoint * clCov * jacPointToPoint.transpose();
186 s += step;
187 if (iLayer < nLayer - 1) {
188 Vector2d scat(0., 0.);
189 // point with scatterer
190 GblPoint pointScat(jacPointToPoint);
191 if (iLayer != 4) {
192 pointScat.addScatterer(scat, scatPrec);
193 } else {
194 // measure scattering (with 2 local parameters, no scatterer)
195 sScat = s;
196 }
197 listOfPoints.push_back(pointScat);
198 iLabel = listOfPoints.size();
199 if (iLabel == seedLabel) {
200 clSeed = clCov.inverse();
201 }
202 // scatter a little
203 for (unsigned int i = 0; i < 2; ++i) {
204 clPar[i + 1] += scatErr[i] * unrm();
205 clCov(i + 1, i + 1) += scatErr[i] * scatErr[i];
206 }
207 // propagate to next measurement layer
208 clPar = jacPointToPoint * clPar;
209 clCov = jacPointToPoint * clCov * jacPointToPoint.transpose();
210 s += step;
211 }
212 }
213//
214 // create trajectory
215 GblTrajectory traj(listOfPoints);
216 //GblTrajectory traj(listOfPoints, seedLabel, clSeed); // with external seed
217 //traj.printPoints();
218
219 if (not traj.isValid()) {
220 std::cout << " Invalid GblTrajectory -> skip" << std::endl;
221 continue;
222 }
223// fit trajectory
224 double Chi2;
225 int Ndf;
226 double lostWeight;
227 traj.fit(Chi2, Ndf, lostWeight);
228// std::cout << " Fit: " << Chi2 << ", " << Ndf << ", " << lostWeight << std::endl;
229 VectorXd aCorrection(7);
230 MatrixXd aCovariance(7, 7);
231 traj.getResults(10, aCorrection, aCovariance);
232 // measured scattering parameters
233 scatVar(0) += aCorrection(5) * aCorrection(5);
234 scatVar(1) += aCorrection(6) * aCorrection(6);
235 // contribution from fit
236 scatVar(0) -= aCovariance(5, 5);
237 scatVar(1) -= aCovariance(6, 6);
238 /* look at residuals
239 for (unsigned int label = 1; label <= listOfPoints.size(); ++label) {
240 unsigned int numData = 0;
241 std::cout << " measResults, label " << label << std::endl;
242 VectorXd residuals(2), measErr(2), resErr(2), downWeights(2);
243 traj.getMeasResults(label, numData, residuals, measErr, resErr,
244 downWeights);
245 std::cout << " measResults, numData " << numData << std::endl;
246 for (unsigned int i = 0; i < numData; ++i) {
247 std::cout << " measResults " << label << " " << i << " "
248 << residuals[i] << " " << measErr[i] << " " << resErr[i]
249 << std::endl;
250 }
251 } */
252// debug printout
253 //traj.printTrajectory(1);
254 //traj.printPoints(1);
255 //traj.printData();
256 Chi2Sum += Chi2;
257 NdfSum += Ndf;
258 LostSum += lostWeight;
259 numFit++;
260 }
261
262 clock_t endTime = clock();
263 double diff = endTime - startTime;
264 double cps = CLOCKS_PER_SEC;
265 std::cout << " Time elapsed " << diff / cps << " s" << std::endl;
266 std::cout << " Chi2/Ndf = " << Chi2Sum / NdfSum << std::endl;
267 std::cout << " Tracks fitted " << numFit << std::endl;
268 std::cout << " ScatErr4 " << sqrt(scatVar[0] / numFit) << " "
269 << sqrt(scatVar[1] / numFit) << std::endl;
270 if (LostSum > 0.)
271 std::cout << " Weight lost " << LostSum << std::endl;
272}
273
GblTrajectory definition.
Definitions for GBL utilities.
Point on trajectory.
Definition: GblPoint.h:62
void addMeasurement(const Eigen::MatrixBase< Projection > &aProjection, const Eigen::MatrixBase< Residuals > &aResiduals, const Eigen::MatrixBase< Precision > &aPrecision, double minPrecision=0.)
Add a measurement to a point.
Definition: GblPoint.h:238
void addLocals(const Eigen::MatrixBase< Derivative > &aDerivatives)
Add local derivatives to a point.
Definition: GblPoint.h:287
void addScatterer(const Eigen::Vector2d &aResiduals, const Eigen::Matrix2d &aPrecision)
Add a thin scatterer to a point.
Definition: GblPoint.cpp:159
GBL trajectory.
Definition: GblTrajectory.h:50
bool isValid() const
Retrieve validity of trajectory.
unsigned int getResults(int aSignedLabel, Eigen::VectorXd &localPar, Eigen::MatrixXd &localCov) const
Get fit results at point.
unsigned int fit(double &Chi2, int &Ndf, double &lostWeight, const std::string &optionList="", unsigned int aLabel=0)
Perform fit of (valid) trajectory.
void example3()
Definition: example3.cpp:37
Namespace for the general broken lines package.
double unrm()
unit normal distribution, Box-Muller method, polar form
Eigen::Matrix< double, 5, 1 > Vector5d
Matrix5d gblSimpleJacobian(double ds, double cosl, double bfac)
Simple jacobian.
Eigen::Matrix< double, 5, 5 > Matrix5d
Definition: GblData.h:46