ATLAS Offline Software
VertexSeedFinderTestAlg.cxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration.
3  */
13 #include "GaudiKernel/SystemOfUnits.h"
14 #include "StoreGate/WriteHandle.h"
15 //undefine NDEBUG after EDM
16 #undef NDEBUG
18 #include "TestTools/FLOATassert.h"
19 #include "TestTools/random.h"
20 #include <cassert>
21 #include <cmath>
22 
23 
24 using Gaudi::Units::mm;
25 using Gaudi::Units::MeV;
26 using Gaudi::Units::GeV;
27 
28 
29 namespace {
30 
31 
32 AmgSymMatrix(5) cov5()
33 {
34  AmgSymMatrix(5) m;
35  m.setIdentity();
36  return m;
37 }
38 
39 
40 void 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
54 std::ostream& printVec3D (const Amg::Vector3D& a)
55 {
56  std::cout << a.x() << " " << a.y() << " " << a.z();
57  return std::cout;
58 }
59 #endif
60 void 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 
83 void 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 }
91 void 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 }
101 void 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 
119 namespace 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;
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 
289 VertexSeedFinderTestAlg::makeMcEventCollection (const EventContext& ctx) const
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
grepfile.info
info
Definition: grepfile.py:38
Trk::VertexSeedFinderTestAlg::m_expected1Indices
Gaudi::Property< std::vector< int > > m_expected1Indices
Definition: VertexSeedFinderTestAlg.h:63
Trk::y
@ y
Definition: ParamDefs.h:62
beamspotman.r
def r
Definition: beamspotman.py:676
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
fitman.sz
sz
Definition: fitman.py:527
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
Trk::z
@ z
global position (cartesian)
Definition: ParamDefs.h:63
Trk::VertexSeedFinderTestAlg::m_npart3
Gaudi::Property< unsigned int > m_npart3
Definition: VertexSeedFinderTestAlg.h:68
xAOD::Vertex
Vertex_v1 Vertex
Define the latest version of the vertex class.
Definition: Event/xAOD/xAODTracking/xAODTracking/Vertex.h:16
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
Trk::VertexSeedFinderTestAlg::m_mcEventCollectionKey
SG::WriteHandleKey< McEventCollection > m_mcEventCollectionKey
Definition: VertexSeedFinderTestAlg.h:71
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:79
Trk::VertexSeedFinderTestAlg::initialize
virtual StatusCode initialize() override
Standard Gaudi initialize method.
Definition: VertexSeedFinderTestAlg.cxx:125
python.SystemOfUnits.MeV
int MeV
Definition: SystemOfUnits.py:154
Trk::VertexSeedFinderTestAlg::m_expected1Weights
Gaudi::Property< std::vector< float > > m_expected1Weights
Definition: VertexSeedFinderTestAlg.h:61
skel.it
it
Definition: skel.GENtoEVGEN.py:423
plotBeamSpotVxVal.cov
cov
Definition: plotBeamSpotVxVal.py:201
test_pyathena.pt
pt
Definition: test_pyathena.py:11
Trk::VertexSeedFinderTestAlg::m_expected1RModes
Gaudi::Property< std::vector< float > > m_expected1RModes
Definition: VertexSeedFinderTestAlg.h:57
M_PI
#define M_PI
Definition: ActiveFraction.h:11
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
drawFromPickle.cos
cos
Definition: drawFromPickle.py:36
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
D3PDTest::rng
uint32_t rng()
Definition: FillerAlg.cxx:40
Trk::VertexSeedFinderTestAlg::m_expected2
Gaudi::Property< std::vector< double > > m_expected2
Definition: VertexSeedFinderTestAlg.h:50
HepMC::fillBarcodesAttribute
void fillBarcodesAttribute(GenEvent *)
Definition: GenEvent.h:504
AmgSymMatrix
#define AmgSymMatrix(dim)
Definition: EventPrimitives.h:52
Athena_test::normal_distribution
Definition: random.h:102
Trk::VertexSeedFinderTestAlg::m_expected1PhiModes
Gaudi::Property< std::vector< float > > m_expected1PhiModes
Definition: VertexSeedFinderTestAlg.h:55
Trk::VertexSeedFinderTestAlg::m_expected3
Gaudi::Property< std::vector< double > > m_expected3
Definition: VertexSeedFinderTestAlg.h:52
Trk::VertexSeedFinderTestAlg::m_expected1CorrDist
Gaudi::Property< std::vector< double > > m_expected1CorrDist
Definition: VertexSeedFinderTestAlg.h:65
Trk::VertexSeedFinderTestAlg::m_finder
ToolHandle< Trk::IVertexSeedFinder > m_finder
Definition: VertexSeedFinderTestAlg.h:74
Trk::VertexSeedFinderTestAlg::execute
virtual StatusCode execute() override
Execute the algorithm.
Definition: VertexSeedFinderTestAlg.cxx:136
random.h
Very simple random numbers for regression testing.
WriteHandle.h
Handle class for recording to StoreGate.
HepMC::set_signal_process_vertex
void set_signal_process_vertex(GenEvent *e, T v)
Definition: GenEvent.h:528
ParticleGun_EoverP_Config.mom
mom
Definition: ParticleGun_EoverP_Config.py:63
HepMC::newGenVertexPtr
GenVertexPtr newGenVertexPtr(const HepMC::FourVector &pos=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), const int i=0)
Definition: GenVertex.h:64
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
extractSporadic.h
list h
Definition: extractSporadic.py:97
Athena_test::isEqual
bool isEqual(double x1, double x2, double thresh=1e-6)
Definition: FLOATassert.h:26
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::VertexSeedFinderTestAlg::makeMcEventCollection
StatusCode makeMcEventCollection(const EventContext &ctx) const
Make a test McEventCollection.
Definition: VertexSeedFinderTestAlg.cxx:289
Trk::ParametersBase
Definition: ParametersBase.h:55
Trk::VertexSeedFinderTestAlg::m_expected1ZModes
Gaudi::Property< std::vector< float > > m_expected1ZModes
Definition: VertexSeedFinderTestAlg.h:59
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
python.Utils.unixtools.which
def which(filename, env=os.environ)
UNIX-style which ---------------------------------------------------------—.
Definition: unixtools.py:39
Trk::VertexSeedFinderTestAlg::m_priVert
Gaudi::Property< std::vector< double > > m_priVert
Definition: VertexSeedFinderTestAlg.h:46
VertexSeedFinderTestAlg.h
Algorithm for doing unit tests of the seed finder tools. Basic test only, not terribly comprehensive.
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
charge
double charge(const T &p)
Definition: AtlasPID.h:494
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
python.SystemOfUnits.mm
int mm
Definition: SystemOfUnits.py:83
ReadCellNoiseFromCoolCompare.v2
v2
Definition: ReadCellNoiseFromCoolCompare.py:364
python.PyAthena.v
v
Definition: PyAthena.py:157
SG::WriteHandle< McEventCollection >
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
a
TList * a
Definition: liststreamerinfos.cxx:10
HepMC::newGenParticlePtr
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
FLOATassert.h
h
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
IMode3dFinder.h
Athena_test::URNG
Generator compatible with the C++11 STL UniformRandomNumberGenerator.
Definition: random.h:78
Athena_test::uniform_real_distribution
Definition: random.h:133
pvec
std::array< fp_t, 2 > pvec
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:9
Trk::phi
@ phi
Definition: ParamDefs.h:81
Trk::VertexSeedFinderTestAlg::m_expected1
Gaudi::Property< std::vector< double > > m_expected1
Definition: VertexSeedFinderTestAlg.h:48
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
Trk::x
@ x
Definition: ParamDefs.h:61
SG::AllowEmpty
@ AllowEmpty
Definition: StoreGate/StoreGate/VarHandleKey.h:30
GeV
#define GeV
Definition: CaloTransverseBalanceVecMon.cxx:30
HepMC::signal_process_vertex
GenVertex * signal_process_vertex(const GenEvent *e)
Definition: GenEvent.h:503