ATLAS Offline Software
xAODTestTypelessRead.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
15 #include "xAODTestTypelessRead.h"
29 #include "AthLinks/ElementLink.h"
31 #include "CxxUtils/StrFormat.h"
32 #include "GaudiKernel/System.h"
33 #include <memory>
34 #include <sstream>
35 
36 
37 namespace DMTest {
38 
39 
46  ISvcLocator *pSvcLocator)
47  : AthAlgorithm (name, pSvcLocator),
48  m_count(0)
49 {
50  declareProperty ("WritePrefix", m_writePrefix);
51 }
52 
53 
58 {
59  return StatusCode::SUCCESS;
60 }
61 
62 
63 namespace {
64 
65 
66 void dumpAuxItem (std::ostream& ost,
67  SG::auxid_t auxid,
68  const SG::AuxVectorData& c, size_t i)
69 {
71  const std::type_info* ti = r.getType(auxid);
72  std::string head = r.getName(auxid) + ": ";
73  if (ti == &typeid(int))
74  ost << head << c.getData<int> (auxid, i) << "; ";
75  else if (ti == &typeid(unsigned int))
76  ost << head << c.getData<unsigned int> (auxid, i) << "; ";
77  else if (ti == &typeid(float))
78  ost << head << CxxUtils::strformat ("%.3f", c.getData<float> (auxid, i)) << "; ";
79  else if (ti == &typeid(ElementLink<DMTest::CVec>)) {
81  c.getData<ElementLink<DMTest::CVec> > (auxid, i);
82  ost << head << el.dataID() << "[" << el.index() << "]; ";
83  }
84 #if 0
85  else if (ti == &typeid(SG::PackedElement<unsigned int>))
86  ost << head << c.getData<SG::PackedElement<unsigned int> > (auxid, i) << "; ";
87  else if (ti == &typeid(SG::PackedElement<float>))
88  ost << head << c.getData<SG::PackedElement<float> > (auxid, i) << "; ";
89 #endif
90  else if (ti == &typeid(std::vector<unsigned int>)) {
91  ost << "\n " << head << "[";
92  for (auto ii : c.getData<std::vector<unsigned int> > (auxid, i))
93  ost << ii << " ";
94  ost << "]; ";
95  }
96  else if (ti == &typeid(std::vector<int>)) {
97  ost << "\n " << head << "[";
98  for (auto ii : c.getData<std::vector<int> > (auxid, i))
99  ost << ii << " ";
100  ost << "]; ";
101  }
102  else if (ti == &typeid(std::vector<float>) ||
103  strcmp (ti->name(), typeid(std::vector<float>).name()) == 0)
104  {
105  ost << "\n " << head << "[";
106  for (auto ii : c.getData<std::vector<float> > (auxid, i))
107  ost << CxxUtils::strformat ("%.3f", ii) << " ";
108  ost << "]; ";
109  }
110 #if 0
111  else if (ti == &typeid(SG::PackedElement<std::vector<unsigned int> >)) {
112  ost << "\n " << head << "[";
113  for (auto ii : c.getData<SG::PackedElement<std::vector<unsigned int> > > (auxid, i))
114  ost << ii << " ";
115  ost << "]; ";
116  }
117  else if (ti == &typeid(SG::PackedElement<std::vector<int> >)) {
118  ost << "\n " << head << "[";
119  for (auto ii : c.getData<SG::PackedElement<std::vector<int> > > (auxid, i))
120  ost << ii << " ";
121  ost << "]; ";
122  }
123  else if (ti == &typeid(SG::PackedElement<std::vector<float> >)) {
124  ost << "\n " << head << "[";
125  for (auto ii : c.getData<SG::PackedElement<std::vector<float> > > (auxid, i))
126  ost << CxxUtils::strformat ("%.3f", ii) << " ";
127  ost << "]; ";
128  }
129 #endif
130  else
131  ost << head << "xxx " << ti->name() << "; ";
132 }
133 
134 
135 std::map<std::string, SG::auxid_t> get_map (const SG::AuxVectorData* vec)
136 {
138 
139  // Sort auxids in name order.
140  std::map<std::string, SG::auxid_t> auxid_map;
141  for (SG::auxid_t auxid : vec->getAuxIDs())
142  auxid_map[r.getName(auxid)] = auxid;
143 
144  return auxid_map;
145 }
146 
147 
148 std::map<std::string, SG::auxid_t> get_map (const SG::AuxElement* elt)
149 {
150  return get_map (elt->container());
151 }
152 
153 
154 void dumpelt (std::ostream& ost,
155  const SG::AuxVectorData* cont,
156  size_t index,
157  const std::map<std::string, SG::auxid_t>& auxid_map)
158 {
159  for (const auto& m : auxid_map)
160  dumpAuxItem (ost, m.second, *cont, index);
161  ost << "\n";
162 }
163 
164 
165 template <class OBJ>
166 void dumpobj (std::ostream& ost,
167  const OBJ* obj,
168  const std::map<std::string, SG::auxid_t>& auxid_map)
169 {
170  for (size_t i = 0; i < obj->size(); i++) {
171  ost << " ";
172  if (!obj->hasStore()) {
173  // Handle view container.
174  const auto* elt = obj->at(i);
175  dumpelt (ost, elt->container(), elt->index(), auxid_map);
176  }
177  else
178  dumpelt (ost, obj, i, auxid_map);
179  }
180 }
181 
182 
183 void dumpobj (std::ostream& ost,
184  const DMTest::C* obj,
185  const std::map<std::string, SG::auxid_t>& auxid_map)
186 {
187  const SG::AuxVectorData* cont = obj->container();
188  dumpelt (ost, cont, 0, auxid_map);
189 }
190 
191 
192 void copy (DMTest::CVec& to, const DMTest::CVec& from)
193 {
194  for (size_t i = 0; i < from.size(); i++) {
195  to.push_back (new C);
196  *to.back() = *from[i];
197  }
198 }
199 
200 
201 void copy (DMTest::HVec& to, const DMTest::HVec& from)
202 {
203  for (size_t i = 0; i < from.size(); i++) {
204  to.push_back (new H);
205  *to.back() = *from[i];
206  }
207 }
208 
209 
211 {
212  to.meta1 = from.meta1;
213  copy (static_cast<DMTest::CVec&>(to),
214  static_cast<const DMTest::CVec&>(from));
215 }
216 
217 
218 void copy (DMTest::C& to, const DMTest::C& from)
219 {
220  to = from;
221 }
222 
223 
224 } // anonymous namespace
225 
226 
227 template <class OBJ, class AUX>
230 {
231  const OBJ* obj = nullptr;
232  CHECK( evtStore()->retrieve (obj, key) );
233 
235 
236  std::map<std::string, SG::auxid_t> auxid_map = get_map (obj);
237  std::ostringstream ost1;
238  ost1 << key << " types: ";
239  for (const auto& m : auxid_map)
240  ost1 << r.getName(m.second) << "/"
241  << System::typeinfoName (*r.getType(m.second)) << " ";
242  ATH_MSG_INFO (ost1.str());
243 
244  std::ostringstream ost2;
245  dumpobj (ost2, obj, auxid_map);
246  ATH_MSG_INFO (ost2.str());
247 
248  if (!m_writePrefix.empty()) {
249  // Passing this as the third arg of record will make the object const.
250  bool LOCKED = false;
251 
252  auto objnew = std::make_unique<OBJ>();
253  auto store = std::make_unique<AUX>();
254  objnew->setStore (store.get());
255  copy (*objnew, *obj);
256  CHECK (evtStore()->record (std::move(objnew), m_writePrefix + key, LOCKED));
257  CHECK (evtStore()->record (std::move(store), m_writePrefix + key + "Aux.", LOCKED));
258  }
259  return StatusCode::SUCCESS;
260 }
261 
262 
263 template <class OBJ>
266 {
267  const OBJ* obj = nullptr;
268  CHECK( evtStore()->retrieve (obj, key) );
269 
270  if (!obj->empty()) {
271  std::map<std::string, SG::auxid_t> auxid_map = get_map (obj->front());
272  ATH_MSG_INFO (key);
273  std::ostringstream ost;
274  dumpobj (ost, obj, auxid_map);
275  ATH_MSG_INFO (ost.str());
276  }
277 
278  if (!m_writePrefix.empty()) {
279  CHECK (evtStore()->record (std::make_unique<OBJ>(*obj),
280  m_writePrefix + key, false));
281  }
282  return StatusCode::SUCCESS;
283 }
284 
285 
290 {
291  ++m_count;
293 
294  CHECK(( testit<CVec, CAuxContainer> ("cvec") ));
295  CHECK(( testit<C, CInfoAuxContainer> ("cinfo") ));
296  CHECK(( testit<CVec, CTrigAuxContainer> ("ctrig") ));
297  CHECK(( testit<CVecWithData, CAuxContainer> ("cvecWD") ));
298  CHECK(( testit_view<CView> ("cview") ));
299  CHECK(( testit<HVec, HAuxContainer> ("hvec") ));
300  CHECK(( testit_view<HView> ("hview") ));
301 
302  return StatusCode::SUCCESS;
303 }
304 
305 
310 {
311  return StatusCode::SUCCESS;
312 }
313 
314 
315 } // namespace DMTest
316 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
beamspotman.r
def r
Definition: beamspotman.py:676
store
StoreGateSvc * store
Definition: fbtTestBasics.cxx:69
DMTest::xAODTestTypelessRead::testit
StatusCode testit(const char *key)
Definition: xAODTestTypelessRead.cxx:229
python.trigbs_prescaleL1.ost
ost
Definition: trigbs_prescaleL1.py:104
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::AuxTypeRegistry::instance
static AuxTypeRegistry & instance()
Return the singleton registry instance.
Definition: AuxTypeRegistry.cxx:49
DMTest::xAODTestTypelessRead::finalize
virtual StatusCode finalize()
Algorithm finalization; called at the end of the job.
Definition: xAODTestTypelessRead.cxx:309
H.h
Test for xAOD schema evolution.
index
Definition: index.py:1
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
SG::AuxElement
Base class for elements of a container that can have aux data.
Definition: AuxElement.h:446
DMTest::C
C_v1 C
Definition: C.h:26
DMTest::CVecWithData_v1::meta1
int meta1
Definition: CVecWithData_v1.h:31
CAuxContainer.h
Class used for testing xAOD data reading/writing.
DMTest::C_v1
Definition: C_v1.h:29
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
DMTest::xAODTestTypelessRead::execute
virtual StatusCode execute()
Algorithm event processing.
Definition: xAODTestTypelessRead.cxx:289
HVec.h
SG::AuxTypeRegistry
Handle mappings between names and auxid_t.
Definition: AuxTypeRegistry.h:62
CInfoAuxContainer.h
Class used for testing xAOD data reading/writing.
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
DMTest::CVecWithData_v1
Definition: CVecWithData_v1.h:28
SG::auxid_t
size_t auxid_t
Identifier for a particular aux data item.
Definition: AuxTypes.h:27
DMTest::xAODTestTypelessRead::xAODTestTypelessRead
xAODTestTypelessRead(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition: xAODTestTypelessRead.cxx:45
StrFormat.h
Provide helper functions to create formatted strings.
CVec.h
Class used for testing xAOD data reading/writing.
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
C.h
Class used for testing xAOD data reading/writing.
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
DMTest::xAODTestTypelessRead::m_writePrefix
std::string m_writePrefix
Parameter: Prefix for names written to SG. Null for no write.
Definition: xAODTestTypelessRead.h:67
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
AthAlgorithm
Definition: AthAlgorithm.h:47
HAuxContainer.h
Test for xAOD schema evolution.
CView.h
Class used for testing ViewVector reading/writing.
xAODTestTypelessRead.h
Algorithm to test reading xAOD objects with auxiliary data, without compile-time typing of aux data.
head
std::string head(std::string s, const std::string &pattern)
head of a string
Definition: computils.cxx:310
CxxUtils::strformat
std::string strformat(const char *fmt,...)
return a std::string according to a format fmt and varargs
Definition: StrFormat.cxx:49
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
errorcheck.h
Helpers for checking error return status codes and reporting errors.
CVecWithData.h
Test writing a container with metadata.
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:32
DMTest::xAODTestTypelessRead::initialize
virtual StatusCode initialize()
Algorithm initialization; called at the beginning of the job.
Definition: xAODTestTypelessRead.cxx:57
DMTest::xAODTestTypelessRead::m_count
int m_count
Event counter.
Definition: xAODTestTypelessRead.h:70
AuxTypeRegistry.h
Handle mappings between names and auxid_t.
DMTest::xAODTestTypelessRead::testit_view
StatusCode testit_view(const char *key)
Definition: xAODTestTypelessRead.cxx:265
CTrigAuxContainer.h
Class used for testing xAOD data reading/writing.
HView.h
SG::AuxVectorData
Manage lookup of vectors of auxiliary data.
Definition: AuxVectorData.h:167
calibdata.copy
bool copy
Definition: calibdata.py:27
SG::AuxElement::container
const SG::AuxVectorData * container() const
Return the container holding this element.
python.PyAthena.obj
obj
Definition: PyAthena.py:135
AuxStoreInternal.h
An auxiliary data store that holds data internally.
python.compressB64.c
def c
Definition: compressB64.py:93
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DMTest::H
H_v2 H
Definition: DataModelTestDataRead/DataModelTestDataRead/H.h:26
DMTest
Definition: B.h:23
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37