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 #include "CxxUtils/StrFormat.h"
21 #include <format>
22 #include <vector>
23 #include <sstream>
24 #include <iostream>
25 
26 
27 namespace SGdebug {
28 
29 
34 std::string aux_var_name (SG::auxid_t id)
35 {
37  return reg.getClassName(id) + "::" + reg.getName(id);
38 }
39 
40 
46 {
47  std::cout << aux_var_name(id) << "\n";
48 }
49 
50 
51 /******************************************************************************
52  * Print the list of aux variables given some object.
53  */
54 
55 
60 void print_aux_vars (const SG::auxid_set_t& auxids,
61  const SG::auxid_set_t& decors /*= SG::auxid_set_t()*/)
62 {
64  std::vector<SG::auxid_t> ids (auxids.begin(), auxids.end());
65  std::sort (ids.begin(), ids.end());
66 
67  for (SG::auxid_t id : ids) {
68  std::cout << id << " "
69  << reg.getClassName(id) << "::" << reg.getName(id) << " "
70  << "[" << reg.getTypeName(id);
71 
72  SG::AuxVarFlags flags = reg.getFlags(id);
74  std::cout << " (atomic)";
75  }
77  std::cout << " (linked)";
78  }
79  if (decors.test (id)) {
80  std::cout << " (decor)";
81  }
82 
83  std::cout << "]\n";
84  }
85 }
86 
87 
93 {
94  print_aux_vars (store.getAuxIDs(), store.getDecorIDs());
95 }
96 
97 
103 {
105 }
106 
107 
113 {
114  print_aux_vars (vec.getAuxIDs(), vec.getDecorIDs());
115 }
116 
117 
123 {
124  print_aux_vars (*vec);
125 }
126 
127 
133 {
134  print_aux_vars (elt.getAuxIDs(), elt.getDecorIDs());
135 }
136 
137 
143 {
144  print_aux_vars (*elt);
145 }
146 
147 
148 /******************************************************************************
149  * Dump out aux variables.
150  */
151 
152 
153 namespace {
154 
155 
156 template <class T>
157 void convert (std::ostream& os, const T& x)
158 {
159  os << x;
160 }
161 
162 
163 void convert (std::ostream& os, const float x)
164 {
165  os << CxxUtils::strformat ("%.3f", x);
166 }
167 
168 
169 void convert (std::ostream& os, const double x)
170 {
171  os << CxxUtils::strformat ("%.3f", x);
172 }
173 
174 
175 void convert (std::ostream& os, const SG::JaggedVecEltBase& x, size_t i)
176 {
177  os << std::format ("[{},{}]", x.begin(i), x.end());
178 }
179 
180 
181 void convert (std::ostream& os, const DataLinkBase& x)
182 {
183 #ifdef XAOD_STANDALONE
184  os << std::format ("DataLink[{}]", x.persKey());
185 #else
186  os << std::format ("DataLink[{}/{}]", x.proxy() ? x.proxy()->clID() : CLID_NULL, x.dataID());
187 #endif
188 }
189 
190 
191 void convert (std::ostream& os, const SG::PackedLinkBase& x)
192 {
193  os << std::format ("PackedLink[{}/{}]", x.collection(), x.index());
194 }
195 
196 
197 template <class T>
198 void convert (std::ostream& os, const std::vector<T>& x)
199 {
200  os << "[";
201  bool first = true;
202  // using `decltype(auto)` in case T=bool
203  // cppcheck-suppress internalAstError
204  for (decltype(auto) elt : x) {
205  if (first)
206  first = false;
207  else
208  os << ", ";
209  convert (os, elt);
210  }
211  os << "]";
212 }
213 
214 
215 struct AuxVarSort
216 {
217  AuxVarSort (SG::auxid_t the_id)
218  : id(the_id),
219  name(aux_var_name(the_id))
220  {}
221 
222  SG::auxid_t id;
223  std::string name;
224 };
225 
226 
227 bool operator< (const AuxVarSort& a, const AuxVarSort& b )
228 {
229  return a.name < b.name;
230 }
231 
232 
233 } // anonymous namespace
234 
235 
242 std::string aux_var_as_string (SG::auxid_t auxid, const void* p, size_t i)
243 {
244  if (!p) {
245  return "(null)";
246  }
247 
248  std::ostringstream os;
249 
251  const std::type_info* ti = r.getType(auxid);
252 #define CONVERT(T) if (ti == &typeid(T)) convert (os, *reinterpret_cast<const T*>(p)); else
253 #define CONVERT1(T) CONVERT(T) CONVERT(std::vector<T>)
254  CONVERT1 (int)
255  CONVERT1 (unsigned int)
256  CONVERT1 (short)
257  CONVERT1 (unsigned short)
258  CONVERT1 (char)
259  CONVERT1 (unsigned char)
260  CONVERT1 (long)
261  CONVERT1 (unsigned long)
262  CONVERT1 (long long)
263  CONVERT1 (unsigned long long)
264  CONVERT1 (float)
265  CONVERT1 (double)
266  CONVERT1 (bool)
267  CONVERT1 (std::string)
268  //else
269  {
270  std::string tiname = AthContainers_detail::typeinfoName(*ti);
271  if (tiname.starts_with ("SG::JaggedVecElt<")) {
272  convert (os, *reinterpret_cast<const SG::JaggedVecEltBase*>(p), i);
273  }
274  else if (tiname.starts_with ("DataLink<")) {
275  convert (os, *reinterpret_cast<const DataLinkBase*>(p));
276  }
277  else if (tiname.starts_with ("SG::PackedLink<")) {
278  convert (os, *reinterpret_cast<const SG::PackedLinkBase*>(p));
279  }
280  else if (tiname.starts_with ("std::vector<SG::PackedLink<")) {
281  convert (os, *reinterpret_cast<const std::vector<SG::PackedLinkBase>*>(p));
282  }
283  else {
284  os << "<??? " << tiname << ">";
285  }
286  }
287  return os.str();
288 }
289 
290 
297 void dump_aux_vars (std::ostream& os, const SG::IConstAuxStore& store, size_t i)
298 {
299  if (i >= store.size()) return;
301  const SG::auxid_set_t& ids = store.getAuxIDs();
302  std::vector<AuxVarSort> vars (ids.begin(), ids.end());
303  std::sort (vars.begin(), vars.end());
304  for (const AuxVarSort& v : vars) {
305  if (reg.isLinked (v.id)) continue;
306  const void* pbeg = store.getData (v.id);
307  size_t eltsz = reg.getEltSize (v.id);
308  const char* p = reinterpret_cast<const char*>(pbeg) + eltsz*i;
309  os << v.name << " " << aux_var_as_string (v.id, p, i) << "\n";
310  SG::auxid_t linked_id = reg.linkedVariable (v.id);
311  if (linked_id != SG::null_auxid) {
312  os << " linked: " << aux_var_name (linked_id) << " ";
313  const SG::IAuxTypeVector* lv = store.linkedVector (v.id);
314  if (!lv) {
315  os << "(missing linkedVector)\n";
316  continue;
317  }
318  size_t sz = lv->size();
319  const char* lbeg = reinterpret_cast<const char*>(lv->toPtr());
320  size_t leltsz = reg.getEltSize (linked_id);
321  os << "[";
322  bool first = true;
323  for (size_t j = 0; j < sz; j++) {
324  if (first)
325  first = false;
326  else
327  os << ", ";
328  const char* p = reinterpret_cast<const char*>(lbeg) + leltsz*j;
329  os << aux_var_as_string (linked_id, p, i);
330  }
331  os << "]\n";
332  }
333  }
334 }
335 
336 
343 {
344  dump_aux_vars (std::cout, store, i);
345 }
346 
347 
354 {
355  dump_aux_vars (*store, i);
356 }
357 
358 
364 {
365  size_t sz = store.size();
366  for (size_t i = 0; i < sz; i++) {
367  std::cout << "=== Element " << i << "\n";
368  dump_aux_vars (store, i);
369  }
370 }
371 
372 
378 {
379  dump_aux_vars (*store);
380 }
381 
382 
388 void dump_aux_vars (const SG::AuxVectorData& vec, size_t i)
389 {
390  const SG::IConstAuxStore* store = vec.getConstStore();
391  if (store)
392  dump_aux_vars (*store, i);
393 }
394 
395 
401 void dump_aux_vars (const SG::AuxVectorData* vec, size_t i)
402 {
403  dump_aux_vars (*vec, i);
404 }
405 
406 
412 {
413  const SG::IConstAuxStore* store = vec.getConstStore();
414  if (store)
415  dump_aux_vars (*store);
416 }
417 
418 
424 {
425  dump_aux_vars (*vec);
426 }
427 
428 
433 void dump_aux_vars (const SG::AuxElement& elt)
434 {
435  const SG::AuxVectorData* cont = elt.container();
436  if (cont)
437  dump_aux_vars (*cont, elt.index());
438 }
439 
440 
445 void dump_aux_vars (const SG::AuxElement* elt)
446 {
447  dump_aux_vars (*elt);
448 }
449 
450 
451 } // namespace SG
operator<
bool operator<(const DataVector< T > &a, const DataVector< T > &b)
Vector ordering relation.
beamspotman.r
def r
Definition: beamspotman.py:676
store
StoreGateSvc * store
Definition: fbtTestBasics.cxx:71
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:12
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
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:354
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:297
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:224
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:225
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:377
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:60
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:45
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:34
DataLinkBase
Type-independent part of DataLink; holds the persistent state.
Definition: AthLinks/DataLinkBase.h:37
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:242
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.