forked from sPHENIX-Collaboration/GenFit
/
GblTrajectory.cc
1203 lines (1129 loc) · 43.4 KB
/
GblTrajectory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GblTrajectory.cpp
*
* Created on: Aug 18, 2011
* Author: kleinwrt
*/
/** \file
* GblTrajectory methods.
*
* \author Claus Kleinwort, DESY, 2011 (Claus.Kleinwort@desy.de)
*
* \copyright
* Copyright (c) 2011 - 2016 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY \n\n
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version. \n\n
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details. \n\n
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* General information
*
* Introduction
*
* For a track with an initial trajectory from a prefit of the
* measurements (internal seed) or an external prediction
* (external seed) the description of multiple scattering is
* added by offsets in a local system. Along the initial
* trajectory points are defined with can describe a measurement
* or a (thin) scatterer or both. Measurements are arbitrary
* functions of the local track parameters at a point (e.g. 2D:
* position, 4D: direction+position). The refit provides corrections
* to the local track parameters (in the local system) and the
* corresponding covariance matrix at any of those points.
* Non-diagonal covariance matrices of
* measurements will be diagonalized internally.
* Outliers can be down-weighted by use of M-estimators.
*
* A position measurement is in a plane defined by two directions.
* Along one direction the measurement precision may be zero,
* defining a 1D measurement in the other direction.
*
* The broken lines trajectory is defined by (2D) offsets at the
* first and last point and all points with a scatterer. The
* prediction for a measurement is obtained by interpolation of
* the enclosing offsets and for triplets of adjacent offsets
* kink angles are determined. This requires for all points the
* jacobians for propagation to the previous and next offset.
* These are calculated from the point-to-point jacobians along
* the initial trajectory. The sequence of points has to be
* strictly monotonic in arc-length.
*
* Additional local or global parameters can be added and the
* trajectories can be written to special binary files for
* calibration and alignment with Millepede-II.
* (V. Blobel, NIM A, 566 (2006), pp. 5-13).
*
* Besides simple trajectories describing the path of a single
* particle composed trajectories are supported. These are
* constructed from the trajectories of multiple particles and
* some external parameters (like those describing a decay)
* and transformations at the first points from the external
* to the local track parameters.
*
* The conventions for the coordinate systems follow:
* Derivation of Jacobians for the propagation of covariance
* matrices of track parameters in homogeneous magnetic fields
* A. Strandlie, W. Wittek, NIM A, 566 (2006) 687-698.
*
* Calling sequence
*
* -# Create list of points on initial trajectory:\n
* <tt>std::vector<GblPoint> list</tt>
* -# For all points on initial trajectory:
* - Create points and add appropriate attributes:\n
* - <tt>point = gbl::GblPoint(..)</tt>
* - <tt>point.addMeasurement(..)</tt>
* - Add additional local or global parameters to measurement:\n
* - <tt>point.addLocals(..)</tt>
* - <tt>point.addGlobals(..)</tt>
* - <tt>point.addScatterer(..)</tt>
* - Add point (ordered by arc length) to list:\n
* <tt>list.push_back(point)</tt>
* -# Create (simple) trajectory from list of points:\n
* <tt>traj = gbl::GblTrajectory (list)</tt>
* -# Optionally with external seed:\n
* <tt>traj = gbl::GblTrajectory (list,seed)</tt>
* -# Optionally check validity of trajectory:\n
* <tt>if (not traj.isValid()) .. //abort</tt>
* -# Fit trajectory, return error code,
* get Chi2, Ndf (and weight lost by M-estimators):\n
* <tt>ierr = traj.fit(..)</tt>
* -# For any point on initial trajectory:
* - Get corrections and covariance matrix for track parameters:\n
* <tt>[..] = traj.getResults(label)</tt>
* - Optionally get residuals with errors for measurements:\n
* <tt>[..] = traj.getMeasResults(label) </tt>
* - Optionally get residuals with errors for scatterers:\n
* <tt>[..] = traj.getScatResults(label) </tt>
* -# Optionally write trajectory to MP binary file (doesn't needs to be fitted):\n
* <tt>traj.milleOut(..)</tt>
*
* Local system and local parameters
* At each point on the trajectory a local coordinate system with local track
* parameters has to be defined. The first of the five parameters describes
* the bending, the next two the direction and the last two the position (offsets).
* The curvilinear system (T,U,V) with parameters (q/p, lambda, phi, x_t, y_t)
* is well suited.
*
* Implementation
*
* Matrices are implemented with ROOT (root.cern.ch). User input or output is in the
* form of TMatrices. Internally SMatrices are used for fixes sized and simple matrices
* based on std::vector<> for variable sized matrices.
*
* References
* - V. Blobel, C. Kleinwort, F. Meier,
* Fast alignment of a complex tracking detector using advanced track models,
* Computer Phys. Communications (2011), doi:10.1016/j.cpc.2011.03.017
* - C. Kleinwort, General Broken Lines as advanced track fitting method,
* NIM A, 673 (2012), 107-110, doi:10.1016/j.nima.2012.01.024
*/
#include "GblTrajectory.h"
//! Namespace for the general broken lines package
namespace gbl {
/// Create new (simple) trajectory from list of points.
/**
* Curved trajectory in space (default) or without curvature (q/p) or in one
* plane (u-direction) only.
* \param [in] aPointList List of points
* \param [in] flagCurv Use q/p
* \param [in] flagU1dir Use in u1 direction
* \param [in] flagU2dir Use in u2 direction
*/
GblTrajectory::GblTrajectory(const std::vector<GblPoint> &aPointList,
bool flagCurv, bool flagU1dir, bool flagU2dir) :
numAllPoints(aPointList.size()), numPoints(), numOffsets(0), numInnerTrans(
0), numCurvature(flagCurv ? 1 : 0), numParameters(0), numLocals(
0), numMeasurements(0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
if (flagU1dir)
theDimension.push_back(0);
if (flagU2dir)
theDimension.push_back(1);
// simple (single) trajectory
thePoints.push_back(aPointList);
numPoints.push_back(numAllPoints);
construct(); // construct trajectory
}
/// Create new (simple) trajectory from list of points with external seed.
/**
* Curved trajectory in space (default) or without curvature (q/p) or in one
* plane (u-direction) only.
* \param [in] aPointList List of points
* \param [in] aLabel (Signed) label of point for external seed
* (<0: in front, >0: after point, slope changes at scatterer!)
* \param [in] aSeed Precision matrix of external seed
* \param [in] flagCurv Use q/p
* \param [in] flagU1dir Use in u1 direction
* \param [in] flagU2dir Use in u2 direction
*/
GblTrajectory::GblTrajectory(const std::vector<GblPoint> &aPointList,
unsigned int aLabel, const TMatrixDSym &aSeed, bool flagCurv,
bool flagU1dir, bool flagU2dir) :
numAllPoints(aPointList.size()), numPoints(), numOffsets(0), numInnerTrans(
0), numCurvature(flagCurv ? 1 : 0), numParameters(0), numLocals(
0), numMeasurements(0), externalPoint(aLabel), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(
aSeed), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
if (flagU1dir)
theDimension.push_back(0);
if (flagU2dir)
theDimension.push_back(1);
// simple (single) trajectory
thePoints.push_back(aPointList);
numPoints.push_back(numAllPoints);
construct(); // construct trajectory
}
/// Create new composed trajectory from list of points and transformations.
/**
* Composed of curved trajectory in space.
* \param [in] aPointsAndTransList List containing pairs with list of points and transformation (at inner (first) point)
*/
GblTrajectory::GblTrajectory(
const std::vector<std::pair<std::vector<GblPoint>, TMatrixD> > &aPointsAndTransList) :
numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(), externalMeasurements(), externalPrecisions() {
for (unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
thePoints.push_back(aPointsAndTransList[iTraj].first);
numPoints.push_back(thePoints.back().size());
numAllPoints += numPoints.back();
innerTransformations.push_back(aPointsAndTransList[iTraj].second);
}
theDimension.push_back(0);
theDimension.push_back(1);
numCurvature = innerTransformations[0].GetNcols();
construct(); // construct (composed) trajectory
}
/// Create new composed trajectory from list of points and transformations with (independent) external measurements.
/**
* Composed of curved trajectory in space.
* \param [in] aPointsAndTransList List containing pairs with list of points and transformation (at inner (first) point)
* \param [in] extDerivatives Derivatives of external measurements vs external parameters
* \param [in] extMeasurements External measurements (residuals)
* \param [in] extPrecisions Precision of external measurements
*/
GblTrajectory::GblTrajectory(
const std::vector<std::pair<std::vector<GblPoint>, TMatrixD> > &aPointsAndTransList,
const TMatrixD &extDerivatives, const TVectorD &extMeasurements,
const TVectorD &extPrecisions) :
numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations(), externalDerivatives(
extDerivatives), externalMeasurements(extMeasurements), externalPrecisions(
extPrecisions) {
for (unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
thePoints.push_back(aPointsAndTransList[iTraj].first);
numPoints.push_back(thePoints.back().size());
numAllPoints += numPoints.back();
innerTransformations.push_back(aPointsAndTransList[iTraj].second);
}
theDimension.push_back(0);
theDimension.push_back(1);
numCurvature = innerTransformations[0].GetNcols();
construct(); // construct (composed) trajectory
}
/// Create new composed trajectory from list of points and transformations with (correlated) external measurements.
/**
* Composed of curved trajectory in space.
* \param [in] aPointsAndTransList List containing pairs with list of points and transformation (at inner (first) point)
* \param [in] extDerivatives Derivatives of external measurements vs external parameters
* \param [in] extMeasurements External measurements (residuals)
* \param [in] extPrecisions Precision of external measurements
*/
GblTrajectory::GblTrajectory(
const std::vector<std::pair<std::vector<GblPoint>, TMatrixD> > &aPointsAndTransList,
const TMatrixD &extDerivatives, const TVectorD &extMeasurements,
const TMatrixDSym &extPrecisions) :
numAllPoints(), numPoints(), numOffsets(0), numInnerTrans(
aPointsAndTransList.size()), numParameters(0), numLocals(0), numMeasurements(
0), externalPoint(0), theDimension(0), thePoints(), theData(), measDataIndex(), scatDataIndex(), externalSeed(), innerTransformations() {
// diagonalize external measurement
TMatrixDSymEigen extEigen(extPrecisions);
TMatrixD extTransformation = extEigen.GetEigenVectors();
extTransformation.T();
externalDerivatives.ResizeTo(extDerivatives);
externalDerivatives = extTransformation * extDerivatives;
externalMeasurements.ResizeTo(extMeasurements);
externalMeasurements = extTransformation * extMeasurements;
externalPrecisions.ResizeTo(extMeasurements);
externalPrecisions = extEigen.GetEigenValues();
for (unsigned int iTraj = 0; iTraj < aPointsAndTransList.size(); ++iTraj) {
thePoints.push_back(aPointsAndTransList[iTraj].first);
numPoints.push_back(thePoints.back().size());
numAllPoints += numPoints.back();
innerTransformations.push_back(aPointsAndTransList[iTraj].second);
}
theDimension.push_back(0);
theDimension.push_back(1);
numCurvature = innerTransformations[0].GetNcols();
construct(); // construct (composed) trajectory
}
GblTrajectory::~GblTrajectory() {
}
/// Retrieve validity of trajectory
bool GblTrajectory::isValid() const {
return constructOK;
}
/// Retrieve number of point from trajectory
unsigned int GblTrajectory::getNumPoints() const {
return numAllPoints;
}
/// Construct trajectory from list of points.
/**
* Trajectory is prepared for fit or output to binary file, may consists of sub-trajectories.
*/
void GblTrajectory::construct() {
constructOK = false;
fitOK = false;
unsigned int aLabel = 0;
if (numAllPoints < 2) {
std::cout << " GblTrajectory construction failed: too few GblPoints "
<< std::endl;
return;
}
// loop over trajectories
numTrajectories = thePoints.size();
//std::cout << " numTrajectories: " << numTrajectories << ", " << innerTransformations.size() << std::endl;
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
std::vector<GblPoint>::iterator itPoint;
for (itPoint = thePoints[iTraj].begin();
itPoint < thePoints[iTraj].end(); ++itPoint) {
numLocals = std::max(numLocals, itPoint->getNumLocals());
numMeasurements += itPoint->hasMeasurement();
itPoint->setLabel(++aLabel);
}
}
defineOffsets();
calcJacobians();
try {
prepare();
} catch (std::overflow_error &e) {
std::cout << " GblTrajectory construction failed: " << e.what()
<< std::endl;
return;
}
constructOK = true;
// number of fit parameters
numParameters = (numOffsets - 2 * numInnerTrans) * theDimension.size()
+ numCurvature + numLocals;
}
/// Define offsets from list of points.
/**
* Define offsets at points with scatterers and first and last point.
* All other points need interpolation from adjacent points with offsets.
*/
void GblTrajectory::defineOffsets() {
// loop over trajectories
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
// first point is offset
thePoints[iTraj].front().setOffset(numOffsets++);
// intermediate scatterers are offsets
std::vector<GblPoint>::iterator itPoint;
for (itPoint = thePoints[iTraj].begin() + 1;
itPoint < thePoints[iTraj].end() - 1; ++itPoint) {
if (itPoint->hasScatterer()) {
itPoint->setOffset(numOffsets++);
} else {
itPoint->setOffset(-numOffsets);
}
}
// last point is offset
thePoints[iTraj].back().setOffset(numOffsets++);
}
}
/// Calculate Jacobians to previous/next scatterer from point to point ones.
void GblTrajectory::calcJacobians() {
SMatrix55 scatJacobian;
// loop over trajectories
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
// forward propagation (all)
GblPoint* previousPoint = &thePoints[iTraj].front();
unsigned int numStep = 0;
std::vector<GblPoint>::iterator itPoint;
for (itPoint = thePoints[iTraj].begin() + 1;
itPoint < thePoints[iTraj].end(); ++itPoint) {
if (numStep == 0) {
scatJacobian = itPoint->getP2pJacobian();
} else {
scatJacobian = itPoint->getP2pJacobian() * scatJacobian;
}
numStep++;
itPoint->addPrevJacobian(scatJacobian); // iPoint -> previous scatterer
if (itPoint->getOffset() >= 0) {
previousPoint->addNextJacobian(scatJacobian); // lastPoint -> next scatterer
numStep = 0;
previousPoint = &(*itPoint);
}
}
// backward propagation (without scatterers)
for (itPoint = thePoints[iTraj].end() - 1;
itPoint > thePoints[iTraj].begin(); --itPoint) {
if (itPoint->getOffset() >= 0) {
scatJacobian = itPoint->getP2pJacobian();
continue; // skip offsets
}
itPoint->addNextJacobian(scatJacobian); // iPoint -> next scatterer
scatJacobian = scatJacobian * itPoint->getP2pJacobian();
}
}
}
/// Get jacobian for transformation from fit to track parameters at point.
/**
* Jacobian broken lines (q/p,..,u_i,u_i+1..) to track (q/p,u',u) parameters
* including additional local parameters.
* \param [in] aSignedLabel (Signed) label of point for external seed
* (<0: in front, >0: after point, slope changes at scatterer!)
* \return List of fit parameters with non zero derivatives and
* corresponding transformation matrix
*/
std::pair<std::vector<unsigned int>, TMatrixD> GblTrajectory::getJacobian(
int aSignedLabel) const {
unsigned int nDim = theDimension.size();
unsigned int nCurv = numCurvature;
unsigned int nLocals = numLocals;
unsigned int nBorder = nCurv + nLocals;
unsigned int nParBRL = nBorder + 2 * nDim;
unsigned int nParLoc = nLocals + 5;
std::vector<unsigned int> anIndex;
anIndex.reserve(nParBRL);
TMatrixD aJacobian(nParLoc, nParBRL);
aJacobian.Zero();
unsigned int aLabel = abs(aSignedLabel);
unsigned int firstLabel = 1;
unsigned int lastLabel = 0;
unsigned int aTrajectory = 0;
// loop over trajectories
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
aTrajectory = iTraj;
lastLabel += numPoints[iTraj];
if (aLabel <= lastLabel)
break;
if (iTraj < numTrajectories - 1)
firstLabel += numPoints[iTraj];
}
int nJacobian; // 0: prev, 1: next
// check consistency of (index, direction)
if (aSignedLabel > 0) {
nJacobian = 1;
if (aLabel >= lastLabel) {
aLabel = lastLabel;
nJacobian = 0;
}
} else {
nJacobian = 0;
if (aLabel <= firstLabel) {
aLabel = firstLabel;
nJacobian = 1;
}
}
const GblPoint aPoint = thePoints[aTrajectory][aLabel - firstLabel];
std::vector<unsigned int> labDer(5);
SMatrix55 matDer;
getFitToLocalJacobian(labDer, matDer, aPoint, 5, nJacobian);
// from local parameters
for (unsigned int i = 0; i < nLocals; ++i) {
aJacobian(i + 5, i) = 1.0;
anIndex.push_back(i + 1);
}
// from trajectory parameters
unsigned int iCol = nLocals;
for (unsigned int i = 0; i < 5; ++i) {
if (labDer[i] > 0) {
anIndex.push_back(labDer[i]);
for (unsigned int j = 0; j < 5; ++j) {
aJacobian(j, iCol) = matDer(j, i);
}
++iCol;
}
}
return std::make_pair(anIndex, aJacobian);
}
/// Get (part of) jacobian for transformation from (trajectory) fit to track parameters at point.
/**
* Jacobian broken lines (q/p,..,u_i,u_i+1..) to local (q/p,u',u) parameters.
* \param [out] anIndex List of fit parameters with non zero derivatives
* \param [out] aJacobian Corresponding transformation matrix
* \param [in] aPoint Point to use
* \param [in] measDim Dimension of 'measurement'
* (<=2: calculate only offset part, >2: complete matrix)
* \param [in] nJacobian Direction (0: to previous offset, 1: to next offset)
*/
void GblTrajectory::getFitToLocalJacobian(std::vector<unsigned int> &anIndex,
SMatrix55 &aJacobian, const GblPoint &aPoint, unsigned int measDim,
unsigned int nJacobian) const {
unsigned int nDim = theDimension.size();
unsigned int nCurv = numCurvature;
unsigned int nLocals = numLocals;
int nOffset = aPoint.getOffset();
if (nOffset < 0) // need interpolation
{
SMatrix22 prevW, prevWJ, nextW, nextWJ, matN;
SVector2 prevWd, nextWd;
int ierr;
aPoint.getDerivatives(0, prevW, prevWJ, prevWd); // W-, W- * J-, W- * d-
aPoint.getDerivatives(1, nextW, nextWJ, nextWd); // W-, W- * J-, W- * d-
const SMatrix22 sumWJ(prevWJ + nextWJ);
matN = sumWJ.Inverse(ierr); // N = (W- * J- + W+ * J+)^-1
// derivatives for u_int
const SMatrix22 prevNW(matN * prevW); // N * W-
const SMatrix22 nextNW(matN * nextW); // N * W+
const SVector2 prevNd(matN * prevWd); // N * W- * d-
const SVector2 nextNd(matN * nextWd); // N * W+ * d+
unsigned int iOff = nDim * (-nOffset - 1) + nLocals + nCurv + 1; // first offset ('i' in u_i)
// local offset
if (nCurv > 0) {
aJacobian.Place_in_col(-prevNd - nextNd, 3, 0); // from curvature
anIndex[0] = nLocals + 1;
}
aJacobian.Place_at(prevNW, 3, 1); // from 1st Offset
aJacobian.Place_at(nextNW, 3, 3); // from 2nd Offset
for (unsigned int i = 0; i < nDim; ++i) {
anIndex[1 + theDimension[i]] = iOff + i;
anIndex[3 + theDimension[i]] = iOff + nDim + i;
}
// local slope and curvature
if (measDim > 2) {
// derivatives for u'_int
const SMatrix22 prevWPN(nextWJ * prevNW); // W+ * J+ * N * W-
const SMatrix22 nextWPN(prevWJ * nextNW); // W- * J- * N * W+
const SVector2 prevWNd(nextWJ * prevNd); // W+ * J+ * N * W- * d-
const SVector2 nextWNd(prevWJ * nextNd); // W- * J- * N * W+ * d+
if (nCurv > 0) {
aJacobian(0, 0) = 1.0;
aJacobian.Place_in_col(prevWNd - nextWNd, 1, 0); // from curvature
}
aJacobian.Place_at(-prevWPN, 1, 1); // from 1st Offset
aJacobian.Place_at(nextWPN, 1, 3); // from 2nd Offset
}
} else { // at point
// anIndex must be sorted
// forward : iOff2 = iOff1 + nDim, index1 = 1, index2 = 3
// backward: iOff2 = iOff1 - nDim, index1 = 3, index2 = 1
unsigned int iOff1 = nDim * nOffset + nCurv + nLocals + 1; // first offset ('i' in u_i)
unsigned int index1 = 3 - 2 * nJacobian; // index of first offset
unsigned int iOff2 = iOff1 + nDim * (nJacobian * 2 - 1); // second offset ('i' in u_i)
unsigned int index2 = 1 + 2 * nJacobian; // index of second offset
// local offset
aJacobian(3, index1) = 1.0; // from 1st Offset
aJacobian(4, index1 + 1) = 1.0;
for (unsigned int i = 0; i < nDim; ++i) {
anIndex[index1 + theDimension[i]] = iOff1 + i;
}
// local slope and curvature
if (measDim > 2) {
SMatrix22 matW, matWJ;
SVector2 vecWd;
aPoint.getDerivatives(nJacobian, matW, matWJ, vecWd); // W, W * J, W * d
double sign = (nJacobian > 0) ? 1. : -1.;
if (nCurv > 0) {
aJacobian(0, 0) = 1.0;
aJacobian.Place_in_col(-sign * vecWd, 1, 0); // from curvature
anIndex[0] = nLocals + 1;
}
aJacobian.Place_at(-sign * matWJ, 1, index1); // from 1st Offset
aJacobian.Place_at(sign * matW, 1, index2); // from 2nd Offset
for (unsigned int i = 0; i < nDim; ++i) {
anIndex[index2 + theDimension[i]] = iOff2 + i;
}
}
}
}
/// Get jacobian for transformation from (trajectory) fit to kink parameters at point.
/**
* Jacobian broken lines (q/p,..,u_i-1,u_i,u_i+1..) to kink (du') parameters.
* \param [out] anIndex List of fit parameters with non zero derivatives
* \param [out] aJacobian Corresponding transformation matrix
* \param [in] aPoint Point to use
*/
void GblTrajectory::getFitToKinkJacobian(std::vector<unsigned int> &anIndex,
SMatrix27 &aJacobian, const GblPoint &aPoint) const {
unsigned int nDim = theDimension.size();
unsigned int nCurv = numCurvature;
unsigned int nLocals = numLocals;
int nOffset = aPoint.getOffset();
SMatrix22 prevW, prevWJ, nextW, nextWJ;
SVector2 prevWd, nextWd;
aPoint.getDerivatives(0, prevW, prevWJ, prevWd); // W-, W- * J-, W- * d-
aPoint.getDerivatives(1, nextW, nextWJ, nextWd); // W-, W- * J-, W- * d-
const SMatrix22 sumWJ(prevWJ + nextWJ); // W- * J- + W+ * J+
const SVector2 sumWd(prevWd + nextWd); // W+ * d+ + W- * d-
unsigned int iOff = (nOffset - 1) * nDim + nCurv + nLocals + 1; // first offset ('i' in u_i)
// local offset
if (nCurv > 0) {
aJacobian.Place_in_col(-sumWd, 0, 0); // from curvature
anIndex[0] = nLocals + 1;
}
aJacobian.Place_at(prevW, 0, 1); // from 1st Offset
aJacobian.Place_at(-sumWJ, 0, 3); // from 2nd Offset
aJacobian.Place_at(nextW, 0, 5); // from 1st Offset
for (unsigned int i = 0; i < nDim; ++i) {
anIndex[1 + theDimension[i]] = iOff + i;
anIndex[3 + theDimension[i]] = iOff + nDim + i;
anIndex[5 + theDimension[i]] = iOff + nDim * 2 + i;
}
}
/// Get fit results at point.
/**
* Get corrections and covariance matrix for local track and additional parameters
* in forward or backward direction.
*
* The point is identified by its label (1..number(points)), the sign distinguishes the
* backward (facing previous point) and forward 'side' (facing next point).
* For scatterers the track direction may change in between.
*
* \param [in] aSignedLabel (Signed) label of point on trajectory
* (<0: in front, >0: after point, slope changes at scatterer!)
* \param [out] localPar Corrections for local parameters
* \param [out] localCov Covariance for local parameters
* \return error code (non-zero if trajectory not fitted successfully)
*/
unsigned int GblTrajectory::getResults(int aSignedLabel, TVectorD &localPar,
TMatrixDSym &localCov) const {
if (not fitOK)
return 1;
std::pair<std::vector<unsigned int>, TMatrixD> indexAndJacobian =
getJacobian(aSignedLabel);
unsigned int nParBrl = indexAndJacobian.first.size();
TVectorD aVec(nParBrl); // compressed vector
for (unsigned int i = 0; i < nParBrl; ++i) {
aVec[i] = theVector(indexAndJacobian.first[i] - 1);
}
TMatrixDSym aMat = theMatrix.getBlockMatrix(indexAndJacobian.first); // compressed matrix
localPar = indexAndJacobian.second * aVec;
localCov = aMat.Similarity(indexAndJacobian.second);
return 0;
}
/// Get residuals from fit at point for measurement.
/**
* Get (diagonalized) residual, error of measurement and residual and down-weighting
* factor for measurement at point
*
* \param [in] aLabel Label of point on trajectory
* \param [out] numData Number of data blocks from measurement at point
* \param [out] aResiduals Measurements-Predictions
* \param [out] aMeasErrors Errors of Measurements
* \param [out] aResErrors Errors of Residuals (including correlations from track fit)
* \param [out] aDownWeights Down-Weighting factors
* \return error code (non-zero if trajectory not fitted successfully)
*/
unsigned int GblTrajectory::getMeasResults(unsigned int aLabel,
unsigned int &numData, TVectorD &aResiduals, TVectorD &aMeasErrors,
TVectorD &aResErrors, TVectorD &aDownWeights) {
numData = 0;
if (not fitOK)
return 1;
unsigned int firstData = measDataIndex[aLabel - 1]; // first data block with measurement
numData = measDataIndex[aLabel] - firstData; // number of data blocks
for (unsigned int i = 0; i < numData; ++i) {
getResAndErr(firstData + i, aResiduals[i], aMeasErrors[i],
aResErrors[i], aDownWeights[i]);
}
return 0;
}
/// Get (kink) residuals from fit at point for scatterer.
/**
* Get (diagonalized) residual, error of measurement and residual and down-weighting
* factor for scatterering kinks at point
*
* \param [in] aLabel Label of point on trajectory
* \param [out] numData Number of data blocks from scatterer at point
* \param [out] aResiduals (kink)Measurements-(kink)Predictions
* \param [out] aMeasErrors Errors of (kink)Measurements
* \param [out] aResErrors Errors of Residuals (including correlations from track fit)
* \param [out] aDownWeights Down-Weighting factors
* \return error code (non-zero if trajectory not fitted successfully)
*/
unsigned int GblTrajectory::getScatResults(unsigned int aLabel,
unsigned int &numData, TVectorD &aResiduals, TVectorD &aMeasErrors,
TVectorD &aResErrors, TVectorD &aDownWeights) {
numData = 0;
if (not fitOK)
return 1;
unsigned int firstData = scatDataIndex[aLabel - 1]; // first data block with scatterer
numData = scatDataIndex[aLabel] - firstData; // number of data blocks
for (unsigned int i = 0; i < numData; ++i) {
getResAndErr(firstData + i, aResiduals[i], aMeasErrors[i],
aResErrors[i], aDownWeights[i]);
}
return 0;
}
/// Get (list of) labels of points on (simple) valid trajectory
/**
* \param [out] aLabelList List of labels (aLabelList[i] = i+1)
* \return error code (non-zero if trajectory not valid (constructed successfully))
*/
unsigned int GblTrajectory::getLabels(std::vector<unsigned int> &aLabelList) {
if (not constructOK)
return 1;
unsigned int aLabel = 0;
unsigned int nPoint = thePoints[0].size();
aLabelList.resize(nPoint);
for (unsigned i = 0; i < nPoint; ++i) {
aLabelList[i] = ++aLabel;
}
return 0;
}
/// Get (list of lists of) labels of points on (composed) valid trajectory
/**
* \param [out] aLabelList List of of lists of labels
* \return error code (non-zero if trajectory not valid (constructed successfully))
*/
unsigned int GblTrajectory::getLabels(
std::vector<std::vector<unsigned int> > &aLabelList) {
if (not constructOK)
return 1;
unsigned int aLabel = 0;
aLabelList.resize(numTrajectories);
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
unsigned int nPoint = thePoints[iTraj].size();
aLabelList[iTraj].resize(nPoint);
for (unsigned i = 0; i < nPoint; ++i) {
aLabelList[iTraj][i] = ++aLabel;
}
}
return 0;
}
/// Get residual and errors from data block.
/**
* Get residual, error of measurement and residual and down-weighting
* factor for (single) data block
* \param [in] aData Label of data block
* \param [out] aResidual Measurement-Prediction
* \param [out] aMeasError Error of Measurement
* \param [out] aResError Error of Residual (including correlations from track fit)
* \param [out] aDownWeight Down-Weighting factor
*/
void GblTrajectory::getResAndErr(unsigned int aData, double &aResidual,
double &aMeasError, double &aResError, double &aDownWeight) {
double aMeasVar;
std::vector<unsigned int>* indLocal;
std::vector<double>* derLocal;
theData[aData].getResidual(aResidual, aMeasVar, aDownWeight, indLocal,
derLocal);
unsigned int nParBrl = (*indLocal).size();
TVectorD aVec(nParBrl); // compressed vector of derivatives
for (unsigned int j = 0; j < nParBrl; ++j) {
aVec[j] = (*derLocal)[j];
}
TMatrixDSym aMat = theMatrix.getBlockMatrix(*indLocal); // compressed (covariance) matrix
double aFitVar = aMat.Similarity(aVec); // variance from track fit
aMeasError = sqrt(aMeasVar); // error of measurement
aResError = (aFitVar < aMeasVar ? sqrt(aMeasVar - aFitVar) : 0.); // error of residual
}
/// Build linear equation system from data (blocks).
void GblTrajectory::buildLinearEquationSystem() {
unsigned int nBorder = numCurvature + numLocals;
theVector.resize(numParameters);
theMatrix.resize(numParameters, nBorder);
double aValue, aWeight;
std::vector<unsigned int>* indLocal;
std::vector<double>* derLocal;
std::vector<GblData>::iterator itData;
for (itData = theData.begin(); itData < theData.end(); ++itData) {
itData->getLocalData(aValue, aWeight, indLocal, derLocal);
for (unsigned int j = 0; j < indLocal->size(); ++j) {
theVector((*indLocal)[j] - 1) += (*derLocal)[j] * aWeight * aValue;
}
theMatrix.addBlockMatrix(aWeight, indLocal, derLocal);
}
}
/// Prepare fit for simple or composed trajectory
/**
* Generate data (blocks) from measurements, kinks, external seed and measurements.
*/
void GblTrajectory::prepare() {
unsigned int nDim = theDimension.size();
// upper limit
unsigned int maxData = numMeasurements + nDim * (numOffsets - 2)
+ externalSeed.GetNrows();
theData.reserve(maxData);
measDataIndex.resize(numAllPoints + 3); // include external seed and measurements
scatDataIndex.resize(numAllPoints + 1);
unsigned int nData = 0;
std::vector<TMatrixD> innerTransDer;
std::vector<std::vector<unsigned int> > innerTransLab;
// composed trajectory ?
if (numInnerTrans > 0) {
//std::cout << "composed trajectory" << std::endl;
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
// innermost point
GblPoint* innerPoint = &thePoints[iTraj].front();
// transformation fit to local track parameters
std::vector<unsigned int> firstLabels(5);
SMatrix55 matFitToLocal, matLocalToFit;
getFitToLocalJacobian(firstLabels, matFitToLocal, *innerPoint, 5);
// transformation local track to fit parameters
int ierr;
matLocalToFit = matFitToLocal.Inverse(ierr);
TMatrixD localToFit(5, 5);
for (unsigned int i = 0; i < 5; ++i) {
for (unsigned int j = 0; j < 5; ++j) {
localToFit(i, j) = matLocalToFit(i, j);
}
}
// transformation external to fit parameters at inner (first) point
innerTransDer.push_back(localToFit * innerTransformations[iTraj]);
innerTransLab.push_back(firstLabels);
}
}
// measurements
SMatrix55 matP;
// loop over trajectories
std::vector<GblPoint>::iterator itPoint;
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
for (itPoint = thePoints[iTraj].begin();
itPoint < thePoints[iTraj].end(); ++itPoint) {
SVector5 aMeas, aPrec;
unsigned int nLabel = itPoint->getLabel();
unsigned int measDim = itPoint->hasMeasurement();
if (measDim) {
const TMatrixD localDer = itPoint->getLocalDerivatives();
const std::vector<int> globalLab = itPoint->getGlobalLabels();
const TMatrixD globalDer = itPoint->getGlobalDerivatives();
TMatrixD transDer;
itPoint->getMeasurement(matP, aMeas, aPrec);
unsigned int iOff = 5 - measDim; // first active component
std::vector<unsigned int> labDer(5);
SMatrix55 matDer, matPDer;
unsigned int nJacobian =
(itPoint < thePoints[iTraj].end() - 1) ? 1 : 0; // last point needs backward propagation
getFitToLocalJacobian(labDer, matDer, *itPoint, measDim,
nJacobian);
if (measDim > 2) {
matPDer = matP * matDer;
} else { // 'shortcut' for position measurements
matPDer.Place_at(
matP.Sub<SMatrix22>(3, 3)
* matDer.Sub<SMatrix25>(3, 0), 3, 0);
}
if (numInnerTrans > 0) {
// transform for external parameters
TMatrixD proDer(measDim, 5);
// match parameters
unsigned int ifirst = 0;
unsigned int ilabel = 0;
while (ilabel < 5) {
if (labDer[ilabel] > 0) {
while (innerTransLab[iTraj][ifirst]
!= labDer[ilabel] and ifirst < 5) {
++ifirst;
}
if (ifirst >= 5) {
labDer[ilabel] -= 2 * nDim * (iTraj + 1); // adjust label
} else {
// match
labDer[ilabel] = 0; // mark as related to external parameters
for (unsigned int k = iOff; k < 5; ++k) {
proDer(k - iOff, ifirst) = matPDer(k,
ilabel);
}
}
}
++ilabel;
}
transDer.ResizeTo(measDim, numCurvature);
transDer = proDer * innerTransDer[iTraj];
}
for (unsigned int i = iOff; i < 5; ++i) {
if (aPrec(i) > 0.) {
GblData aData(nLabel, aMeas(i), aPrec(i));
aData.addDerivatives(i, labDer, matPDer, iOff, localDer,
globalLab, globalDer, numLocals, transDer);
theData.push_back(aData);
nData++;
}
}
}
measDataIndex[nLabel] = nData;
}
}
// pseudo measurements from kinks
SMatrix22 matT;
scatDataIndex[0] = nData;
scatDataIndex[1] = nData;
// loop over trajectories
for (unsigned int iTraj = 0; iTraj < numTrajectories; ++iTraj) {
for (itPoint = thePoints[iTraj].begin() + 1;
itPoint < thePoints[iTraj].end() - 1; ++itPoint) {
SVector2 aMeas, aPrec;
unsigned int nLabel = itPoint->getLabel();
if (itPoint->hasScatterer()) {
itPoint->getScatterer(matT, aMeas, aPrec);
TMatrixD transDer;
std::vector<unsigned int> labDer(7);
SMatrix27 matDer, matTDer;
getFitToKinkJacobian(labDer, matDer, *itPoint);
matTDer = matT * matDer;
if (numInnerTrans > 0) {
// transform for external parameters
TMatrixD proDer(nDim, 5);
// match parameters
unsigned int ifirst = 0;
unsigned int ilabel = 0;
while (ilabel < 7) {
if (labDer[ilabel] > 0) {
while (innerTransLab[iTraj][ifirst]
!= labDer[ilabel] and ifirst < 5) {
++ifirst;
}
if (ifirst >= 5) {
labDer[ilabel] -= 2 * nDim * (iTraj + 1); // adjust label
} else {
// match
labDer[ilabel] = 0; // mark as related to external parameters
for (unsigned int k = 0; k < nDim; ++k) {
proDer(k, ifirst) = matTDer(k, ilabel);
}
}
}
++ilabel;
}
transDer.ResizeTo(nDim, numCurvature);
transDer = proDer * innerTransDer[iTraj];
}
for (unsigned int i = 0; i < nDim; ++i) {
unsigned int iDim = theDimension[i];
if (aPrec(iDim) > 0.) {
GblData aData(nLabel, aMeas(iDim), aPrec(iDim));
aData.addDerivatives(iDim, labDer, matTDer, numLocals,
transDer);
theData.push_back(aData);
nData++;
}
}
}
scatDataIndex[nLabel] = nData;
}
scatDataIndex[thePoints[iTraj].back().getLabel()] = nData;
}
// external seed
if (externalPoint > 0) {
std::pair<std::vector<unsigned int>, TMatrixD> indexAndJacobian =
getJacobian(externalPoint);
std::vector<unsigned int> externalSeedIndex = indexAndJacobian.first;
std::vector<double> externalSeedDerivatives(externalSeedIndex.size());
const TMatrixDSymEigen externalSeedEigen(externalSeed);
const TVectorD valEigen = externalSeedEigen.GetEigenValues();
TMatrixD vecEigen = externalSeedEigen.GetEigenVectors();
vecEigen = vecEigen.T() * indexAndJacobian.second;
for (int i = 0; i < externalSeed.GetNrows(); ++i) {
if (valEigen(i) > 0.) {
for (int j = 0; j < externalSeed.GetNcols(); ++j) {
externalSeedDerivatives[j] = vecEigen(i, j);
}
GblData aData(externalPoint, 0., valEigen(i));
aData.addDerivatives(externalSeedIndex,
externalSeedDerivatives);
theData.push_back(aData);
nData++;
}
}
}
measDataIndex[numAllPoints + 1] = nData;
// external measurements
unsigned int nExt = externalMeasurements.GetNrows();
if (nExt > 0) {
std::vector<unsigned int> index(numCurvature);
std::vector<double> derivatives(numCurvature);
for (unsigned int iExt = 0; iExt < nExt; ++iExt) {
for (unsigned int iCol = 0; iCol < numCurvature; ++iCol) {
index[iCol] = numLocals + iCol + 1;
derivatives[iCol] = externalDerivatives(iExt, iCol);
}
GblData aData(1U, externalMeasurements(iExt),