Python Configuration and Execution

Load and Register an Algorithm

After the compilation, a dynamic library is generated. Suppose it is named as "libFirstAlg.so". We can load the library in python like this,

import Sniper
Sniper.loadDll("libFirstAlg.so")

Then the algorithm FirstAlg is automatically registered and can be used in SNiPER.

However, if we want to use the package in Python style, we can wrap the previous code in a Python module. In the FirstAlg package, we can make the Python module by adding the code in the following file (please keep the directory structure),

python/FirstAlg/__init__.py

Then we can load and register FirstAlg with a single line in the main Python configuration script,

import FirstAlg

A SNiPER application is always start from a Task instance. When an algorithm is registered, we can create its instance via the Task,

task.createAlg("FirstAlg")

Configure a Job

The following Python code shows a complete executable SNiPER application,

#!/usr/bin/env python

import Sniper
task = Sniper.Task("task")  # create a Task instance
task.setEvtMax(3)  # events loop number (3 times)
task.setLogLevel(2)  # the SniperLog print level

import FirstAlg
alg = task.createAlg("FirstAlg")  # create a FirstAlg instance
alg.property("TheValue").set(2)
alg.property("Message").set(" the value is ")

task.run()

In the example, we will execute the FirstAlg 3 time during events loop. The 2 properties are set via their names. The values of the correlated C++ variables will be directly modified. The result of this configuration is,

$ python run.py
*****************************************
***     Welcome to SNiPER Python      ***
*****************************************
Running @ lxslc603.ihep.ac.cn on Mon Dec 10 10:18:39 2017

task:FirstAlg.initialize      DEBUG: in the FirstAlg::initialize()
task.initialize                INFO: initialized
task:FirstAlg.execute         DEBUG: in the FirstAlg::execute()
task:FirstAlg.execute          INFO: Loop 1 the value is 2
task:FirstAlg.execute         DEBUG: in the FirstAlg::execute()
task:FirstAlg.execute          INFO: Loop 2 the value is 4
task:FirstAlg.execute         DEBUG: in the FirstAlg::execute()
task:FirstAlg.execute          INFO: Loop 3 the value is 12
task:FirstAlg.finalize        DEBUG: in the FirstAlg::finalize()
task.finalize                  INFO: finalized

***  SNiPER Terminated Successfully!  ***

DLE Instance Name

In the previous section, we defined a constructor with a string parameter in FirstAlg. That means we can assign a name to the algorithm instance. In fact, each DLE (algorithm, service and task) instance has a name, and the name can be customizd. The default name of algorithm/service is its class name. We can assign a different one when we create the instance,

task.createAlg("FirstAlg/NewName")

The sub-string after "/" is the instance name we assigned.

In this way, we can create more than one instances of an algorithm without conflicts. For example,

#!/usr/bin/env python

import Sniper
task = Sniper.Task("App")
task.setEvtMax(3)
task.setLogLevel(3)

import FirstAlg
alg1 = task.createAlg("FirstAlg/Alg1")
alg1.property("TheValue").set(2)
alg1.property("Message").set(" the value is ")

alg2 = task.createAlg("FirstAlg/Alg2")
alg2.property("TheValue").set(20)
alg2.property("Message").set(" the value is ")

task.run()

In this example, we create 2 instances of FirstAlg with different names Alg1 and Alg2. Please notice that,

  • we assign a new name ("App") to the Task instance, too;
  • we changed the log level to 3, so the DEBUG messages will not be printed;
  • the 2 algorithm instances, Alg1 and Alg2, have different property values;

Then the result of this configuration is,

$ python run.py
*****************************************
***     Welcome to SNiPER Python      ***
*****************************************
Running @ lxslc603.ihep.ac.cn on Mon Dec 10 10:56:17 2017

App.initialize                 INFO: initialized
App:Alg1.execute               INFO: Loop 1 the value is 2
App:Alg2.execute               INFO: Loop 1 the value is 20
App:Alg1.execute               INFO: Loop 2 the value is 4
App:Alg2.execute               INFO: Loop 2 the value is 40
App:Alg1.execute               INFO: Loop 3 the value is 12
App:Alg2.execute               INFO: Loop 3 the value is 120
App.finalize                   INFO: finalized

***  SNiPER Terminated Successfully!  ***

Please compare the logs and find the difference to the previous example. It is helpful to understand the DLE names and the SniperLog.

results matching ""

    No results matching ""