ATLAS Offline Software
Loading...
Searching...
No Matches
DataHandleTestTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8
9//
10// includes
11//
12
14
19#include <AsgTesting/UnitTest.h>
20#include <map>
21
22#ifndef SIMULATIONBASE
24#endif
25
26//
27// method implementations
28//
29
30namespace asg
31{
32 DataHandleTestTool ::
33 DataHandleTestTool (const std::string& val_name)
34 : AsgTool (val_name)
35 {
36 declareProperty ("readFailure", m_readFailure, "whether to expect a read failure");
37 declareProperty ("readDecorFailure", m_readDecorFailure, "whether to expect a read decoration failure");
38 declareProperty ("readArray", m_readArray, "whether to read from the array");
39 declareProperty ("doWriteName", m_doWriteName, "if we should write, the name we expect to write to");
40 declareProperty ("doWriteDecorName", m_doWriteDecorName, "if we should write a decoration, the name we expect to write to");
41 declareProperty ("doWriteDecorNameExisting", m_doWriteDecorNameExisting, "if we should try to overwrite an existing decoration, the name we expect to write to");
42 }
43
44
45
46 DataHandleTestTool ::
47 ~DataHandleTestTool ()
48 {
49 }
50
51
52
53 StatusCode DataHandleTestTool ::
54 initialize ()
55 {
56#ifndef SIMULATIONBASE
57 ANA_CHECK (m_readKey.initialize ());
58 ANA_CHECK (m_readKeyEmpty.initialize (!m_readKeyEmpty.empty ()));
59 ANA_CHECK (m_readDecorKey.initialize ());
60 ANA_CHECK (m_readDecorKeyEmpty.initialize (!m_readDecorKeyEmpty.empty ()));
61 ANA_CHECK (m_writeKey.initialize (!m_writeKey.empty()));
62 ANA_CHECK (m_readKeyArray.initialize());
63 ANA_CHECK (m_writeDecorKey.initialize (!m_writeDecorKey.empty ()));
65#endif
66 return StatusCode::SUCCESS;
67 }
68
69
70
71 void DataHandleTestTool ::
72 runTest ()
73 {
74#ifndef SIMULATIONBASE
75 const xAOD::MuonContainer *muonsStore {nullptr};
76 ASSERT_SUCCESS (evtStore()->retrieve (muonsStore, "Muons"));
77 ASSERT_NE (0u, muonsStore->size());
78 const xAOD::Muon *testMuon = (*muonsStore)[0];
79
80 auto readHandle = makeHandle (m_readKey);
81 if (m_readFailure == true)
82 {
83 EXPECT_FALSE (readHandle.isPresent());
84 EXPECT_EQ (nullptr, readHandle.get());
85 EXPECT_FALSE (readHandle.isValid());
86 } else
87 {
88 EXPECT_TRUE (readHandle.isPresent());
89 EXPECT_EQ (muonsStore, readHandle.get());
90 EXPECT_TRUE (readHandle.isValid());
91 }
92
94 if (m_readDecorFailure == true)
95 {
96 EXPECT_TRUE(readDecorHandle.isPresent());
97 EXPECT_FALSE(readDecorHandle.isAvailable());
98 EXPECT_ANY_THROW (readDecorHandle (*testMuon));
99 } else
100 {
101 EXPECT_TRUE(readDecorHandle.isPresent());
102 EXPECT_TRUE(readDecorHandle.isAvailable());
104 EXPECT_EQ (acc (*testMuon), readDecorHandle (*testMuon));
105 }
106
107 if (m_readArray)
108 {
109 EXPECT_EQ (1u, m_readKeyArray.size());
110 auto handles = m_readKeyArray.makeHandles();
111 EXPECT_EQ (muonsStore, handles[0].get());
112 } else
113 {
114 EXPECT_EQ (0u, m_readKeyArray.size());
115 }
116
117 if (!m_doWriteName.empty())
118 {
119 auto writeHandle = makeHandle (m_writeKey);
120 auto newMuons = std::make_unique<xAOD::MuonContainer>();
121 auto newAux = std::make_unique<xAOD::MuonAuxContainer>();
122 xAOD::MuonContainer *recordMuons {newMuons.get()};
123 xAOD::MuonAuxContainer *recordAux {newAux.get()};
124 EXPECT_SUCCESS (writeHandle.record (std::move (newMuons), std::move (newAux)));
125 const xAOD::MuonContainer *retrieveMuons {nullptr};
126 EXPECT_SUCCESS (evtStore()->retrieve (retrieveMuons, m_doWriteName));
127 EXPECT_EQ (recordMuons, retrieveMuons);
128 xAOD::MuonAuxContainer *retrieveAux {nullptr};
129 EXPECT_SUCCESS (evtStore()->retrieve (retrieveAux, m_doWriteName + "Aux."));
130 EXPECT_EQ (recordAux, retrieveAux);
131 }
132
133 if (!m_doWriteDecorName.empty())
134 {
135 auto writeDecorHandle = SG::makeHandle<unsigned> (m_writeDecorKey);
136 EXPECT_TRUE(writeDecorHandle.isPresent());
137 EXPECT_FALSE(writeDecorHandle.isAvailable());
138 writeDecorHandle (*(*muonsStore)[0]) = 42u;
140 EXPECT_EQ (42u, acc (*(*muonsStore)[0]));
141 }
142
143 if (!m_doWriteDecorNameExisting.empty())
144 {
145 auto writeDecorHandleExisting = SG::makeHandle<float> (m_writeDecorKeyExisting);
146 EXPECT_TRUE(writeDecorHandleExisting.isPresent());
147 EXPECT_TRUE(writeDecorHandleExisting.isAvailable());
148 }
149#endif
150 }
151}
Handle class for reading a decoration on an object.
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
Handle class for recording to StoreGate.
#define ANA_CHECK(EXP)
check whether the given expression was successful
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
const T * get(size_type n) const
Access an element, as an rvalue.
size_type size() const noexcept
Returns the number of elements in the collection.
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:569
Handle class for reading a decoration on an object.
bool isPresent() const
Is the referenced container present in SG?
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:130
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Muon_v1 Muon
Reference the current persistent version:
MuonAuxContainer_v5 MuonAuxContainer
Definition of the current Muon auxiliary container.
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
SG::ReadHandleKey< xAOD::MuonContainer > m_readKey
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_writeDecorKeyExisting
SG::ReadDecorHandleKey< xAOD::MuonContainer > m_readDecorKey
SG::ReadDecorHandleKey< xAOD::MuonContainer > m_readDecorKeyEmpty
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_writeDecorKey
SG::WriteHandleKey< xAOD::MuonContainer > m_writeKey
SG::ReadHandleKeyArray< xAOD::MuonContainer > m_readKeyArray
SG::ReadHandleKey< xAOD::MuonContainer > m_readKeyEmpty