ATLAS Offline Software
checkTP.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 # @file: checkTP.py
6 # @purpose: dump the layout of a class (data members and bases)
7 # Inspired from SealDictTest/DictClassCheck.py from RD.
8 # @author: Sebastien Binet <binet@cern.ch>
9 # @date: September 2006
10 #
11 # @example:
12 #
13 # python checkTP.py TruthParticle
14 #
15 # if checkTP.py has been made 'chmod +x' one can just do:
16 # ./checkTP.py CaloCellContainer
17 
18 from __future__ import print_function
19 
20 import sys
21 import os
22 
23 __author__ = "Sebastien Binet"
24 
25 # MN: this has no effect when using RootType
26 S = 4 # SCOPED
27 SF = 5 # SCOPED|FINAL
28 SQ = 6 # SCOPED|QUALIFIED
29 SFQ = 7 # SCOPED|FINAL|QUALIFIED
30 DICTSCOPE = SF
31 
32 _cpp_builtins = (
33  'char', 'unsigned char', 'signed char',
34  'short', 'unsigned short',
35  'int', 'unsigned int',
36  'long', 'unsigned long',
37  'long long', 'unsigned long long', 'ulonglong',
38  'float',
39  'double',
40  'bool',
41  )
42 
44  def __init__(self, offset, name, _type):
45  self.offset = offset
46  self.name = name
47  self.type = _type
48  return
49 
51  def __init__(self, offset, name, _type):
52  self.name = name
53  self.type = _type
54  return
55 
56 class Columbo(object):
57 
58  def __init__(self):
59  object.__init__(self)
60  print ("")
61  print ("#"*80)
62  print ("## initializing...")
63  import cppyy
64  self.gbl = cppyy.gbl
65  self.Type = cppyy.gbl.RootType
66 
67  self.report = []
68  return
69 
70  def loadDicts(self, klassName):
71  klassNames = [klassName]
72  print ("## loading dictionary... [%s]" % klassName)
73 
74 
75  if klassName.startswith("std::_"):
76  return klassNames
77  #return []
78 
79 
80  if klassName in _cpp_builtins:
81  return klassNames
82 
83  loaded = False
84  try:
85  loaded = getattr (self.gbl, klassName)
86  except Exception as e:
87  print ("Error loading dict. for [%s]" % klassName)
88  print ("--> ", e)
89  if not loaded:
90  print ("Failed to load dict for [%s]" % klassName)
91  return klassNames
92  klass = self.Type.ByName(klassName)
93 
94  if not klass.IsStruct() and not klass.IsClass():
95  return klassNames
96 
97  for i in range(klass.BaseSize()):
98  baseKlassName = klass.BaseAt(i).Name()
99  klassNames.extend (self.loadDicts(baseKlassName))
100  pass
101 
104  return klassNames
105 
106  def dumpDataMembers(self, klass):
107  dataMembers = []
108  for i in range(klass.DataMemberSize()):
109  d = klass.DataMemberAt(i)
110  scope = klass.Name()
111  offset = '<s>' if d.IsStatic() else d.Offset()
112  fullname = '::'.join([scope, d.Name(SFQ)])
113  typename = d.TypeOf().Name(SFQ)
114  dataMembers.append( DataMember(offset, fullname, typename) )
115  return dataMembers
116 
117  def dumpFctMembers(self, klass):
118  fctMembers = []
119  for i in range(klass.FunctionMemberSize()):
120  f = klass.FunctionMemberAt(i)
121  fctMembers.append( FctMember( f.Name(SFQ),
122  f.TypeOf().Name(SFQ) ) )
123  pass
124  return fctMembers
125 
126  def inspect(self, klassName):
127  self.report = []
128  print ("")
129  print ("#"*80)
130  print ("## loading all relevant dictionaries...")
131  try:
132  klassNames = self.loadDicts(klassName)
133  print ("#"*80)
134  except Exception as err:
135  print ("")
136  print ("#"*80)
137  print ("## ERROR while trying to load dict for [%s]" % klassName)
138  print ("## -Most probably you DIDN'T give a fully qualified name !")
139  print ("## Ex: try 'Analysis::Muon' instead of 'Muon'")
140  print ("##")
141  print ("## -Could also mean that you are missing a dictionary ")
142  print ("## of one of the base classes...")
143  print ("#"*80)
144  print (err)
145  raise
146  return
147 
148  print ("")
149  print ("#"*80)
150  print ("## infos for class [%s]:" % klassName)
151  print ("## sizeof(%s) = %i" % \
152  (klassName,
153  self.Type.SizeOf(self.Type.ByName(klassName))))
154  print ("##")
155  print ("## (offset, data member name, data member type)")
156  print ("")
157  # we want to dump from the base to the most derived class
158  klassNames.reverse()
159  for klass in klassNames:
160  line = "%s %s %s" % (
161  "-" * (40-len(klass)//2-1),
162  "[%s]" % klass,
163  "-" * (40-len(klass)//2-1) )
164 
165  print (line)
166  self.report.append(line)
167  dataMembers = self.dumpDataMembers( self.Type.ByName(klass) )
168  for i in dataMembers:
169  line = "%3s %s %-50s %s %s" % ( str(i.offset),
170  " "*5,
171  i.name,
172  " "*5, i.type )
173  print (line)
174  self.report.append(line)
175  print ("#"*80)
176  return
177 
178  def save(self, fileName = "./columbo.out" ):
179  file = open(os.path.expandvars(os.path.expanduser(fileName)),
180  "w+")
181  for line in self.report:
182  file.writelines(line + os.linesep)
183  pass
184  file.close()
185 
186  pass # Columbo
187 
188 
189 if __name__ == '__main__':
190  if len(sys.argv) > 1:
191  klassName = sys.argv[1]
192  else:
193  klassName = "xAOD::TruthParticle_v1"
194  pass
195 
196  columbo = Columbo()
197  columbo.inspect(klassName)
198  columbo.save()
checkTP.FctMember
Definition: checkTP.py:50
checkTP.Columbo.gbl
gbl
Definition: checkTP.py:64
checkTP.dumpFctMembers
def dumpFctMembers(self, klass)
Definition: checkTP.py:117
checkTP.FctMember.name
name
Definition: checkTP.py:52
checkTP.Columbo.__init__
def __init__(self)
Definition: checkTP.py:58
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
checkTP.Columbo.report
report
Definition: checkTP.py:67
checkTP.save
def save(self, fileName="./columbo.out")
Definition: checkTP.py:178
checkTP.Columbo.loadDicts
def loadDicts(self, klassName)
Definition: checkTP.py:70
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
checkTP.DataMember.__init__
def __init__(self, offset, name, _type)
Definition: checkTP.py:44
checkTP.inspect
def inspect(self, klassName)
Definition: checkTP.py:126
checkTP.FctMember.__init__
def __init__(self, offset, name, _type)
Definition: checkTP.py:51
checkTP.FctMember.type
type
Definition: checkTP.py:53
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
checkTP.dumpDataMembers
def dumpDataMembers(self, klass)
for i in xrange(klass.DataMemberSize()): mbr = klass.DataMemberAt(i).TypeOf().Name(DICTSCOPE) klassNa...
Definition: checkTP.py:106
checkTP.DataMember
Definition: checkTP.py:43
checkTP.Columbo.Type
Type
Definition: checkTP.py:65
checkTP.DataMember.offset
offset
Definition: checkTP.py:45
Trk::open
@ open
Definition: BinningType.h:40
checkTP.Columbo
Definition: checkTP.py:56
checkTP.DataMember.name
name
Definition: checkTP.py:46
checkTP.DataMember.type
type
Definition: checkTP.py:47
pickleTool.object
object
Definition: pickleTool.py:30
str
Definition: BTagTrackIpAccessor.cxx:11