ATLAS Offline Software
ReadDataReentrant.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
14 #undef NDEBUG
15 #include "ReadDataReentrant.h"
16 
17 #include <list>
18 #include <map>
19 #include <vector>
21 #include "MyContObj.h"
22 #include "MapStringFloat.h"
23 #include "StoreGateExample_ClassDEF.h" /*the CLIDs for the containers*/
24 
25 #include "GaudiKernel/ISvcLocator.h"
27 
28 #include "StoreGate/SGIterator.h"
29 #include "StoreGate/UpdateHandle.h"
30 #include "StoreGate/ReadHandle.h"
31 #include "AthLinks/ElementLink.h"
32 
35 
37 
38 ReadDataReentrant::ReadDataReentrant(const std::string& name, ISvcLocator* pSvcLocator) :
39  AthReentrantAlgorithm(name, pSvcLocator)
40 {
41 
42  declareProperty ("CObjKey", m_cobjKey = std::string("cobj"));
43  declareProperty ("VFloatKey", m_vFloatKey = std::string("vFloat"));
44  declareProperty ("PLinkListKey", m_pLinkListKey = std::string("WriteDataReentrant"));
45  declareProperty ("LinkVectorKey", m_linkVectorKey = std::string("linkvec"));
46  declareProperty ("TestObjectKey", m_testObjectKey = "testobj");
47  declareProperty ("DObjKeyArray", m_dobjKeyArray = {"dobj_a1", "dobj_a2"});
48 }
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
51 
53 {
55 
56  ATH_MSG_INFO ("in initialize()");
57 
64  ATH_CHECK( m_dobjKeyArray.initialize() );
65 
66  return StatusCode::SUCCESS;
67 }
68 
69 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
70 
71 StatusCode ReadDataReentrant::execute (const EventContext& ctx) const
72 {
73 
74  // An algorithm, like this one, retrieving an object from the StoreGate(SG)
75  // can either ask for:
76  // i) the most recent (default) object of a given type
77  // ii) a specific object of a given type
78  // iii) all objects of a given type
79  //This example will show how to perform these three tasks
80 
81 
82  ATH_MSG_INFO ("in execute()");
83 
85  // Part 1: retrieving individual objects from SG
86  //
87  // i)Get the most recent (default) object of type MyDataObj
88  // we start by creating a *const* pointer: we won't be able
89  // to modify the MyDataObj we retrieve.
90  //unless you know what you are doing, *always* use const pointers
91  //FIXME const MyDataObj* dobj;
92 
93  //We pass the pointer to the StoreGate that will set it to point to
94  //the default MyDataObj. More precisely the handle will now contain
95  //all infos necessary to access (or create) the default MyDataObj when
96  //we will use it
97  //FIXME new (tag StoreGate-02-12-08) keyless record scheme does not allow it!
98  //FIXME if (StatusCode::SUCCESS != p_SGevent->retrieve(dobj) ) {
99  //FIXME log << MSG::ERROR
100  //FIXME << "Could not find default MyDataObj"
101  //FIXME << endmsg;
102  //FIXME return( StatusCode::FAILURE);
103  //FIXME }
104  //FIXME log << MSG::INFO
105  //FIXME << "default MyDataObj Val: " << dobj->val()
106  //FIXME << endmsg;
107 
109  // ii) Get a specific MyDataObj by providing its key
110  // (in this case the name of the algo which recorded it)
111 
113  if (testobj->val() != 10) std::abort();
114 
115  // Reading the array of handles.
116  std::vector<SG::ReadHandle<MyDataObj> > vh = m_dobjKeyArray.makeHandles (ctx);
117  for (size_t i = 0; i < vh.size(); i++) {
118  assert (vh[i]->val() == static_cast<int> (100+i));
119  }
120 
121 #if 0
122  // iii) Get all recorded instances of MyDataObj, print out their contents
124 
125  // (a SG::ConstIterator is an iterator class that we also use to defer
126  // and control the access to the persistent object)
129 
130  if (StatusCode::SUCCESS != evtStore()->retrieve(iMyD, eMyD) ) {
131  ATH_MSG_ERROR ("Could not find MyDataObj list");
132  return( StatusCode::FAILURE);
133  }
134 
135  // while(iMyD != eMyD) {
136  // ATH_MSG_INFO ((*iMyD).val());
137  // ++iMyD;
138  // }
139  ATH_MSG_WARNING ("FIXME loop of ConstIterator bombs");
140 #endif
141 
142 
143 
145 // Get the default listof MyContObj, print out its contents
146 
147  //the CLID of list<MyContObj> must be defined using the CLASSDEF
148  //macros. See StoreGateExample_ClassDEF.h for a few examples
149  //If no CLID is defined StoreGate assumes the object is (convertible to a)
150  //DataObject. If this is not the case an error message is issued:
151  //uncomment below to see how your compiler catches an undefined CLID
152  //ERROR p_SGevent->retrieve(errorH);
153 
155  for (const MyContObj* obj : *list) {
156  float time = obj->time();
157  int ID = obj->id();
158 
159  ATH_MSG_INFO ("Time: " << time << " ID: " << ID);
160  }
161 
163 
164 // Get the std::vector, print out its contents
165 
167  for (unsigned int it=0; it<pVec->size(); it++) {
168  ATH_MSG_INFO ("pVec [" << it << "] = " << (*pVec)[it]);
169  }
170 
172  // test if an object is in the store
173  //FIXME if (p_SGevent->contains<MyDataObj>(SG::DEFAULTKEY)) {
174  //FIXME log << MSG::INFO
175  //FIXME <<"event store contains default MyDataObj"
176  //FIXME <<endmsg;
177  //FIXME } else {
178  //FIXME log << MSG::ERROR
179  //FIXME <<"event store claims it does not contain default MyDataObj"
180  //FIXME <<endmsg;
181  //FIXME return( StatusCode::FAILURE);
182  //FIXME }
183 
184 #if 0
185  // test if an object is not in the store
186  const SG::ReadHandle<MyDataObj> nonexisting (m_nonexistingKey, ctx);
187  if (nonexisting.isValid()) {
189  ("event store claims it contains MyDataObj with FunnyNonExistingKey");
190  return( StatusCode::FAILURE);
191  } else {
193  ("event store does not contain MyDataObj with FunnyNonExistingKey");
194  }
195 #endif
196 
197 
199  // Part 2: retrieving DataLinks
200 
201  // Get the list of links, print out its contents
202 
203  typedef ElementLink<std::vector<float> > VecElemLink;
204  auto pList = SG::makeHandle (m_pLinkListKey, ctx);
205  for (const VecElemLink& l : *pList) {
206  ATH_MSG_INFO ("ListVecLinks::linked element " << *l);
207  }
208 
209  // Get the vector of links, print out its contents
210  typedef ElementLink<MapStringFloat> MapElemLink;
212  for (const MapElemLink& l : *vectorHandle) {
213  ATH_MSG_INFO
214  ("VectorMapLinks::linked element: key " << l.index()
215  << " - value " << (*l)
216  << " - stored as " << l);
217  }
218 
219  //try to "read back" a link
220  // istrstream istr("due 0");
221  // MapElemLink toBeRead;
222  // istr >> toBeRead;
223  // if (!toBeRead.isValid()) {
224  // log << MSG::ERROR
225  // << "Could not read back MapElement"
226  // << endmsg;
227  // return( StatusCode::FAILURE);
228  // } else {
229  // log << MSG::INFO << "MapElement read back: key " << toBeRead->first
230  // << " value " << toBeRead->second <<endmsg;
231  // }
232 
234  // Part 3: symbolic links and derived types
235  // Get all objects as its base class
236 
237 #if 0
240 
241  if (StatusCode::SUCCESS != evtStore()->retrieve(it2, iEnd2) ) {
242 
243  ATH_MSG_ERROR ("Could not find base class list");
244  return( StatusCode::FAILURE);
245  }
246 
247  // not much we can do with an empty base class,
248  // so print the path of its persistable companion (the DataBucket)
249  ATH_MSG_INFO (" retrieve as BaseClass, print its address: ");
250  while(it2 != iEnd2) {
251  ATH_MSG_INFO (&*it2);
252  ++it2;
253  }
254 #endif
255 
257  // Part 4: Get the event header, print out event and run number
258 
259 #if 0
261  if(eventInfo.isValid()) {
262  int event = eventInfo->eventNumber();
263  int run = eventInfo->runNumber();
264  ATH_MSG_INFO (" EventInfo : "
265  << " event: " << event
266  << " run: " << run);
267  }
268  else
269  {
270  ATH_MSG_ERROR (" Unable to retrieve EventInfo from StoreGate ");
271  return StatusCode::SUCCESS;
272  }
273 #endif
274 
275  return StatusCode::SUCCESS;
276 }
277 
278 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
279 
281 
282  ATH_MSG_INFO ("in finalize()");
283  return StatusCode::SUCCESS;
284 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
StoreGateExample_ClassDEF.h
ReadDataReentrant::m_pLinkListKey
SG::ReadHandleKey< std::list< ElementLink< std::vector< float > > > > m_pLinkListKey
Definition: ReadDataReentrant.h:41
ReadDataReentrant::ReadDataReentrant
ReadDataReentrant(const std::string &name, ISvcLocator *pSvcLocator)
Definition: ReadDataReentrant.cxx:38
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.
MyContObj.h
ReadDataReentrant::m_eventInfo
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfo
Definition: ReadDataReentrant.h:44
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
ReadDataReentrant::m_dobjKeyArray
SG::ReadHandleKeyArray< MyDataObj > m_dobjKeyArray
Definition: ReadDataReentrant.h:45
skel.it
it
Definition: skel.GENtoEVGEN.py:423
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
SGIterator.h
A standard conforming forward iterator over items in StoreGate.
TestDataObject::val
void val(int i)
Definition: AthExStoreGateExample/MyDataObj.h:39
ReadDataReentrant::m_vFloatKey
SG::ReadHandleKey< std::vector< float > > m_vFloatKey
Definition: ReadDataReentrant.h:40
xAOD::EventInfo_v1::runNumber
uint32_t runNumber() const
The current event's run number.
ReadDataReentrant::m_linkVectorKey
SG::ReadHandleKey< std::vector< ElementLink< MapStringFloat > > > m_linkVectorKey
Definition: ReadDataReentrant.h:42
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:269
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
ReadDataReentrant::m_cobjKey
SG::ReadHandleKey< DataVector< MyContObj > > m_cobjKey
Definition: ReadDataReentrant.h:39
MapStringFloat.h
ReadDataReentrant.h
Testing reentrant algorithms.
ReadDataReentrant::initialize
virtual StatusCode initialize() override final
Definition: ReadDataReentrant.cxx:52
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
MyContObj
Definition: MyContObj.h:11
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
ReadDataReentrant::finalize
virtual StatusCode finalize() override final
Definition: ReadDataReentrant.cxx:280
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
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
errorcheck::ReportMessage::hideErrorLocus
static void hideErrorLocus(bool flag=true)
If set to true, hide the source file and line number in the output.
Definition: errorcheck.cxx:197
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
errorcheck.h
Helpers for checking error return status codes and reporting errors.
MyDataObj.h
ReadDataReentrant::m_testObjectKey
SG::ReadHandleKey< TestDataObject > m_testObjectKey
Definition: ReadDataReentrant.h:43
DataVector.h
An STL vector of pointers that by default owns its pointed-to elements.
UpdateHandle.h
Handle class for modifying an existing object in StoreGate.
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
Pythia8_RapidityOrderMPI.val
val
Definition: Pythia8_RapidityOrderMPI.py:14
ReadHandle.h
Handle class for reading from StoreGate.
DefaultKey.h
python.PyAthena.obj
obj
Definition: PyAthena.py:135
SG::ConstIterator
Definition: SGIterator.h:163
ReadDataReentrant::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: ReadDataReentrant.cxx:71