GeneralBrokenLines V03-01-03
using EIGEN
example4.cpp
Go to the documentation of this file.
1/*
2 * example4.cpp
3 *
4 * Created on: Dec 16, 2022
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 example4() {
39
59//MP MilleBinary mille; // for producing MillePede-II binary file
60 unsigned int nTry = 1000; //: number of tries
61 unsigned int nLayer = 10; //: number of detector layers
62 bool useThickScatterer = true; //: use thick (GBL) scatterers at measurements
63 std::cout << " Gbltst-thickScat $Id: 4dad593b54fd71605954af13372263355af83f16 $ " << nTry << ", " << nLayer
64 << ", useThickScat: " << useThickScatterer << 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// U = Z x T / |Z x T|, V = T x U
76 Matrix<double, 2, 3> uvDir;
77 uvDir(0, 0) = -sinPhi;
78 uvDir(0, 1) = cosPhi;
79 uvDir(0, 2) = 0.;
80 uvDir(1, 0) = -sinLambda * cosPhi;
81 uvDir(1, 1) = -sinLambda * sinPhi;
82 uvDir(1, 2) = cosLambda;
83// measurement resolution
84 Vector2d measErr;
85 measErr << 0.001, 0.001;
86 Vector2d measPrec; // (independent) precisions
87 measPrec << 1.0 / (measErr(0) * measErr(0)), 1.0 / (measErr(1) * measErr(1));
88// scattering error
89 Vector2d scatErr;
90 scatErr << 0.001, 0.001;
91 Vector2d scatPrec;
92 Vector2d scat(0., 0.);
93 scatPrec << 1.0 / (scatErr(0) * scatErr(0)), 1.0 / (scatErr(1) * scatErr(1));
94 Matrix4d scatPrecThick;
95 Vector4d scatThick(0., 0., 0., 0.);
96// (RMS of) CurviLinear track parameters (Q/P, slopes, offsets)
97 Vector5d clPar;
98 Vector5d clErr;
99 clErr << 0.001, -0.1, 0.2, -0.15, 0.25;
100// scattering cov matrix for thick scatterers
101 Matrix4d scatCov;
102// number of thin scatterers in thick scatterer
103 unsigned int numScat;
104// additional parameters
105 Vector2d addPar;
106 addPar << 0.0025, -0.005;
107 std::vector<int> globalLabels;
108 globalLabels.push_back(4711);
109 globalLabels.push_back(4712);
110// global labels for MP
111 /*MP std::vector<int> globalLabels(2);
112 globalLabels[0] = 11;
113 globalLabels[1] = 12; */
114
115 double bfac = 0.2998; // Bz*c for Bz=1
116 double step = 1.5 / cosLambda; // constant steps in RPhi
117
118 double Chi2Sum = 0.;
119 int NdfSum = 0;
120 double LostSum = 0.;
121 int numFit = 0;
122
123 for (unsigned int iTry = 1; iTry <= nTry; ++iTry) {
124 // curvilinear track parameters
125 for (unsigned int i = 0; i < 5; ++i) {
126 clPar[i] = clErr[i] * unrm();
127 }
128// std::cout << " Try " << iTry << ":" << clPar << std::endl;
129 Matrix2d addDer;
130 addDer.setZero();
131 addDer(0, 0) = 1.;
132 addDer(1, 1) = 1.;
133// arclength
134 //double s = 0.;
135 Matrix5d jacPointToPoint, jacToNextPlane;
136 jacPointToPoint.setIdentity();
137 scatCov.setZero();
138 numScat = 0;
139// create list of points
140 std::vector<GblPoint> listOfPoints;
141 listOfPoints.reserve(2 * nLayer);
142
143 for (unsigned int iLayer = 0; iLayer < nLayer; ++iLayer) {
144// std::cout << " Layer " << iLayer << ", " << s << std::endl;
145// measurement directions
146 double sinStereo = (iLayer % 2 == 0) ? 0. : 0.1;
147 double cosStereo = sqrt(1.0 - sinStereo * sinStereo);
148 Matrix<double, 3, 2> mDirT;
149 mDirT.setZero();
150 mDirT(1, 0) = cosStereo;
151 mDirT(2, 0) = sinStereo;
152 mDirT(1, 1) = -sinStereo;
153 mDirT(2, 1) = cosStereo;
154// projection measurement to local (curvilinear uv) directions (duv/dm)
155 Matrix2d proM2l = uvDir * mDirT;
156// projection local (uv) to measurement directions (dm/duv)
157 Matrix2d proL2m = proM2l.inverse();
158 // point with (independent) measurements (in measurement system)
159 GblPoint pointMeas(jacPointToPoint);
160 // measurement - prediction in measurement system with error
161 Vector2d meas = proL2m * clPar.tail(2);
162 //MP meas += addDer * addPar; // additional parameters
163 for (unsigned int i = 0; i < 2; ++i) {
164 meas[i] += measErr[i] * unrm();
165 }
166 pointMeas.addMeasurement(proL2m, meas, measPrec);
167
168 // additional local parameters?
169// point.addLocals(addDer);
170//MP point.addGlobals(globalLabels, addDer);
171 addDer *= -1.; // Der flips sign every measurement
172// describe scattering with thick scatterer ?
173 if (useThickScatterer) {
174 if (numScat) {
175 if (numScat == 1) {
176 std::cout
177 << " Singular cov. matrix for thick scatterer at layer "
178 << iLayer << std::endl;
179 std::cout << scatCov << std::endl;
180 return;
181 }
182 scatPrecThick = scatCov.inverse();
183 pointMeas.addThickScatterer(scatThick, scatPrecThick);
184 scatCov.setZero();
185 numScat = 0;
186 }
187 } else {
188// describe scattering with two thin scatterers
189 pointMeas.addScatterer(scat, scatPrec);
190 }
191// scatter at measurement too
192 // scatter a little
193 numScat++;
194 for (unsigned int i = 0; i < 2; ++i) {
195 clPar[i + 1] += scatErr[i] * unrm();
196 scatCov(i, i) += scatErr[i] * scatErr[i];
197 }
198// add point to trajectory
199 listOfPoints.push_back(pointMeas);
200// no scattering after last measurement
201 if (iLayer == nLayer - 1)
202 break;
203// propagate to scattering plane
204 jacToNextPlane = gblSimpleJacobian(step, cosLambda, bfac);
205 jacPointToPoint = jacToNextPlane;
206 //jac2 = gblSimpleJacobian2(step, cosLambda, bfac);
207 clPar = jacToNextPlane * clPar;
208 scatCov = jacToNextPlane.bottomRightCorner<4, 4>() * scatCov
209 * jacToNextPlane.bottomRightCorner<4, 4>().transpose();
210 //s += step;
211
212 if (useThickScatterer) {
213// no intermediate GblPoint, update jacobian
214 jacPointToPoint = jacToNextPlane * jacPointToPoint;
215 } else {
216// describe scattering with two thin scatterers, requires intermediate GblPoint
217 GblPoint pointScat(jacToNextPlane);
218 pointScat.addScatterer(scat, scatPrec);
219 listOfPoints.push_back(pointScat);
220 }
221 // scatter a little
222 numScat++;
223 for (unsigned int i = 0; i < 2; ++i) {
224 clPar[i + 1] += scatErr[i] * unrm();
225 scatCov(i, i) += scatErr[i] * scatErr[i];
226 }
227// propagate to next measurement layer
228 clPar = jacToNextPlane * clPar;
229 scatCov = jacToNextPlane.bottomRightCorner<4, 4>() * scatCov
230 * jacToNextPlane.bottomRightCorner<4, 4>().transpose();
231 //s += step;
232 }
233//
234 // create trajectory
235 GblTrajectory traj(listOfPoints);
236 //GblTrajectory traj(listOfPoints, seedLabel, clSeed); // with external seed
237 //traj.printPoints();
238 /*
239 if (not traj.isValid()) {
240 std::cout << " Invalid GblTrajectory -> skip" << std::endl;
241 continue;
242 }*/
243// fit trajectory
244 double Chi2;
245 int Ndf;
246 double lostWeight;
247 traj.fit(Chi2, Ndf, lostWeight);
248 //std::cout << " Fit: " << Chi2 << ", " << Ndf << ", " << lostWeight << std::endl;
249 /* look at (track parameter) corrections
250 VectorXd aCorrection(5);
251 MatrixXd aCovariance(5, 5);
252 traj.getResults(1, aCorrection, aCovariance);
253 std::cout << " cor " << std::endl << aCorrection << std::endl;
254 std::cout << " cov " << std::endl << aCovariance << std::endl;
255 */
256 /* look at residuals
257 for (unsigned int label = 1; label <= listOfPoints.size(); ++label) {
258 unsigned int numData = 0;
259 std::cout << " measResults, label " << label << std::endl;
260 VectorXd residuals(2), measErr(2), resErr(2), downWeights(2);
261 traj.getMeasResults(label, numData, residuals, measErr, resErr,
262 downWeights);
263 std::cout << " measResults, numData " << numData << std::endl;
264 for (unsigned int i = 0; i < numData; ++i) {
265 std::cout << " measResults " << label << " " << i << " "
266 << residuals[i] << " " << measErr[i] << " " << resErr[i]
267 << std::endl;
268 }
269 } */
270// debug printout
271 //traj.printTrajectory(1);
272 //traj.printPoints();
273 //traj.printData();
274// write to MP binary file
275//MP traj.milleOut(mille);
276 Chi2Sum += Chi2;
277 NdfSum += Ndf;
278 LostSum += lostWeight;
279 numFit++;
280 }
281
282 clock_t endTime = clock();
283 double diff = endTime - startTime;
284 double cps = CLOCKS_PER_SEC;
285 std::cout << " Time elapsed " << diff / cps << " s" << std::endl;
286 std::cout << " Chi2/Ndf = " << Chi2Sum / NdfSum << std::endl;
287 std::cout << " Tracks fitted " << numFit << std::endl;
288 if (LostSum > 0.)
289 std::cout << " Weight lost " << LostSum << std::endl;
290}
291
GblTrajectory definition.
Definitions for GBL utilities.
Point on trajectory.
Definition: GblPoint.h:62
void addThickScatterer(const Eigen::Vector4d &aResiduals, const Eigen::Matrix4d &aPrecision)
Add a thick scatterer to a point.
Definition: GblPoint.cpp:232
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 example4()
Definition: example4.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