GeneralBrokenLines V03-01-03
using EIGEN
exampleSit.cpp
Go to the documentation of this file.
1/*
2 * exampleSit.cpp
3 *
4 * Created on: 11 Oct 2018
5 * Author: kleinwrt
6 */
7
30#include <time.h>
31#include "exampleSit.h"
32#include "GblTrajectory.h"
33
34using namespace gbl;
35using namespace Eigen;
36
38
106
107 // detector layers (ordered in X):
108 // name, position (x,y,z), thickness (X/X_0), (1 or 2) measurements (direction in YZ, resolution)
109 std::vector<GblDetectorLayer> layers;
110 layers.push_back(
111 CreateLayerSit("PIX1", 0, 2.0, 0., 0., 0.0033, 0., 0.0010, 90.,
112 0.0020)); // pixel
113 layers.push_back(
114 CreateLayerSit("PIX2", 1, 3.0, 0., 0., 0.0033, 0., 0.0010, 90.,
115 0.0020)); // pixel
116 layers.push_back(
117 CreateLayerSit("PIX3", 2, 4.0, 0., 0., 0.0033, 0., 0.0010, 90.,
118 0.0020)); // pixel
119 layers.push_back(
120 CreateLayerSit("S2D4", 3, 6.0, 0., 0., 0.0033, 0., 0.0025, 5.0,
121 0.0025)); // strip 2D, +5 deg stereo angle
122 layers.push_back(
123 CreateLayerSit("S2D5", 4, 8.0, 0., 0., 0.0033, 0., 0.0025, -5.,
124 0.0025)); // strip 2D, -5 deg stereo angle
125 layers.push_back(
126 CreateLayerSit("S2D6", 5, 10., 0., 0., 0.0033, 0., 0.0025, 5.0,
127 0.0025)); // strip 2D, +5 deg stereo angle
128 layers.push_back(
129 CreateLayerSit("S2D7", 6, 12., 0., 0., 0.0033, 0., 0.0025, -5.,
130 0.0025)); // strip 2D, -5 deg stereo angle
131 layers.push_back(
132 CreateLayerSit("S1D8", 7, 15., 0., 0., 0.0033, 45., 0.0040)); // strip 1D, sensitivity to linear combination of Y and Z
133 layers.push_back(
134 CreateLayerSit("S1D9", 8, 16., 0., 0., 0.0033, -45., 0.0040)); // strip 1D, sensitivity to linear combination of Y and Z
135
136 /* print layers
137 for (unsigned int iLayer = 0; iLayer < layers.size(); ++iLayer) {
138 layers[iLayer].print();
139 } */
140
141 // Alignment with MillePede-II requires for alignables with a single 1D measurements to fix the unmeasured
142 // direction with a (linear equality) constraint (unless alignment equal to measurement system).
143 std::cout
144 << "! MillePede-II: constraints for alignables with SINGLE 1D measurements"
145 << std::endl;
146 for (unsigned int iLayer = 0; iLayer < layers.size(); ++iLayer) {
147 // count number of measurements per alignable
148 unsigned int numMeas = 0;
149 for (unsigned int jLayer = 0; jLayer < layers.size(); ++jLayer) {
150 if (layers[iLayer].getLayerID() == layers[jLayer].getLayerID())
151 numMeas++;
152 }
153 // alignable with single measurement
154 if (numMeas == 1)
155 layers[iLayer].printMP2Constraint();
156 }
157 std::cout << "! End of lines to be added to MillePede-II steering file"
158 << std::endl;
159 std::cout << std::endl;
160
161 unsigned int nTry = 10000; //: number of tries
162 std::cout << " GblSit $Id$ " << nTry << ", " << layers.size() << std::endl;
163 srand(4711);
164 clock_t startTime = clock();
165
166 double qbyp = 0.2; // 5 GeV
167 const double bfac = 0.003; // B*c for 1 T
168 // const double bfac = 0.; // B*c for 0 T
169
170 MilleBinary mille; // for producing MillePede-II binary file
171
172 double Chi2Sum = 0.;
173 int NdfSum = 0;
174 double LostSum = 0.;
175 int numFit = 0;
176
177 for (unsigned int iTry = 0; iTry < nTry; ++iTry) {
178
179 // helix parameter for track generation
180 const double genDca = 0.1 * unrm(); // normal
181 const double genZ0 = 0.1 * unrm(); // normal
182 const double genPhi0 = 0.2 * (2. * unif() - 1.); // uniform
183 const double genDzds = 0.3 * (2. * unif() - 1.); // uniform
184 const double genCurv = bfac * qbyp * sqrt(1. + genDzds * genDzds);
185
186 //
187 // generate hits
188 //
189 std::vector<Vector2d> hits;
190 double curv(genCurv), phi0(genPhi0), dca(genDca), dzds(genDzds), z0(
191 genZ0);
192 const double cosLambda = 1. / sqrt(1. + dzds * dzds);
193 for (unsigned int iLayer = 0; iLayer < layers.size(); ++iLayer) {
194 // local constant (Bfield) helix
195 GblSimpleHelix hlx = GblSimpleHelix(curv, phi0, dca, dzds, z0);
196 // prediction from local helix
197 GblHelixPrediction pred = layers[iLayer].intersectWithHelix(hlx);
198 // std::cout << " layer " << iLayer << " arc-length " << pred.getArcLength() << std::endl;
199 Vector2d meas = pred.getMeasPred();
200 // smear according to resolution
201 Vector2d sigma = layers[iLayer].getResolution();
202 meas[0] += sigma[0] * unrm();
203 meas[1] += sigma[1] * unrm();
204 // save hit
205 hits.push_back(meas);
206 // scatter at intersection point
207 Vector3d measPos = pred.getPosition();
208 double radlen = layers[iLayer].getRadiationLength()
209 / fabs(pred.getCosIncidence());
210 double errMs = gblMultipleScatteringError(qbyp, radlen); // simple model
211 // move to intersection point
212 hlx.moveToXY(measPos[0], measPos[1], phi0, dca, z0); // update phi0, dca, z0
213 phi0 += unrm() * errMs / cosLambda; // scattering for phi
214 dzds += unrm() * errMs / (cosLambda * cosLambda); // scattering for dzds
215 GblSimpleHelix newhlx = GblSimpleHelix(curv, phi0, dca, dzds, z0); // after scattering
216 // move back
217 newhlx.moveToXY(-measPos[0], -measPos[1], phi0, dca, z0); // update phi0, dca, z0
218 }
219
220 //
221 // fit track with GBL
222 //
223 // seed (with true parameters)
224 double seedCurv(genCurv), seedPhi0(genPhi0), seedDca(genDca), seedDzds(
225 genDzds), seedZ0(genZ0);
226 GblSimpleHelix seed = GblSimpleHelix(seedCurv, seedPhi0, seedDca,
227 seedDzds, seedZ0);
228 // (previous) arc-length
229 double sOld = 0.;
230 const double cosLambdaSeed = 1. / sqrt(1. + (seedDzds * seedDzds));
231 // list of points on trajectory
232 std::vector<GblPoint> listOfPoints;
233 for (unsigned int iLayer = 0; iLayer < layers.size(); ++iLayer) {
234 // std::cout << " hit " << iLayer << " " << hits[iLayer].transpose() << std::endl;
235 GblDetectorLayer &layer = layers[iLayer];
236 // prediction from seeding helix
237 GblHelixPrediction pred = layer.intersectWithHelix(seed);
238 double sArc = pred.getArcLength(); // arc-length
239 Vector2d measPrediction = pred.getMeasPred(); // measurement prediction
240 Vector2d measPrecision = layer.getPrecision(); // measurement precision
241 // residuals
242 Vector2d res(hits[iLayer][0] - measPrediction[0],
243 hits[iLayer][1] - measPrediction[1]);
244 // transformation global system to local (curvilinear) (u,v) (matrix from row vectors)
245 Matrix<double, 2, 3> transG2l = pred.getCurvilinearDirs();
246 // transformation measurement system to global system
247 Matrix3d transM2g = layer.getMeasSystemDirs().inverse();
248 // projection matrix (measurement plane to local (u,v))
249 Matrix2d proM2l = transG2l * transM2g.block<3, 2>(0, 0); // skip measurement normal
250 // projection matrix (local (u,v) to measurement plane)
251 Matrix2d proL2m = proM2l.inverse();
252 // propagation
253 Matrix5d jacPointToPoint = gblSimpleJacobian(
254 (sArc - sOld) / cosLambdaSeed, cosLambdaSeed, bfac);
255 sOld = sArc;
256 // point with (independent) measurements (in measurement system)
257 GblPoint point(jacPointToPoint);
258 point.addMeasurement(proL2m, res, measPrecision);
259 // global labels and parameters for rigid body alignment
260 std::vector<int> labGlobal(6);
261 for (int p = 0; p < 6; p++)
262 labGlobal[p] = layer.getRigidBodyGlobalLabel(p);
263 Vector3d pos = pred.getPosition();
264 Vector3d dir = pred.getDirection();
265 Matrix<double, 2, 6> derGlobal = layer.getRigidBodyDerLocal(pos,
266 dir);
267 point.addGlobals(labGlobal, derGlobal);
268 // add scatterer to point
269 double radlen = layer.getRadiationLength()
270 / fabs(pred.getCosIncidence());
271 double errMs = gblMultipleScatteringError(qbyp, radlen); // simple model
272 if (errMs > 0.) {
273 Vector2d scat(0., 0.);
274 Vector2d scatPrec(1. / (errMs * errMs), 1. / (errMs * errMs)); // scattering precision matrix is diagonal in curvilinear system
275 point.addScatterer(scat, scatPrec);
276 }
277 // add point to trajectory
278 listOfPoints.push_back(point);
279 }
280 // create trajectory
281 GblTrajectory traj(listOfPoints, bfac != 0.);
282 // fit trajectory
283 double Chi2;
284 int Ndf;
285 double lostWeight;
286 unsigned int ierr = traj.fit(Chi2, Ndf, lostWeight);
287 // std::cout << " Fit " << iTry << ": "<< Chi2 << ", " << Ndf << ", " << lostWeight << std::endl;
288 // successfully fitted?
289 if (!ierr) {
290 // write to MP binary file
291 traj.milleOut(mille);
292 // update statistics
293 Chi2Sum += Chi2;
294 NdfSum += Ndf;
295 LostSum += lostWeight;
296 numFit++;
297 }
298 }
299 clock_t endTime = clock();
300 double diff = endTime - startTime;
301 double cps = CLOCKS_PER_SEC;
302 std::cout << " Time elapsed " << diff / cps << " s" << std::endl;
303 std::cout << " Chi2/Ndf = " << Chi2Sum / NdfSum << std::endl;
304 std::cout << " Tracks fitted " << numFit << std::endl;
305 if (LostSum > 0.)
306 std::cout << " Weight lost " << LostSum << std::endl;
307}
308
309namespace gbl {
310
312
324GblDetectorLayer CreateLayerSit(const std::string aName, unsigned int layer,
325 double xPos, double yPos, double zPos, double thickness, double uAngle,
326 double uRes) {
327 Vector3d aCenter(xPos, yPos, zPos);
328 Vector2d aResolution(uRes, 0.);
329 Vector2d aPrecision(1. / (uRes * uRes), 0.);
330 Matrix3d measTrafo;
331 const double cosU = cos(uAngle / 180. * M_PI);
332 const double sinU = sin(uAngle / 180. * M_PI);
333 measTrafo << 0., cosU, sinU, 0., -sinU, cosU, 1., 0., 0.; // U,V,N
334 Matrix3d alignTrafo;
335 alignTrafo << 0., 1., 0., 0., 0., 1., 1., 0., 0.; // Y,Z,X
336 return GblDetectorLayer(aName, layer, 1, thickness, aCenter, aResolution,
337 aPrecision, measTrafo, alignTrafo);
338}
339
341
357GblDetectorLayer CreateLayerSit(const std::string aName, unsigned int layer,
358 double xPos, double yPos, double zPos, double thickness, double uAngle,
359 double uRes, double vAngle, double vRes) {
360 Vector3d aCenter(xPos, yPos, zPos);
361 Vector2d aResolution(uRes, vRes);
362 Vector2d aPrecision(1. / (uRes * uRes), 1. / (vRes * vRes));
363 Matrix3d measTrafo;
364 const double cosU = cos(uAngle / 180. * M_PI);
365 const double sinU = sin(uAngle / 180. * M_PI);
366 const double cosV = cos(vAngle / 180. * M_PI);
367 const double sinV = sin(vAngle / 180. * M_PI);
368 measTrafo << 0., cosU, sinU, 0., cosV, sinV, 1., 0., 0.; // U,V,N
369 Matrix3d alignTrafo;
370 alignTrafo << 0., 1., 0., 0., 0., 1., 1., 0., 0.; // Y,Z,X
371 return GblDetectorLayer(aName, layer, 2, thickness, aCenter, aResolution,
372 aPrecision, measTrafo, alignTrafo);
373}
374
375}
GblTrajectory definition.
Detector layer.
Definition: GblUtilities.h:107
unsigned int getRigidBodyGlobalLabel(const unsigned int aPar) const
Get global label.
GblHelixPrediction intersectWithHelix(GblSimpleHelix hlx) const
Intersect with helix.
double getRadiationLength() const
Get radiation length.
Eigen::Matrix< double, 2, 6 > getRigidBodyDerLocal(Eigen::Vector3d &position, Eigen::Vector3d &direction) const
Get rigid body derivatives in local (alignment) frame (rotated in measurement plane).
Eigen::Matrix3d getMeasSystemDirs() const
Get directions of measurement system.
Eigen::Vector2d getPrecision() const
Get precision.
Prediction on helix.
Definition: GblUtilities.h:49
Eigen::Matrix< double, 2, 3 > getCurvilinearDirs() const
Get curvilinear directions (U,V)
double getArcLength() const
Get arc-length.
const Eigen::Vector2d & getMeasPred() const
Get (measurement) prediction.
double getCosIncidence() const
Get cosine of incidence.
const Eigen::Vector3d & getPosition() const
Get position.
const Eigen::Vector3d & getDirection() const
Get position.
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 addGlobals(const std::vector< int > &aLabels, const Eigen::MatrixBase< Derivative > &aDerivatives)
Add global derivatives to a point.
Definition: GblPoint.h:293
void addScatterer(const Eigen::Vector2d &aResiduals, const Eigen::Matrix2d &aPrecision)
Add a thin scatterer to a point.
Definition: GblPoint.cpp:159
Simple helix.
Definition: GblUtilities.h:78
void moveToXY(double xPos, double yPos, double &newPhi0, double &newDca, double &newZ0) const
Move to new reference point (X,Y)
GBL trajectory.
Definition: GblTrajectory.h:50
void milleOut(MilleBinary &aMille)
Write valid trajectory to Millepede-II binary file.
unsigned int fit(double &Chi2, int &Ndf, double &lostWeight, const std::string &optionList="", unsigned int aLabel=0)
Perform fit of (valid) trajectory.
Millepede-II (binary) record.
Definition: MilleBinary.h:81
void exampleSit()
Silicon tracker example.
Definition: exampleSit.cpp:105
Definitions for exampleSit.
Namespace for the general broken lines package.
GblDetectorLayer CreateLayerSit(const std::string aName, unsigned int layer, double xPos, double yPos, double zPos, double thickness, double uAngle, double uRes)
Create a silicon layer with 1D measurement.
Definition: exampleSit.cpp:324
double unrm()
unit normal distribution, Box-Muller method, polar form
double gblMultipleScatteringError(double qbyp, double xbyx0)
Multiple scattering error.
double unif()
uniform distribution [0..1]
Matrix5d gblSimpleJacobian(double ds, double cosl, double bfac)
Simple jacobian.
GblDetectorLayer CreateLayerSit(const std::string aName, unsigned int layer, double xPos, double yPos, double zPos, double thickness, double uAngle, double uRes, double vAngle, double vRes)
Create a silicon layer with 2D measurement.
Definition: exampleSit.cpp:357
Eigen::Matrix< double, 5, 5 > Matrix5d
Definition: GblData.h:46