ATLAS Offline Software
Loading...
Searching...
No Matches
VertexSeedFinderTestAlg.cxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration.
3 */
10
11
13#include "GaudiKernel/SystemOfUnits.h"
15//undefine NDEBUG after EDM
16#undef NDEBUG
19#include "TestTools/random.h"
20#include <cassert>
21#include <cmath>
22
23
24using Gaudi::Units::mm;
25using Gaudi::Units::MeV;
26using Gaudi::Units::GeV;
27
28
29namespace {
30
31
32AmgSymMatrix(5) cov5()
33{
34 AmgSymMatrix(5) m;
35 m.setIdentity();
36 return m;
37}
38
39
40void initVertex (xAOD::Vertex& v)
41{
42 v.makePrivateStore();
43 v.setX (1.7);
44 v.setY (1.3);
45 v.setZ (-6);
46
47 AmgSymMatrix(3) cov;
48 cov.setIdentity();
49 v.setCovariancePosition (cov);
50}
51
52
53#if 0
54std::ostream& printVec3D (const Amg::Vector3D& a)
55{
56 std::cout << a.x() << " " << a.y() << " " << a.z();
57 return std::cout;
58}
59#endif
60void assertVec3D (const char* which,
61 const Amg::Vector3D& a,
62 const std::vector<double>& v,
63 int offs = 0)
64{
65 if (v.size() - offs < 3) std::abort();
66 if ( ! Athena_test::isEqual (a.x(), v[offs+0], 1e-5) ||
67 ! Athena_test::isEqual (a.y(), v[offs+1], 1e-5) ||
68 ! Athena_test::isEqual (a.z(), v[offs+2], 1e-5) )
69 {
70 std::cerr << "VertexSeedFinderTestAlg::assertVec3D mismatch " << which
71 << ": ["
72 << a.x() << ", "
73 << a.y() << ", "
74 << a.z() << "] / ["
75 << v[offs+0] << ", "
76 << v[offs+1] << ", "
77 << v[offs+2] << "]\n";
78 std::abort();
79 }
80}
81
82
83void dumpVector (const std::vector<float>& v)
84{
85 std::cerr << " [";
86 for (float f : v) {
87 std::cerr << f << ", ";
88 }
89 std::cerr << "]\n";
90}
91void failVector (const char* which,
92 const std::vector<float>& a,
93 const std::vector<float>& b)
94{
95 std::cerr << "VertexSeedFinderTestAlg::assertVector mismatch " << which
96 << "\n";
97 dumpVector (a);
98 dumpVector (b);
99 std::abort();
100}
101void assertVector (const char* which,
102 const std::vector<float>& a,
103 const std::vector<float>& b)
104{
105 if (a.size() != b.size()) {
106 failVector (which, a, b);
107 }
108 for (size_t i=0; i < a.size(); i++) {
109 if (! Athena_test::isEqual (a[i], b[i], 1e-5) ) {
110 failVector (which, a, b);
111 }
112 }
113}
114
115
116} // anonymous namespace
117
118
119namespace Trk {
120
121
126{
127 ATH_CHECK( m_finder.retrieve() );
129 return StatusCode::SUCCESS;
130}
131
132
136StatusCode VertexSeedFinderTestAlg::execute(const EventContext& ctx)
137{
138 ATH_MSG_INFO ("execute");
139
140 double vx = 0;
141 double vy = 0;
142 if (!m_priVert.empty()) {
143 if (m_priVert.size() != 2) std::abort();
144 vx = m_priVert[0];
145 vy = m_priVert[1];
146 }
147
148 Amg::Vector3D pos0 { 0, 0, 0 };
149
150 Amg::Vector3D pos1a { 2*mm, 1*mm, -10*mm };
151 Amg::Vector3D mom1a { 400*MeV, 600*MeV, 200*MeV };
152 Amg::Vector3D pos1b { 1*mm, 2*mm, -3*mm };
153 Amg::Vector3D mom1b { 600*MeV, 400*MeV, -200*MeV };
154 Amg::Vector3D pos1c { 1.2*mm, 1.3*mm, -7*mm };
155 Amg::Vector3D mom1c { 300*MeV, 1000*MeV, 100*MeV };
156
157 auto p1a = std::make_unique<Trk::Perigee>(pos1a, mom1a, 1, pos0, cov5());
158 auto p1b = std::make_unique<Trk::Perigee>(pos1b, mom1b, -1, pos0, cov5());
159 auto p1c = std::make_unique<Trk::Perigee>(pos1c, mom1c, -1, pos0, cov5());
160
161 std::vector<const Trk::TrackParameters*> v1a { p1a.get(), p1b.get(), p1c.get() };
162 std::vector<const Trk::TrackParameters*> v1b { p1c.get(), p1a.get(), p1b.get() };
163
164 if (!m_expected1.empty()) {
165 ATH_MSG_INFO ("testing 1");
166 Amg::Vector3D p = m_finder->findSeed (vx, vy, v1a);
167 assertVec3D ("1a", p, m_expected1);
168
169 std::unique_ptr<Trk::IMode3dInfo> info;
170 p = m_finder->findSeed (vx, vy, info, v1b);
171 assertVec3D ("1b", p, m_expected1);
172
173 if (!m_expected1PhiModes.empty()) {
174 std::vector<float> phi;
175 std::vector<float> r;
176 std::vector<float> z;
177 std::vector<float> w;
178 size_t sz = info->Modes1d (phi, r, z, w);
179 assert (sz == phi.size());
180 assert (sz == r.size());
181 assert (sz == z.size());
182 assert (sz == w.size());
183 assertVector ("phiModes", phi, m_expected1PhiModes);
184 assertVector ("rModes", r, m_expected1RModes);
185 assertVector ("zModes", z, m_expected1ZModes);
186 assertVector ("weights", w, m_expected1Weights);
187 }
188
189 if (!m_expected1Indices.empty()) {
190 std::vector<const Trk::TrackParameters*> p;
191 size_t sz = info->perigeesAtSeed (p, v1a);
192 assert (sz == p.size());
193 std::vector<int> ndx;
194 for (const Trk::TrackParameters* pp : p) {
195 auto it = std::find (v1a.begin(), v1a.end(), pp);
196 assert (it != v1a.end());
197 ndx.push_back (it - v1a.begin());
198 }
199 assert (ndx == m_expected1Indices);
200 }
201
202 if (!m_expected1CorrDist.empty()) {
203 double cXY = 0;
204 double cZ = 0;
205 info->getCorrelationDistance (cXY, cZ);
206 assert (Athena_test::isEqual (cXY, m_expected1CorrDist[0], 1e-5));
207 assert (Athena_test::isEqual (cZ, m_expected1CorrDist[1], 1e-5));
208 }
209 }
210
211 xAOD::Vertex vert1;
212 initVertex (vert1);
213
214 if (!m_expected2.empty()) {
215 ATH_MSG_INFO ("testing 2");
216 Amg::Vector3D p = m_finder->findSeed (vx, vy, v1a, &vert1);
217 assertVec3D ("2a", p, m_expected2);
218
219 p = m_finder->findSeed (vx, vy, v1b, &vert1);
220 assertVec3D ("2b", p, m_expected2);
221 }
222
223 std::vector<std::unique_ptr<Trk::Perigee> > perigees;
224 std::vector<const Trk::TrackParameters*> pvec;
225 Athena_test::normal_distribution<double> xdist (1*mm, 0.1*mm);
226 Athena_test::normal_distribution<double> ydist (-0.7*mm, 0.1*mm);
227 Athena_test::normal_distribution<double> z1dist (12*mm, 1*mm);
228 Athena_test::normal_distribution<double> z2dist (-3*mm, 0.5*mm);
233 for (unsigned int i=0; i < m_npart3; i++) {
234 double x = xdist(rng);
235 double y = ydist(rng);
236 double z;
237 if ((i%3) == 0) {
238 z = z2dist(rng);
239 }
240 else {
241 z = z1dist(rng);
242 }
243
244 Amg::Vector3D pos { x, y, z };
245 double pt = ptdist(rng);
246 double phi = phidist(rng);
247 double eta = etadist(rng);
248 Amg::Vector3D mom { pt*cos(phi), pt*sin(phi), pt*sinh(eta) };
249 double charge = etadist(rng) > 0 ? 1 : -1;
250 perigees.emplace_back (std::make_unique<Trk::Perigee> (pos, mom, charge, pos0,
251 cov5()));
252 pvec.push_back (perigees.back().get());
253 }
254
255 if (!m_mcEventCollectionKey.empty()) {
257 }
258
259 if (!m_expected3.empty()) {
260 ATH_MSG_INFO ("testing 3");
261 if (m_expected3.size() == 3) {
262 Amg::Vector3D p = m_finder->findSeed (vx, vy, pvec);
263 assertVec3D ("3a", p, m_expected3);
264 }
265 else {
266 std::vector<Amg::Vector3D> seeds = m_finder->findMultiSeeds (pvec);
267 if (seeds.size() != m_expected3.size()/3) {
268 std::cerr << "VertexSeedFinderTestAlg size mismatch 3b "
269 << seeds.size() << " / " << m_expected3.size()/3 << "\n";
270 std::abort();
271 }
272 int i = 0;
273 for (const auto& seed : seeds) {
274 assertVec3D ("3b", seed, m_expected3, i);
275 i += 3;
276 }
277 }
278 }
279
280 return StatusCode::SUCCESS;
281}
282
283
287StatusCode
289{
290 auto *evt1 = new HepMC::GenEvent();
291 auto *evt2 = new HepMC::GenEvent();
292 auto *evt3 = new HepMC::GenEvent();
293 auto v1 = HepMC::newGenVertexPtr(HepMC::FourVector{1*mm, 2*mm, 12*mm,0.0});
294 auto v2 = HepMC::newGenVertexPtr(HepMC::FourVector{0.3*mm,-0.7*mm,-3*mm,0.0});
295 auto v3 = HepMC::newGenVertexPtr(HepMC::FourVector{0.6*mm, 0.2*mm, 7*mm,0.0});
299
303 Athena_test::URNG rng (123);
304 for (unsigned int i = 0; i < 1000; i++) {
305 double pt = ptdist(rng);
306 double phi = phidist(rng);
307 double eta = etadist(rng);
308 double e = pt*sinh(eta);
309 double charge = etadist(rng) > 0 ? 1 : -1;
310 HepMC::FourVector mom {pt*cos(phi), pt*sin(phi), e, e };
311 auto p = HepMC::newGenParticlePtr ( mom, charge * 11, 1 );
312
313 double vrand = etadist(rng);
314 if (vrand < 0) {
315 HepMC::signal_process_vertex(evt1)->add_particle_out (p);
316 }
317 else if (vrand < 3) {
318 HepMC::signal_process_vertex(evt2)->add_particle_out (p);
319 }
320 else {
321 HepMC::signal_process_vertex(evt3)->add_particle_out (p);
322 }
323 }
324
325 auto evtcoll = std::make_unique<McEventCollection>();
329 evtcoll->push_back (evt1);
330 evtcoll->push_back (evt2);
331 evtcoll->push_back (evt3);
332
334 ATH_CHECK( h.record (std::move (evtcoll)) );
335 return StatusCode::SUCCESS;
336}
337
338
339} // namespace Trk
#define M_PI
Scalar eta() const
pseudorapidity method
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_INFO(x)
double charge(const T &p)
Definition AtlasPID.h:997
#define AmgSymMatrix(dim)
boost::graph_traits< boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS > >::vertex_descriptor Vertex
std::array< fp_t, 2 > pvec
static const double MeV
static Double_t sz
static Double_t a
Handle class for recording to StoreGate.
Algorithm for doing unit tests of the seed finder tools. Basic test only, not terribly comprehensive.
Header file for AthHistogramAlgorithm.
Gaudi::Property< std::vector< float > > m_expected1RModes
virtual StatusCode execute(const EventContext &ctx) override
Execute the algorithm.
Gaudi::Property< std::vector< double > > m_expected1CorrDist
ToolHandle< Trk::IVertexSeedFinder > m_finder
Gaudi::Property< std::vector< double > > m_priVert
virtual StatusCode initialize() override
Standard Gaudi initialize method.
Gaudi::Property< std::vector< float > > m_expected1PhiModes
Gaudi::Property< unsigned int > m_npart3
Gaudi::Property< std::vector< double > > m_expected2
Gaudi::Property< std::vector< float > > m_expected1Weights
StatusCode makeMcEventCollection(const EventContext &ctx) const
Make a test McEventCollection.
Gaudi::Property< std::vector< int > > m_expected1Indices
Gaudi::Property< std::vector< float > > m_expected1ZModes
SG::WriteHandleKey< McEventCollection > m_mcEventCollectionKey
Gaudi::Property< std::vector< double > > m_expected1
Gaudi::Property< std::vector< double > > m_expected3
int r
Definition globals.cxx:22
Eigen::Matrix< double, 3, 1 > Vector3D
bool isEqual(double x1, double x2, double thresh=1e-6)
Definition FLOATassert.h:26
HepMC3::FourVector FourVector
void set_signal_process_vertex(GenEvent *e, T &v)
Definition GenEvent.h:591
ConstGenVertexPtr signal_process_vertex(const GenEvent *e)
Definition GenEvent.h:597
GenParticlePtr newGenParticlePtr(const HepMC3::FourVector &mom=HepMC3::FourVector::ZERO_VECTOR(), int pid=0, int status=0)
Definition GenParticle.h:21
void fillBarcodesAttribute(GenEvent *e)
Definition GenEvent.h:393
GenVertexPtr newGenVertexPtr(const HepMC3::FourVector &pos=HepMC3::FourVector::ZERO_VECTOR(), const int i=0)
Definition GenVertex.h:25
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
Ensure that the ATLAS eigen extensions are properly loaded.
@ x
Definition ParamDefs.h:55
@ z
global position (cartesian)
Definition ParamDefs.h:57
@ y
Definition ParamDefs.h:56
@ phi
Definition ParamDefs.h:75
ParametersBase< TrackParametersDim, Charged > TrackParameters
which(filename, env=os.environ)
UNIX-style which ---------------------------------------------------------—.
Definition unixtools.py:39
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Vertex_v1 Vertex
Define the latest version of the vertex class.
Very simple random numbers for regression testing.
Generator compatible with the C++11 STL UniformRandomNumberGenerator.
Definition random.h:78