Skip to content

Commit f448345

Browse files
committed
Harmonize acts and traccc parameter smearing
1 parent 7a19579 commit f448345

19 files changed

+142
-101
lines changed

core/include/traccc/edm/track_parameters.hpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "traccc/definitions/qualifiers.hpp"
1515
#include "traccc/definitions/track_parametrization.hpp"
1616
#include "traccc/edm/container.hpp"
17+
#include "traccc/utils/trigonometric_helpers.hpp"
1718

1819
// detray include(s).
1920
#include <detray/tracks/tracks.hpp>
@@ -50,17 +51,7 @@ using bound_track_parameters_collection_types =
5051
template <detray::concepts::algebra algebra_t>
5152
TRACCC_HOST_DEVICE inline void wrap_phi(
5253
bound_track_parameters<algebra_t>& param) {
53-
54-
traccc::scalar phi = param.phi();
55-
static constexpr traccc::scalar TWOPI =
56-
2.f * traccc::constant<traccc::scalar>::pi;
57-
phi = math::fmod(phi, TWOPI);
58-
if (phi > traccc::constant<traccc::scalar>::pi) {
59-
phi -= TWOPI;
60-
} else if (phi < -traccc::constant<traccc::scalar>::pi) {
61-
phi += TWOPI;
62-
}
63-
param.set_phi(phi);
54+
param.set_phi(detail::wrap_phi(param.phi()));
6455
}
6556

6657
/// Covariance inflation used for track fitting

core/include/traccc/utils/seed_generator.hpp

Lines changed: 122 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// Library include(s).
1111
#include "traccc/edm/track_parameters.hpp"
1212
#include "traccc/utils/particle.hpp"
13+
#include "traccc/utils/trigonometric_helpers.hpp"
1314

1415
// detray include(s).
1516
#include <detray/geometry/barcode.hpp>
@@ -28,14 +29,53 @@ struct seed_generator {
2829
using algebra_type = typename detector_t::algebra_type;
2930
using ctx_t = typename detector_t::geometry_context;
3031

32+
/// Configure the seed generator
33+
struct config {
34+
// Smearing parameters
35+
/// Constant term of the loc0 resolution.
36+
double sigma_loc0 = 20 * traccc::unit<scalar>::um;
37+
/// Pt-dependent loc0 resolution of the form sigma_loc0 =
38+
/// A*exp(-1.*abs(B)*pt).
39+
double sigma_loc0_pT_a = 30 * traccc::unit<scalar>::um;
40+
double sigma_loc0_pT_b = 0.3 / traccc::unit<scalar>::GeV;
41+
/// Constant term of the loc1 resolution.
42+
double sigma_loc1 = 20 * traccc::unit<scalar>::um;
43+
/// Pt-dependent loc1 resolution of the form sigma_loc1 =
44+
/// A*exp(-1.*abs(B)*pt).
45+
double sigma_loc1_pT_a = 30 * traccc::unit<scalar>::um;
46+
double sigma_loc1_pT_b = 0.3 / traccc::unit<scalar>::GeV;
47+
/// Time resolution.
48+
double sigma_time = 1 * traccc::unit<scalar>::ns;
49+
/// Phi angular resolution.
50+
double sigma_phi = 1 * traccc::unit<scalar>::degree;
51+
/// Theta angular resolution.
52+
double sigma_theta = 1 * traccc::unit<scalar>::degree;
53+
/// Relative transverse momentum resolution.
54+
double sigma_pT_rel = 0.05;
55+
56+
/// Optional. Initial sigmas for the track parameters which overwrites
57+
/// the smearing params if set.
58+
std::optional<std::array<double, e_bound_size>> initial_sigmas;
59+
/// Initial sigma(q/pt) for the track parameters.
60+
/// @note The resulting q/p sigma is added to the one in `initialSigmas`
61+
double initialsigma_qopt =
62+
0.1 * traccc::unit<scalar>::e / traccc::unit<scalar>::GeV;
63+
/// Initial sigma(pt)/pt for the track parameters.
64+
/// @note The resulting q/p sigma is added to the one in `initialSigmas`
65+
double initialsigma_pT_rel = 0.1;
66+
67+
/// Inflation factors for the variances
68+
std::array<scalar, e_bound_size> cov_inflation{1., 1., 1., 1., 1., 1.};
69+
};
70+
3171
/// Constructor with detector
3272
///
3373
/// @param det input detector
3474
/// @param stddevs standard deviations for parameter smearing
35-
seed_generator(const detector_t& det,
36-
const std::array<scalar, e_bound_size>& stddevs,
37-
const std::size_t sd = 0, ctx_t ctx = {})
38-
: m_detector(det), m_stddevs(stddevs), m_ctx(ctx) {
75+
seed_generator(const detector_t& det, const config& cfg = {},
76+
const std::size_t sd = std::mt19937::default_seed,
77+
ctx_t ctx = {})
78+
: m_detector(det), m_ctx(ctx), m_cfg(cfg) {
3979
m_generator.seed(static_cast<std::mt19937::result_type>(sd));
4080
}
4181

@@ -73,32 +113,97 @@ struct seed_generator {
73113
sf);
74114
}
75115

76-
for (std::size_t i = 0; i < e_bound_size; i++) {
116+
// Call the smearing operator
117+
(*this)(bound_param, ptc_type);
77118

78-
if (m_stddevs[i] != scalar{0}) {
79-
bound_param[i] = std::normal_distribution<scalar>(
80-
bound_param[i], m_stddevs[i])(m_generator);
81-
}
119+
return bound_param;
120+
}
82121

83-
getter::element(bound_param.covariance(), i, i) =
84-
m_stddevs[i] * m_stddevs[i];
122+
/// Seed generator operation
123+
///
124+
/// @param vertex vertex of particle
125+
/// @param stddevs standard deviations for track parameter smearing
126+
void operator()(bound_track_parameters<algebra_type>& bound_param,
127+
const traccc::pdg_particle<scalar>& ptc_type) {
128+
129+
const scalar q{ptc_type.charge()};
130+
const auto pos = bound_param.bound_local();
131+
const auto time = bound_param.time();
132+
const auto phi = bound_param.phi();
133+
const auto theta = bound_param.theta();
134+
const auto pt = bound_param.pT(q);
135+
const auto qop = bound_param.qop();
136+
137+
// Compute momentum-dependent resolutions
138+
const double sigma_loc_0 =
139+
m_cfg.sigma_loc0 +
140+
m_cfg.sigma_loc0_pT_a *
141+
math::exp(-1.0 * math::fabs(m_cfg.sigma_loc0_pT_b) * pt);
142+
const double sigma_loc_1 =
143+
m_cfg.sigma_loc1 +
144+
m_cfg.sigma_loc1_pT_a *
145+
math::exp(-1.0 * math::fabs(m_cfg.sigma_loc1_pT_b) * pt);
146+
// Shortcuts for other resolutions
147+
const double sigma_qop = math::sqrt(
148+
math::pow(m_cfg.sigma_pT_rel * qop, 2) +
149+
math::pow(m_cfg.sigma_theta * (qop * math::tan(theta)), 2));
150+
151+
// Smear the position/time
152+
// Note that we smear d0 and z0 in the perigee frame
153+
const scalar smeared_loc0 =
154+
std::normal_distribution<scalar>(pos[0], sigma_loc_0)(m_generator);
155+
const scalar smeared_loc1 =
156+
std::normal_distribution<scalar>(pos[1], sigma_loc_1)(m_generator);
157+
bound_param.set_bound_local({smeared_loc0, smeared_loc1});
158+
159+
// Time
160+
bound_param.set_time(std::normal_distribution<scalar>(
161+
time, m_cfg.sigma_time)(m_generator));
162+
163+
// Smear direction angles phi,theta ensuring correct bounds
164+
const scalar smeared_phi =
165+
std::normal_distribution<scalar>(phi, m_cfg.sigma_phi)(m_generator);
166+
const scalar smeared_theta = std::normal_distribution<scalar>(
167+
theta, m_cfg.sigma_theta)(m_generator);
168+
const auto [new_phi, new_theta] =
169+
detail::wrap_phi_theta(smeared_phi, smeared_theta);
170+
bound_param.set_phi(new_phi);
171+
bound_param.set_theta(new_theta);
172+
173+
// Compute smeared q/p
174+
bound_param.set_qop(
175+
std::normal_distribution<scalar>(qop, sigma_qop)(m_generator));
176+
177+
// Build the track covariance matrix using the smearing sigmas
178+
std::array<scalar, e_bound_size> sigmas{
179+
sigma_loc_0, sigma_loc_1, m_cfg.sigma_phi,
180+
m_cfg.sigma_theta, sigma_qop, m_cfg.sigma_time};
181+
182+
for (std::size_t i = e_bound_loc0; i < e_bound_size; ++i) {
183+
double sigma = sigmas[i];
184+
double variance = sigma * sigma;
185+
186+
// Inflate the initial covariance
187+
variance *= m_cfg.cov_inflation[i];
188+
189+
getter::element(bound_param.covariance(), i, i) = variance;
85190
}
86191

87192
assert(!bound_param.is_invalid());
88-
89-
return bound_param;
90193
}
91194

92195
private:
93-
// Random generator
196+
/// Random generator
94197
std::random_device m_rd{};
95198
std::mt19937 m_generator{m_rd()};
96199

97-
// Detector object
200+
/// Detector object
98201
const detector_t& m_detector;
99-
/// Standard deviations for parameter smearing
100-
std::array<scalar, e_bound_size> m_stddevs;
202+
/// Geometry context
101203
ctx_t m_ctx;
204+
205+
/// Seed generator configuration
206+
config m_cfg{};
102207
};
103208

104209
} // namespace traccc

examples/run/cpu/misaligned_truth_fitting_example.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@ int main(int argc, char* argv[]) {
110110
* Do the reconstruction
111111
*****************************/
112112

113-
/// Standard deviations for seed track parameters
114-
static constexpr std::array<scalar, e_bound_size> stddevs = {
115-
0.03f * traccc::unit<scalar>::mm,
116-
0.03f * traccc::unit<scalar>::mm,
117-
0.017f,
118-
0.017f,
119-
0.001f / traccc::unit<scalar>::GeV,
120-
1.f * traccc::unit<scalar>::ns};
121-
122113
// Fitting algorithm objects
123114
// Alg0
124115
traccc::fitting_config fit_cfg0(fitting_opts);
@@ -135,9 +126,9 @@ int main(int argc, char* argv[]) {
135126

136127
// Seed generators
137128
traccc::seed_generator<host_detector_type> sg0(
138-
host_det, stddevs, 0, fit_cfg0.propagation.context);
129+
host_det, {}, 0, fit_cfg0.propagation.context);
139130
traccc::seed_generator<host_detector_type> sg1(
140-
host_det, stddevs, 0, fit_cfg1.propagation.context);
131+
host_det, {}, 0, fit_cfg1.propagation.context);
141132

142133
// Iterate over events
143134
for (auto event = input_opts.skip;

examples/run/cpu/truth_finding_example.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,6 @@ int seq_run(const traccc::opts::track_finding& finding_opts,
9090
* Do the reconstruction
9191
*****************************/
9292

93-
// Standard deviations for seed track parameters
94-
static constexpr std::array<traccc::scalar, traccc::e_bound_size> stddevs =
95-
{1e-4f * traccc::unit<traccc::scalar>::mm,
96-
1e-4f * traccc::unit<traccc::scalar>::mm,
97-
1e-3f,
98-
1e-3f,
99-
1e-4f / traccc::unit<traccc::scalar>::GeV,
100-
1e-4f * traccc::unit<traccc::scalar>::ns};
101-
10293
// Propagation configuration
10394
detray::propagation::config propagation_config(propagation_opts);
10495

@@ -136,7 +127,7 @@ int seq_run(const traccc::opts::track_finding& finding_opts,
136127
[&]<typename detector_traits_t>(
137128
const typename detector_traits_t::host& det) {
138129
traccc::seed_generator<typename detector_traits_t::host> sg(
139-
det, stddevs);
130+
det);
140131
evt_data.generate_truth_candidates(truth_track_candidates, sg,
141132
host_mr,
142133
truth_finding_opts.m_pT_min);

examples/run/cpu/truth_fitting_example.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,6 @@ int main(int argc, char* argv[]) {
9191
* Do the reconstruction
9292
*****************************/
9393

94-
/// Standard deviations for seed track parameters
95-
static constexpr std::array<scalar, e_bound_size> stddevs = {
96-
0.03f * traccc::unit<scalar>::mm,
97-
0.03f * traccc::unit<scalar>::mm,
98-
0.017f,
99-
0.017f,
100-
0.001f / traccc::unit<scalar>::GeV,
101-
1.f * traccc::unit<scalar>::ns};
102-
10394
// Fitting algorithm object
10495
traccc::fitting_config fit_cfg(fitting_opts);
10596
fit_cfg.propagation = propagation_opts;
@@ -126,7 +117,7 @@ int main(int argc, char* argv[]) {
126117
const typename detector_traits_t::host& det) {
127118
// Seed generator
128119
traccc::seed_generator<typename detector_traits_t::host> sg(
129-
det, stddevs);
120+
det);
130121
evt_data.generate_truth_candidates(truth_track_candidates, sg,
131122
host_mr);
132123
});

examples/run/cuda/truth_finding_example_cuda.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,6 @@ int seq_run(const traccc::opts::track_finding& finding_opts,
122122
traccc::buffer_from_host_detector(polymorphic_detector, device_mr,
123123
async_copy);
124124

125-
// Standard deviations for seed track parameters
126-
static constexpr std::array<traccc::scalar, traccc::e_bound_size> stddevs =
127-
{1e-4f * traccc::unit<traccc::scalar>::mm,
128-
1e-4f * traccc::unit<traccc::scalar>::mm,
129-
1e-3f,
130-
1e-3f,
131-
1e-4f / traccc::unit<traccc::scalar>::GeV,
132-
1e-4f * traccc::unit<traccc::scalar>::ns};
133-
134125
// Propagation configuration
135126
detray::propagation::config propagation_config(propagation_opts);
136127

@@ -174,7 +165,7 @@ int seq_run(const traccc::opts::track_finding& finding_opts,
174165
const typename detector_traits_t::host& det) {
175166
// Seed generator
176167
traccc::seed_generator<typename detector_traits_t::host> sg(
177-
det, stddevs);
168+
det);
178169
evt_data.generate_truth_candidates(truth_track_candidates, sg,
179170
host_mr,
180171
truth_finding_opts.m_pT_min);

examples/run/cuda/truth_fitting_example_cuda.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,6 @@ int main(int argc, char* argv[]) {
120120
traccc::buffer_from_host_detector(polymorphic_detector, device_mr,
121121
async_copy);
122122

123-
/// Standard deviations for seed track parameters
124-
static constexpr std::array<scalar, e_bound_size> stddevs = {
125-
0.03f * traccc::unit<scalar>::mm,
126-
0.03f * traccc::unit<scalar>::mm,
127-
0.017f,
128-
0.017f,
129-
0.001f / traccc::unit<scalar>::GeV,
130-
1.f * traccc::unit<scalar>::ns};
131-
132123
// Fitting algorithm object
133124
traccc::fitting_config fit_cfg(fitting_opts);
134125
fit_cfg.propagation = propagation_opts;
@@ -159,7 +150,7 @@ int main(int argc, char* argv[]) {
159150
const typename detector_traits_t::host& det) {
160151
// Seed generator
161152
traccc::seed_generator<typename detector_traits_t::host> sg(
162-
det, stddevs);
153+
det);
163154
evt_data.generate_truth_candidates(truth_track_candidates, sg,
164155
host_mr);
165156
});

tests/cpu/test_ckf_combinatorics_telescope.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ TEST_P(CpuCkfCombinatoricsTelescopeTests, Run) {
114114
*****************************/
115115

116116
// Seed generator
117-
seed_generator<host_detector_type> sg(detector.as<detector_traits>(),
118-
stddevs);
117+
seed_generator<host_detector_type> sg(detector.as<detector_traits>());
119118

120119
// Finding algorithm configuration
121120
traccc::finding_config cfg_no_limit;

tests/cpu/test_ckf_sparse_tracks_telescope.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ TEST_P(CkfSparseTrackTelescopeTests, Run) {
126126
*****************************/
127127

128128
// Seed generator
129-
seed_generator<host_detector_type> sg(detector.as<detector_traits>(),
130-
stddevs);
129+
seed_generator<host_detector_type> sg(detector.as<detector_traits>());
131130

132131
// Finding algorithm configuration
133132
typename traccc::finding_config cfg;

tests/cpu/test_kalman_fitter_hole_count.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ TEST_P(KalmanFittingHoleCountTests, Run) {
121121
***************/
122122

123123
// Seed generator
124-
seed_generator<host_detector_type> sg(detector.as<detector_traits>(),
125-
stddevs);
124+
seed_generator<host_detector_type> sg(detector.as<detector_traits>());
126125

127126
// Fitting algorithm object
128127
traccc::fitting_config fit_cfg;

0 commit comments

Comments
 (0)