FairShip
Loading...
Searching...
No Matches
test_straw_patrec.cxx
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// Tests of the straw pattern recognition: the sign convention of the drift
6// distances against the ACTS straw surface, and finding a synthetic track.
7
8#include <cmath>
9#include <iostream>
10#include <memory>
11#include <vector>
12
13#include "Acts/Geometry/GeometryContext.hpp"
14#include "Acts/Surfaces/LineBounds.hpp"
15#include "Acts/Surfaces/StrawSurface.hpp"
16#include "ActsFrame.hpp"
17#include "StrawPatRec.hpp"
18
19namespace {
20
21constexpr double kStereoAngle = 4.57 * M_PI / 180.;
22constexpr double kPitch = 2.; // cm
23constexpr double kRadius = 1.; // cm
24
25int failures = 0;
26
27void check(bool condition, const char* what) {
28 if (!condition) {
29 std::cout << "FAIL: " << what << std::endl;
30 ++failures;
31 }
32}
33
34SHiP::StrawWire makeWire(int station, int view, int layer, double angle,
35 double offset, double z) {
36 // Wires along (cos a, sin a); offset along the normal (-sin a, cos a)
37 const double dx = std::cos(angle);
38 const double dy = std::sin(angle);
39 return SHiP::StrawWire{0, station, view, layer, -dy * offset, dx * offset,
40 z, dx, dy, 0., kRadius, 250.};
41}
42
43double viewAngle(int view) {
44 return view == 1 ? kStereoAngle : view == 2 ? -kStereoAngle : 0.;
45}
46
48double actsLoc0(const SHiP::LineSegment& line, const SHiP::StrawWire& wire) {
49 const auto gctx = Acts::GeometryContext::dangerouslyDefaultConstruct();
50 Acts::Transform3 ship = Acts::Transform3::Identity();
51 const Acts::Vector3 axis(wire.dx, wire.dy, wire.dz);
52 const Acts::Vector3 other = axis.cross(Acts::Vector3::UnitZ()).normalized();
53 ship.linear().col(0) = other;
54 ship.linear().col(1) = axis.cross(other);
55 ship.linear().col(2) = axis;
56 ship.translation() = Acts::Vector3(wire.x, wire.y, wire.z);
57 const auto surface = Acts::Surface::makeShared<Acts::StrawSurface>(
59 std::make_shared<const Acts::LineBounds>(kRadius * 10., 2500.));
60 const Acts::Vector3 position =
61 SHiP::ActsFrame::positionToActs(line.x(wire.z), line.y(wire.z), wire.z);
62 const Acts::Vector3 direction =
63 (SHiP::ActsFrame::toActsRotation() * Acts::Vector3(line.tx, line.ty, 1.))
64 .normalized();
65 const auto pca = surface->intersect(gctx, position, direction).closest();
66 const auto local = surface->globalToLocal(gctx, pca.position(), direction);
67 return local.value()[0] / SHiP::ActsFrame::kShipLength;
68}
69
70void testSignConvention() {
71 for (const int view : {0, 1, 2}) {
72 for (const double offset : {-0.4, 0.7}) {
73 const auto wire = makeWire(1, view, 0, viewAngle(view), 0., 100.);
75 line.z0 = 100.;
76 // Move the track off the wire along its normal
77 line.x0 =
78 -std::sin(viewAngle(view)) * offset + 0.3 * std::cos(viewAngle(view));
79 line.y0 =
80 std::cos(viewAngle(view)) * offset + 0.3 * std::sin(viewAngle(view));
81 line.tx = 0.02;
82 line.ty = -0.03;
83 const double ours = SHiP::signedWireDistance(line, wire);
84 const double acts = actsLoc0(line, wire);
85 check(std::abs(ours - acts) < 1e-6,
86 "signed wire distance matches ACTS straw loc0");
87 check(std::abs(std::abs(ours) - std::abs(offset)) < 0.01,
88 "signed wire distance has the expected size");
89 }
90 }
91}
92
94void addStation(int station, double z0, const SHiP::LineSegment& line,
95 std::vector<SHiP::StrawWire>& wires) {
96 for (int view = 0; view < 4; ++view) {
97 for (int layer = 0; layer < 2; ++layer) {
98 const double z = z0 + 10. * view + 2. * layer;
99 const double angle = viewAngle(view);
100 // Coordinate of the track along the normal to the wires
101 const double u =
102 -std::sin(angle) * line.x(z) + std::cos(angle) * line.y(z);
103 const double stagger = layer * kPitch / 2.;
104 const double offset =
105 std::round((u - stagger) / kPitch) * kPitch + stagger;
106 wires.push_back(makeWire(station, view, layer, angle, offset, z));
107 }
108 }
109}
110
111void testFindsSyntheticTrack() {
112 // A track bent in y at the magnet, at z = 500 cm, by a field integral of
113 // 0.65 T m: positive charge, 10 GeV
114 constexpr double bendingZ = 500.;
115 constexpr double fieldIntegral = 0.65;
116 constexpr double momentum = 10.;
118 up.z0 = bendingZ;
119 up.x0 = 12.;
120 up.tx = 0.01;
121 up.y0 = -20.;
122 up.ty = 0.005;
123 SHiP::LineSegment down = up;
124 down.ty = up.ty + 0.299792458 * fieldIntegral / momentum;
125
126 std::vector<SHiP::StrawWire> wires;
127 addStation(1, 0., up, wires);
128 addStation(2, 200., up, wires);
129 addStation(3, 800., down, wires);
130 addStation(4, 1000., down, wires);
131 std::vector<SHiP::PatRecHit> hits;
132 for (std::size_t i = 0; i < wires.size(); ++i) {
133 const auto& line = wires[i].station <= 2 ? up : down;
134 hits.push_back({static_cast<int>(i), &wires[i],
135 std::abs(SHiP::signedWireDistance(line, wires[i])), 0.012});
136 }
137
139 fieldIntegral);
140 const auto tracks = patRec.findTracks(hits);
141 check(tracks.size() == 1, "one track found");
142 if (tracks.size() != 1) {
143 return;
144 }
145 const auto& track = tracks.front();
146 check(track.charge == 1, "charge from the kink");
147 check(std::abs(track.momentum - momentum) / momentum < 0.01,
148 "momentum from the kink");
149 check(track.upstream.hits.size() == 16 && track.downstream.hits.size() == 16,
150 "all hits on the segments");
151 for (const auto* segment : {&track.upstream, &track.downstream}) {
152 const auto& truth = segment == &track.upstream ? up : down;
153 check(std::abs(segment->x(bendingZ) - truth.x(bendingZ)) < 0.01 &&
154 std::abs(segment->y(bendingZ) - truth.y(bendingZ)) < 0.01,
155 "segment position");
156 for (std::size_t k = 0; k < segment->hits.size(); ++k) {
157 const auto& wire = wires[segment->hits[k]];
158 const double distance = SHiP::signedWireDistance(truth, wire);
159 check(std::abs(distance) < 0.05 ||
160 segment->signs[k] == (distance >= 0. ? 1 : -1),
161 "side of the wire");
162 }
163 }
164}
165
166} // namespace
167
168int main() {
169 testSignConvention();
170 testFindsSyntheticTrack();
171 if (failures == 0) {
172 std::cout << "All straw pattern recognition tests passed" << std::endl;
173 }
174 return failures == 0 ? 0 : 1;
175}
Acts::Transform3 transformToActs(const Acts::Transform3 &shipTransform)
Transform of a TGeo placement (SHiP frame, cm) in the ACTS frame.
Definition ActsFrame.hpp:56
Acts::RotationMatrix3 toActsRotation()
Rotation taking SHiP-frame vectors to the ACTS frame.
Definition ActsFrame.hpp:31
Acts::Vector3 positionToActs(double x, double y, double z)
Definition ActsFrame.hpp:38
constexpr double kShipLength
Length unit of the SHiP frame, in ACTS units.
Definition ActsFrame.hpp:36
double signedWireDistance(const LineSegment &line, const StrawWire &wire)
int i
Definition ShipAna.py:108
double y(double z) const
double x(double z) const
A straw wire in the SHiP frame (cm), for pattern recognition.
double dx
Unit vector along the wire.
double x
Wire centre.
int main()