ATLAS Offline Software
DataHandleTestTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 
9 //
10 // includes
11 //
12 
15 
17 
22 #include <AsgTesting/UnitTest.h>
23 #include <gtest/gtest.h>
24 #include <map>
25 
26 #ifndef SIMULATIONBASE
28 #endif
29 
30 //
31 // method implementations
32 //
33 
34 namespace asg
35 {
37  DataHandleTestTool (const std::string& val_name)
38  : AsgTool (val_name)
39  {
40  declareProperty ("readFailure", m_readFailure, "whether to expect a read failure");
41  declareProperty ("readDecorFailure", m_readDecorFailure, "whether to expect a read decoration failure");
42  declareProperty ("readArray", m_readArray, "whether to read from the array");
43  declareProperty ("doWriteName", m_doWriteName, "if we should write, the name we expect to write to");
44  declareProperty ("doWriteDecorName", m_doWriteDecorName, "if we should write a decoration, the name we expect to write to");
45  declareProperty ("doWriteDecorNameExisting", m_doWriteDecorNameExisting, "if we should try to overwrite an existing decoration, the name we expect to write to");
46  }
47 
48 
49 
52  {
53  }
54 
55 
56 
58  initialize ()
59  {
60 #ifndef SIMULATIONBASE
61  ANA_CHECK (m_readKey.initialize ());
62  ANA_CHECK (m_readKeyEmpty.initialize (!m_readKeyEmpty.empty ()));
63  ANA_CHECK (m_readDecorKey.initialize ());
64  ANA_CHECK (m_readDecorKeyEmpty.initialize (!m_readDecorKeyEmpty.empty ()));
65  ANA_CHECK (m_writeKey.initialize (!m_writeKey.empty()));
66  ANA_CHECK (m_readKeyArray.initialize());
67  ANA_CHECK (m_writeDecorKey.initialize (!m_writeDecorKey.empty ()));
69 #endif
70  return StatusCode::SUCCESS;
71  }
72 
73 
74 
75  void DataHandleTestTool ::
77  {
78 #ifndef SIMULATIONBASE
79  const xAOD::MuonContainer *muonsStore {nullptr};
80  ASSERT_SUCCESS (evtStore()->retrieve (muonsStore, "Muons"));
81  ASSERT_NE (0u, muonsStore->size());
82  const xAOD::Muon *testMuon = (*muonsStore)[0];
83 
84  auto readHandle = makeHandle (m_readKey);
85  if (m_readFailure == true)
86  {
87  EXPECT_FALSE (readHandle.isPresent());
88  EXPECT_EQ (nullptr, readHandle.get());
89  EXPECT_FALSE (readHandle.isValid());
90  } else
91  {
92  EXPECT_TRUE (readHandle.isPresent());
93  EXPECT_EQ (muonsStore, readHandle.get());
94  EXPECT_TRUE (readHandle.isValid());
95  }
96 
97  SG::ReadDecorHandle<xAOD::MuonContainer,float> readDecorHandle (m_readDecorKey);
98  if (m_readDecorFailure == true)
99  {
100  EXPECT_TRUE(readDecorHandle.isPresent());
101  EXPECT_FALSE(readDecorHandle.isAvailable());
102  EXPECT_ANY_THROW (readDecorHandle (*testMuon));
103  } else
104  {
105  EXPECT_TRUE(readDecorHandle.isPresent());
106  EXPECT_TRUE(readDecorHandle.isAvailable());
108  EXPECT_EQ (acc (*testMuon), readDecorHandle (*testMuon));
109  }
110 
111  if (m_readArray)
112  {
113  EXPECT_EQ (1u, m_readKeyArray.size());
114  auto handles = m_readKeyArray.makeHandles();
115  EXPECT_EQ (muonsStore, handles[0].get());
116  } else
117  {
118  EXPECT_EQ (0u, m_readKeyArray.size());
119  }
120 
121  if (!m_doWriteName.empty())
122  {
123  auto writeHandle = makeHandle (m_writeKey);
124  auto newMuons = std::make_unique<xAOD::MuonContainer>();
125  auto newAux = std::make_unique<xAOD::MuonAuxContainer>();
126  xAOD::MuonContainer *recordMuons {newMuons.get()};
127  xAOD::MuonAuxContainer *recordAux {newAux.get()};
128  EXPECT_SUCCESS (writeHandle.record (std::move (newMuons), std::move (newAux)));
129  const xAOD::MuonContainer *retrieveMuons {nullptr};
130  EXPECT_SUCCESS (evtStore()->retrieve (retrieveMuons, m_doWriteName));
131  EXPECT_EQ (recordMuons, retrieveMuons);
132  xAOD::MuonAuxContainer *retrieveAux {nullptr};
133  EXPECT_SUCCESS (evtStore()->retrieve (retrieveAux, m_doWriteName + "Aux."));
134  EXPECT_EQ (recordAux, retrieveAux);
135  }
136 
137  if (!m_doWriteDecorName.empty())
138  {
139  auto writeDecorHandle = SG::makeHandle<unsigned> (m_writeDecorKey);
140  EXPECT_TRUE(writeDecorHandle.isPresent());
141  EXPECT_FALSE(writeDecorHandle.isAvailable());
142  writeDecorHandle (*(*muonsStore)[0]) = 42u;
143  SG::AuxElement::ConstAccessor<unsigned> acc (m_doWriteDecorName);
144  EXPECT_EQ (42u, acc (*(*muonsStore)[0]));
145  }
146 
147  if (!m_doWriteDecorNameExisting.empty())
148  {
149  auto writeDecorHandleExisting = SG::makeHandle<float> (m_writeDecorKeyExisting);
150  EXPECT_TRUE(writeDecorHandleExisting.isPresent());
151  EXPECT_TRUE(writeDecorHandleExisting.isAvailable());
152  }
153 #endif
154  }
155 }
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::ATLAS_NOT_THREAD_SAFE
void DataHandleTestTool ::runTest ATLAS_NOT_THREAD_SAFE()
Definition: DataHandleTestTool.cxx:76
asg::DataHandleTestTool::DataHandleTestTool
DataHandleTestTool(const std::string &val_name)
standard constructor
Definition: DataHandleTestTool.cxx:37
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:259
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
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
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
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
ATLAS_NO_CHECK_FILE_THREAD_SAFETY
Definition: DataHandleTestTool.cxx:14
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
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:58
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
WriteDecorHandle.h
Handle class for adding a decoration to an object.
asg::DataHandleTestTool::~DataHandleTestTool
~DataHandleTestTool()
standard destructor
Definition: DataHandleTestTool.cxx:51
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
checker_macros.h
Define macros for attributes used to control the static checker.
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