Implemented a Python Auditor, allowing to register python callbacks

This MR was built on top of !1724 (merged) and should thus be merged after it

The callbacks will be executed at the point specified (before or after any stage of any algorithm). In case of empty callback, an interactive python prompt is given.

Callbacks can have arguments. They need to use annotation to expose their types and the types used need to be registered via registerCallBackType together with a callable that will generate the value passed to the callback and has signature (evtNb : int, slot : int, subslot : int), meaning that it gets the EventContext. Here is a small example :

"""code from module TestMe"""
class Hello:
    pass
def getHello(evtNb, slot, subslot):
    if evtNb == 0:
        return "Hello World, English version"
    else:
        return "Bonjour monde, version francaise"
registerCallBackType(Hello, getHello)

def helloWorld(hello: Hello):
    return f"{hello}"

"""code form option file"""
AuditorSvc().Auditors.append(
    PythonAuditor( Callbacks=[ ( "Before", "MyAlgo", "Execute", "TestMe:helloWorld" ), ])

In case the type of an argument of a registered callable is Annotated[T, ...], T should be registered and the associated callable will be passed the metadata on top of the event context in a 4th argument of type dict. This allows to write code like :

"""code from the GaudiAud.wrappers module, provided in this commit"""
def getTES(evtNb, slot, subslot, metadata=("")):
    if len(metadata) != 1 or not metadata[0].isinstance(str):
        raise Exception(
            "Invalid usage of TES annotation. Should have a single string matadata"
        )
    return AppMgr().evtSvc(slot)[metadata[0]]
TES = iDataSvc
registerCallBackType(TES, getTES)

"""user code in module MyModule"""
from GaudiAud.wrappers import TES
def myCallBack(tracks: Annotated[TES, "/Event/MyTracks"]):
    for track in tracks:
        print("Track momentum : (", track.px(), track.py(), track.pz(), ")")

"""user option file"""
AuditorSvc().Auditors.append(
    PythonAuditor( Callbacks=[ ( "Before", "MyAlgo", "Execute", "MyModule:myCallBack" ), ])
Edited by Sebastien Ponce

Merge request reports

Loading