GeneralBrokenLines V03-01-01
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
79void exampleSit() {
80
81 // detector layers (ordered in X):
82 // name, position (x,y,z), thickness (X/X_0), (1 or 2) measurements (direction in YZ, resolution)
83 std::vector<GblDetectorLayer> layers;
84 layers.push_back(
85 CreateLayerSit("PIX1", 0, 2.0, 0., 0., 0.0033, 0., 0.0010, 90.,
86 0.0020)); // pixel
87 layers.push_back(
88 CreateLayerSit("PIX2", 1, 3.0, 0., 0., 0.0033, 0., 0.0010, 90.,
89 0.0020)); // pixel
90 layers.push_back(
91 CreateLayerSit("PIX3", 2, 4.0, 0., 0., 0.0033, 0., 0.0010, 90.,
92 0.0020)); // pixel
93 layers.push_back(
94 CreateLayerSit("S2D4", 3, 6.0, 0., 0., 0.0033, 0., 0.0025, 5.0,
95 0.0025)); // strip 2D, +5 deg stereo angle
96 layers.push_back(
97 CreateLayerSit("S2D5", 4, 8.0, 0., 0., 0.0033, 0., 0.0025, -5.,
98 0.0025)); // strip 2D, -5 deg stereo angle
99 layers.push_back(
100 CreateLayerSit("S2D6", 5, 10., 0., 0., 0.0033, 0., 0.0025, 5.0,
101 0.0025)); // strip 2D, +5 deg stereo angle
102 layers.push_back(
103 CreateLayerSit("S2D7", 6, 12., 0., 0., 0.0033, 0., 0.0025, -5.,
104 0.0025)); // strip 2D, -5 deg stereo angle
105 layers.push_back(
106 CreateLayerSit("S1D8", 7, 15., 0., 0., 0.0033, 0., 0.0040)); // strip 1D, no sensitivity to Z
107
108 /* print layers
109 for (unsigned int iLayer = 0; iLayer < layers.size(); ++iLayer) {
110 layers[iLayer].print();
111 } */
112
113 unsigned int nTry = 10000; //: number of tries
114 std::cout << " GblSit $Id$ " << nTry << ", " << layers.size()
115 << std::endl;
116 srand(4711);
117 clock_t startTime = clock();
118
119 double qbyp = 0.2; // 5 GeV
120 const double bfac = 0.003; // B*c for 1 T
121 // const double bfac = 0.; // B*c for 0 T
122
123 MilleBinary mille; // for producing MillePede-II binary file
124
125 double Chi2Sum = 0.;
126 int NdfSum = 0;
127 double LostSum = 0.;
128 int numFit = 0;
129
130 for (unsigned int iTry = 0; iTry < nTry; ++iTry) {
131
132 // helix parameter for track generation
133 const double genDca = 0.1 * unrm(); // normal
134 const double genZ0 = 0.1 * unrm(); // normal
135 const double genPhi0 = 0.2 * (2. * unif() - 1.); // uniform
136 const double genDzds = 0.3 * (2. * unif() - 1.); // uniform
137 const double genCurv = bfac * qbyp * sqrt(1. + genDzds * genDzds);
138
139 //
140 // generate hits
141 //
142 std::vector<Vector2d> hits;
143 double curv(genCurv), phi0(genPhi0), dca(genDca), dzds(genDzds), z0(
144 genZ0);
145 const double cosLambda = 1. / sqrt(1. + dzds * dzds);
146 for (unsigned int iLayer = 0; iLayer < layers.size(); ++iLayer) {
147 // local constant (Bfield) helix
148 GblSimpleHelix hlx = GblSimpleHelix(curv, phi0, dca, dzds, z0);
149 // prediction from local helix
150 GblHelixPrediction pred = layers[iLayer].intersectWithHelix(hlx);
151 // std::cout << " layer " << iLayer << " arc-length " << pred.getArcLength() << std::endl;
152 Vector2d meas = pred.getMeasPred();
153 // smear according to resolution
154 Vector2d sigma = layers[iLayer].getResolution();
155 meas[0] += sigma[0] * unrm();
156 meas[1] += sigma[1] * unrm();
157 // save hit
158 hits.push_back(meas);
159 // scatter at intersection point
160 Vector3d measPos = pred.getPosition();
161 double radlen = layers[iLayer].getRadiationLength()
162 / fabs(pred.getCosIncidence());
163 double errMs = gblMultipleScatteringError(qbyp, radlen); // simple model
164 // move to intersection point
165 hlx.moveToXY(measPos[0], measPos[1], phi0, dca, z0); // update phi0, dca, z0
166 phi0 += unrm() * errMs / cosLambda; // scattering for phi
167 dzds += unrm() * errMs / (cosLambda * cosLambda); // scattering for dzds
168 GblSimpleHelix newhlx = GblSimpleHelix(curv, phi0, dca, dzds, z0); // after scattering
169 // move back
170 newhlx.moveToXY(-measPos[0], -measPos[1], phi0, dca, z0); // update phi0, dca, z0
171 }
172
173 //
174 // fit track with GBL
175 //
176 // seed (with true parameters)
177 double seedCurv(genCurv), seedPhi0(genPhi0), seedDca(genDca), seedDzds(
178 genDzds), seedZ0(genZ0);
179 GblSimpleHelix seed = GblSimpleHelix(seedCurv, seedPhi0, seedDca,
180 seedDzds, seedZ0);
181 // (previous) arc-length
182 double sOld = 0.;
183 const double cosLambdaSeed = 1. / sqrt(1. + (seedDzds * seedDzds));
184 // list of points on trajectory
185 std::vector<GblPoint> listOfPoints;
186 for (unsigned int iLayer = 0; iLayer < layers.size(); ++iLayer) {
187 // std::cout << " hit " << iLayer << " " << hits[iLayer].transpose() << std::endl;
188 GblDetectorLayer& layer = layers[iLayer];
189 // prediction from seeding helix
190 GblHelixPrediction pred = layer.intersectWithHelix(seed);
191 double sArc = pred.getArcLength(); // arc-length
192 Vector2d measPrediction = pred.getMeasPred(); // measurement prediction
193 Vector2d measPrecision = layer.getPrecision(); // measurement precision
194 // residuals
195 Vector2d res(hits[iLayer][0] - measPrediction[0],
196 hits[iLayer][1] - measPrediction[1]);
197 // transformation global system to local (curvilinear) (u,v) (matrix from row vectors)
198 Matrix<double, 2, 3> transG2l = pred.getCurvilinearDirs();
199 // transformation measurement system to global system
200 Matrix3d transM2g = layer.getMeasSystemDirs().inverse();
201 // projection matrix (measurement plane to local (u,v))
202 Matrix2d proM2l = transG2l * transM2g.block<3, 2>(0, 0); // skip measurement normal
203 // projection matrix (local (u,v) to measurement plane)
204 Matrix2d proL2m = proM2l.inverse();
205 // propagation
206 Matrix5d jacPointToPoint = gblSimpleJacobian(
207 (sArc - sOld) / cosLambdaSeed, cosLambdaSeed, bfac);
208 sOld = sArc;
209 // point with (independent) measurements (in measurement system)
210 GblPoint point(jacPointToPoint);
211 point.addMeasurement(proL2m, res, measPrecision);
212 // global labels and parameters for rigid body alignment
213 std::vector<int> labGlobal(6);
214 unsigned int layerID = layer.getLayerID();
215 for (int p = 0; p < 6; p++)
216 labGlobal[p] = layerID * 10 + p + 1;
217 Vector3d pos = pred.getPosition();
218 Vector3d dir = pred.getDirection();
219 Matrix<double, 2, 6> derGlobal = layer.getRigidBodyDerLocal(pos,
220 dir);
221 point.addGlobals(labGlobal, derGlobal);
222 // add scatterer to point
223 double radlen = layer.getRadiationLength()
224 / fabs(pred.getCosIncidence());
225 double errMs = gblMultipleScatteringError(qbyp, radlen); // simple model
226 if (errMs > 0.) {
227 Vector2d scat(0., 0.);
228 Vector2d scatPrec(1. / (errMs * errMs), 1. / (errMs * errMs)); // scattering precision matrix is diagonal in curvilinear system
229 point.addScatterer(scat, scatPrec);
230 }
231 // add point to trajectory
232 listOfPoints.push_back(point);
233 }
234 // create trajectory
235 GblTrajectory traj(listOfPoints, bfac != 0.);
236 // fit trajectory
237 double Chi2;
238 int Ndf;
239 double lostWeight;
240 unsigned int ierr = traj.fit(Chi2, Ndf, lostWeight);
241 // std::cout << " Fit " << iTry << ": "<< Chi2 << ", " << Ndf << ", " << lostWeight << std::endl;
242 // successfully fitted?
243 if (!ierr) {
244 // write to MP binary file
245 traj.milleOut(mille);
246 // update statistics
247 Chi2Sum += Chi2;
248 NdfSum += Ndf;
249 LostSum += lostWeight;
250 numFit++;
251 }
252 }
253 clock_t endTime = clock();
254 double diff = endTime - startTime;
255 double cps = CLOCKS_PER_SEC;
256 std::cout << " Time elapsed " << diff / cps << " s" << std::endl;
257 std::cout << " Chi2/Ndf = " << Chi2Sum / NdfSum << std::endl;
258 std::cout << " Tracks fitted " << numFit << std::endl;
259 if (LostSum > 0.)
260 std::cout << " Weight lost " << LostSum << std::endl;
261}
262
263namespace gbl {
264
266
278GblDetectorLayer CreateLayerSit(const std::string aName, unsigned int layer,
279 double xPos, double yPos, double zPos, double thickness, double uAngle,
280 double uRes) {
281 Vector3d aCenter(xPos, yPos, zPos);
282 Vector2d aResolution(uRes, 0.);
283 Vector2d aPrecision(1. / (uRes * uRes), 0.);
284 Matrix3d measTrafo;
285 const double cosU = cos(uAngle / 180. * M_PI);
286 const double sinU = sin(uAngle / 180. * M_PI);
287 measTrafo << 0., cosU, sinU, 0., -sinU, cosU, 1., 0., 0.; // U,V,N
288 Matrix3d alignTrafo;
289 alignTrafo << 0., 1., 0., 0., 0., 1., 1., 0., 0.; // Y,Z,X
290 return GblDetectorLayer(aName, layer, 1, thickness, aCenter, aResolution,
291 aPrecision, measTrafo, alignTrafo);
292}
293
295
311GblDetectorLayer CreateLayerSit(const std::string aName, unsigned int layer,
312 double xPos, double yPos, double zPos, double thickness, double uAngle,
313 double uRes, double vAngle, double vRes) {
314 Vector3d aCenter(xPos, yPos, zPos);
315 Vector2d aResolution(uRes, vRes);
316 Vector2d aPrecision(1. / (uRes * uRes), 1. / (vRes * vRes));
317 Matrix3d measTrafo;
318 const double cosU = cos(uAngle / 180. * M_PI);
319 const double sinU = sin(uAngle / 180. * M_PI);
320 const double cosV = cos(vAngle / 180. * M_PI);
321 const double sinV = sin(vAngle / 180. * M_PI);
322 measTrafo << 0., cosU, sinU, 0., cosV, sinV, 1., 0., 0.; // U,V,N
323 Matrix3d alignTrafo;
324 alignTrafo << 0., 1., 0., 0., 0., 1., 1., 0., 0.; // Y,Z,X
325 return GblDetectorLayer(aName, layer, 2, thickness, aCenter, aResolution,
326 aPrecision, measTrafo, alignTrafo);
327}
328
329}
GblTrajectory definition.
Detector layer.
Definition: GblUtilities.h:107
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).
unsigned int getLayerID() const
Get layer ID.
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:79
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:278
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:311
Eigen::Matrix< double, 5, 5 > Matrix5d
Definition: GblData.h:46