FairShip
Loading...
Searching...
No Matches
MagneticTrackPropagator.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5#ifndef NEWMUONDIS_MAGNETICTRACKPROPAGATOR_H_
6#define NEWMUONDIS_MAGNETICTRACKPROPAGATOR_H_
7
8#include <array>
9#include <cstddef>
10#include <limits>
11#include <utility>
12#include <vector>
13
14#include "TVector3.h"
15
16class ShipBFieldMap;
17class TGeoManager;
18
19// Magnetic transport only: cm, GeV/c, charge in units of e. The momentum
20// vector supplies both magnitude and direction. Field/geometry are borrowed
21// and must remain alive and unchanged while this object is used.
23 public:
24 explicit MagneticTrackPropagator(ShipBFieldMap* field = nullptr,
25 TGeoManager* geometry = nullptr,
26 ShipBFieldMap* muonShieldField = nullptr);
27 void SetAccuracy(double positionTolerance = 1.e-3,
28 double relativeMomentumTolerance = 1.e-6,
29 double maxStep = 5.);
30 double GetPlaneZ(const char* volumeName = "Tr1") const;
31 std::pair<double, double> GetVolumeZRange(const char* volumeName) const;
32 // Global x/y limits of the downstream (+local-z) face of a box volume.
33 std::array<double, 4> GetVolumeExitFaceXY(const char* volumeName) const;
34 std::pair<double, double> GetMuonShieldZRange() const {
35 return fMuonShieldZRange;
36 }
37 bool HasMuonShieldField() const { return fMuonShieldField != nullptr; }
38 // Restrict the initial shield scan; a later upstream request widens it
39 // safely.
40 void SetMuonShieldMinZ(double minimumZ);
41 double GetMuonShieldMinZ() const { return fMuonShieldMinZ; }
42 // Test a forward trajectory against an axis-aligned detector volume.
43 bool IntersectsBox(double charge, const TVector3& position,
44 const TVector3& momentum, const TVector3& minimum,
45 const TVector3& maximum) const;
46
47 // Both directions in z are supported, provided pz never changes sign.
48 // False means invalid input, a turning track, or failure to converge.
49 bool Extrapolate(double charge, const TVector3& position,
50 const TVector3& momentum, double z, TVector3& result,
51 TVector3& resultMomentum) const;
52 bool BuildTrajectory(double charge, const TVector3& position,
53 const TVector3& momentum, double endZ);
54 // Cubic interpolation of cached positions; never evaluates the field.
55 bool PositionAt(double z, TVector3& position) const;
56 std::size_t GetTrajectorySize() const { return fTrajectory.size(); }
57 bool GetTrajectoryState(std::size_t index, TVector3& position,
58 TVector3& momentum) const;
59 // Downstream end of all nonzero interpolation cells, including fringes.
60 double GetFieldEndZ(double minimumZ) const;
61 // Evaluate the map at the point, including interpolation and fringes.
62 bool HasFieldAt(const TVector3& position) const;
63
64 private:
65 struct State {
66 TVector3 position;
67 TVector3 momentum;
68 };
69 struct Box {
70 TVector3 minimum, maximum;
71 };
72 struct FieldMap {
73 ShipBFieldMap* field; // Borrowed; the caller owns the map.
74 std::vector<Box> regions;
75 double gridStep;
76 };
77 static bool InsideBox(const TVector3& position, const Box& box);
78 static bool RayBoxInterval(const TVector3& position,
79 const TVector3& direction, const Box& box,
80 double& entry, double& exit);
81 static bool SegmentIntersectsBox(const State& start, const State& end,
82 const Box& box);
83 void AddFieldMap(ShipBFieldMap* field, double minimumZ) const;
84 void EnsureMuonShield(double minimumZ) const;
85 bool Derivative(const State& state, double charge, double pzSign,
86 State& derivative) const;
87 bool RKStep(const State& state, double charge, double pzSign, double dz,
88 State& result) const;
89 bool Propagate(double charge, const TVector3& position,
90 const TVector3& momentum, double z, State& result,
91 std::vector<State>* trajectory,
92 const Box* box = nullptr) const;
93 TGeoManager* fGeometry;
94 mutable std::vector<FieldMap>
97 nullptr;
98 mutable bool fMuonShieldReady = false;
99 mutable double fMuonShieldMinZ = -std::numeric_limits<double>::infinity();
100 std::pair<double, double> fMuonShieldZRange = {0., 0.};
101 double fPositionTolerance = 1.e-3;
102 double fMomentumTolerance = 1.e-6;
103 double fMaxStep = 5.;
104 std::vector<State> fTrajectory;
105};
106
107#endif // NEWMUONDIS_MAGNETICTRACKPROPAGATOR_H_
bool PositionAt(double z, TVector3 &position) const
void EnsureMuonShield(double minimumZ) const
std::size_t GetTrajectorySize() const
bool fMuonShieldReady
Borrowed; scanned on first transport.
MagneticTrackPropagator(ShipBFieldMap *field=nullptr, TGeoManager *geometry=nullptr, ShipBFieldMap *muonShieldField=nullptr)
bool Propagate(double charge, const TVector3 &position, const TVector3 &momentum, double z, State &result, std::vector< State > *trajectory, const Box *box=nullptr) const
bool HasFieldAt(const TVector3 &position) const
double GetPlaneZ(const char *volumeName="Tr1") const
bool GetTrajectoryState(std::size_t index, TVector3 &position, TVector3 &momentum) const
static bool RayBoxInterval(const TVector3 &position, const TVector3 &direction, const Box &box, double &entry, double &exit)
ShipBFieldMap * fMuonShieldField
Maps and conservative nonzero 3D regions.
double GetFieldEndZ(double minimumZ) const
bool RKStep(const State &state, double charge, double pzSign, double dz, State &result) const
bool IntersectsBox(double charge, const TVector3 &position, const TVector3 &momentum, const TVector3 &minimum, const TVector3 &maximum) const
bool Extrapolate(double charge, const TVector3 &position, const TVector3 &momentum, double z, TVector3 &result, TVector3 &resultMomentum) const
void AddFieldMap(ShipBFieldMap *field, double minimumZ) const
std::pair< double, double > GetVolumeZRange(const char *volumeName) const
std::pair< double, double > GetMuonShieldZRange() const
void SetMuonShieldMinZ(double minimumZ)
std::vector< FieldMap > fFields
Borrowed geometry.
std::array< double, 4 > GetVolumeExitFaceXY(const char *volumeName) const
std::pair< double, double > fMuonShieldZRange
static bool InsideBox(const TVector3 &position, const Box &box)
bool BuildTrajectory(double charge, const TVector3 &position, const TVector3 &momentum, double endZ)
static bool SegmentIntersectsBox(const State &start, const State &end, const Box &box)
void SetAccuracy(double positionTolerance=1.e-3, double relativeMomentumTolerance=1.e-6, double maxStep=5.)
bool Derivative(const State &state, double charge, double pzSign, State &derivative) const
Class that defines a (3d) magnetic field map (distances in cm, fields in tesla).