11 from ROOT
import Belle2, kIsPublic, kIsStatic, TVector3, TLorentzVector
12 from ROOT.Belle2
import Const
14 from basf2
import Module, B2FATAL
17 def 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
22 tclass = getattr(Belle2, classname).Class()
23 members = {e.GetName()
for e
in tclass.GetListOfMethods()
24 if (e.Property() & kIsPublic)
and not (e.Property() & kIsStatic)}
29 "CheckTObjectHashConsistency",
"Class",
"Class_Name",
"Class_Version",
30 "DeclFileLine",
"DeclFileName",
"Dictionary",
"ImplFileLine",
31 "ImplFileName",
"IsA",
"ShowMembers",
"Streamer",
"StreamerNVirtual",
32 "operator!=",
"operator=",
"operator==",
34 classname, f
"~{classname}",
37 return list(sorted(members))
42 Class to print contents of a StoreObjPtr or StoreArray.
44 This class is inteded to print the contents of dataobjects to the standard
45 output to monitor changes to the contents among versions.
49 >>> printer = DataStorePrinter("MCParticle", ["getVertex"], {"hasStatus": [1, 2, 4]})
52 will loop over all MCParticle instances in the MCParticles StoreArray and
53 print someting like ::
64 def __init__(self, name, simple, withArgument=None, array=True):
69 name (str): class name of the DataStore object
70 simple (list): list of member names to print which do not need any additional
72 withArgument (dict or None): dictionary of member names and a list of
73 all argument combinations to call them.
74 array (bool): if True we print a StoreArray, otherwise a single StoreObjPtr
90 for member, arguments
in withArgument.items():
91 for args
in arguments:
93 if not isinstance(args, list)
or isinstance(args, tuple):
96 self.
object_membersobject_members.append((member, args,
None,
None))
103 def add_member(self, name, arguments=None, print_callback=None, display=None):
105 Add an additional member to be printed.
108 name (str): name of the member
109 arguments (list or callable): arguments to pass to the member when calling it
110 If this is a callable object then the function is called with
111 the object as first argument and the member name to be tested as
112 second argument. The function is supposed to return the list of
113 arguments to pass to the member when calling. Possible return
114 valus for the callable are:
116 * a `list` of arguments to be passed to the member. An empty
117 `list` means to call the member with no arguments.
118 * a `tuple` of `lists <list>` to call the member once for each
119 list of arguments in the tuple
120 * `None` or an empty tuple to not call the member at all
121 print_callback (function or None): if not None a function to print
122 the result of the member call. The function will be called with
123 the arguments (name, arguments, result) and should print the
124 result on stdout without any additional information.
125 display (str or None): display string to use when printing member call
126 info instead of function name + arguments. If it is not given
127 the default output will be ``{membername}({arguments}):``
128 followed by the result.
130 if arguments
is None:
132 self.
object_membersobject_members.append((name, arguments, print_callback, display))
137 """Print all the objects currently existing"""
140 for i, obj
in enumerate(data):
148 """Print all the public member functions we will not test"""
149 members = get_public_members(self.
namename)
151 for member
in members:
154 print(f
"Untested method {self.name}::{member}")
157 """Print all defined members for each object with given index.
158 If we print a StoreObjPtr then index is None and this function is called
159 once. If we print StoreArrays this function is called once for each
160 entry in the array with index set to the position in the array
163 if index
is not None:
164 index =
"#%d" % index
168 print(f
"{self.name}{index}:")
172 for name, arguments, callback, display
in self.
object_membersobject_members:
175 if callable(arguments):
176 all_args = arguments(obj, name)
183 if isinstance(all_args, list):
184 all_args = (all_args,)
188 all_args = (arguments,)
190 for args
in all_args:
191 result = getattr(obj, name)(*args)
194 if display
is not None:
195 print(
" " + display +
": ", end=
"")
198 print(
" {}({}): ".format(name,
",".join(map(str, args))), end=
"")
200 if callback
is not None:
201 print(
"", end=
"", flush=
True)
202 callback(name, args, result)
208 """ Print the result of calling a certain member.
209 As we want the test to be independent of memory we have to be a bit careful
210 how to not just print() but check whether the object is maybe a pointer to another
211 DataStore object or if it is a TObject with implements Print().
212 Also, call recursively std::pair
215 result: object to print
216 depth (int): depth for recursive printing, controls the level of indent
217 weight (float or None): weight to print in addition to object, only used for
222 print(
" " * (depth + 1), end=
"")
224 if weight
is not None:
225 weight =
" (weight: %.6g)" % weight
230 if hasattr(result,
"getArrayName")
and hasattr(result,
"getArrayIndex"):
232 print(
"-> NULL%s" % weight)
234 print(
"-> %s#%d%s" % (result.getArrayName(), result.getArrayIndex(), weight))
236 elif hasattr(result,
"GetNrows")
and hasattr(result,
"GetNcols"):
237 print(weight, end=
"")
238 for row
in range(result.GetNrows()):
239 print(
"\n" +
" " * (depth + 2), end=
"")
240 for col
in range(result.GetNcols()):
241 print(
"%13.6e " % result(row, col), end=
"")
245 elif isinstance(result, TVector3):
246 print(
"(" +
",".join(
"%.6g" % result[i]
for i
in range(3)) +
")")
247 elif isinstance(result, XYZVector):
250 elif isinstance(result, TLorentzVector):
251 print(
"(" +
",".join(
"%.6g" % result[i]
for i
in range(4)) +
")")
253 elif hasattr(result,
"first")
and hasattr(result,
"second"):
254 print(
"pair%s" % weight)
258 elif (hasattr(result,
"size")
and hasattr(result,
"begin")
259 and hasattr(result,
"end"))
and not hasattr(result,
"npos") \
260 and not isinstance(result, Const.DetectorSet):
261 print(
"size(%d)%s" % (result.size(), weight))
264 weight_getter = getattr(result,
"weight",
None)
268 for i, e
in enumerate(result):
269 if weight_getter
is not None:
270 weight = weight_getter(i)
271 self.
_printResult_printResult(e, depth + 1, weight=weight)
273 elif isinstance(result, float):
274 print(f
"{result:.6g}{weight}")
276 elif isinstance(result, str)
and len(result) == 1:
277 print(ord(result), weight, sep=
"")
280 print(result, weight, sep=
"")
287 """Call all DataStorePrinter objects in for each event"""
289 def __init__(self, objects_to_print, print_untested=False):
294 objects_to_print (list): list of object to print
303 """Print all untested members if requested"""
308 printer.print_untested()
311 """print the contents of the mdst mdst_dataobjects"""
315 except Exception
as e:
316 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?