4 from ROOT
import Belle2, kIsPublic, kIsStatic, TVector3, TLorentzVector
5 from basf2
import Module
8 def get_public_members(classname):
10 Return a list of public, non-static member functions for a given classname.
11 The class must exist in the Belle2 namespace and have a ROOT dictionary
13 tclass = getattr(Belle2, classname).Class()
14 members = {e.GetName()
for e
in tclass.GetListOfMethods()
15 if (e.Property() & kIsPublic)
and not (e.Property() & kIsStatic)}
20 "CheckTObjectHashConsistency",
"Class",
"Class_Name",
"Class_Version",
21 "DeclFileLine",
"DeclFileName",
"Dictionary",
"ImplFileLine",
22 "ImplFileName",
"IsA",
"ShowMembers",
"Streamer",
"StreamerNVirtual",
23 "operator!=",
"operator=",
"operator==",
25 classname, f
"~{classname}",
28 return list(sorted(members))
33 Class to print contents of a StoreObjPtr or StoreArray.
35 This class is inteded to print the contents of dataobjects to the standard
36 output to monitor changes to the contents among versions.
40 >>> printer = DataStorePrinter("MCParticle", ["getVertex"], {"hasStatus": [1, 2, 4]})
43 will loop over all MCParticle instances in the MCParticles StoreArray and
44 print someting like ::
55 def __init__(self, name, simple, withArgument=None, array=True):
60 name (str): class name of the DataStore object
61 simple (list): list of member names to print which do not need any additional
63 withArgument (dict or None): dictionary of member names and a list of
64 all argument combinations to call them.
65 array (bool): if True we print a StoreArray, otherwise a single StoreObjPtr
81 for member, arguments
in withArgument.items():
82 for args
in arguments:
84 if not isinstance(args, list)
or isinstance(args, tuple):
94 def add_member(self, name, arguments=[], print_callback=None, display=None):
96 Add an additional member to be printed.
99 name (str): name of the member
100 arguments (list or callable): arguments to pass to the member when calling it
101 If this is a callable object then the function is called with
102 the object as first argument and the member name to be tested as
103 second argument. The function is supposed to return the list of
104 arguments to pass to the member when calling. Possible return
105 valus for the callable are:
107 * a `list` of arguments to be passed to the member. An empty
108 `list` means to call the member with no arguments.
109 * a `tuple` of `lists <list>` to call the member once for each
110 list of arguments in the tuple
111 * `None` or an empty tuple to not call the member at all
112 print_callback (function or None): if not None a function to print
113 the result of the member call. The function will be called with
114 the arguments (name, arguments, result) and should print the
115 result on stdout without any additional information.
116 display (str or None): display string to use when printing member call
117 info instead of function name + arguments. If it is not given
118 the default output will be ``{membername}({arguments}):``
119 followed by the result.
121 self.
object_members.append((name, arguments, print_callback, display))
126 """Print all the objects currently existing"""
129 for i, obj
in enumerate(data):
137 """Print all the public member functions we will not test"""
138 members = get_public_members(self.
name)
140 for member
in members:
143 print(f
"Untested method {self.name}::{member}")
146 """Print all defined members for each object with given index.
147 If we print a StoreObjPtr then index is None and this function is called
148 once. If we print StoreArrays this function is called once for each
149 entry in the array with index set to the position in the array
152 if index
is not None:
153 index =
"#%d" % index
164 if callable(arguments):
165 all_args = arguments(obj, name)
172 if isinstance(all_args, list):
173 all_args = (all_args,)
177 all_args = (arguments,)
179 for args
in all_args:
180 result = getattr(obj, name)(*args)
183 if display
is not None:
184 print(
" " + display +
": ", end=
"")
187 print(
" %s(%s): " % (name,
",".join(map(str, args))), end=
"")
189 if callback
is not None:
190 print(
"", end=
"", flush=
True)
191 callback(name, args, result)
197 """ Print the result of calling a certain member.
198 As we want the test to be independent of memory we have to be a bit careful
199 how to not just print() but check whether the object is maybe a pointer to another
200 DataStore object or if it is a TObject with implements Print().
201 Also, call recursively std::pair
204 result: object to print
205 depth (int): depth for recursive printing, controls the level of indent
206 weight (float or None): weight to print in addition to object, only used for
211 print(
" " * (depth + 1), end=
"")
213 if weight
is not None:
214 weight =
" (weight: %.6g)" % weight
219 if hasattr(result,
"getArrayName")
and hasattr(result,
"getArrayIndex"):
221 print(
"-> NULL%s" % weight)
223 print(
"-> %s#%d%s" % (result.getArrayName(), result.getArrayIndex(), weight))
225 elif hasattr(result,
"GetNrows")
and hasattr(result,
"GetNcols"):
226 print(weight, end=
"")
227 for row
in range(result.GetNrows()):
228 print(
"\n" +
" " * (depth + 2), end=
"")
229 for col
in range(result.GetNcols()):
230 print(
"%13.6e " % result(row, col), end=
"")
234 elif isinstance(result, TVector3):
235 print(
"(" +
",".join(
"%.6g" % result[i]
for i
in range(3)) +
")")
236 elif isinstance(result, TLorentzVector):
237 print(
"(" +
",".join(
"%.6g" % result[i]
for i
in range(4)) +
")")
239 elif hasattr(result,
"first")
and hasattr(result,
"second"):
240 print(
"pair%s" % weight)
244 elif hasattr(result,
"size")
and hasattr(result,
"begin")
and hasattr(result,
"end"):
245 print(
"size(%d)%s" % (result.size(), weight))
248 weight_getter = getattr(result,
"weight",
None)
252 for i, e
in enumerate(result):
253 if weight_getter
is not None:
254 weight = weight_getter(i)
257 elif isinstance(result, float):
258 print(
"%.6g%s" % (result, weight))
260 elif isinstance(result, str)
and len(result) == 1:
261 print(ord(result), weight, sep=
"")
264 print(result, weight, sep=
"")
271 """Call all DataStorePrinter objects in for each event"""
273 def __init__(self, objects_to_print, print_untested=False):
278 objects_to_print (list): list of object to print
287 """Print all untested members if requested"""
292 printer.print_untested()
295 """print the contents of the mdst mdst_dataobjects"""