forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TEveTrackPropagator.cxx
1494 lines (1229 loc) · 41.5 KB
/
TEveTrackPropagator.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
// @(#)root/eve:$Id$
// Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
/*************************************************************************
* Copyright (C) 1995-2007, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
#include "TEveTrackPropagator.h"
#include "TEveTrack.h"
#include "TEveTrans.h"
#include "TMath.h"
#include <cassert>
/** \class TEveMagField
\ingroup TEve
Abstract base-class for interfacing to magnetic field needed by the
TEveTrackPropagator.
To implement your own version, redefine the following virtual functions:
virtual Double_t GetMaxFieldMagD() const;
virtual TEveVectorD GetFieldD(Double_t x, Double_t y, Double_t z) const;
See sub-classes TEveMagFieldConst and TEveMagFieldDuo for two simple implementations.
NOTE: For backward compatibility float versions are the primary
sources of field information in this base-class. The float versions are
not used in TEve and can be ignored in sub-classes.
NOTE: Magnetic field direction convention is inverted.
*/
ClassImp(TEveMagField);
/** \class TEveMagFieldConst
\ingroup TEve
Implements constant magnetic field, given by a vector fB.
NOTE: Magnetic field direction convention is inverted.
*/
ClassImp(TEveMagFieldConst);
/** \class TEveMagFieldDuo
\ingroup TEve
Implements constant magnetic filed that switches on given axial radius fR2
from vector fBIn to fBOut.
NOTE: Magnetic field direction convention is inverted.
*/
ClassImp(TEveMagFieldDuo);
namespace
{
//const Double_t kBMin = 1e-6;
const Double_t kPtMinSqr = 1e-20;
const Double_t kAMin = 1e-10;
const Double_t kStepEps = 1e-3;
}
////////////////////////////////////////////////////////////////////////////////
/// Default constructor.
TEveTrackPropagator::Helix_t::Helix_t() :
fCharge(0),
fMaxAng(45), fMaxStep(20.f), fDelta(0.1),
fPhi(0), fValid(kFALSE),
fLam(-1), fR(-1), fPhiStep(-1), fSin(-1), fCos(-1),
fRKStep(20.0),
fPtMag(-1), fPlMag(-1), fLStep(-1)
{
}
////////////////////////////////////////////////////////////////////////////////
/// Common update code for helix and RK propagation.
void TEveTrackPropagator::Helix_t::UpdateCommon(const TEveVectorD& p, const TEveVectorD& b)
{
fB = b;
// base vectors
fE1 = b;
fE1.Normalize();
fPlMag = p.Dot(fE1);
fPl = fE1*fPlMag;
fPt = p - fPl;
fPtMag = fPt.Mag();
fE2 = fPt;
fE2.Normalize();
}
////////////////////////////////////////////////////////////////////////////////
/// Update helix parameters.
void TEveTrackPropagator::Helix_t::UpdateHelix(const TEveVectorD& p, const TEveVectorD& b,
Bool_t full_update, Bool_t enforce_max_step)
{
UpdateCommon(p, b);
// helix parameters
TMath::Cross(fE1.Arr(), fE2.Arr(), fE3.Arr());
if (fCharge < 0) fE3.NegateXYZ();
if (full_update)
{
using namespace TMath;
Double_t a = fgkB2C * b.Mag() * Abs(fCharge);
if (a > kAMin && fPtMag*fPtMag > kPtMinSqr)
{
fValid = kTRUE;
fR = Abs(fPtMag / a);
fLam = fPlMag / fPtMag;
// get phi step, compare fMaxAng with fDelta
fPhiStep = fMaxAng * DegToRad();
if (fR > fDelta)
{
Double_t ang = 2.0 * ACos(1.0f - fDelta/fR);
if (ang < fPhiStep)
fPhiStep = ang;
}
// check max step size
Double_t curr_step = fR * fPhiStep * Sqrt(1.0f + fLam*fLam);
if (curr_step > fMaxStep || enforce_max_step)
fPhiStep *= fMaxStep / curr_step;
fLStep = fR * fPhiStep * fLam;
fSin = Sin(fPhiStep);
fCos = Cos(fPhiStep);
}
else
{
fValid = kFALSE;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Update helix for stepper RungeKutta.
void TEveTrackPropagator::Helix_t::UpdateRK(const TEveVectorD& p, const TEveVectorD& b)
{
UpdateCommon(p, b);
if (fCharge)
{
fValid = kTRUE;
// cached values for propagator
fB = b;
fPlMag = p.Dot(fB);
}
else
{
fValid = kFALSE;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Step helix for given momentum p from vertex v.
void TEveTrackPropagator::Helix_t::Step(const TEveVector4D& v, const TEveVectorD& p,
TEveVector4D& vOut, TEveVectorD& pOut)
{
vOut = v;
if (fValid)
{
TEveVectorD d = fE2*(fR*fSin) + fE3*(fR*(1-fCos)) + fE1*fLStep;
vOut += d;
vOut.fT += TMath::Abs(fLStep);
pOut = fPl + fE2*(fPtMag*fCos) + fE3*(fPtMag*fSin);
fPhi += fPhiStep;
}
else
{
// case: pT < kPtMinSqr or B < kBMin
// might happen if field directon changes pT ~ 0 or B becomes zero
vOut += p * (fMaxStep / p.Mag());
vOut.fT += fMaxStep;
pOut = p;
}
}
/** \class TEveTrackPropagator
\ingroup TEve
Holding structure for a number of track rendering parameters.
Calculates path taking into account the parameters.
NOTE: Magnetic field direction convention is inverted.
This is decoupled from TEveTrack/TEveTrackList to allow sharing of the
Propagator among several instances. Back references are kept so the tracks
can be recreated when the parameters change.
TEveTrackList has Get/Set methods for RnrStlye. TEveTrackEditor and
TEveTrackListEditor provide editor access.
Enum EProjTrackBreaking_e and member fProjTrackBreaking specify whether 2D
projected tracks get broken into several segments when the projected space
consists of separate domains (like Rho-Z). The track-breaking is enabled by
default.
*/
ClassImp(TEveTrackPropagator);
Double_t TEveTrackPropagator::fgDefMagField = 0.5;
const Double_t TEveTrackPropagator::fgkB2C = 0.299792458e-2;
TEveTrackPropagator TEveTrackPropagator::fgDefault;
Double_t TEveTrackPropagator::fgEditorMaxR = 2000;
Double_t TEveTrackPropagator::fgEditorMaxZ = 4000;
////////////////////////////////////////////////////////////////////////////////
/// Default constructor.
TEveTrackPropagator::TEveTrackPropagator(const char* n, const char* t,
TEveMagField *field, Bool_t own_field) :
TEveElementList(n, t),
TEveRefBackPtr(),
fStepper(kHelix),
fMagFieldObj(field),
fOwnMagFiledObj(own_field),
fMaxR (350), fMaxZ (450),
fNMax (4096), fMaxOrbs (0.5),
fEditPathMarks (kTRUE),
fFitDaughters (kTRUE), fFitReferences (kTRUE),
fFitDecay (kTRUE),
fFitCluster2Ds (kTRUE), fFitLineSegments (kTRUE),
fRnrDaughters (kFALSE), fRnrReferences (kFALSE),
fRnrDecay (kFALSE), fRnrCluster2Ds (kFALSE),
fRnrFV (kFALSE),
fPMAtt(), fFVAtt(),
fProjTrackBreaking(kPTB_Break), fRnrPTBMarkers(kFALSE), fPTBAtt(),
fV()
{
fPMAtt.SetMarkerColor(kYellow);
fPMAtt.SetMarkerStyle(2);
fPMAtt.SetMarkerSize(2);
fFVAtt.SetMarkerColor(kRed);
fFVAtt.SetMarkerStyle(4);
fFVAtt.SetMarkerSize(1.5);
fPTBAtt.SetMarkerColor(kBlue);
fPTBAtt.SetMarkerStyle(4);
fPTBAtt.SetMarkerSize(0.8);
if (fMagFieldObj == nullptr) {
fMagFieldObj = new TEveMagFieldConst(0., 0., fgDefMagField);
fOwnMagFiledObj = kTRUE;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor.
TEveTrackPropagator::~TEveTrackPropagator()
{
if (fOwnMagFiledObj)
{
delete fMagFieldObj;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Virtual from TEveRefBackPtr - track reference count has reached zero.
void TEveTrackPropagator::OnZeroRefCount()
{
CheckReferenceCount("TEveTrackPropagator::OnZeroRefCount ");
}
////////////////////////////////////////////////////////////////////////////////
/// Check reference count - virtual from TEveElement.
/// Must also take into account references from TEveRefBackPtr.
void TEveTrackPropagator::CheckReferenceCount(const TEveException& eh)
{
if (fRefCount <= 0)
{
TEveElementList::CheckReferenceCount(eh);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Element-change notification.
/// Stamp all tracks as requiring display-list regeneration.
/// Virtual from TEveElement.
void TEveTrackPropagator::ElementChanged(Bool_t update_scenes, Bool_t redraw)
{
TEveTrack* track;
RefMap_i i = fBackRefs.begin();
while (i != fBackRefs.end())
{
track = dynamic_cast<TEveTrack*>(i->first);
track->StampObjProps();
++i;
}
TEveElementList::ElementChanged(update_scenes, redraw);
}
////////////////////////////////////////////////////////////////////////////////
/// Initialize internal data-members for given particle parameters.
void TEveTrackPropagator::InitTrack(const TEveVectorD &v, Int_t charge)
{
fV = v;
fPoints.push_back(fV);
// init helix
fH.fPhi = 0;
fH.fCharge = charge;
}
////////////////////////////////////////////////////////////////////////////////
/// TEveVectorF wrapper.
void TEveTrackPropagator::InitTrack(const TEveVectorF& v, Int_t charge)
{
TEveVectorD vd(v);
InitTrack(vd, charge);
}
////////////////////////////////////////////////////////////////////////////////
/// Reset cache holding particle trajectory.
void TEveTrackPropagator::ResetTrack()
{
fLastPoints.clear();
fPoints.swap(fLastPoints);
// reset helix
fH.fPhi = 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Get index of current point on track.
Int_t TEveTrackPropagator::GetCurrentPoint() const
{
return fPoints.size() - 1;
}
////////////////////////////////////////////////////////////////////////////////
/// Calculate track length from start_point to end_point.
/// If end_point is less than 0, distance to the end is returned.
Double_t TEveTrackPropagator::GetTrackLength(Int_t start_point, Int_t end_point) const
{
if (end_point < 0) end_point = fPoints.size() - 1;
Double_t sum = 0;
for (Int_t i = start_point; i < end_point; ++i)
{
sum += (fPoints[i+1] - fPoints[i]).Mag();
}
return sum;
}
////////////////////////////////////////////////////////////////////////////////
/// Propagate particle with momentum p to vertex v.
Bool_t TEveTrackPropagator::GoToVertex(TEveVectorD& v, TEveVectorD& p)
{
Update(fV, p, kTRUE);
if ((v-fV).Mag() < kStepEps)
{
fPoints.push_back(v);
return kTRUE;
}
return fH.fValid ? LoopToVertex(v, p) : LineToVertex(v);
}
////////////////////////////////////////////////////////////////////////////////
/// Propagate particle with momentum p to line with start point s and vector r
/// to the second point.
Bool_t TEveTrackPropagator::GoToLineSegment(const TEveVectorD& s, const TEveVectorD& r, TEveVectorD& p)
{
Update(fV, p, kTRUE);
if (!fH.fValid)
{
TEveVectorD v;
ClosestPointBetweenLines(s, r, fV, p, v);
LineToVertex(v);
return kTRUE;
}
else
{
return LoopToLineSegment(s, r, p);
}
}
////////////////////////////////////////////////////////////////////////////////
/// TEveVectorF wrapper.
Bool_t TEveTrackPropagator::GoToVertex(TEveVectorF& v, TEveVectorF& p)
{
TEveVectorD vd(v), pd(p);
Bool_t result = GoToVertex(vd, pd);
v = vd; p = pd;
return result;
}
////////////////////////////////////////////////////////////////////////////////
/// TEveVectorF wrapper.
Bool_t TEveTrackPropagator::GoToLineSegment(const TEveVectorF& s, const TEveVectorF& r, TEveVectorF& p)
{
TEveVectorD sd(s), rd(r), pd(p);
Bool_t result = GoToLineSegment(sd, rd, pd);
p = pd;
return result;
}
////////////////////////////////////////////////////////////////////////////////
/// Propagate particle to bounds.
/// Return TRUE if hit bounds.
void TEveTrackPropagator::GoToBounds(TEveVectorD& p)
{
Update(fV, p, kTRUE);
fH.fValid ? LoopToBounds(p): LineToBounds(p);
}
////////////////////////////////////////////////////////////////////////////////
/// TEveVectorF wrapper.
void TEveTrackPropagator::GoToBounds(TEveVectorF& p)
{
TEveVectorD pd(p);
GoToBounds(pd);
p = pd;
}
////////////////////////////////////////////////////////////////////////////////
/// Update helix / B-field projection state.
void TEveTrackPropagator::Update(const TEveVector4D& v, const TEveVectorD& p,
Bool_t full_update, Bool_t enforce_max_step)
{
if (fStepper == kHelix)
{
fH.UpdateHelix(p, fMagFieldObj->GetFieldD(v), !fMagFieldObj->IsConst() || full_update, enforce_max_step);
}
else
{
fH.UpdateRK(p, fMagFieldObj->GetFieldD(v));
if (full_update)
{
using namespace TMath;
Float_t a = fgkB2C * fMagFieldObj->GetMaxFieldMagD() * Abs(fH.fCharge);
if (a > kAMin)
{
fH.fR = p.Mag() / a;
// get phi step, compare fDelta with MaxAng
fH.fPhiStep = fH.fMaxAng * DegToRad();
if (fH.fR > fH.fDelta )
{
Double_t ang = 2.0 * ACos(1.0f - fH.fDelta/fH.fR);
if (ang < fH.fPhiStep)
fH.fPhiStep = ang;
}
// check against maximum step-size
fH.fRKStep = fH.fR * fH.fPhiStep * Sqrt(1 + fH.fLam*fH.fLam);
if (fH.fRKStep > fH.fMaxStep || enforce_max_step)
{
fH.fPhiStep *= fH.fMaxStep / fH.fRKStep;
fH.fRKStep = fH.fMaxStep;
}
}
else
{
fH.fRKStep = fH.fMaxStep;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Wrapper to step helix.
void TEveTrackPropagator::Step(const TEveVector4D &v, const TEveVectorD &p, TEveVector4D &vOut, TEveVectorD &pOut)
{
if (fStepper == kHelix)
{
fH.Step(v, p, vOut, pOut);
}
else
{
Double_t vecRKIn[7];
vecRKIn[0] = v.fX;
vecRKIn[1] = v.fY;
vecRKIn[2] = v.fZ;
Double_t pm = p.Mag();
Double_t nm = 1.0 / pm;
vecRKIn[3] = p.fX*nm;
vecRKIn[4] = p.fY*nm;
vecRKIn[5] = p.fZ*nm;
vecRKIn[6] = p.Mag();
Double_t vecRKOut[7];
StepRungeKutta(fH.fRKStep, vecRKIn, vecRKOut);
vOut.fX = vecRKOut[0];
vOut.fY = vecRKOut[1];
vOut.fZ = vecRKOut[2];
vOut.fT = v.fT + fH.fRKStep;
pm = vecRKOut[6];
pOut.fX = vecRKOut[3]*pm;
pOut.fY = vecRKOut[4]*pm;
pOut.fZ = vecRKOut[5]*pm;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Propagate charged particle with momentum p to bounds.
/// It is expected that Update() with full-update was called before.
void TEveTrackPropagator::LoopToBounds(TEveVectorD& p)
{
const Double_t maxRsq = fMaxR*fMaxR;
TEveVector4D currV(fV);
TEveVector4D forwV(fV);
TEveVectorD forwP (p);
Int_t np = fPoints.size();
Double_t maxPhi = fMaxOrbs*TMath::TwoPi();
while (fH.fPhi < maxPhi && np<fNMax)
{
Step(currV, p, forwV, forwP);
// cross R
if (forwV.Perp2() > maxRsq)
{
Float_t t = (fMaxR - currV.R()) / (forwV.R() - currV.R());
if (t < 0 || t > 1)
{
Warning("HelixToBounds", "In MaxR crossing expected t>=0 && t<=1: t=%f, r1=%f, r2=%f, MaxR=%f.",
t, currV.R(), forwV.R(), fMaxR);
return;
}
TEveVectorD d(forwV);
d -= currV;
d *= t;
d += currV;
fPoints.push_back(d);
return;
}
// cross Z
else if (TMath::Abs(forwV.fZ) > fMaxZ)
{
Double_t t = (fMaxZ - TMath::Abs(currV.fZ)) / TMath::Abs((forwV.fZ - currV.fZ));
if (t < 0 || t > 1)
{
Warning("HelixToBounds", "In MaxZ crossing expected t>=0 && t<=1: t=%f, z1=%f, z2=%f, MaxZ=%f.",
t, currV.fZ, forwV.fZ, fMaxZ);
return;
}
TEveVectorD d(forwV -currV);
d *= t;
d += currV;
fPoints.push_back(d);
return;
}
currV = forwV;
p = forwP;
Update(currV, p);
fPoints.push_back(currV);
++np;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Propagate charged particle with momentum p to vertex v.
/// It is expected that Update() with full-update was called before.
Bool_t TEveTrackPropagator::LoopToVertex(TEveVectorD& v, TEveVectorD& p)
{
const Double_t maxRsq = fMaxR * fMaxR;
TEveVector4D currV(fV);
TEveVector4D forwV(fV);
TEveVectorD forwP(p);
Int_t first_point = fPoints.size();
Int_t np = first_point;
Double_t prod0=0, prod1;
do
{
Step(currV, p, forwV, forwP);
Update(forwV, forwP);
if (PointOverVertex(v, forwV, &prod1))
{
break;
}
if (IsOutsideBounds(forwV, maxRsq, fMaxZ))
{
fV = currV;
return kFALSE;
}
fPoints.push_back(forwV);
currV = forwV;
p = forwP;
prod0 = prod1;
++np;
} while (np < fNMax);
// make the remaining fractional step
if (np > first_point)
{
if ((v - currV).Mag() > kStepEps)
{
Double_t step_frac = prod0 / (prod0 - prod1);
if (step_frac > 0)
{
// Step for fraction of previous step size.
// We pass 'enforce_max_step' flag to Update().
Float_t orig_max_step = fH.fMaxStep;
fH.fMaxStep = step_frac * (forwV - currV).Mag();
Update(currV, p, kTRUE, kTRUE);
Step(currV, p, forwV, forwP);
p = forwP;
currV = forwV;
fPoints.push_back(currV);
++np;
fH.fMaxStep = orig_max_step;
}
// Distribute offset to desired crossing point over all segment.
TEveVectorD off(v - currV);
off *= 1.0f / currV.fT;
DistributeOffset(off, first_point, np, p);
fV = v;
return kTRUE;
}
}
fPoints.push_back(v);
fV = v;
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Propagate charged particle with momentum p to line segment with point s and
/// vector r to the second point. It is expected that Update() with full-update
/// was called before. Returns kFALSE if hits bounds.
Bool_t TEveTrackPropagator::LoopToLineSegment(const TEveVectorD& s, const TEveVectorD& r, TEveVectorD& p)
{
const Double_t maxRsq = fMaxR * fMaxR;
const Double_t rMagInv = 1./r.Mag();
TEveVector4D currV(fV);
TEveVector4D forwV(fV);
TEveVectorD forwP(p);
Int_t first_point = fPoints.size();
Int_t np = first_point;
TEveVectorD forwC;
TEveVectorD currC;
do
{
Step(currV, p, forwV, forwP);
Update(forwV, forwP);
ClosestPointFromVertexToLineSegment(forwV, s, r, rMagInv, forwC);
// check forwV is over segment with orthogonal component of
// momentum to vector r
TEveVectorD b = r; b.Normalize();
Double_t x = forwP.Dot(b);
TEveVectorD pTPM = forwP - x*b;
if (pTPM.Dot(forwC - forwV) < 0)
{
break;
}
if (IsOutsideBounds(forwV, maxRsq, fMaxZ))
{
fV = currV;
return kFALSE;
}
fPoints.push_back(forwV);
currV = forwV;
p = forwP;
currC = forwC;
++np;
} while (np < fNMax);
// Get closest point on segment relative to line with forw and currV points.
TEveVectorD v;
ClosestPointBetweenLines(s, r, currV, forwV - currV, v);
// make the remaining fractional step
if (np > first_point)
{
if ((v - currV).Mag() > kStepEps)
{
TEveVector last_step = forwV - currV;
TEveVector delta = v - currV;
Double_t step_frac = last_step.Dot(delta) / last_step.Mag2();
if (step_frac > 0)
{
// Step for fraction of previous step size.
// We pass 'enforce_max_step' flag to Update().
Float_t orig_max_step = fH.fMaxStep;
fH.fMaxStep = step_frac * (forwV - currV).Mag();
Update(currV, p, kTRUE, kTRUE);
Step(currV, p, forwV, forwP);
p = forwP;
currV = forwV;
fPoints.push_back(currV);
++np;
fH.fMaxStep = orig_max_step;
}
// Distribute offset to desired crossing point over all segment.
TEveVectorD off(v - currV);
off *= 1.0f / currV.fT;
DistributeOffset(off, first_point, np, p);
fV = v;
return kTRUE;
}
}
fPoints.push_back(v);
fV = v;
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Distribute offset between first and last point index and rotate
/// momentum.
void TEveTrackPropagator::DistributeOffset(const TEveVectorD& off, Int_t first_point, Int_t np, TEveVectorD& p)
{
// Calculate the required momentum rotation.
// lpd - last-points-delta
TEveVectorD lpd0(fPoints[np-1]);
lpd0 -= fPoints[np-2];
lpd0.Normalize();
for (Int_t i = first_point; i < np; ++i)
{
fPoints[i] += off * fPoints[i].fT;
}
TEveVectorD lpd1(fPoints[np-1]);
lpd1 -= fPoints[np-2];
lpd1.Normalize();
TEveTrans tt;
tt.SetupFromToVec(lpd0, lpd1);
// TEveVectorD pb4(p);
// printf("Rotating momentum: p0 = "); p.Dump();
tt.RotateIP(p);
// printf(" p1 = "); p.Dump();
// printf(" n1=%f, n2=%f, dp = %f deg\n", pb4.Mag(), p.Mag(),
// TMath::RadToDeg()*TMath::ACos(p.Dot(pb4)/(pb4.Mag()*p.Mag())));
}
////////////////////////////////////////////////////////////////////////////////
/// Propagate neutral particle to vertex v.
Bool_t TEveTrackPropagator::LineToVertex(TEveVectorD& v)
{
TEveVector4D currV = v;
currV.fX = v.fX;
currV.fY = v.fY;
currV.fZ = v.fZ;
fPoints.push_back(currV);
fV = v;
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Propagate neutral particle with momentum p to bounds.
void TEveTrackPropagator::LineToBounds(TEveVectorD& p)
{
Double_t tZ = 0, tR = 0, tB = 0;
// time where particle intersect +/- fMaxZ
if (p.fZ > 0)
tZ = (fMaxZ - fV.fZ) / p.fZ;
else if (p.fZ < 0)
tZ = - (fMaxZ + fV.fZ) / p.fZ;
else
tZ = 1e99;
// time where particle intersects cylinder
Double_t a = p.fX*p.fX + p.fY*p.fY;
Double_t b = 2.0 * (fV.fX*p.fX + fV.fY*p.fY);
Double_t c = fV.fX*fV.fX + fV.fY*fV.fY - fMaxR*fMaxR;
Double_t d = b*b - 4.0*a*c;
if (d >= 0) {
Double_t sqrtD = TMath::Sqrt(d);
tR = (-b - sqrtD) / (2.0 * a);
if (tR < 0) {
tR = (-b + sqrtD) / (2.0 * a);
}
tB = tR < tZ ? tR : tZ; // compare the two times
} else {
tB = tZ;
}
TEveVectorD nv(fV.fX + p.fX*tB, fV.fY + p.fY*tB, fV.fZ + p.fZ*tB);
LineToVertex(nv);
}
////////////////////////////////////////////////////////////////////////////////
/// Intersect helix with a plane. Current position and argument p define
/// the helix.
Bool_t TEveTrackPropagator::HelixIntersectPlane(const TEveVectorD& p,
const TEveVectorD& point,
const TEveVectorD& normal,
TEveVectorD& itsect)
{
TEveVectorD pos(fV);
TEveVectorD mom(p);
if (fMagFieldObj->IsConst())
fH.UpdateHelix(mom, fMagFieldObj->GetFieldD(pos), kFALSE, kFALSE);
TEveVectorD n(normal);
TEveVectorD delta = pos - point;
Double_t d = delta.Dot(n);
if (d > 0) {
n.NegateXYZ(); // Turn normal around so that we approach from negative side of the plane
d = -d;
}
TEveVector4D forwV;
TEveVectorD forwP;
TEveVector4D pos4(pos);
while (kTRUE)
{
Update(pos4, mom);
Step(pos4, mom, forwV , forwP);
Double_t new_d = (forwV - point).Dot(n);
if (new_d < d)
{
// We are going further away ... fail intersect.
Warning("HelixIntersectPlane", "going away from the plane.");
return kFALSE;
}
if (new_d > 0)
{
delta = forwV - pos4;
itsect = pos4 + delta * ((point - pos4).Dot(n) / delta.Dot(n));
return kTRUE;
}
pos4 = forwV;
mom = forwP;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Intersect line with a plane. Current position and argument p define
/// the line.
Bool_t TEveTrackPropagator::LineIntersectPlane(const TEveVectorD& p,
const TEveVectorD& point,
const TEveVectorD& normal,
TEveVectorD& itsect)
{
TEveVectorD pos(fV.fX, fV.fY, fV.fZ);
TEveVectorD delta = point - pos;
Double_t pn = p.Dot(normal);
if (pn == 0)
{
return kFALSE;
}
Double_t t = delta.Dot(normal) / pn;
if (t < 0) {
return kFALSE;
} else {
itsect = pos + p*t;
return kTRUE;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Find intersection of currently propagated track with a plane.
/// Current track position is used as starting point.
///
/// Args:
/// - p - track momentum to use for extrapolation
/// - point - a point on a plane
/// - normal - normal of the plane
/// - itsect - output, point of intersection
/// Returns:
/// - kFALSE if intersection can not be found, kTRUE otherwise.
Bool_t TEveTrackPropagator::IntersectPlane(const TEveVectorD& p,
const TEveVectorD& point,
const TEveVectorD& normal,
TEveVectorD& itsect)
{
if (fH.fCharge && fMagFieldObj)
return HelixIntersectPlane(p, point, normal, itsect);
else
return LineIntersectPlane(p, point, normal, itsect);
}
////////////////////////////////////////////////////////////////////////////////
/// Get closest point from given vertex v to line segment defined with s and r.
/// Argument rMagInv is cached. rMagInv= 1./rMag()
void TEveTrackPropagator::ClosestPointFromVertexToLineSegment(const TEveVectorD& v,
const TEveVectorD& s,
const TEveVectorD& r,
Double_t rMagInv,
TEveVectorD& c)
{
TEveVectorD dir = v - s;
TEveVectorD b1 = r * rMagInv;
// parallel distance
Double_t dot = dir.Dot(b1);
TEveVectorD dirI = dot * b1;
Double_t facX = dot * rMagInv;
if (facX <= 0)
c = s;
else if (facX >= 1)
c = s + r;
else
c = s + dirI;
}
////////////////////////////////////////////////////////////////////////////////
/// Get closest point on line defined with vector p0 and u.
/// Return false if the point is forced on the line segment.
Bool_t TEveTrackPropagator::ClosestPointBetweenLines(const TEveVectorD& p0,
const TEveVectorD& u,
const TEveVectorD& q0,
const TEveVectorD& v,
TEveVectorD& out)
{
TEveVectorD w0 = p0 -q0;
Double_t a = u.Mag2();
Double_t b = u.Dot(v);
Double_t c = v.Mag2();
Double_t d = u.Dot(w0);
Double_t e = v.Dot(w0);
Double_t x = (b*e - c*d)/(a*c -b*b);
Bool_t force = (x < 0 || x > 1);
out = p0 + TMath::Range(0., 1., x) * u;
return force;
}