11 from ROOT
import Belle2, kIsPublic, kIsStatic, TVector3, TLorentzVector
12 from basf2
import Module, B2FATAL
15 def get_public_members(classname):
17 Return a list of public, non-static member functions for a given classname.
18 The class must exist in the Belle2 namespace and have a ROOT dictionary
20 tclass = getattr(Belle2, classname).Class()
21 members = {e.GetName()
for e
in tclass.GetListOfMethods()
22 if (e.Property() & kIsPublic)
and not (e.Property() & kIsStatic)}
27 "CheckTObjectHashConsistency",
"Class",
"Class_Name",
"Class_Version",
28 "DeclFileLine",
"DeclFileName",
"Dictionary",
"ImplFileLine",
29 "ImplFileName",
"IsA",
"ShowMembers",
"Streamer",
"StreamerNVirtual",
30 "operator!=",
"operator=",
"operator==",
32 classname, f
"~{classname}",
35 return list(sorted(members))
40 Class to print contents of a StoreObjPtr or StoreArray.
42 This class is inteded to print the contents of dataobjects to the standard
43 output to monitor changes to the contents among versions.
47 >>> printer = DataStorePrinter("MCParticle", ["getVertex"], {"hasStatus": [1, 2, 4]})
50 will loop over all MCParticle instances in the MCParticles StoreArray and
51 print someting like ::
62 def __init__(self, name, simple, withArgument=None, array=True):
67 name (str): class name of the DataStore object
68 simple (list): list of member names to print which do not need any additional
70 withArgument (dict or None): dictionary of member names and a list of
71 all argument combinations to call them.
72 array (bool): if True we print a StoreArray, otherwise a single StoreObjPtr
88 for member, arguments
in withArgument.items():
89 for args
in arguments:
91 if not isinstance(args, list)
or isinstance(args, tuple):
94 self.
object_membersobject_members.append((member, args,
None,
None))
101 def add_member(self, name, arguments=None, print_callback=None, display=None):
103 Add an additional member to be printed.
106 name (str): name of the member
107 arguments (list or callable): arguments to pass to the member when calling it
108 If this is a callable object then the function is called with
109 the object as first argument and the member name to be tested as
110 second argument. The function is supposed to return the list of
111 arguments to pass to the member when calling. Possible return
112 valus for the callable are:
114 * a `list` of arguments to be passed to the member. An empty
115 `list` means to call the member with no arguments.
116 * a `tuple` of `lists <list>` to call the member once for each
117 list of arguments in the tuple
118 * `None` or an empty tuple to not call the member at all
119 print_callback (function or None): if not None a function to print
120 the result of the member call. The function will be called with
121 the arguments (name, arguments, result) and should print the
122 result on stdout without any additional information.
123 display (str or None): display string to use when printing member call
124 info instead of function name + arguments. If it is not given
125 the default output will be ``{membername}({arguments}):``
126 followed by the result.
128 if arguments
is None:
130 self.
object_membersobject_members.append((name, arguments, print_callback, display))
135 """Print all the objects currently existing"""
138 for i, obj
in enumerate(data):
146 """Print all the public member functions we will not test"""
147 members = get_public_members(self.
namename)
149 for member
in members:
152 print(f
"Untested method {self.name}::{member}")
155 """Print all defined members for each object with given index.
156 If we print a StoreObjPtr then index is None and this function is called
157 once. If we print StoreArrays this function is called once for each
158 entry in the array with index set to the position in the array
161 if index
is not None:
162 index =
"#%d" % index
166 print(f
"{self.name}{index}:")
170 for name, arguments, callback, display
in self.
object_membersobject_members:
173 if callable(arguments):
174 all_args = arguments(obj, name)
181 if isinstance(all_args, list):
182 all_args = (all_args,)
186 all_args = (arguments,)
188 for args
in all_args:
189 result = getattr(obj, name)(*args)
192 if display
is not None:
193 print(
" " + display +
": ", end=
"")
196 print(
" {}({}): ".format(name,
",".join(map(str, args))), end=
"")
198 if callback
is not None:
199 print(
"", end=
"", flush=
True)
200 callback(name, args, result)
206 """ Print the result of calling a certain member.
207 As we want the test to be independent of memory we have to be a bit careful
208 how to not just print() but check whether the object is maybe a pointer to another
209 DataStore object or if it is a TObject with implements Print().
210 Also, call recursively std::pair
213 result: object to print
214 depth (int): depth for recursive printing, controls the level of indent
215 weight (float or None): weight to print in addition to object, only used for
220 print(
" " * (depth + 1), end=
"")
222 if weight
is not None:
223 weight =
" (weight: %.6g)" % weight
228 if hasattr(result,
"getArrayName")
and hasattr(result,
"getArrayIndex"):
230 print(
"-> NULL%s" % weight)
232 print(
"-> %s#%d%s" % (result.getArrayName(), result.getArrayIndex(), weight))
234 elif hasattr(result,
"GetNrows")
and hasattr(result,
"GetNcols"):
235 print(weight, end=
"")
236 for row
in range(result.GetNrows()):
237 print(
"\n" +
" " * (depth + 2), end=
"")
238 for col
in range(result.GetNcols()):
239 print(
"%13.6e " % result(row, col), end=
"")
243 elif isinstance(result, TVector3):
244 print(
"(" +
",".join(
"%.6g" % result[i]
for i
in range(3)) +
")")
245 elif isinstance(result, TLorentzVector):
246 print(
"(" +
",".join(
"%.6g" % result[i]
for i
in range(4)) +
")")
248 elif hasattr(result,
"first")
and hasattr(result,
"second"):
249 print(
"pair%s" % weight)
253 elif (hasattr(result,
"size")
and hasattr(result,
"begin")
and hasattr(result,
"end"))
and not hasattr(result,
"npos"):
254 print(
"size(%d)%s" % (result.size(), weight))
257 weight_getter = getattr(result,
"weight",
None)
261 for i, e
in enumerate(result):
262 if weight_getter
is not None:
263 weight = weight_getter(i)
264 self.
_printResult_printResult(e, depth + 1, weight=weight)
266 elif isinstance(result, float):
267 print(f
"{result:.6g}{weight}")
269 elif isinstance(result, str)
and len(result) == 1:
270 print(ord(result), weight, sep=
"")
273 print(result, weight, sep=
"")
280 """Call all DataStorePrinter objects in for each event"""
282 def __init__(self, objects_to_print, print_untested=False):
287 objects_to_print (list): list of object to print
296 """Print all untested members if requested"""
301 printer.print_untested()
304 """print the contents of the mdst mdst_dataobjects"""
308 except Exception
as e:
309 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?