ATLAS Offline Software
Control/AthenaExamples/AthExStoreGateExample/src/ReadData.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #undef NDEBUG
6 #include "ReadData.h"
7 
8 #include <list>
9 #include <map>
10 #include <vector>
12 #include "MyContObj.h"
13 #include "MapStringFloat.h"
14 #include "StoreGateExample_ClassDEF.h" /*the CLIDs for the containers*/
15 
16 #include "GaudiKernel/ISvcLocator.h"
18 
19 #include "StoreGate/SGIterator.h"
20 #include "AthLinks/ElementLink.h"
21 
23 
25 
26 ReadData::ReadData(const std::string& name, ISvcLocator* pSvcLocator) :
27  AthAlgorithm(name, pSvcLocator)
28 {
29 
30  // Declare the properties
31  declareProperty("DataProducer", m_DataProducer);
32 
33 }
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
36 
38 
39 
40  ATH_MSG_INFO ("in initialize()");
41 
42  // Print out the location of the data objects
43  ATH_MSG_INFO ("Data Object producer: " << m_DataProducer);
44 
45  //locate the StoreGateSvc and initialize our local ptr
46  StatusCode sc = evtStore().retrieve();
47  if (!sc.isSuccess())
48  ATH_MSG_ERROR ("Could not find StoreGateSvc");
49 
52 
54  return sc;
55 }
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
58 
60 
61  // An algorithm, like this one, retrieving an object from the StoreGate(SG)
62  // can either ask for:
63  // i) the most recent (default) object of a given type
64  // ii) a specific object of a given type
65  // iii) all objects of a given type
66  //This example will show how to perform these three tasks
67 
68 
69  ATH_MSG_INFO ("in execute()");
70 
72  // Part 1: retrieving individual objects from SG
73  //
74  // i)Get the most recent (default) object of type MyDataObj
75  // we start by creating a *const* pointer: we won't be able
76  // to modify the MyDataObj we retrieve.
77  //unless you know what you are doing, *always* use const pointers
78  //FIXME const MyDataObj* dobj;
79 
80  //We pass the pointer to the StoreGate that will set it to point to
81  //the default MyDataObj. More precisely the handle will now contain
82  //all infos necessary to access (or create) the default MyDataObj when
83  //we will use it
84  //FIXME new (tag StoreGate-02-12-08) keyless record scheme does not allow it!
85  //FIXME if (StatusCode::SUCCESS != p_SGevent->retrieve(dobj) ) {
86  //FIXME log << MSG::ERROR
87  //FIXME << "Could not find default MyDataObj"
88  //FIXME << endmsg;
89  //FIXME return( StatusCode::FAILURE);
90  //FIXME }
91  //FIXME log << MSG::INFO
92  //FIXME << "default MyDataObj Val: " << dobj->val()
93  //FIXME << endmsg;
94 
96  // ii) Get a specific MyDataObj by providing its key
97  // (in this case the name of the algo which recorded it)
98 
99  ATH_MSG_INFO ("Get the MyDataObj recorded by " << m_DataProducer);
100 
101  //this time we set a *non-const* pointer. *If* we get back a valid
102  //pointer we'll be able to modify the content of the underlying transient obj
103  MyDataObj* dobj3;
104  //Most objects recorded to the SG may not be retrieved
105  //using a non-const handle, unless the original producer of the object
106  //allowed for later modifications invoking StoreGateSvc::record with
107  //the last "allowMods" optional argument = true
108 
109  // We retrieve our MyDataObj, using m_DataProducer as our key
110  if (StatusCode::SUCCESS != evtStore()->retrieve(dobj3, m_DataProducer)) {
111  ATH_MSG_ERROR ("Could not find MyDataObj " << m_DataProducer);
112  return (StatusCode::FAILURE);
113  } else {
114  dobj3->val(4); // should be able to do this
115  }
116 
117  // Also check retrieving it as a const object via ReadHandle.
118  assert (m_dobj3->val() == 4);
119 
120  //not every type can be used as a key.
121  //These requirements are checked at compile time in the record method
122  //using StoreGate/constraints/KeyConcept.h
123  //uncomment to see how the key concept is checked by the compiler
124  // p_SGevent->retrieve(dobj3, 1.0);
125  //concept checking does not work using egcs 1.1 (gcc 2.91)
126  //(but you still get a compiler error message...)
127 
129  // iii) Get all recorded instances of MyDataObj, print out their contents
130 
131  // (a SG::ConstIterator is an iterator class that we also use to defer
132  // and control the access to the persistent object)
135 
136  if (StatusCode::SUCCESS != evtStore()->retrieve(iMyD, eMyD) ) {
137  ATH_MSG_ERROR ("Could not find MyDataObj list");
138  return( StatusCode::FAILURE);
139  }
140 
141  // while(iMyD != eMyD) {
142  // ATH_MSG_INFO ((*iMyD).val());
143  // ++iMyD;
144  // }
145  ATH_MSG_WARNING ("FIXME loop of ConstIterator bombs");
146 
147 
148 
150 // Get the default listof MyContObj, print out its contents
151 
152  //the CLID of list<MyContObj> must be defined using the CLASSDEF
153  //macros. See StoreGateExample_ClassDEF.h for a few examples
154  //If no CLID is defined StoreGate assumes the object is (convertible to a)
155  //DataObject. If this is not the case an error message is issued:
156  //uncomment below to see how your compiler catches an undefined CLID
157  //ERROR p_SGevent->retrieve(errorH);
158 
160 
161  //now let's try again with a const pointer
163 
164  if (StatusCode::SUCCESS != evtStore()->retrieve(list) ) {
165  ATH_MSG_ERROR ("Could not find list of MyContObj");
166  return( StatusCode::FAILURE);
167  } else {
168  ATH_MSG_INFO ("Retrieved DataVector of MyContObj using a const pointer");
169  }
170  for (const MyContObj* obj : *list) {
171  float time = obj->time();
172  int ID = obj->id();
173 
174  ATH_MSG_INFO ("Time: " << time << " ID: " << ID);
175  }
176 
178 
179 // Get the std::vector, print out its contents
180 
181  const std::vector<float>* pVec;
182 
183  if (StatusCode::SUCCESS != evtStore()->retrieve(pVec) ) {
184  ATH_MSG_ERROR ("Could not find vector<int>");
185  return( StatusCode::FAILURE);
186  }
187 
188  for (unsigned int it=0; it<pVec->size(); it++) {
189  ATH_MSG_INFO ("pVec [" << it << "] = " << (*pVec)[it]);
190  }
191 
193  // test if an object is in the store
194  //FIXME if (p_SGevent->contains<MyDataObj>(SG::DEFAULTKEY)) {
195  //FIXME log << MSG::INFO
196  //FIXME <<"event store contains default MyDataObj"
197  //FIXME <<endmsg;
198  //FIXME } else {
199  //FIXME log << MSG::ERROR
200  //FIXME <<"event store claims it does not contain default MyDataObj"
201  //FIXME <<endmsg;
202  //FIXME return( StatusCode::FAILURE);
203  //FIXME }
204 
205  // test if an object is not in the store
206  if (evtStore()->contains<MyDataObj>("FunnyNonExistingKey")) {
208  ("event store claims it contains MyDataObj with FunnyNonExistingKey");
209  return( StatusCode::FAILURE);
210  } else {
212  ("event store does not contain MyDataObj with FunnyNonExistingKey");
213  }
214 
215 
216 
218  // Part 2: retrieving DataLinks
219 
220  // Get the list of links, print out its contents
221 
222  typedef ElementLink<std::vector<float> > VecElemLink;
223  const std::list<VecElemLink>* pList = nullptr;
224 
225  if (!(evtStore()->retrieve(pList)).isSuccess() ) {
226  ATH_MSG_ERROR ("Could not find list of links");
227  return( StatusCode::FAILURE);
228  } else
229  ATH_MSG_INFO ("Retrieved list of links");
230 
231  std::list<VecElemLink>::const_iterator itL= pList->begin();
232  std::list<VecElemLink>::const_iterator endL= pList->end();
233 
234  while(itL != endL) {
235  ATH_MSG_INFO ("ListVecLinks::linked element " << **itL);
236  ++itL;
237  }
238 
239 // Get the vector of links, print out its contents
240 
241  typedef ElementLink<MapStringFloat> MapElemLink;
242  const std::vector<MapElemLink>* vectorHandle;
243 
244  if (StatusCode::SUCCESS != evtStore()->retrieve(vectorHandle) ) {
245  ATH_MSG_ERROR ("Could not find vector of links");
246  return( StatusCode::FAILURE);
247  } else
248  ATH_MSG_INFO ("Retrieved vector of links");
249 
250  std::vector<MapElemLink>::const_iterator itV= vectorHandle->begin();
251  std::vector<MapElemLink>::const_iterator endV= vectorHandle->end();
252  while(itV != endV) {
253  ATH_MSG_INFO
254  ("VectorMapLinks::linked element: key " << itV->index()
255  << " - value " << (**itV)
256  << " - stored as " << *itV);
257  ++itV;
258  }
259 
260  //try to "read back" a link
261  // istrstream istr("due 0");
262  // MapElemLink toBeRead;
263  // istr >> toBeRead;
264  // if (!toBeRead.isValid()) {
265  // log << MSG::ERROR
266  // << "Could not read back MapElement"
267  // << endmsg;
268  // return( StatusCode::FAILURE);
269  // } else {
270  // log << MSG::INFO << "MapElement read back: key " << toBeRead->first
271  // << " value " << toBeRead->second <<endmsg;
272  // }
273 
275  // Part 3: symbolic links and derived types
276  // Get all objects as its base class
277 
280 
281  if (StatusCode::SUCCESS != evtStore()->retrieve(it2, iEnd2) ) {
282 
283  ATH_MSG_ERROR ("Could not find base class list");
284  return( StatusCode::FAILURE);
285  }
286 
287  // not much we can do with an empty base class,
288  // so print the path of its persistable companion (the DataBucket)
289  ATH_MSG_INFO (" retrieve as BaseClass, print its address: ");
290  while(it2 != iEnd2) {
291  ATH_MSG_INFO (&*it2);
292  ++it2;
293  }
294 
296  // Part 4: Get the event header, print out event and run number
297 
299  if(eventInfo.isValid()) {
300  int event = eventInfo->eventNumber();
301  int run = eventInfo->runNumber();
302  ATH_MSG_INFO (" EventInfo : "
303  << " event: " << event
304  << " run: " << run);
305  }
306  else
307  {
308  ATH_MSG_ERROR (" Unable to retrieve EventInfo from StoreGate ");
309  return StatusCode::SUCCESS;
310  }
311 
312  return StatusCode::SUCCESS;
313 }
314 
315 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
316 
318 
319  ATH_MSG_INFO ("in finalize()");
320  return StatusCode::SUCCESS;
321 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
StoreGateExample_ClassDEF.h
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ID
std::vector< Identifier > ID
Definition: CalibHitIDCheck.h:24
xAOD::EventInfo_v1::eventNumber
uint64_t eventNumber() const
The current event's event number.
ReadData.h
This file contains the class definition for the ReadData class.
MyContObj.h
ReadData::ReadData
ReadData(const std::string &name, ISvcLocator *pSvcLocator)
Definition: Control/AthenaExamples/AthExStoreGateExample/src/ReadData.cxx:26
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
SG::VarHandleBase::assign
StatusCode assign(const std::string &sgkey)
Update the underlying key from a string.
Definition: StoreGate/src/VarHandleBase.cxx:635
MyDataObj::val
void val(int i)
Definition: AthExStoreGateExample/MyDataObj.h:24
skel.it
it
Definition: skel.GENtoEVGEN.py:423
ReadData::finalize
StatusCode finalize()
Definition: Control/AthenaExamples/AthExStoreGateExample/src/ReadData.cxx:317
ReadData::m_DataProducer
std::string m_DataProducer
Definition: Control/AthenaExamples/AthExStoreGateExample/src/ReadData.h:29
SGIterator.h
A standard conforming forward iterator over items in StoreGate.
xAOD::EventInfo_v1::runNumber
uint32_t runNumber() const
The current event's run number.
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
MapStringFloat.h
ReadData::m_eventInfo
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfo
Definition: Control/AthenaExamples/AthExStoreGateExample/src/ReadData.h:31
ReadData::execute
StatusCode execute()
Definition: Control/AthenaExamples/AthExStoreGateExample/src/ReadData.cxx:59
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
MyContObj
Definition: MyContObj.h:11
MyDataObj
Definition: AthExStoreGateExample/MyDataObj.h:16
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
run
Definition: run.py:1
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
MyDataObj.h
ReadData::m_dobj3
SG::ReadHandle< MyDataObj > m_dobj3
Definition: Control/AthenaExamples/AthExStoreGateExample/src/ReadData.h:30
DataVector.h
An STL vector of pointers that by default owns its pointed-to elements.
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
SG::VarHandleBase::initialize
StatusCode initialize(bool used=true)
Verify that the handle has been configured properly.
Definition: StoreGate/src/VarHandleBase.cxx:465
ReadData::initialize
StatusCode initialize()
Definition: Control/AthenaExamples/AthExStoreGateExample/src/ReadData.cxx:37
DefaultKey.h
python.PyAthena.obj
obj
Definition: PyAthena.py:135
SG::ConstIterator
Definition: SGIterator.h:163