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
137{
138 ATH_MSG_INFO ("execute");
139 const EventContext& ctx = Gaudi::Hive::currentContext();
140
141 double vx = 0;
142 double vy = 0;
143 if (!m_priVert.empty()) {
144 if (m_priVert.size() != 2) std::abort();
145 vx = m_priVert[0];
146 vy = m_priVert[1];
147 }
148
149 Amg::Vector3D pos0 { 0, 0, 0 };
150
151 Amg::Vector3D pos1a { 2*mm, 1*mm, -10*mm };
152 Amg::Vector3D mom1a { 400*MeV, 600*MeV, 200*MeV };
153 Amg::Vector3D pos1b { 1*mm, 2*mm, -3*mm };
154 Amg::Vector3D mom1b { 600*MeV, 400*MeV, -200*MeV };
155 Amg::Vector3D pos1c { 1.2*mm, 1.3*mm, -7*mm };
156 Amg::Vector3D mom1c { 300*MeV, 1000*MeV, 100*MeV };
157
158 auto p1a = std::make_unique<Trk::Perigee>(pos1a, mom1a, 1, pos0, cov5());
159 auto p1b = std::make_unique<Trk::Perigee>(pos1b, mom1b, -1, pos0, cov5());
160 auto p1c = std::make_unique<Trk::Perigee>(pos1c, mom1c, -1, pos0, cov5());
161
162 std::vector<const Trk::TrackParameters*> v1a { p1a.get(), p1b.get(), p1c.get() };
163 std::vector<const Trk::TrackParameters*> v1b { p1c.get(), p1a.get(), p1b.get() };
164
165 if (!m_expected1.empty()) {
166 ATH_MSG_INFO ("testing 1");
167 Amg::Vector3D p = m_finder->findSeed (vx, vy, v1a);
168 assertVec3D ("1a", p, m_expected1);
169
170 std::unique_ptr<Trk::IMode3dInfo> info;
171 p = m_finder->findSeed (vx, vy, info, v1b);
172 assertVec3D ("1b", p, m_expected1);
173
174 if (!m_expected1PhiModes.empty()) {
175 std::vector<float> phi;
176 std::vector<float> r;
177 std::vector<float> z;
178 std::vector<float> w;
179 size_t sz = info->Modes1d (phi, r, z, w);
180 assert (sz == phi.size());
181 assert (sz == r.size());
182 assert (sz == z.size());
183 assert (sz == w.size());
184 assertVector ("phiModes", phi, m_expected1PhiModes);
185 assertVector ("rModes", r, m_expected1RModes);
186 assertVector ("zModes", z, m_expected1ZModes);
187 assertVector ("weights", w, m_expected1Weights);
188 }
189
190 if (!m_expected1Indices.empty()) {
191 std::vector<const Trk::TrackParameters*> p;
192 size_t sz = info->perigeesAtSeed (p, v1a);
193 assert (sz == p.size());
194 std::vector<int> ndx;
195 for (const Trk::TrackParameters* pp : p) {
196 auto it = std::find (v1a.begin(), v1a.end(), pp);
197 assert (it != v1a.end());
198 ndx.push_back (it - v1a.begin());
199 }
200 assert (ndx == m_expected1Indices);
201 }
202
203 if (!m_expected1CorrDist.empty()) {
204 double cXY = 0;
205 double cZ = 0;
206 info->getCorrelationDistance (cXY, cZ);
207 assert (Athena_test::isEqual (cXY, m_expected1CorrDist[0], 1e-5));
208 assert (Athena_test::isEqual (cZ, m_expected1CorrDist[1], 1e-5));
209 }
210 }
211
212 xAOD::Vertex vert1;
213 initVertex (vert1);
214
215 if (!m_expected2.empty()) {
216 ATH_MSG_INFO ("testing 2");
217 Amg::Vector3D p = m_finder->findSeed (vx, vy, v1a, &vert1);
218 assertVec3D ("2a", p, m_expected2);
219
220 p = m_finder->findSeed (vx, vy, v1b, &vert1);
221 assertVec3D ("2b", p, m_expected2);
222 }
223
224 std::vector<std::unique_ptr<Trk::Perigee> > perigees;
225 std::vector<const Trk::TrackParameters*> pvec;
226 Athena_test::normal_distribution<double> xdist (1*mm, 0.1*mm);
227 Athena_test::normal_distribution<double> ydist (-0.7*mm, 0.1*mm);
228 Athena_test::normal_distribution<double> z1dist (12*mm, 1*mm);
229 Athena_test::normal_distribution<double> z2dist (-3*mm, 0.5*mm);
234 for (unsigned int i=0; i < m_npart3; i++) {
235 double x = xdist(rng);
236 double y = ydist(rng);
237 double z;
238 if ((i%3) == 0) {
239 z = z2dist(rng);
240 }
241 else {
242 z = z1dist(rng);
243 }
244
245 Amg::Vector3D pos { x, y, z };
246 double pt = ptdist(rng);
247 double phi = phidist(rng);
248 double eta = etadist(rng);
249 Amg::Vector3D mom { pt*cos(phi), pt*sin(phi), pt*sinh(eta) };
250 double charge = etadist(rng) > 0 ? 1 : -1;
251 perigees.emplace_back (std::make_unique<Trk::Perigee> (pos, mom, charge, pos0,
252 cov5()));
253 pvec.push_back (perigees.back().get());
254 }
255
256 if (!m_mcEventCollectionKey.empty()) {
258 }
259
260 if (!m_expected3.empty()) {
261 ATH_MSG_INFO ("testing 3");
262 if (m_expected3.size() == 3) {
263 Amg::Vector3D p = m_finder->findSeed (vx, vy, pvec);
264 assertVec3D ("3a", p, m_expected3);
265 }
266 else {
267 std::vector<Amg::Vector3D> seeds = m_finder->findMultiSeeds (pvec);
268 if (seeds.size() != m_expected3.size()/3) {
269 std::cerr << "VertexSeedFinderTestAlg size mismatch 3b "
270 << seeds.size() << " / " << m_expected3.size()/3 << "\n";
271 std::abort();
272 }
273 int i = 0;
274 for (const auto& seed : seeds) {
275 assertVec3D ("3b", seed, m_expected3, i);
276 i += 3;
277 }
278 }
279 }
280
281 return StatusCode::SUCCESS;
282}
283
284
288StatusCode
290{
291 auto *evt1 = new HepMC::GenEvent();
292 auto *evt2 = new HepMC::GenEvent();
293 auto *evt3 = new HepMC::GenEvent();
294 auto v1 = HepMC::newGenVertexPtr(HepMC::FourVector{1*mm, 2*mm, 12*mm,0.0});
295 auto v2 = HepMC::newGenVertexPtr(HepMC::FourVector{0.3*mm,-0.7*mm,-3*mm,0.0});
296 auto v3 = HepMC::newGenVertexPtr(HepMC::FourVector{0.6*mm, 0.2*mm, 7*mm,0.0});
300
304 Athena_test::URNG rng (123);
305 for (unsigned int i = 0; i < 1000; i++) {
306 double pt = ptdist(rng);
307 double phi = phidist(rng);
308 double eta = etadist(rng);
309 double e = pt*sinh(eta);
310 double charge = etadist(rng) > 0 ? 1 : -1;
311 HepMC::FourVector mom {pt*cos(phi), pt*sin(phi), e, e };
312 auto p = HepMC::newGenParticlePtr ( mom, charge * 11, 1 );
313
314 double vrand = etadist(rng);
315 if (vrand < 0) {
316 HepMC::signal_process_vertex(evt1)->add_particle_out (p);
317 }
318 else if (vrand < 3) {
319 HepMC::signal_process_vertex(evt2)->add_particle_out (p);
320 }
321 else {
322 HepMC::signal_process_vertex(evt3)->add_particle_out (p);
323 }
324 }
325
326 auto evtcoll = std::make_unique<McEventCollection>();
330 evtcoll->push_back (evt1);
331 evtcoll->push_back (evt2);
332 evtcoll->push_back (evt3);
333
335 ATH_CHECK( h.record (std::move (evtcoll)) );
336 return StatusCode::SUCCESS;
337}
338
339
340} // 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 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
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
virtual StatusCode execute() override
Execute the algorithm.
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
void set_signal_process_vertex(GenEvent *e, T v)
Definition GenEvent.h:651
GenVertexPtr newGenVertexPtr(const HepMC::FourVector &pos=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), const int i=0)
Definition GenVertex.h:64
GenParticlePtr newGenParticlePtr(const HepMC::FourVector &mom=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), int pid=0, int status=0)
Definition GenParticle.h:39
void fillBarcodesAttribute(GenEvent *)
Definition GenEvent.h:627
GenVertex * signal_process_vertex(const GenEvent *e)
Definition GenEvent.h:626
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