ATLAS Offline Software
Control/AthContainers/Root/debug.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
13 #include "AthContainers/debug.h"
19 #include "AthLinks/DataLinkBase.h"
20 #ifndef XAOD_STANDALONE
21 # include "SGTools/DataProxy.h"
22 #endif
23 #include "CxxUtils/StrFormat.h"
24 #include <format>
25 #include <vector>
26 #include <sstream>
27 #include <iostream>
28 
29 
30 namespace SGdebug {
31 
32 
37 std::string aux_var_name (SG::auxid_t id)
38 {
40  return reg.getClassName(id) + "::" + reg.getName(id);
41 }
42 
43 
49 {
50  std::cout << aux_var_name(id) << "\n";
51 }
52 
53 
54 /******************************************************************************
55  * Print the list of aux variables given some object.
56  */
57 
58 
63 void print_aux_vars (const SG::auxid_set_t& auxids,
64  const SG::auxid_set_t& decors /*= SG::auxid_set_t()*/)
65 {
67  std::vector<SG::auxid_t> ids (auxids.begin(), auxids.end());
68  std::sort (ids.begin(), ids.end());
69 
70  for (SG::auxid_t id : ids) {
71  std::cout << id << " "
72  << reg.getClassName(id) << "::" << reg.getName(id) << " "
73  << "[" << reg.getTypeName(id);
74 
75  SG::AuxVarFlags flags = reg.getFlags(id);
77  std::cout << " (atomic)";
78  }
80  std::cout << " (linked)";
81  }
82  if (decors.test (id)) {
83  std::cout << " (decor)";
84  }
85 
86  std::cout << "]\n";
87  }
88 }
89 
90 
96 {
97  print_aux_vars (store.getAuxIDs(), store.getDecorIDs());
98 }
99 
100 
106 {
108 }
109 
110 
116 {
117  print_aux_vars (vec.getAuxIDs(), vec.getDecorIDs());
118 }
119 
120 
126 {
127  print_aux_vars (*vec);
128 }
129 
130 
136 {
137  print_aux_vars (elt.getAuxIDs(), elt.getDecorIDs());
138 }
139 
140 
146 {
147  print_aux_vars (*elt);
148 }
149 
150 
151 /******************************************************************************
152  * Dump out aux variables.
153  */
154 
155 
156 namespace {
157 
158 
159 template <class T>
160 void convert (std::ostream& os, const T& x)
161 {
162  os << x;
163 }
164 
165 
166 void convert (std::ostream& os, const float x)
167 {
168  os << CxxUtils::strformat ("%.3f", x);
169 }
170 
171 
172 void convert (std::ostream& os, const double x)
173 {
174  os << CxxUtils::strformat ("%.3f", x);
175 }
176 
177 
178 void convert (std::ostream& os, const SG::JaggedVecEltBase& x, size_t i)
179 {
180  os << std::format ("[{},{}]", x.begin(i), x.end());
181 }
182 
183 
184 void convert (std::ostream& os, const DataLinkBase& x)
185 {
186 #ifdef XAOD_STANDALONE
187  os << std::format ("DataLink[{}]", x.persKey());
188 #else
189  os << std::format ("DataLink[{}/{}]", x.proxy() ? x.proxy()->clID() : CLID_NULL, x.dataID());
190 #endif
191 }
192 
193 
194 void convert (std::ostream& os, const SG::PackedLinkBase& x)
195 {
196  os << std::format ("PackedLink[{}/{}]", x.collection(), x.index());
197 }
198 
199 
200 template <class T>
201 void convert (std::ostream& os, const std::vector<T>& x)
202 {
203  os << "[";
204  bool first = true;
205  // using `decltype(auto)` in case T=bool
206  // cppcheck-suppress internalAstError
207  for (decltype(auto) elt : x) {
208  if (first)
209  first = false;
210  else
211  os << ", ";
212  convert (os, elt);
213  }
214  os << "]";
215 }
216 
217 
218 struct AuxVarSort
219 {
220  AuxVarSort (SG::auxid_t the_id)
221  : id(the_id),
222  name(aux_var_name(the_id))
223  {}
224 
225  SG::auxid_t id;
226  std::string name;
227 };
228 
229 
230 bool operator< (const AuxVarSort& a, const AuxVarSort& b )
231 {
232  return a.name < b.name;
233 }
234 
235 
236 } // anonymous namespace
237 
238 
245 std::string aux_var_as_string (SG::auxid_t auxid, const void* p, size_t i)
246 {
247  if (!p) {
248  return "(null)";
249  }
250 
251  std::ostringstream os;
252 
254  const std::type_info* ti = r.getType(auxid);
255 #define CONVERT(T) if (ti == &typeid(T)) convert (os, *reinterpret_cast<const T*>(p)); else
256 #define CONVERT1(T) CONVERT(T) CONVERT(std::vector<T>)
257  CONVERT1 (int)
258  CONVERT1 (unsigned int)
259  CONVERT1 (short)
260  CONVERT1 (unsigned short)
261  CONVERT1 (char)
262  CONVERT1 (unsigned char)
263  CONVERT1 (long)
264  CONVERT1 (unsigned long)
265  CONVERT1 (long long)
266  CONVERT1 (unsigned long long)
267  CONVERT1 (float)
268  CONVERT1 (double)
269  CONVERT1 (bool)
270  CONVERT1 (std::string)
271  //else
272  {
273  std::string tiname = AthContainers_detail::typeinfoName(*ti);
274  if (tiname.starts_with ("SG::JaggedVecElt<")) {
275  convert (os, *reinterpret_cast<const SG::JaggedVecEltBase*>(p), i);
276  }
277  else if (tiname.starts_with ("DataLink<")) {
278  convert (os, *reinterpret_cast<const DataLinkBase*>(p));
279  }
280  else if (tiname.starts_with ("SG::PackedLink<")) {
281  convert (os, *reinterpret_cast<const SG::PackedLinkBase*>(p));
282  }
283  else if (tiname.starts_with ("std::vector<SG::PackedLink<")) {
284  convert (os, *reinterpret_cast<const std::vector<SG::PackedLinkBase>*>(p));
285  }
286  else {
287  os << "<??? " << tiname << ">";
288  }
289  }
290  return os.str();
291 }
292 
293 
300 void dump_aux_vars (std::ostream& os, const SG::IConstAuxStore& store, size_t i)
301 {
302  if (i >= store.size()) return;
304  const SG::auxid_set_t& ids = store.getAuxIDs();
305  std::vector<AuxVarSort> vars (ids.begin(), ids.end());
306  std::sort (vars.begin(), vars.end());
307  for (const AuxVarSort& v : vars) {
308  if (reg.isLinked (v.id)) continue;
309  const void* pbeg = store.getData (v.id);
310  size_t eltsz = reg.getEltSize (v.id);
311  const char* p = reinterpret_cast<const char*>(pbeg) + eltsz*i;
312  os << v.name << " " << aux_var_as_string (v.id, p, i) << "\n";
313  SG::auxid_t linked_id = reg.linkedVariable (v.id);
314  if (linked_id != SG::null_auxid) {
315  os << " linked: " << aux_var_name (linked_id) << " ";
316  const SG::IAuxTypeVector* lv = store.linkedVector (v.id);
317  if (!lv) {
318  os << "(missing linkedVector)\n";
319  continue;
320  }
321  size_t sz = lv->size();
322  const char* lbeg = reinterpret_cast<const char*>(lv->toPtr());
323  size_t leltsz = reg.getEltSize (linked_id);
324  os << "[";
325  bool first = true;
326  for (size_t j = 0; j < sz; j++) {
327  if (first)
328  first = false;
329  else
330  os << ", ";
331  const char* p = reinterpret_cast<const char*>(lbeg) + leltsz*j;
332  os << aux_var_as_string (linked_id, p, i);
333  }
334  os << "]\n";
335  }
336  }
337 }
338 
339 
346 {
347  dump_aux_vars (std::cout, store, i);
348 }
349 
350 
357 {
358  dump_aux_vars (*store, i);
359 }
360 
361 
367 {
368  size_t sz = store.size();
369  for (size_t i = 0; i < sz; i++) {
370  std::cout << "=== Element " << i << "\n";
371  dump_aux_vars (store, i);
372  }
373 }
374 
375 
381 {
382  dump_aux_vars (*store);
383 }
384 
385 
391 void dump_aux_vars (const SG::AuxVectorData& vec, size_t i)
392 {
393  const SG::IConstAuxStore* store = vec.getConstStore();
394  if (store)
395  dump_aux_vars (*store, i);
396 }
397 
398 
404 void dump_aux_vars (const SG::AuxVectorData* vec, size_t i)
405 {
406  dump_aux_vars (*vec, i);
407 }
408 
409 
415 {
416  const SG::IConstAuxStore* store = vec.getConstStore();
417  if (store)
418  dump_aux_vars (*store);
419 }
420 
421 
427 {
428  dump_aux_vars (*vec);
429 }
430 
431 
436 void dump_aux_vars (const SG::AuxElement& elt)
437 {
438  const SG::AuxVectorData* cont = elt.container();
439  if (cont)
440  dump_aux_vars (*cont, elt.index());
441 }
442 
443 
448 void dump_aux_vars (const SG::AuxElement* elt)
449 {
450  dump_aux_vars (*elt);
451 }
452 
453 
454 } // namespace SG
operator<
bool operator<(const DataVector< T > &a, const DataVector< T > &b)
Vector ordering relation.
SGTest::store
TestStore store
Definition: TestStore.cxx:23
beamspotman.r
def r
Definition: beamspotman.py:676
fitman.sz
sz
Definition: fitman.py:527
vtune_athena.format
format
Definition: vtune_athena.py:14
SG::AuxTypeRegistry::instance
static AuxTypeRegistry & instance()
Return the singleton registry instance.
Definition: AuxTypeRegistry.cxx:639
AthenaPoolTestRead.flags
flags
Definition: AthenaPoolTestRead.py:8
SG::AuxElement
Base class for elements of a container that can have aux data.
Definition: AuxElement.h:483
SGdebug
Definition: Control/AthContainers/AthContainers/debug.h:22
SG::AuxTypeRegistry::getName
std::string getName(SG::auxid_t auxid) const
Return the name of an aux data item.
Definition: AuxTypeRegistry.cxx:881
CxxUtils::ConcurrentBitset::end
const_iterator end() const
Return an end iterator.
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:9
x
#define x
SG::AuxTypeRegistry
Handle mappings between names and auxid_t.
Definition: AuxTypeRegistry.h:61
SG::JaggedVecEltBase
Describe one element of a jagged vector (base class).
Definition: JaggedVecImpl.h:52
SG::Linked
@ Linked
Mark that this variable is linked to another one.
Definition: AuxTypes.h:77
SG::auxid_t
size_t auxid_t
Identifier for a particular aux data item.
Definition: AuxTypes.h:27
StrFormat.h
Provide helper functions to create formatted strings.
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
Athena::typeinfoName
std::string typeinfoName(const std::type_info &ti)
Convert a type_info to a demangled string.
Definition: AthenaKernel/src/ClassName.cxx:23
SG::AuxTypeRegistry::getTypeName
std::string getTypeName(SG::auxid_t auxid) const
Return the type name of an aux data item.
Definition: AuxTypeRegistry.cxx:923
lumiFormat.i
int i
Definition: lumiFormat.py:85
SG::AuxTypeRegistry::getFlags
Flags getFlags(SG::auxid_t auxid) const
Return flags associated with an auxiliary variable.
Definition: AuxTypeRegistry.cxx:990
PackedLinkImpl.h
Definition of PackedLink type.
SG::AuxTypeRegistry::linkedVariable
SG::auxid_t linkedVariable(SG::auxid_t auxid) const
Return the auxid if the linked variable, if there is one.
Definition: AuxTypeRegistry.cxx:1006
SG::AuxTypeRegistry::getEltSize
size_t getEltSize(SG::auxid_t auxid) const
Return size of an element in the STL vector.
Definition: AuxTypeRegistry.cxx:977
error.h
Helper for emitting error messages.
SG::AuxElement::getAuxIDs
const SG::auxid_set_t & getAuxIDs() const
Return a set of identifiers for existing data items for this object.
Definition: AuxElement.cxx:355
SG::AuxElement::index
size_t index() const
Return the index of this element within its container.
SG::AuxVarFlags
AuxVarFlags
Additional flags to qualify an auxiliary variable.
Definition: AuxTypes.h:58
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
SG::AuxTypeRegistry::getClassName
std::string getClassName(SG::auxid_t auxid) const
Return the class name associated with an aux data item (may be blank).
Definition: AuxTypeRegistry.cxx:895
CxxUtils::ConcurrentBitset::begin
const_iterator begin() const
Return a begin iterator.
SGdebug::dump_aux_vars
void dump_aux_vars(std::ostream &os, const SG::IConstAuxStore &store, size_t i)
Dump aux variables from a store for a single element.
Definition: Control/AthContainers/Root/debug.cxx:300
CxxUtils::strformat
std::string strformat(const char *fmt,...)
return a std::string according to a format fmt and varargs
Definition: StrFormat.cxx:49
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
python.subdetectors.mmg.ids
ids
Definition: mmg.py:8
SG::AuxTypeRegistry::isLinked
bool isLinked(SG::auxid_t auxid) const
Test whether this is a linked variable.
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
TMVAToMVAUtils::convert
std::unique_ptr< MVAUtils::BDT > convert(TMVA::MethodBDT *bdt, bool isRegression=true, bool useYesNoLeaf=false)
Definition: TMVAToMVAUtils.h:114
SG::Atomic
@ Atomic
Mark that this variable should only be accessed atomically.
Definition: AuxTypes.h:70
SG::AuxElement::getDecorIDs
const SG::auxid_set_t & getDecorIDs() const
Return a set of identifiers for decorations for this object.
Definition: AuxElement.cxx:378
JaggedVecImpl.h
Definition of JaggedVecElt.
python.PyAthena.v
v
Definition: PyAthena.py:154
a
TList * a
Definition: liststreamerinfos.cxx:10
SG::PackedLinkBase
A packed version of ElementLink.
Definition: PackedLinkImpl.h:47
CONVERT1
#define CONVERT1(T)
DeMoScan.first
bool first
Definition: DeMoScan.py:536
SG::IAuxTypeVector
Abstract interface for manipulating vectors of arbitrary types.
Definition: IAuxTypeVector.h:42
AuxTypeRegistry.h
Handle mappings between names and auxid_t.
SG::auxid_set_t
A set of aux data identifiers.
Definition: AuxTypes.h:47
SGdebug::print_aux_vars
void print_aux_vars(const SG::auxid_set_t &auxids, const SG::auxid_set_t &decors=SG::auxid_set_t())
Print the list of aux variables in a set.
Definition: Control/AthContainers/Root/debug.cxx:63
SGdebug::print_aux_var_name
void print_aux_var_name(SG::auxid_t id)
Print the name corresponding to a given aux id.
Definition: Control/AthContainers/Root/debug.cxx:48
SG::AuxVectorData
Manage lookup of vectors of auxiliary data.
Definition: AuxVectorData.h:168
SG::AuxElement::container
const SG::AuxVectorData * container() const
Return the container holding this element.
IConstAuxStore.h
Interface for const operations on an auxiliary store.
SG::IConstAuxStore
Interface for const operations on an auxiliary store.
Definition: IConstAuxStore.h:64
SG::IAuxTypeVector::size
virtual size_t size() const =0
Return the size of the vector.
SGdebug::aux_var_name
std::string aux_var_name(SG::auxid_t id)
Return the name corresponding to a given aux id.
Definition: Control/AthContainers/Root/debug.cxx:37
DataLinkBase
Type-independent part of DataLink; holds the persistent state.
Definition: AthLinks/DataLinkBase.h:39
SGdebug::aux_var_as_string
std::string aux_var_as_string(SG::auxid_t auxid, const void *p, size_t i)
Convert an aux variable to a string.
Definition: Control/AthContainers/Root/debug.cxx:245
SG::IAuxTypeVector::toPtr
virtual void * toPtr()=0
Return a pointer to the start of the vector's data.
debug.h
Helper functions intended to be called from the debugger.
CxxUtils::ConcurrentBitset::test
bool test(bit_t bit) const
Test to see if a bit is set.
DataProxy.h