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 {
63  std::vector<SG::auxid_t> ids (auxids.begin(), auxids.end());
64  std::sort (ids.begin(), ids.end());
65 
66  for (SG::auxid_t id : ids) {
67  std::cout << id << " "
68  << reg.getClassName(id) << "::" << reg.getName(id) << " "
69  << "[" << reg.getTypeName(id);
70 
71  SG::AuxVarFlags flags = reg.getFlags(id);
73  std::cout << " (atomic)";
74  }
76  std::cout << " (linked)";
77  }
78 
79  std::cout << "]\n";
80  }
81 }
82 
83 
89 {
90  print_aux_vars (store.getAuxIDs());
91 }
92 
93 
99 {
101 }
102 
103 
109 {
110  print_aux_vars (vec.getAuxIDs());
111 }
112 
113 
119 {
120  print_aux_vars (*vec);
121 }
122 
123 
129 {
130  print_aux_vars (elt.getAuxIDs());
131 }
132 
133 
139 {
140  print_aux_vars (*elt);
141 }
142 
143 
144 /******************************************************************************
145  * Dump out aux variables.
146  */
147 
148 
149 namespace {
150 
151 
152 template <class T>
153 void convert (std::ostream& os, const T& x)
154 {
155  os << x;
156 }
157 
158 
159 void convert (std::ostream& os, const float x)
160 {
161  os << CxxUtils::strformat ("%.3f", x);
162 }
163 
164 
165 void convert (std::ostream& os, const double x)
166 {
167  os << CxxUtils::strformat ("%.3f", x);
168 }
169 
170 
171 void convert (std::ostream& os, const SG::JaggedVecEltBase& x, size_t i)
172 {
173  os << std::format ("[{},{}]", x.begin(i), x.end());
174 }
175 
176 
177 void convert (std::ostream& os, const DataLinkBase& x)
178 {
179 #ifdef XAOD_STANDALONE
180  os << std::format ("DataLink[{}]", x.persKey());
181 #else
182  os << std::format ("DataLink[{}/{}]", x.proxy() ? x.proxy()->clID() : CLID_NULL, x.dataID());
183 #endif
184 }
185 
186 
187 void convert (std::ostream& os, const SG::PackedLinkBase& x)
188 {
189  os << std::format ("PackedLink[{}/{}]", x.collection(), x.index());
190 }
191 
192 
193 template <class T>
194 void convert (std::ostream& os, const std::vector<T>& x)
195 {
196  os << "[";
197  bool first = true;
198  // using `decltype(auto)` in case T=bool
199  // cppcheck-suppress internalAstError
200  for (decltype(auto) elt : x) {
201  if (first)
202  first = false;
203  else
204  os << ", ";
205  convert (os, elt);
206  }
207  os << "]";
208 }
209 
210 
211 struct AuxVarSort
212 {
213  AuxVarSort (SG::auxid_t the_id)
214  : id(the_id),
215  name(aux_var_name(the_id))
216  {}
217 
218  SG::auxid_t id;
219  std::string name;
220 };
221 
222 
223 bool operator< (const AuxVarSort& a, const AuxVarSort& b )
224 {
225  return a.name < b.name;
226 }
227 
228 
229 } // anonymous namespace
230 
231 
238 std::string aux_var_as_string (SG::auxid_t auxid, const void* p, size_t i)
239 {
240  if (!p) {
241  return "(null)";
242  }
243 
244  std::ostringstream os;
245 
247  const std::type_info* ti = r.getType(auxid);
248 #define CONVERT(T) if (ti == &typeid(T)) convert (os, *reinterpret_cast<const T*>(p)); else
249 #define CONVERT1(T) CONVERT(T) CONVERT(std::vector<T>)
250  CONVERT1 (int)
251  CONVERT1 (unsigned int)
252  CONVERT1 (short)
253  CONVERT1 (unsigned short)
254  CONVERT1 (char)
255  CONVERT1 (unsigned char)
256  CONVERT1 (long)
257  CONVERT1 (unsigned long)
258  CONVERT1 (long long)
259  CONVERT1 (unsigned long long)
260  CONVERT1 (float)
261  CONVERT1 (double)
262  CONVERT1 (bool)
263  CONVERT1 (std::string)
264  //else
265  {
266  std::string tiname = AthContainers_detail::typeinfoName(*ti);
267  if (tiname.starts_with ("SG::JaggedVecElt<")) {
268  convert (os, *reinterpret_cast<const SG::JaggedVecEltBase*>(p), i);
269  }
270  else if (tiname.starts_with ("DataLink<")) {
271  convert (os, *reinterpret_cast<const DataLinkBase*>(p));
272  }
273  else if (tiname.starts_with ("SG::PackedLink<")) {
274  convert (os, *reinterpret_cast<const SG::PackedLinkBase*>(p));
275  }
276  else if (tiname.starts_with ("std::vector<SG::PackedLink<")) {
277  convert (os, *reinterpret_cast<const std::vector<SG::PackedLinkBase>*>(p));
278  }
279  else {
280  os << "<??? " << tiname << ">";
281  }
282  }
283  return os.str();
284 }
285 
286 
293 void dump_aux_vars (std::ostream& os, const SG::IConstAuxStore& store, size_t i)
294 {
295  if (i >= store.size()) return;
297  const SG::auxid_set_t& ids = store.getAuxIDs();
298  std::vector<AuxVarSort> vars (ids.begin(), ids.end());
299  std::sort (vars.begin(), vars.end());
300  for (const AuxVarSort& v : vars) {
301  if (reg.isLinked (v.id)) continue;
302  const void* pbeg = store.getData (v.id);
303  size_t eltsz = reg.getEltSize (v.id);
304  const char* p = reinterpret_cast<const char*>(pbeg) + eltsz*i;
305  os << v.name << " " << aux_var_as_string (v.id, p, i) << "\n";
306  SG::auxid_t linked_id = reg.linkedVariable (v.id);
307  if (linked_id != SG::null_auxid) {
308  os << " linked: " << aux_var_name (linked_id) << " ";
309  const SG::IAuxTypeVector* lv = store.linkedVector (v.id);
310  if (!lv) {
311  os << "(missing linkedVector)\n";
312  continue;
313  }
314  size_t sz = lv->size();
315  const char* lbeg = reinterpret_cast<const char*>(lv->toPtr());
316  size_t leltsz = reg.getEltSize (linked_id);
317  os << "[";
318  bool first = true;
319  for (size_t j = 0; j < sz; j++) {
320  if (first)
321  first = false;
322  else
323  os << ", ";
324  const char* p = reinterpret_cast<const char*>(lbeg) + leltsz*j;
325  os << aux_var_as_string (linked_id, p, i);
326  }
327  os << "]\n";
328  }
329  }
330 }
331 
332 
339 {
340  dump_aux_vars (std::cout, store, i);
341 }
342 
343 
350 {
351  dump_aux_vars (*store, i);
352 }
353 
354 
360 {
361  size_t sz = store.size();
362  for (size_t i = 0; i < sz; i++) {
363  std::cout << "=== Element " << i << "\n";
364  dump_aux_vars (store, i);
365  }
366 }
367 
368 
374 {
375  dump_aux_vars (*store);
376 }
377 
378 
384 void dump_aux_vars (const SG::AuxVectorData& vec, size_t i)
385 {
386  const SG::IConstAuxStore* store = vec.getConstStore();
387  if (store)
388  dump_aux_vars (*store, i);
389 }
390 
391 
397 void dump_aux_vars (const SG::AuxVectorData* vec, size_t i)
398 {
399  dump_aux_vars (*vec, i);
400 }
401 
402 
408 {
409  const SG::IConstAuxStore* store = vec.getConstStore();
410  if (store)
411  dump_aux_vars (*store);
412 }
413 
414 
420 {
421  dump_aux_vars (*vec);
422 }
423 
424 
429 void dump_aux_vars (const SG::AuxElement& elt)
430 {
431  const SG::AuxVectorData* cont = elt.container();
432  if (cont)
433  dump_aux_vars (*cont, elt.index());
434 }
435 
436 
441 void dump_aux_vars (const SG::AuxElement* elt)
442 {
443  dump_aux_vars (*elt);
444 }
445 
446 
447 } // 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:640
AthenaPoolTestRead.flags
flags
Definition: AthenaPoolTestRead.py:8
SG::AuxElement
Base class for elements of a container that can have aux data.
Definition: AuxElement.h:472
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:882
CxxUtils::ConcurrentBitset::end
const_iterator end() const
Return an end iterator.
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
x
#define x
SGdebug::print_aux_vars
void print_aux_vars(const SG::auxid_set_t &auxids)
Print the list of aux variables in a set.
Definition: Control/AthContainers/Root/debug.cxx:60
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:924
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:991
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:1007
SG::AuxTypeRegistry::getEltSize
size_t getEltSize(SG::auxid_t auxid) const
Return size of an element in the STL vector.
Definition: AuxTypeRegistry.cxx:978
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:335
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:896
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:293
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:220
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
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
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_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:238
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.