11from ROOT
import Belle2, kIsPublic, kIsStatic, TVector3, TLorentzVector
12from ROOT.Belle2
import Const
14from basf2
import Module, B2FATAL
17def get_public_members(classname):
19 Return a list of public, non-static member functions for a given classname.
20 The
class must exist in the
Belle2 namespace and have a ROOT dictionary
"""
21 tclass = getattr(Belle2, classname).Class()
22 members = {e.GetName() for e
in tclass.GetListOfMethods()
23 if (e.Property() & kIsPublic)
and not (e.Property() & kIsStatic)}
28 "CheckTObjectHashConsistency",
"Class",
"Class_Name",
"Class_Version",
29 "DeclFileLine",
"DeclFileName",
"Dictionary",
"ImplFileLine",
30 "ImplFileName",
"IsA",
"ShowMembers",
"Streamer",
"StreamerNVirtual",
31 "operator!=",
"operator=",
"operator==",
33 classname, f
"~{classname}",
36 return list(sorted(members))
39class DataStorePrinter:
41 Class to print contents of a StoreObjPtr
or StoreArray.
43 This
class is inteded to
print the contents of dataobjects to the standard
44 output to monitor changes to the contents among versions.
48 >>> printer =
DataStorePrinter(
"MCParticle", [
"getVertex"], {
"hasStatus": [1, 2, 4]})
51 will loop over all MCParticle instances
in the MCParticles StoreArray
and
52 print someting like ::
63 def __init__(self, name, simple, withArgument=None, array=True):
68 name (str): class name of the DataStore object
69 simple (list): list of member names to
print which do
not need any additional
71 withArgument (dict
or None): dictionary of member names
and a list of
72 all argument combinations to call them.
73 array (bool):
if True we
print a StoreArray, otherwise a single StoreObjPtr
89 for member, arguments
in withArgument.items():
90 for args
in arguments:
92 if not isinstance(args, list)
or isinstance(args, tuple):
102 def add_member(self, name, arguments=None, print_callback=None, display=None):
104 Add an additional member to be printed.
107 name (str): name of the member
108 arguments (list or callable): arguments to
pass to the member when calling it
109 If this
is a callable object then the function
is called
with
110 the object
as first argument
and the member name to be tested
as
111 second argument. The function
is supposed to
return the list of
112 arguments to
pass to the member when calling. Possible
return
113 valus
for the callable are:
115 * a `list` of arguments to be passed to the member. An empty
116 `list` means to call the member
with no arguments.
117 * a `tuple` of `lists <list>` to call the member once
for each
118 list of arguments
in the tuple
119 * `
None`
or an empty tuple to
not call the member at all
120 print_callback (function
or None):
if not None a function to
print
121 the result of the member call. The function will be called
with
122 the arguments (name, arguments, result)
and should
print the
123 result on stdout without any additional information.
124 display (str
or None): display string to use when printing member call
125 info instead of function name + arguments. If it
is not given
126 the default output will be ``{membername}({arguments}):``
127 followed by the result.
129 if arguments
is None:
131 self.
object_members.append((name, arguments, print_callback, display))
136 """Print all the objects currently existing"""
139 for i, obj
in enumerate(data):
147 """Print all the public member functions we will not test"""
148 members = get_public_members(self.
name)
150 for member
in members:
153 print(f
"Untested method {self.name}::{member}")
156 """Print all defined members for each object with given index.
157 If we print a StoreObjPtr then index
is None and this function
is called
158 once. If we
print StoreArrays this function
is called once
for each
159 entry
in the array
with index set to the position
in the array
162 if index
is not None:
163 index = f
"#{int(index)}"
167 print(f
"{self.name}{index}:")
174 if callable(arguments):
175 all_args = arguments(obj, name)
182 if isinstance(all_args, list):
183 all_args = (all_args,)
187 all_args = (arguments,)
189 for args
in all_args:
190 result = getattr(obj, name)(*args)
193 if display
is not None:
194 print(
" " + display +
": ", end=
"")
197 print(f
" {name}({','.join(map(str, args))}): ", end=
"")
199 if callback
is not None:
200 print(
"", end=
"", flush=
True)
201 callback(name, args, result)
207 """ Print the result of calling a certain member.
208 As we want the test to be independent of memory we have to be a bit careful
209 how to not just
print() but check whether the object
is maybe a pointer to another
210 DataStore object
or if it
is a TObject
with implements Print().
211 Also, call recursively std::pair
214 result: object to
print
215 depth (int): depth
for recursive printing, controls the level of indent
216 weight (float
or None): weight to
print in addition to object, only used
for
221 print(
" " * (depth + 1), end=
"")
223 if weight
is not None:
224 weight = f
" (weight: {weight:.6g})"
229 if hasattr(result,
"getArrayName")
and hasattr(result,
"getArrayIndex"):
231 print(f
"-> NULL{weight}")
233 print(f
"-> {result.getArrayName()}#{int(result.getArrayIndex())}{weight}")
235 elif hasattr(result,
"GetNrows")
and hasattr(result,
"GetNcols"):
236 print(weight, end=
"")
237 for row
in range(result.GetNrows()):
238 print(
"\n" +
" " * (depth + 2), end=
"")
239 for col
in range(result.GetNcols()):
240 print(f
"{result(row, col):13.6e} ", end=
"")
244 elif isinstance(result, TVector3):
245 print(
"(" +
",".join(f
"{result[i]:.6g}" for i
in range(3)) +
")")
246 elif isinstance(result, XYZVector):
247 print(
"(" +
",".join(f
"{Belle2.B2Vector3D(result)[i]:.6g}" for i
in range(3)) +
")")
249 elif isinstance(result, TLorentzVector):
250 print(
"(" +
",".join(f
"{result[i]:.6g}" for i
in range(4)) +
")")
252 elif hasattr(result,
"first")
and hasattr(result,
"second"):
253 print(f
"pair{weight}")
257 elif (hasattr(result,
"size")
and hasattr(result,
"begin")
258 and hasattr(result,
"end"))
and not hasattr(result,
"npos") \
259 and not isinstance(result, Const.DetectorSet):
260 print(f
"size({int(result.size())}){weight}")
263 weight_getter = getattr(result,
"weight",
None)
267 for i, e
in enumerate(result):
268 if weight_getter
is not None:
269 weight = weight_getter(i)
272 elif isinstance(result, float):
273 print(f
"{result:.6g}{weight}")
275 elif isinstance(result, str)
and len(result) == 1:
276 print(ord(result), weight, sep=
"")
279 print(result, weight, sep=
"")
286 """Call all DataStorePrinter objects in for each event"""
288 def __init__(self, objects_to_print, print_untested=False):
293 objects_to_print (list): list of object to print
302 """Print all untested members if requested"""
307 printer.print_untested()
310 """print the contents of the mdst mdst_dataobjects"""
314 except Exception
as e:
315 B2FATAL(
"Error in datastore printer: ", e)
A (simplified) python wrapper for StoreArray.
a (simplified) python wrapper for StoreObjPtr.
def __init__(self, name, simple, withArgument=None, array=True)
def _printObj(self, obj, index=None)
array
if True we print a StoreArray, otherwise a single StoreObjPtr
def add_member(self, name, arguments=None, print_callback=None, display=None)
def _printResult(self, result, depth=0, weight=None)
object_members
list of object members to call and print their results
name
class name of the datastore object
objects_to_print
list of object to print
def __init__(self, objects_to_print, print_untested=False)
print_untested
print untested members?
Abstract base class for different kinds of events.