GeneralBrokenLines V03-01-01
using EIGEN
example2.cpp
Go to the documentation of this file.
1/*
2 * example2.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 example2() {
39
60//MP MilleBinary mille; // for producing MillePede-II binary file
61 unsigned int nTry = 1000; //: number of tries
62 unsigned int nLayer = 10; //: number of detector layers
63 std::cout << " Gbltst-eigen $Id$ " << nTry << ", " << nLayer
64 << std::endl;
65
66 srand(4711);
67
68 clock_t startTime = clock();
69// track direction
70 double sinLambda = 0.3;
71 double cosLambda = sqrt(1.0 - sinLambda * sinLambda);
72 double sinPhi = 0.;
73 double cosPhi = sqrt(1.0 - sinPhi * sinPhi);
74// tDir = (cosLambda * cosPhi, cosLambda * sinPhi, sinLambda)
75 double ti = cosLambda * cosPhi; // T*I
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.001, 0.001;
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
103 double bfac = 0.2998; // Bz*c for Bz=1
104 double step = 1.5 / cosLambda; // constant steps in RPhi
105
106 double Chi2Sum = 0.;
107 int NdfSum = 0;
108 double LostSum = 0.;
109 int numFit = 0;
110
111 for (unsigned int iTry = 1; iTry <= nTry; ++iTry) {
112 // curvilinear track parameters
113 for (unsigned int i = 0; i < 5; ++i) {
114 clPar[i] = clErr[i] * unrm();
115 }
116// std::cout << " Try " << iTry << ":" << clPar << std::endl;
117 Matrix2d addDer;
118 addDer.setZero();
119 addDer(0, 0) = 1.;
120 addDer(1, 1) = 1.;
121// arclength
122 // double s = 0.;
123 Matrix5d jacPointToPoint;
124 jacPointToPoint.setIdentity();
125 Matrix5d oldM2c;
126 oldM2c.setIdentity();
127// create list of points
128 std::vector<GblPoint> listOfPoints;
129 listOfPoints.reserve(2 * nLayer);
130
131 for (unsigned int iLayer = 0; iLayer < nLayer; ++iLayer) {
132// std::cout << " Layer " << iLayer << ", " << s << std::endl;
133// measurement directions
134 double sinStereo = (iLayer % 2 == 0) ? 0. : 0.1;
135 double cosStereo = sqrt(1.0 - sinStereo * sinStereo);
136 Matrix<double, 3, 2> mDirT;
137 mDirT.setZero();
138 mDirT(1, 0) = cosStereo;
139 mDirT(2, 0) = sinStereo;
140 mDirT(1, 1) = -sinStereo;
141 mDirT(2, 1) = cosStereo;
142 double c1 = cosStereo * cosLambda * sinPhi + sinStereo * sinLambda; // T*M1
143 double c2 = -sinStereo * cosLambda * sinPhi + cosStereo * sinLambda; // T*M2
144// projection measurement to local (curvilinear uv) directions (duv/dm)
145 Matrix2d proM2l = uvDir * mDirT;
146// projection local (uv) to measurement directions (dm/duv)
147 Matrix2d proL2m = proM2l.inverse();
148// transformation of track parameters from measurement to curvilinear system
149 Matrix5d meas2crvl;
150 meas2crvl.setZero();
151 meas2crvl(0, 0) = 1.;
152 meas2crvl.block<2, 2>(1, 1) = proM2l * ti;
153 meas2crvl.block<2, 2>(3, 3) = proM2l;
154 Matrix5d crvl2meas = meas2crvl.inverse();
155 // point with (independent) measurements (in measurement system)
156 GblPoint pointMeas(crvl2meas * jacPointToPoint * oldM2c);
157 // measurement - prediction in measurement system with error
158 Vector2d meas = proL2m * clPar.tail(2);
159 for (unsigned int i = 0; i < 2; ++i) {
160 meas[i] += measErr[i] * unrm();
161 }
162 pointMeas.addMeasurement(meas, measPrec);
163
164// add point to trajectory
165 listOfPoints.push_back(pointMeas);
166
167// propagate to scatterer
168 jacPointToPoint = gblSimpleJacobian(step, cosLambda, bfac);
169 clPar = jacPointToPoint * clPar;
170 // s += step;
171 if (iLayer < nLayer - 1) {
172 Vector2d scat(0., 0.);
173 // point with scatterer
174 GblPoint pointScat(crvl2meas * jacPointToPoint * meas2crvl);
175 // scattering precision (full) matrix
176 Matrix2d scatP;
177 double fac = scatPrec(0) * (1 - c1 * c1 - c2 * c2);
178 scatP(0, 0) = fac * (1 - c1 * c1);
179 scatP(0, 1) = fac * (-c1 * c2);
180 scatP(1, 0) = fac * (-c1 * c2);
181 scatP(1, 1) = fac * (1 - c2 * c2);
182 pointScat.addScatterer(scat, scatP);
183 listOfPoints.push_back(pointScat);
184 // scatter a little
185 for (unsigned int i = 0; i < 2; ++i) {
186 clPar[i + 1] += scatErr[i] * unrm();
187 }
188 // propagate to next measurement layer
189 clPar = jacPointToPoint * clPar;
190 // s += step;
191 }
192 oldM2c = meas2crvl;
193 }
194//
195 // create trajectory
196 GblTrajectory traj(listOfPoints);
197 //GblTrajectory traj(listOfPoints, seedLabel, clSeed); // with external seed
198 //traj.printPoints();
199 /*
200 if (not traj.isValid()) {
201 std::cout << " Invalid GblTrajectory -> skip" << std::endl;
202 continue;
203 }*/
204// fit trajectory
205 double Chi2;
206 int Ndf;
207 double lostWeight;
208 traj.fit(Chi2, Ndf, lostWeight);
209 //std::cout << " Fit: " << Chi2 << ", " << Ndf << ", " << lostWeight << std::endl;
210 /* look at (track parameter) corrections
211 VectorXd aCorrection(5);
212 MatrixXd aCovariance(5, 5);
213 traj.getResults(1, aCorrection, aCovariance);
214 std::cout << " cor " << std::endl << aCorrection << std::endl;
215 std::cout << " cov " << std::endl << aCovariance << std::endl;
216 */
217 /* look at residuals
218 for (unsigned int label = 1; label <= listOfPoints.size(); ++label) {
219 unsigned int numData = 0;
220 std::cout << " measResults, label " << label << std::endl;
221 VectorXd residuals(2), measErr(2), resErr(2), downWeights(2);
222 traj.getMeasResults(label, numData, residuals, measErr, resErr,
223 downWeights);
224 std::cout << " measResults, numData " << numData << std::endl;
225 for (unsigned int i = 0; i < numData; ++i) {
226 std::cout << " measResults " << label << " " << i << " "
227 << residuals[i] << " " << measErr[i] << " " << resErr[i]
228 << std::endl;
229 }
230 } */
231// debug printout
232 //traj.printTrajectory(1);
233 //traj.printPoints(1);
234 //traj.printData();
235// write to MP binary file
236//MP traj.milleOut(mille);
237 Chi2Sum += Chi2;
238 NdfSum += Ndf;
239 LostSum += lostWeight;
240 numFit++;
241 }
242
243 clock_t endTime = clock();
244 double diff = endTime - startTime;
245 double cps = CLOCKS_PER_SEC;
246 std::cout << " Time elapsed " << diff / cps << " s" << std::endl;
247 std::cout << " Chi2/Ndf = " << Chi2Sum / NdfSum << std::endl;
248 std::cout << " Tracks fitted " << numFit << std::endl;
249 if (LostSum > 0.)
250 std::cout << " Weight lost " << LostSum << std::endl;
251}
252
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 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
unsigned int fit(double &Chi2, int &Ndf, double &lostWeight, const std::string &optionList="", unsigned int aLabel=0)
Perform fit of (valid) trajectory.
void example2()
Definition: example2.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