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
22 if not hasattr(Belle2, classname):
24 tclass = getattr(Belle2, classname).Class()
25 members = {e.GetName()
for e
in tclass.GetListOfMethods()
26 if (e.Property() & kIsPublic)
and not (e.Property() & kIsStatic)}
31 "CheckTObjectHashConsistency",
"Class",
"Class_Name",
"Class_Version",
32 "DeclFileLine",
"DeclFileName",
"Dictionary",
"ImplFileLine",
33 "ImplFileName",
"IsA",
"ShowMembers",
"Streamer",
"StreamerNVirtual",
34 "operator!=",
"operator=",
"operator==",
36 classname, f
"~{classname}",
39 return list(sorted(members))
44 Class to print contents of a StoreObjPtr or StoreArray.
46 This class is intended to print the contents of dataobjects to the standard
47 output to monitor changes to the contents among versions.
51 >>> printer = DataStorePrinter("MCParticle", ["getVertex"], {"hasStatus": [1, 2, 4]})
54 will loop over all MCParticle instances in the MCParticles StoreArray and
55 print something like ::
66 def __init__(self, name, simple, withArgument=None, array=True):
71 name (str): class name of the DataStore object
72 simple (list): list of member names to print which do not need any additional
74 withArgument (dict or None): dictionary of member names and a list of
75 all argument combinations to call them.
76 array (bool): if True we print a StoreArray, otherwise a single StoreObjPtr
92 for member, arguments
in withArgument.items():
93 for args
in arguments:
95 if not isinstance(args, list)
or isinstance(args, tuple):
105 def add_member(self, name, arguments=None, print_callback=None, display=None):
107 Add an additional member to be printed.
110 name (str): name of the member
111 arguments (list or callable): arguments to pass to the member when calling it
112 If this is a callable object then the function is called with
113 the object as first argument and the member name to be tested as
114 second argument. The function is supposed to return the list of
115 arguments to pass to the member when calling. Possible return
116 values for the callable are:
118 * a `list` of arguments to be passed to the member. An empty
119 `list` means to call the member with no arguments.
120 * a `tuple` of `lists <list>` to call the member once for each
121 list of arguments in the tuple
122 * `None` or an empty tuple to not call the member at all
123 print_callback (function or None): if not None a function to print
124 the result of the member call. The function will be called with
125 the arguments (name, arguments, result) and should print the
126 result on stdout without any additional information.
127 display (str or None): display string to use when printing member call
128 info instead of function name + arguments. If it is not given
129 the default output will be ``{membername}({arguments}):``
130 followed by the result.
132 if arguments
is None:
134 self.
object_members.append((name, arguments, print_callback, display))
139 """Print all the objects currently existing"""
142 if not data.isValid():
143 print(f
"No data for {self.name}")
145 for i, obj
in enumerate(data):
149 if not obj.isValid():
150 print(f
"No data for {self.name}")
156 """Print all the public member functions we will not test"""
157 members = get_public_members(self.
name)
159 for member
in members:
162 print(f
"Untested method {self.name}::{member}")
165 """Print all defined members for each object with given index.
166 If we print a StoreObjPtr then index is None and this function is called
167 once. If we print StoreArrays this function is called once for each
168 entry in the array with index set to the position in the array
171 if index
is not None:
172 index = f
"#{int(index)}"
176 print(f
"{self.name}{index}:")
183 if callable(arguments):
184 all_args = arguments(obj, name)
191 if isinstance(all_args, list):
192 all_args = (all_args,)
196 all_args = (arguments,)
198 for args
in all_args:
199 result = getattr(obj, name)(*args)
202 if display
is not None:
203 print(
" " + display +
": ", end=
"")
206 print(f
" {name}({','.join(map(str, args))}): ", end=
"")
208 if callback
is not None:
209 print(
"", end=
"", flush=
True)
210 callback(name, args, result)
216 """ Print the result of calling a certain member.
217 As we want the test to be independent of memory we have to be a bit careful
218 how to not just print() but check whether the object is maybe a pointer to another
219 DataStore object or if it is a TObject with implements Print().
220 Also, call recursively std::pair
223 result: object to print
224 depth (int): depth for recursive printing, controls the level of indent
225 weight (float or None): weight to print in addition to object, only used for
230 print(
" " * (depth + 1), end=
"")
232 if weight
is not None:
233 weight = f
" (weight: {weight:.6g})"
238 if hasattr(result,
"getArrayName")
and hasattr(result,
"getArrayIndex"):
240 print(f
"-> NULL{weight}")
242 print(f
"-> {result.getArrayName()}#{int(result.getArrayIndex())}{weight}")
244 elif hasattr(result,
"GetNrows")
and hasattr(result,
"GetNcols"):
245 print(weight, end=
"")
246 for row
in range(result.GetNrows()):
247 print(
"\n" +
" " * (depth + 2), end=
"")
248 for col
in range(result.GetNcols()):
249 print(f
"{result(row, col):13.6e} ", end=
"")
253 elif isinstance(result, TVector3):
254 print(
"(" +
",".join(f
"{result[i]:.6g}" for i
in range(3)) +
")")
255 elif isinstance(result, XYZVector):
256 print(f
"({result.X():.6g},{result.Y():.6g},{result.Z():.6g})")
257 elif isinstance(result, TLorentzVector):
258 print(
"(" +
",".join(f
"{result[i]:.6g}" for i
in range(4)) +
")")
260 elif hasattr(result,
"first")
and hasattr(result,
"second"):
261 print(f
"pair{weight}")
265 elif (hasattr(result,
"size")
and hasattr(result,
"begin")
266 and hasattr(result,
"end"))
and not hasattr(result,
"npos") \
267 and not isinstance(result, Const.DetectorSet):
268 print(f
"size({int(result.size())}){weight}")
271 weight_getter = getattr(result,
"weight",
None)
275 for i, e
in enumerate(result):
276 if weight_getter
is not None:
277 weight = weight_getter(i)
280 elif isinstance(result, float):
281 print(f
"{result:.6g}{weight}")
283 elif isinstance(result, str)
and len(result) == 1:
284 print(ord(result), weight, sep=
"")
287 print(result, weight, sep=
"")
294 """Call all DataStorePrinter objects in for each event"""
296 def __init__(self, objects_to_print, print_untested=False):
301 objects_to_print (list): list of object to print
310 """Print all untested members if requested"""
315 printer.print_untested()
318 """print the contents of the mdst mdst_dataobjects"""
322 except Exception
as e:
323 B2FATAL(
"Error in datastore printer: ", e)
A (simplified) python wrapper for StoreArray.
a (simplified) python wrapper for StoreObjPtr.
__init__(self, name, simple, withArgument=None, array=True)
_printObj(self, obj, index=None)
array
if True we print a StoreArray, otherwise a single StoreObjPtr
_printResult(self, result, depth=0, weight=None)
name
class name of the datastore object
add_member(self, name, arguments=None, print_callback=None, display=None)
list object_members
list of object members to call and print their results
__init__(self, objects_to_print, print_untested=False)
objects_to_print
list of object to print
print_untested
print untested members?