Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
30 namespace asg
31 {
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 
48  {
49  }
50 
51 
52 
54  initialize ()
55  {
56 #ifndef SIMULATIONBASE
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 
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 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
asg::DataHandleTestTool::m_readKey
SG::ReadHandleKey< xAOD::MuonContainer > m_readKey
Definition: DataHandleTestTool.h:55
WriteHandle.h
Handle class for recording to StoreGate.
asg::DataHandleTestTool::m_writeDecorKey
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_writeDecorKey
Definition: DataHandleTestTool.h:61
asg::AsgTool
Base class for the dual-use tool implementation classes.
Definition: AsgTool.h:47
EXPECT_SUCCESS
#define EXPECT_SUCCESS(x)
Definition: Control/AthToolSupport/AsgTesting/AsgTesting/UnitTest.h:38
asg::DataHandleTestTool::m_doWriteName
std::string m_doWriteName
Definition: DataHandleTestTool.h:67
asg::DataHandleTestTool::DataHandleTestTool
DataHandleTestTool(const std::string &val_name)
standard constructor
Definition: DataHandleTestTool.cxx:33
asg::DataHandleTestTool::m_doWriteDecorName
std::string m_doWriteDecorName
Definition: DataHandleTestTool.h:68
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
ViewHelper::makeHandle
SG::ReadHandle< T > makeHandle(const SG::View *view, const SG::ReadHandleKey< T > &rhKey, const EventContext &context)
navigate from the TrigComposite to nearest view and fetch object from it
Definition: ViewHelper.h:265
asg
Definition: DataHandleTestTool.h:28
SG::ReadDecorHandle::isPresent
bool isPresent() const
Is the referenced container present in SG?
DataVector::get
const T * get(size_type n) const
Access an element, as an rvalue.
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
asg::DataHandleTestTool::m_readDecorKeyEmpty
SG::ReadDecorHandleKey< xAOD::MuonContainer > m_readDecorKeyEmpty
Definition: DataHandleTestTool.h:58
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
asg::DataHandleTestTool::runTest
void runTest() override
run the test
Definition: DataHandleTestTool.cxx:72
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
asg::DataHandleTestTool::m_readDecorFailure
bool m_readDecorFailure
Definition: DataHandleTestTool.h:66
MuonAuxContainer.h
xAOD::MuonAuxContainer_v5
Temporary container used until we have I/O for AuxStoreInternal.
Definition: MuonAuxContainer_v5.h:31
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
asg::DataHandleTestTool::m_readArray
bool m_readArray
Definition: DataHandleTestTool.h:65
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
asg::DataHandleTestTool::m_doWriteDecorNameExisting
std::string m_doWriteDecorNameExisting
Definition: DataHandleTestTool.h:69
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
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
asg::DataHandleTestTool::m_writeDecorKeyExisting
SG::WriteDecorHandleKey< xAOD::MuonContainer > m_writeDecorKeyExisting
Definition: DataHandleTestTool.h:62
asg::DataHandleTestTool::initialize
StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: DataHandleTestTool.cxx:54
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
WriteDecorHandle.h
Handle class for adding a decoration to an object.
asg::DataHandleTestTool::~DataHandleTestTool
~DataHandleTestTool()
standard destructor
Definition: DataHandleTestTool.cxx:47
DataHandleTestTool.h
ReadHandle.h
Handle class for reading from StoreGate.
asg::DataHandleTestTool::m_readDecorKey
SG::ReadDecorHandleKey< xAOD::MuonContainer > m_readDecorKey
Definition: DataHandleTestTool.h:57
ASSERT_SUCCESS
#define ASSERT_SUCCESS(x)
Definition: Control/AthToolSupport/AsgTesting/AsgTesting/UnitTest.h:35
asg::DataHandleTestTool::m_readKeyEmpty
SG::ReadHandleKey< xAOD::MuonContainer > m_readKeyEmpty
Definition: DataHandleTestTool.h:56
ReadDecorHandle.h
Handle class for reading a decoration on an object.
get
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition: hcg.cxx:127
asg::DataHandleTestTool::m_writeKey
SG::WriteHandleKey< xAOD::MuonContainer > m_writeKey
Definition: DataHandleTestTool.h:60
SG::ReadDecorHandle::isAvailable
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
asg::DataHandleTestTool::m_readKeyArray
SG::ReadHandleKeyArray< xAOD::MuonContainer > m_readKeyArray
Definition: DataHandleTestTool.h:59
UnitTest.h
asg::DataHandleTestTool::m_readFailure
bool m_readFailure
Definition: DataHandleTestTool.h:64