-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAliAlgTrack.cxx
1513 lines (1470 loc) · 55.6 KB
/
AliAlgTrack.cxx
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
/**************************************************************************
* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
* *
* Author: The ALICE Off-line Project. *
* Contributors are mentioned in the code where appropriate. *
* *
* Permission to use, copy, modify and distribute this software and its *
* documentation strictly for non-commercial purposes is hereby granted *
* without fee, provided that the above copyright notice appears in all *
* copies and that both the copyright notice and this permission notice *
* appear in the supporting documentation. The authors make no claims *
* about the suitability of this software for any purpose. It is *
* provided "as is" without express or implied warranty. *
**************************************************************************/
#include <stdio.h>
#include "AliAlgTrack.h"
#include "AliTrackerBase.h"
#include "AliLog.h"
#include "AliAlgSens.h"
#include "AliAlgVol.h"
#include "AliAlgDet.h"
#include "AliAlgAux.h"
#include <TMatrixD.h>
#include <TVectorD.h>
#include <TMatrixDSymEigen.h>
using namespace AliAlgAux;
using namespace TMath;
// RS: this is not good: we define constants outside the class, but it is to
// bypass the CINT limitations on static arrays initializations
const Int_t kRichardsonOrd = 1; // Order of Richardson extrapolation for derivative (min=1)
const Int_t kRichardsonN = kRichardsonOrd+1; // N of 2-point symmetric derivatives needed for requested order
const Int_t kNRDClones = kRichardsonN*2 ;// number of variations for derivative of requested order
//____________________________________________________________________________
AliAlgTrack::AliAlgTrack() :
fNLocPar(0)
,fNLocExtPar(0)
,fNGloPar(0)
,fNDF(0)
,fInnerPointID(0)
// ,fMinX2X0Pt2Account(5/1.0)
,fMinX2X0Pt2Account(0.5e-3/1.0)
,fMass(0.14)
,fChi2(0)
,fChi2CosmUp(0)
,fChi2CosmDn(0)
,fChi2Ini(0)
,fPoints(0)
,fLocPar()
,fGloParID(0)
,fGloParIDA(0)
,fLocParA(0)
{
// def c-tor
for (int i=0;i<2;i++) {
// we start with 0 size buffers for derivatives, they will be expanded automatically
fResid[i].Set(0);
fDResDGlo[i].Set(0);
fDResDLoc[i].Set(0);
//
fResidA[i] = 0;
fDResDLocA[i] = 0;
fDResDGloA[i] = 0;
}
fNeedInv[0] = fNeedInv[1] = kFALSE;
//
}
//____________________________________________________________________________
AliAlgTrack::~AliAlgTrack()
{
// d-tor
}
//____________________________________________________________________________
void AliAlgTrack::Clear(Option_t *)
{
// reset the track
TObject::Clear();
ResetBit(0xffffffff);
fPoints.Clear();
fChi2 = fChi2CosmUp = fChi2CosmDn = fChi2Ini = 0;
fNDF = 0;
fInnerPointID = -1;
fNeedInv[0] = fNeedInv[1] = kFALSE;
fNLocPar = fNLocExtPar = fNGloPar = 0;
//
}
//____________________________________________________________________________
void AliAlgTrack::DefineDOFs()
{
// define varied DOF's (local parameters) for the track:
// 1) kinematic params (5 or 4 depending on Bfield)
// 2) mult. scattering angles (2)
// 3) if requested by point: energy loss
//
fNLocPar = fNLocExtPar = GetFieldON() ? kNKinParBON : kNKinParBOFF;
int np = GetNPoints();
//
// the points are sorted in order opposite to track direction -> outer points come 1st,
// but for the 2-leg cosmic track the innermost points are in the middle (1st lower leg, then upper one)
//
// start along track direction, i.e. last point in the ordered array
int minPar = fNLocPar;
for (int ip=GetInnerPointID()+1;ip--;) { // collision track or cosmic lower leg
AliAlgPoint* pnt = GetPoint(ip);
pnt->SetMinLocVarID(minPar);
if (pnt->ContainsMaterial()) fNLocPar += pnt->GetNMatPar();
pnt->SetMaxLocVarID(fNLocPar); // flag up to which parameted ID this points depends on
}
//
if (IsCosmic()) {
minPar = fNLocPar;
for (int ip=GetInnerPointID()+1;ip<np;ip++) { // collision track or cosmic lower leg
AliAlgPoint* pnt = GetPoint(ip);
pnt->SetMinLocVarID(minPar);
if (pnt->ContainsMaterial()) fNLocPar += pnt->GetNMatPar();
pnt->SetMaxLocVarID(fNLocPar); // flag up to which parameted ID this points depends on
}
}
//
if (fLocPar.GetSize()<fNLocPar) fLocPar.Set(fNLocPar);
fLocPar.Reset();
fLocParA = fLocPar.GetArray();
//
if (fResid[0].GetSize()<np) {
fResid[0].Set(np);
fResid[1].Set(np);
}
if (fDResDLoc[0].GetSize()<fNLocPar*np) {
fDResDLoc[0].Set(fNLocPar*np);
fDResDLoc[1].Set(fNLocPar*np);
}
for (int i=2;i--;) {
fResid[i].Reset();
fDResDLoc[i].Reset();
fResidA[i] = fResid[i].GetArray();
fDResDLocA[i] = fDResDLoc[i].GetArray();
}
//
// memcpy(fLocParA,GetParameter(),fNLocExtPar*sizeof(Double_t));
memset(fLocParA,0,fNLocExtPar*sizeof(Double_t));
}
//______________________________________________________
Bool_t AliAlgTrack::CalcResidDeriv(double *params)
{
// Propagate for given local params and calculate residuals and their derivatives.
// The 1st 4 or 5 elements of params vector should be the reference AliExternalTrackParam
// Then parameters of material corrections for each point
// marked as having materials should come (4 or 5 dependending if ELoss is varied or fixed).
// They correspond to kink parameters
// (AliExternalTrackParam_after_material - AliExternalTrackParam_before_material)
// rotated to frame where they error matrix is diagonal. Their conversion to AliExternalTrackParam
// increment will be done locally in the ApplyMatCorr routine.
//
// If params are not provided, use internal params array
//
if (!params) params = fLocParA;
//
if (!GetResidDone()) CalcResiduals(params);
//
int np = GetNPoints();
//
// collision track or cosmic lower leg
if (!CalcResidDeriv(params,fNeedInv[0],GetInnerPointID(),0)) {
#if DEBUG>3
AliWarning("Failed on derivatives calculation 0");
#endif
return kFALSE;
}
//
if (IsCosmic()) { // cosmic upper leg
if (!CalcResidDeriv(params,fNeedInv[1],GetInnerPointID()+1,np-1)) {
#if DEBUG>3
AliWarning("Failed on derivatives calculation 0");
#endif
}
}
//
SetDerivDone();
return kTRUE;
}
//______________________________________________________
Bool_t AliAlgTrack::CalcResidDeriv(double *params,Bool_t invert,int pFrom,int pTo)
{
// Calculate derivatives of residuals vs params for points pFrom to pT. For cosmic upper leg
// track parameter may require inversion.
// The 1st 4 or 5 elements of params vector should be the reference AliExternalTrackParam
// Then parameters of material corrections for each point
// marked as having materials should come (4 or 5 dependending if ELoss is varied or fixed).
// They correspond to kink parameters
// (AliExternalTrackParam_after_material - AliExternalTrackParam_before_material)
// rotated to frame where they error matrix is diagonal. Their conversion to AliExternalTrackParam
// increment will be done locally in the ApplyMatCorr routine.
//
// The derivatives are calculated using Richardson extrapolation
// (like http://root.cern.ch/root/html/ROOT__Math__RichardsonDerivator.html)
//
AliExternalTrackParam probD[kNRDClones]; // use this to vary supplied param for derivative calculation
double varDelta[kRichardsonN];
const int kInvElem[kNKinParBON] = {-1,1,1,-1,-1};
//
const double kDelta[kNKinParBON] = {0.02,0.02, 0.001,0.001, 0.01}; // variations for ExtTrackParam and material effects
//
double delta[kNKinParBON]; // variations of curvature term are relative
for (int i=kNKinParBOFF;i--;) delta[i] = kDelta[i];
if (GetFieldON()) delta[kParQ2Pt] = kDelta[kParQ2Pt]*Abs(GetParameter()[kParQ2Pt]);
//
int pinc;
if (pTo>pFrom) { // fit in points decreasing order: cosmics upper leg
pTo++;
pinc = 1;
}
else { // fit in points increasing order: collision track or cosmics lower leg
pTo--;
pinc = -1;
}
// 1) derivative wrt AliExternalTrackParam parameters
for (int ipar=fNLocExtPar;ipar--;) {
SetParams(probD,kNRDClones, GetX(),GetAlpha(),params,kTRUE);
if (invert) for (int ic=kNRDClones;ic--;) probD[ic].Invert();
double del = delta[ipar];
//
for (int icl=0;icl<kRichardsonN;icl++) { // calculate kRichardsonN variations with del, del/2, del/4...
varDelta[icl] = del;
ModParam(probD[(icl<<1)+0], ipar, del);
ModParam(probD[(icl<<1)+1], ipar,-del);
del *= 0.5;
}
// propagate varied tracks to each point
for (int ip=pFrom;ip!=pTo;ip+=pinc) { // points are ordered against track direction
AliAlgPoint* pnt = GetPoint(ip);
if (!PropagateParamToPoint(probD, kNRDClones, pnt)) return kFALSE;
// if (pnt->ContainsMaterial()) { // apply material corrections
if (!ApplyMatCorr(probD, kNRDClones, params, pnt)) return kFALSE;
// }
//
if (pnt->ContainsMeasurement()) {
int offsDer = ip*fNLocPar + ipar;
RichardsonDeriv(probD, varDelta, pnt, fDResDLocA[0][offsDer], fDResDLocA[1][offsDer]); // calculate derivatives
if (invert&&kInvElem[ipar]<0) {
fDResDLocA[0][offsDer] = -fDResDLocA[0][offsDer];
fDResDLocA[1][offsDer] = -fDResDLocA[1][offsDer];
}
}
} // loop over points
} // loop over ExtTrackParam parameters
//
// 2) now vary material effect related parameters: MS and eventually ELoss
//
for (int ip=pFrom;ip!=pTo;ip+=pinc) { // points are ordered against track direction
AliAlgPoint* pnt = GetPoint(ip);
//
// global derivatives at this point
if (pnt->ContainsMeasurement() && !CalcResidDerivGlo(pnt)) {
#if DEBUG>3
AliWarningF("Failed on global derivatives calculation at point %d",ip);
pnt->Print("meas");
#endif
return kFALSE;
}
//
if (!pnt->ContainsMaterial()) continue;
//
int nParFreeI = pnt->GetNMatPar();
//
// array delta gives desired variation of parameters in AliExternalTrackParam definition,
// while the variation should be done for parameters in the frame where the vector
// of material corrections has diagonal cov. matrix -> rotate the delta to this frame
double deltaMatD[kNKinParBON];
pnt->DiagMatCorr(delta,deltaMatD);
//
// printf("Vary %d [%+.3e %+.3e %+.3e %+.3e] ",ip,deltaMatD[0],deltaMatD[1],deltaMatD[2],deltaMatD[3]); pnt->Print();
int offsI = pnt->GetMaxLocVarID() - nParFreeI; // the parameters for this point start with this offset
// they are irrelevant for the points upstream
for (int ipar=0;ipar<nParFreeI;ipar++) { // loop over DOFs related to MS and ELoss are point ip
double del = deltaMatD[ipar];
//
// We will vary the tracks starting from the original parameters propagated to given point
// and stored there (before applying material corrections for this point)
//
SetParams(probD,kNRDClones, pnt->GetXPoint(),pnt->GetAlphaSens(),pnt->GetTrParamWSB(),kFALSE);
// no need for eventual track inversion here: if needed, this is already done in ParamWSB
//
int offsIP = offsI+ipar; // parameter entry in the params array
// printf(" Var:%d (%d) %e\n",ipar,offsIP, del);
for (int icl=0;icl<kRichardsonN;icl++) { // calculate kRichardsonN variations with del, del/2, del/4...
varDelta[icl] = del;
double parOrig = params[offsIP];
params[offsIP] += del;
//
// apply varied material effects : incremented by delta
if (!ApplyMatCorr(probD[(icl<<1)+0], params, pnt)) return kFALSE;
//
// apply varied material effects : decremented by delta
params[offsIP] = parOrig - del;
if (!ApplyMatCorr(probD[(icl<<1)+1], params, pnt)) return kFALSE;
//
params[offsIP] = parOrig;
del *= 0.5;
}
if (pnt->ContainsMeasurement()) { // calculate derivatives at the scattering point itself
int offsDerIP = ip*fNLocPar + offsIP;
RichardsonDeriv(probD, varDelta, pnt, fDResDLocA[0][offsDerIP], fDResDLocA[1][offsDerIP]); // calculate derivatives for ip
// printf("DR SELF: %e %e at %d (%d)\n",fDResDLocA[0][offsDerIP], fDResDLocA[1][offsDerIP],offsI, offsDerIP);
}
//
// loop over points whose residuals can be affected by the material effects on point ip
for (int jp=ip+pinc;jp!=pTo;jp+=pinc) {
AliAlgPoint* pntJ = GetPoint(jp);
// printf(" DerFor:%d ",jp); pntJ->Print();
if ( !PropagateParamToPoint(probD, kNRDClones, pntJ) ) return kFALSE;
//
if (pntJ->ContainsMaterial()) { // apply material corrections
if (!ApplyMatCorr(probD,kNRDClones,params,pntJ)) return kFALSE;
}
//
if (pntJ->ContainsMeasurement()) {
int offsDerJ = jp*fNLocPar + offsIP;
// calculate derivatives
RichardsonDeriv(probD, varDelta, pntJ, fDResDLocA[0][offsDerJ], fDResDLocA[1][offsDerJ]);
}
//
} // << loop over points whose residuals can be affected by the material effects on point ip
} // << loop over DOFs related to MS and ELoss are point ip
} // << loop over all points of the track
//
return kTRUE;
}
//______________________________________________________
Bool_t AliAlgTrack::CalcResidDerivGlo(AliAlgPoint* pnt)
{
// calculate residuals derivatives over point's sensor and its parents global params
double deriv[AliAlgVol::kNDOFGeom*3];
//
const AliAlgSens* sens = pnt->GetSensor();
const AliAlgVol* vol = sens;
// precalculated track parameters
double snp=pnt->GetTrParamWSA(kParSnp),tgl=pnt->GetTrParamWSA(kParTgl);
// precalculate track slopes to account tracking X veriation
// these are coeffs to translate deltaX of the point to deltaY and deltaZ of track
double cspi = 1./Sqrt((1-snp)*(1+snp)), slpY = snp*cspi, slpZ = tgl*cspi;
//
pnt->SetDGloOffs(fNGloPar); // mark 1st entry of derivatives
do {
// measurement residuals
int nfree = vol->GetNDOFFree();
if (!nfree) continue; // no free parameters?
sens->DPosTraDParGeom(pnt,deriv,vol==sens ? 0:vol);
//
CheckExpandDerGloBuffer(fNGloPar+nfree); // if needed, expand derivatives buffer
//
for (int ip=0;ip<AliAlgVol::kNDOFGeom;ip++) { // we need only free parameters
if (!vol->IsFreeDOF(ip)) continue;
double* dXYZ = &deriv[ip*3]; // tracking XYZ derivatives over this parameter
// residual is defined as diagonalized track_estimate - measured Y,Z in tracking frame
// where the track is evaluated at measured X!
// -> take into account modified X using track parameterization at the point (paramWSA)
// Attention: small simplifications(to be checked if it is ok!!!):
// effect of changing X is accounted neglecting track curvature to preserve linearity
//
// store diagonalized residuals in track buffer
pnt->DiagonalizeResiduals((dXYZ[AliAlgPoint::kX]*slpY - dXYZ[AliAlgPoint::kY]),
(dXYZ[AliAlgPoint::kX]*slpZ - dXYZ[AliAlgPoint::kZ]),
fDResDGloA[0][fNGloPar],fDResDGloA[1][fNGloPar]);
// and register global ID of varied parameter
fGloParIDA[fNGloPar] = vol->GetParGloID(ip);
fNGloPar++;
}
//
} while( (vol=vol->GetParent()) );
//
// eventual detector calibration parameters
const AliAlgDet* det = sens->GetDetector();
int ndof=0;
if (det && (ndof=det->GetNCalibDOFs())) {
// if needed, expand derivatives buffer
CheckExpandDerGloBuffer(fNGloPar+det->GetNCalibDOFsFree());
for (int idf=0;idf<ndof;idf++) {
if (!det->IsFreeDOF(idf)) continue;
sens->DPosTraDParCalib(pnt,deriv,idf,0);
pnt->DiagonalizeResiduals((deriv[AliAlgPoint::kX]*slpY - deriv[AliAlgPoint::kY]),
(deriv[AliAlgPoint::kX]*slpZ - deriv[AliAlgPoint::kZ]),
fDResDGloA[0][fNGloPar],fDResDGloA[1][fNGloPar]);
// and register global ID of varied parameter
fGloParIDA[fNGloPar] = det->GetParGloID(idf);
fNGloPar++;
}
}
//
pnt->SetNGloDOFs(fNGloPar-pnt->GetDGloOffs()); // mark number of global derivatives filled
//
return kTRUE;
}
//______________________________________________________
Bool_t AliAlgTrack::CalcResiduals(const double *params)
{
// Propagate for given local params and calculate residuals
// The 1st 4 or 5 elements of params vector should be the reference AliExternalTrackParam
// Then parameters of material corrections for each point
// marked as having materials should come (4 or 5 dependending if ELoss is varied or fixed).
// They correspond to kink parameters
// (AliExternalTrackParam_after_material - AliExternalTrackParam_before_material)
// rotated to frame where they error matrix is diagonal. Their conversion to AliExternalTrackParam
// increment will be done locally in the ApplyMatCorr routine.
//
// If params are not provided, use internal params array
//
if (!params) params = fLocParA;
int np = GetNPoints();
fChi2 = 0;
fNDF = 0;
//
// collision track or cosmic lower leg
if (!CalcResiduals(params,fNeedInv[0],GetInnerPointID(),0)) {
#if DEBUG>3
AliWarning("Failed on residuals calculation 0");
#endif
return kFALSE;
}
//
if (IsCosmic()) { // cosmic upper leg
if (!CalcResiduals(params,fNeedInv[1],GetInnerPointID()+1,np-1)) {
#if DEBUG>3
AliWarning("Failed on residuals calculation 1");
#endif
return kFALSE;
}
}
//
fNDF -= fNLocExtPar;
SetResidDone();
return kTRUE;
}
//______________________________________________________
Bool_t AliAlgTrack::CalcResiduals(const double *params,Bool_t invert,int pFrom,int pTo)
{
// Calculate residuals for the single leg from points pFrom to pT
// The 1st 4 or 5 elements of params vector should be corrections to
// the reference AliExternalTrackParam
// Then parameters of material corrections for each point
// marked as having materials should come (4 or 5 dependending if ELoss is varied or fixed).
// They correspond to kink parameters
// (AliExternalTrackParam_after_material - AliExternalTrackParam_before_material)
// rotated to frame where they error matrix is diagonal. Their conversion to AliExternalTrackParam
// increment will be done locally in the ApplyMatCorr routine.
//
AliExternalTrackParam probe;
SetParams(probe,GetX(),GetAlpha(),params,kTRUE);
if (invert) probe.Invert();
int pinc;
if (pTo>pFrom) { // fit in points decreasing order: cosmics upper leg
pTo++;
pinc = 1;
}
else { // fit in points increasing order: collision track or cosmics lower leg
pTo--;
pinc = -1;
}
//
for (int ip=pFrom;ip!=pTo;ip+=pinc) { // points are ordered against track direction
AliAlgPoint* pnt = GetPoint(ip);
if (!PropagateParamToPoint(probe, pnt)) return kFALSE;
//
// store the current track kinematics at the point BEFORE applying eventual material
// corrections. This kinematics will be later varied around supplied parameters (in the CalcResidDeriv)
pnt->SetTrParamWSB(probe.GetParameter());
//
// account for materials
// if (pnt->ContainsMaterial()) { // apply material corrections
if (!ApplyMatCorr(probe, params, pnt)) return kFALSE;
// }
pnt->SetTrParamWSA(probe.GetParameter());
//
if (pnt->ContainsMeasurement()) { // need to calculate residuals in the frame where errors are orthogonal
pnt->GetResidualsDiag(probe.GetParameter(),fResidA[0][ip],fResidA[1][ip]);
fChi2 += fResidA[0][ip]*fResidA[0][ip]/pnt->GetErrDiag(0);
fChi2 += fResidA[1][ip]*fResidA[1][ip]/pnt->GetErrDiag(1);
fNDF += 2;
}
//
if (pnt->ContainsMaterial()) {
// material degrees of freedom do not contribute to NDF since they are constrained by 0 expectation
int nCorrPar = pnt->GetNMatPar();
const double *corrDiag = &fLocParA[pnt->GetMaxLocVarID()-nCorrPar]; // corrections in diagonalized frame
float *corCov = pnt->GetMatCorrCov(); // correction diagonalized covariance
for (int i=0;i<nCorrPar;i++) fChi2 += corrDiag[i]*corrDiag[i]/corCov[i];
}
}
return kTRUE;
}
//______________________________________________________
Bool_t AliAlgTrack::PropagateParamToPoint(AliExternalTrackParam* tr, int nTr, const AliAlgPoint* pnt, double maxStep)
{
// Propagate set of tracks to the point (only parameters, no error matrix)
// VECTORIZE this
//
for (int itr=nTr;itr--;) {
if (!PropagateParamToPoint(tr[itr],pnt,maxStep)) {
#if DEBUG>3
AliErrorF("Failed on clone %d propagation",itr);
tr[itr].Print();
pnt->Print("meas mat");
#endif
return kFALSE;
}
}
return kTRUE;
}
//______________________________________________________
Bool_t AliAlgTrack::PropagateParamToPoint(AliExternalTrackParam &tr, const AliAlgPoint* pnt, double maxStep)
{
// propagate tracks to the point (only parameters, no error matrix)
double xyz[3],bxyz[3];
//
if (!tr.RotateParamOnly(pnt->GetAlphaSens())) {
#if DEBUG>3
AliErrorF("Failed to rotate to alpha=%f",pnt->GetAlphaSens());
tr.Print();
pnt->Print();
#endif
return kFALSE;
}
//
double xTgt = pnt->GetXPoint();
double xBeg = tr.GetX();
double dx = xTgt - xBeg;
int nstep = int(Abs(dx)/maxStep)+1;
dx/=nstep;
//
for (int ist=nstep;ist--;) {
//
double xToGo = xTgt - dx*ist;
tr.GetXYZ(xyz);
//
if (GetFieldON()) {
if (pnt->GetUseBzOnly()) {
if (!tr.PropagateParamOnlyTo(xToGo,AliTrackerBase::GetBz(xyz))) {
#if DEBUG>3
AliErrorF("Failed to propagate(BZ) to X=%f",pnt->GetXPoint());
tr.Print();
pnt->Print();
#endif
return kFALSE;
}
}
else {
AliTrackerBase::GetBxByBz(xyz,bxyz);
if (!tr.PropagateParamOnlyBxByBzTo(xToGo,bxyz)) {
#if DEBUG>3
AliErrorF("Failed to propagate(BXYZ) to X=%f",pnt->GetXPoint());
tr.Print();
pnt->Print();
#endif
return kFALSE;
}
}
}
else { // straigth line propagation
if ( !tr.PropagateParamOnlyTo(xToGo,0) ) {
#if DEBUG>3
AliErrorF("Failed to propagate(B=0) to X=%f",pnt->GetXPoint());
tr.Print();
pnt->Print();
#endif
return kFALSE;
}
}
} // steps
//
return kTRUE;
}
//______________________________________________________
Bool_t AliAlgTrack::PropagateToPoint(AliExternalTrackParam &tr, const AliAlgPoint* pnt,
int minNSteps, double maxStep, Bool_t matCor, double *matPar)
{
// propagate tracks to the point. If matCor is true, then material corrections will be applied.
// if matPar pointer is provided, it will be filled by total x2x0 and signed xrho
if (!tr.Rotate(pnt->GetAlphaSens())) {
#if DEBUG>3
AliWarning(Form("Failed to rotate to alpha=%f",pnt->GetAlphaSens()));
tr.Print();
#endif
return kFALSE;
}
//
double xyz0[3],xyz1[3],bxyz[3],matarr[7];
double xPoint=pnt->GetXPoint(),dx=xPoint-tr.GetX(),dxa=Abs(dx),step=dxa/minNSteps;
if (matPar) matPar[0]=matPar[1]=0;
if (dxa<kTinyDist) return kTRUE;
if (step>maxStep) step = maxStep;
int nstep = int(dxa/step);
step = dxa/nstep;
if (dx<0) step = -step;
//
// printf("-->will go from X:%e to X:%e in %d steps of %f\n",tr.GetX(),xPoint,nstep,step);
// do we go along or against track direction
Bool_t alongTrackDir = (dx>0&&!pnt->IsInvDir()) || (dx<0&&pnt->IsInvDir());
Bool_t queryXYZ = matCor||GetFieldON();
if (queryXYZ) tr.GetXYZ(xyz0);
//
double x2X0Tot=0,xrhoTot=0;
for (int ist=nstep;ist--;) { // single propagation step >>
double xToGo = xPoint - step*ist;
//
if (GetFieldON()) {
if (pnt->GetUseBzOnly()) {
if (!tr.PropagateTo(xToGo,AliTrackerBase::GetBz(xyz0))) {
#if DEBUG>3
AliWarningF("Failed to propagate(BZ) to X=%f",xToGo);
tr.Print();
#endif
return kFALSE;
}
}
else {
AliTrackerBase::GetBxByBz(xyz0,bxyz);
if (!tr.PropagateToBxByBz(xToGo,bxyz)) {
#if DEBUG>3
AliWarningF("Failed to propagate(BXYZ) to X=%f",xToGo);
#endif
return kFALSE;
}
}
}
else { // straigth line propagation
if ( !tr.PropagateTo(xToGo,0) ) {
#if DEBUG>3
AliWarningF("Failed to propagate(B=0) to X=%f",xToGo);
#endif
return kFALSE;
}
}
//
if (queryXYZ) {
tr.GetXYZ(xyz1);
if (matCor) {
AliTrackerBase::MeanMaterialBudget(xyz0,xyz1,matarr);
Double_t xrho=matarr[0]*matarr[4], xx0=matarr[1];
if (alongTrackDir) xrho = -xrho; // if we go along track direction, energy correction is negative
x2X0Tot += xx0;
xrhoTot += xrho;
// printf("MAT %+7.2f %+7.2f %+7.2f -> %+7.2f %+7.2f %+7.2f | %+e %+e | -> %+e %+e | %+e %+e %+e %+e %+e\n",
// xyz0[0],xyz0[1],xyz0[2], xyz1[0],xyz1[1],xyz1[2], tr.Phi(), tr.GetAlpha(),
// x2X0Tot,xrhoTot, matarr[0],matarr[1],matarr[2],matarr[3],matarr[4]);
if (!tr.CorrectForMeanMaterial(xx0,xrho,fMass)) {
#if DEBUG>3
AliWarningF("Failed on CorrectForMeanMaterial(%f,%f,%f)",xx0,xrho,fMass);
tr.Print();
#endif
return kFALSE;
}
}
for (int l=3;l--;) xyz0[l] = xyz1[l];
}
} // single propagation step <<
//
if (matPar) {
matPar[0] = x2X0Tot;
matPar[1] = xrhoTot;
}
return kTRUE;
}
/*
//______________________________________________________
Bool_t AliAlgTrack::ApplyMS(AliExternalTrackParam& trPar, double tms,double pms)
{
//------------------------------------------------------------------------------
// Modify track par (e.g. AliExternalTrackParam) in the tracking frame
// (dip angle lam, az. angle phi)
// by multiple scattering defined by polar and azumuthal scattering angles in
// the track collinear frame (tms and pms resp).
// The updated direction vector in the tracking frame becomes
//
// | Cos[lam]*Cos[phi] Cos[phi]*Sin[lam] -Sin[phi] | | Cos[tms] |
// | Cos[lam]*Sin[phi] Sin[lam]*Sin[phi] Cos[phi] | x | Cos[pms]*Sin[tms]|
// | Sin[lam] -Cos[lam] 0 | | Sin[pms]*Sin[tms]|
//
//------------------------------------------------------------------------------
//
double *par = (double*) trPar.GetParameter();
//
if (Abs(tms)<1e-7) return kTRUE;
//
double snTms = Sin(tms), csTms = Cos(tms);
double snPms = Sin(pms), csPms = Cos(pms);
double snPhi = par[2], csPhi = Sqrt((1.-snPhi)*(1.+snPhi));
double csLam = 1./Sqrt(1.+par[3]*par[3]), snLam = csLam*par[3];
//
double r00 = csLam*csPhi, r01 = snLam*csPhi, &r02 = snPhi;
double r10 = csLam*snPhi, r11 = snLam*snPhi, &r12 = csPhi;
double &r20 = snLam ,&r21 = csLam;
//
double &v0 = csTms, v1 = snTms*csPms, v2 = snTms*snPms;
//
double px = r00*v0 + r01*v1 - r02*v2;
double py = r10*v0 + r11*v1 + r12*v2;
double pz = r20*v0 - r21*v1;
//
double pt = Sqrt(px*px + py*py);
par[2] = py/pt;
par[3] = pz/pt;
par[4]*= csLam/pt;
//
return kTRUE;
}
*/
//______________________________________________________
Bool_t AliAlgTrack::ApplyMatCorr(AliExternalTrackParam& trPar, const Double_t *corrPar, const AliAlgPoint* pnt)
{
// Modify track param (e.g. AliExternalTrackParam) in the tracking frame
// by delta accounting for material effects
// Note: corrPar contains delta to track parameters rotated by the matrix
// DIAGONALIZING ITS COVARIANCE MATRIX!
// transform parameters from the frame diagonalizing the errors to track frame
double corr[kNKinParBON] = {0};
if (pnt->ContainsMaterial()) { // are there free params from meterials?
int nCorrPar = pnt->GetNMatPar();
const double *corrDiag = &corrPar[pnt->GetMaxLocVarID()-nCorrPar]; // material corrections for this point start here
pnt->UnDiagMatCorr(corrDiag, corr); // this is to account for MS and RANDOM Eloss (if varied)
}
// to this we should add expected parameters modification due to the deterministic eloss
float *detELoss = pnt->GetMatCorrExp();
for (int i=kNKinParBON;i--;) corr[i] += detELoss[i];
//corr[kParQ2Pt] += detELoss[kParQ2Pt];
// printf("apply corr UD %+.3e %+.3e %+.3e %+.3e %+.3e\n",corr[0],corr[1],corr[2],corr[3],corr[4]);
// printf(" corr D %+.3e %+.3e %+.3e %+.3e\n",corrDiag[0],corrDiag[1],corrDiag[2],corrDiag[3]);
// printf("at point :"); pnt->Print();
return ApplyMatCorr(trPar,corr);
//
}
//______________________________________________________
Bool_t AliAlgTrack::ApplyMatCorr(AliExternalTrackParam& trPar, const Double_t *corr)
{
// Modify track param (e.g. AliExternalTrackParam) in the tracking frame
// by delta accounting for material effects
// Note: corr contains delta to track frame, NOT in diagonalized one
const double kMaxSnp = 0.95;
double* par = (double*)trPar.GetParameter();
double snpNew = par[kParSnp]+corr[kParSnp];
if (Abs(snpNew)>kMaxSnp) {
#if DEBUG>3
AliErrorF("Snp is too large: %f",snpNew);
printf("DeltaPar: ");
for (int i=0;i<kNKinParBON;i++) printf("%+.3e ",corr[i]); printf("\n");
trPar.Print();
#endif
return kFALSE;
}
par[kParY] += corr[kParY];
par[kParZ] += corr[kParZ];
par[kParSnp] = snpNew;
par[kParTgl] += corr[kParTgl];
par[kParQ2Pt] += corr[kParQ2Pt];
return kTRUE;
}
//______________________________________________________
Bool_t AliAlgTrack::ApplyMatCorr(AliExternalTrackParam* trSet, int ntr, const Double_t *corrDiag, const AliAlgPoint* pnt)
{
// Modify set of track params (e.g. AliExternalTrackParam) in the tracking frame
// by delta accounting for material effects
// Note: corrDiag contain delta to track parameters rotated by the matrix DIAGONALIZING ITS
// COVARIANCE MATRIX
// transform parameters from the frame diagonalizing the errors to track frame
double corr[kNKinParBON] = {0};
if (pnt->ContainsMaterial()) { // are there free params from meterials?
int nCorrPar = pnt->GetNMatPar();
const double *corrDiagP = &corrDiag[pnt->GetMaxLocVarID()-nCorrPar]; // material corrections for this point start here
pnt->UnDiagMatCorr(corrDiagP, corr);
}
float *detELoss = pnt->GetMatCorrExp();
for (int i=kNKinParBON;i--;) corr[i] += detELoss[i];
// if (!pnt->GetELossVaried()) corr[kParQ2Pt] = pnt->GetMatCorrExp()[kParQ2Pt]; // fixed eloss expected effect
// printf("apply corr UD %+.3e %+.3e %+.3e %+.3e\n",corr[0],corr[1],corr[2],corr[3]);
// printf(" corr D %+.3e %+.3e %+.3e %+.3e\n",corrDiagP[0],corrDiagP[1],corrDiagP[2],corrDiagP[3]);
// printf("at point :"); pnt->Print();
//
for (int itr=ntr;itr--;) {
if (!ApplyMatCorr(trSet[itr],corr)) {
#if DEBUG>3
AliErrorF("Failed on clone %d materials",itr);
trSet[itr].Print();
#endif
return kFALSE;
}
}
return kTRUE;
}
//______________________________________________
Double_t AliAlgTrack::RichardsonExtrap(double *val, int ord)
{
// Calculate Richardson extrapolation of order ord (starting from 1)
// The array val should contain estimates ord+1 of derivatives with variations
// d, d/2 ... d/2^ord.
// The array val is overwritten
//
if (ord==1) return (4.*val[1] - val[0])*(1./3);
do {for (int i=0;i<ord;i++) val[i] = (4.*val[i+1] - val[i])*(1./3);} while(--ord);
return val[0];
}
//______________________________________________
Double_t AliAlgTrack::RichardsonExtrap(const double *val, int ord)
{
// Calculate Richardson extrapolation of order ord (starting from 1)
// The array val should contain estimates ord+1 of derivatives with variations
// d, d/2 ... d/2^ord.
// The array val is not overwritten
//
if (ord==1) return (4.*val[1] - val[0])*(1./3);
double* buff = new double[ord+1];
memcpy(buff,val,(ord+1)*sizeof(double));
do {for (int i=0;i<ord;i++) buff[i] = (4.*buff[i+1] - buff[i])*(1./3);} while(--ord);
return buff[0];
}
//______________________________________________
void AliAlgTrack::RichardsonDeriv(const AliExternalTrackParam* trSet, const double *delta, const AliAlgPoint* pnt, double& derY, double& derZ)
{
// Calculate Richardson derivatives for diagonalized Y and Z from a set of kRichardsonN pairs
// of tracks with same parameter of i-th pair varied by +-delta[i]
static double derRichY[kRichardsonN],derRichZ[kRichardsonN];
//
for (int icl=0;icl<kRichardsonN;icl++) { // calculate kRichardsonN variations with del, del/2, del/4...
double resYVP=0,resYVN=0,resZVP=0,resZVN=0;
pnt->GetResidualsDiag(trSet[(icl<<1)+0].GetParameter(), resYVP, resZVP); // variation with +delta
pnt->GetResidualsDiag(trSet[(icl<<1)+1].GetParameter(), resYVN, resZVN); // variation with -delta
derRichY[icl] = 0.5*(resYVP-resYVN)/delta[icl]; // 2-point symmetric derivatives
derRichZ[icl] = 0.5*(resZVP-resZVN)/delta[icl];
}
derY = RichardsonExtrap(derRichY,kRichardsonOrd); // dY/dPar
derZ = RichardsonExtrap(derRichZ,kRichardsonOrd); // dZ/dPar
//
}
//______________________________________________
void AliAlgTrack::Print(Option_t *opt) const
{
// print track data
printf("%s ",IsCosmic() ? " Cosmic ":"Collision ");
AliExternalTrackParam::Print();
printf("N Free Par: %d (Kinem: %d) | Npoints: %d (Inner:%d) | M : %.3f | Chi2Ini:%.1f Chi2: %.1f/%d",
fNLocPar,fNLocExtPar,GetNPoints(),GetInnerPointID(),fMass,fChi2Ini,fChi2,fNDF);
if (IsCosmic()) {
int npLow = GetInnerPointID();
int npUp = GetNPoints() - npLow - 1;
printf(" [Low:%.1f/%d Up:%.1f/%d]",fChi2CosmDn,npLow, fChi2CosmUp,npUp);
}
printf("\n");
//
TString optS = opt;
optS.ToLower();
Bool_t res = optS.Contains("r") && GetResidDone();
Bool_t der = optS.Contains("d") && GetDerivDone();
Bool_t par = optS.Contains("lc"); // local param corrections
Bool_t paru = optS.Contains("lcu"); // local param corrections in track param frame
//
if (par) {
printf("Ref.track corr: "); for (int i=0;i<fNLocExtPar;i++) printf("%+.3e ",fLocParA[i]); printf("\n");
}
//
if (optS.Contains("p") || res || der) {
for (int ip=0;ip<GetNPoints();ip++) {
printf("#%3d ",ip);
AliAlgPoint* pnt = GetPoint(ip);
pnt->Print(opt);
//
if (res && pnt->ContainsMeasurement()) {
printf(" Residuals : %+.3e %+.3e -> Pulls: %+7.2f %+7.2f\n",
GetResidual(0,ip),GetResidual(1,ip),
GetResidual(0,ip)/sqrt(pnt->GetErrDiag(0)),GetResidual(1,ip)/sqrt(pnt->GetErrDiag(1)));
}
if (der && pnt->ContainsMeasurement()) {
for (int ipar=0;ipar<fNLocPar;ipar++) {
printf(" Dres/dp%03d : %+.3e %+.3e\n",ipar,GetDResDLoc(0,ip)[ipar], GetDResDLoc(1,ip)[ipar]);
}
}
//
if (par && pnt->ContainsMaterial()) { // material corrections
int nCorrPar = pnt->GetNMatPar();
const double *corrDiag = &fLocParA[pnt->GetMaxLocVarID()-nCorrPar];
printf(" Corr.Diag: ");
for (int i=0;i<nCorrPar;i++) printf("%+.3e ",corrDiag[i]); printf("\n");
printf(" Corr.Pull: ");
float *corCov = pnt->GetMatCorrCov(); // correction covariance
//float *corExp = pnt->GetMatCorrExp(); // correction expectation
for (int i=0;i<nCorrPar;i++) printf("%+.3e ",(corrDiag[i]/* - corExp[i]*/)/Sqrt(corCov[i])); printf("\n");
if (paru) { // print also mat.corrections in track frame
double corr[5] = {0};
pnt->UnDiagMatCorr(corrDiag, corr);
// if (!pnt->GetELossVaried()) corr[kParQ2Pt] = pnt->GetMatCorrExp()[kParQ2Pt]; // fixed eloss expected effect
printf(" Corr.Track: ");
for (int i=0;i<kNKinParBON;i++) printf("%+.3e ",corr[i]); printf("\n");
}
}
}
} // print points
}
//______________________________________________
void AliAlgTrack::DumpCoordinates() const
{
// print various coordinates for inspection
printf("gpx/D:gpy/D:gpz/D:gtxb/D:gtyb/D:gtzb/D:gtxa/D:gtya/D:gtza/D:alp/D:px/D:py/D:pz/D:tyb/D:tzb/D:tya/D:tza/D:ey/D:ez/D\n");
for (int ip=0;ip<GetNPoints();ip++) {
AliAlgPoint* pnt = GetPoint(ip);
if (!pnt->ContainsMeasurement()) continue;
pnt->DumpCoordinates();
}
}
//______________________________________________
Bool_t AliAlgTrack::IniFit()
{
// perform initial fit of the track
//
const int kMinNStep = 3;
const double kMaxDefStep = 3.0;
//
AliExternalTrackParam trc = *this;
//
if (!GetFieldON()) { // for field-off data impose nominal momentum
}
fChi2 = fChi2CosmUp = fChi2CosmDn = 0;
//
// the points are ranged from outer to inner for collision tracks,
// and from outer point of lower leg to outer point of upper leg for the cosmic track
//
// the fit will always start from the outgoing track in inward direction
if (!FitLeg(trc,0,GetInnerPointID(),fNeedInv[0])) {
#if DEBUG>3
AliWarning("Failed FitLeg 0");
trc.Print();
#endif
return kFALSE; // collision track or cosmic lower leg
}
//
// printf("Lower leg: %d %d\n",0,GetInnerPointID()); trc.Print();
//
if (IsCosmic()) {
fChi2CosmDn = fChi2;
AliExternalTrackParam trcU = trc;
if (!FitLeg(trcU,GetNPoints()-1,GetInnerPointID()+1,fNeedInv[1])) { //fit upper leg of cosmic track
#if DEBUG>3
AliWarning("Failed FitLeg 0");
trc.Print();
#endif
return kFALSE; // collision track or cosmic lower leg
}
//
// propagate to reference point, which is the inner point of lower leg
const AliAlgPoint* refP = GetPoint(GetInnerPointID());
if (!PropagateToPoint(trcU,refP,kMinNStep,kMaxDefStep,kTRUE)) return kFALSE;
//
fChi2CosmUp = fChi2 - fChi2CosmDn;
// printf("Upper leg: %d %d\n",GetInnerPointID()+1,GetNPoints()-1); trcU.Print();
//
if (!CombineTracks(trc,trcU)) return kFALSE;
//printf("Combined\n"); trc.Print();
}
CopyFrom(&trc);
//
fChi2Ini = fChi2;
return kTRUE;
}
//______________________________________________
Bool_t AliAlgTrack::CombineTracks(AliExternalTrackParam& trcL, const AliExternalTrackParam& trcU)
{
// Assign to trcL the combined tracks (Kalman update of trcL by trcU)
// The trcL and trcU MUST be defined at same X,Alpha
//
// Update equations: tracks described by vectors vL and vU and coviriances CL and CU resp.
// then the gain matrix K = CL*(CL+CU)^-1
// Updated vector and its covariance:
// CL' = CL - K*CL