Chapter 7. Interactive Athena tutorial

Table of Contents

7.1. Getting started
7.2. Histogramming
7.3. A python algorithm

The ATHENA framework can be run interactively by loading it from the python interpreter, rather than starting the ATHENA executable. Background material on python scripting in ATHENA can be found in the ATHENA manual (chapter 6), or better yet, since the manual has not been updated in a while, in the excerpt located at http://cern.ch/wlav/scripting.

[Important]Important

Although this tutorial (and for that matter all other documentation) refers to steering ATHENA through python as "scripting," it is extremely important to realise, that this "scripting" is, in fact, programming.

This tutorial is written for developers of ATHENA algorithms, but most end-users will probably be able to follow along. This text shows how to write algorithms in such a way that they can be used effectively in interactive sessions, how to fill histograms with those algorithms, and how to retrieve these for subsequent display with an analysis package such as ROOT. It is assumed that ASK is properly set up on your system, and that your are working with v1.4; there may be subtle changes in future versions. Further details can be found in the accompanying transparencies. It is recommended to read at least the first part, which covers the issues that you need to be aware of when programming algorithms for interactive use in ATHENA.

7.1. Getting started

There are several ways of getting started with development for ATHENA. One, rather common, way is to take an already working, minimal package, either from a friendly colleague or from the CVS repository, and modify it. The first part of this tutorial will start along those lines: it shows how to create a new package and how to add ATHENA algorithms. Then it will go on to show how to interact with their properties on the ATHENA command prompt.

The transparencies corresponding to this part of the tutorial cover an introduction to ASK and some background on why the steps outlined below are taken and how the tool performs them.

7.1.1. Creating a new package

First, create a new, clean, work directory, and enter it:

$~> mkdir tutorial
$~> cd tutorial
$tutorial>

Startup the ASK tool in GUI mode with the ask -g command, or, if you run on a small screen, with the ask -g --detach command:

$tutorial> ask -g

The latter will start the GUI with two separate windows, which you can order at will, whereas the former will startup with just one window. ASK will start at the release screen, since there are no packages in the current working directory, inviting you to check out or to create a package. Fill in a new package name (e.g. "Example") in the top entry field, and select '8.8.1' (as appropriate) for the base release. Compare with the screen shot below:

Click create, and wait until the tool announces that the package creation is done.

If you take a look in your working directory, you will find that ASK created a new CMT package, with the appropriate structure for an ATHENA component library. Within the package, there is a directory with the same name as the package (here "Example") for include files, a share directory for options and other configuration files, and src and run directories with obvious purpose. A CMT requirements file has been created in the cmt directory and dependencies on some of the more common packages have been setup.

The next step is to add an algorithm to the package that can be used and modified later. Go to the maintenance tab and add the name of an algorithm (e.g. "Hello") in the algorithm names entry field. Compare with the screen shot below:

Click update, and wait until the tool announces that the package update is complete.

The update added skeleton source files for the algorithm (here "Hello"), and changed the ATHENA factory files, as well as the options file, to include the new algorithm. The package is now ready to be used.

Although it is possible to keep working with the GUI, the rest of the tutorial will be carried out using the ASK command line. You can exit the ASK GUI now.

7.1.2. Building and running

Start the ASK CLI from the working directory (here tutorial) with the ask command. The previously created package will be loaded automatically. Then, build it with the make command, as illustrated below:

$tutorial> ask
[...]
 ==>         : (re)loading packages ... Example
 ==>         : current directory is /home/wlav/tutorial/Example/Example-00-00-01
 ==>         : you are working with release 8.8.1
 ==> READY   : setup done for "Example"
[...]
Example> make
 ==>         : retrieving release tool ...
 ==>         : selecting cmt v1r16p20040901
[...]
------> (constituents.make) get_files done
 all ok.
Example>

There is an object called "Example" (or whatever package name you chose) available on the ASK command line. This object represents the package that has been created and has, among others, a data member called "scripts" that lists all the scripts that have been found in the package. Have a look at this variable by printing it (type "Example.scripts" and hit enter, see below). Although you can use any script that is available through the job options search path, it is more practical to work only with local scripts as the primary input script (since you can modify them). Finally, run ATHENA with the package standard job options script.

Example> Example.scripts
['run/Example_jobOptions.py']
Example> athena.py -b run/Example_jobOptions.py
 ==>         : starting Athena ...
[...]
Hello                INFO initialize() ... my message: <none> (0)
[...]
AthenaEventLoopMgr   INFO   ===>>>  start of run 0    <<<===
AthenaEventLoopMgr   INFO   ===>>>  start of event 1  <<<===
Hello                INFO execute() ... my message: >none< (0)
AthenaEventLoopMgr   INFO   ===>>>  end of event 1    <<<===
AthenaEventLoopMgr   INFO   ===>>>  start of event 2  <<<===
Hello                INFO execute() ... my message: >none< (0)
AthenaEventLoopMgr   INFO   ===>>>  end of event 2    <<<===
AthenaEventLoopMgr   INFO   ===>>>  start of event 3  <<<===
Hello                INFO execute() ... my message: <none> (0)
AthenaEventLoopMgr   INFO   ===>>>  end of event 3    <<<===
Hello                INFO finalize() ... my message: <none> (0)
[...]
ApplicationMgr       INFO Application Manager Finalized successfully
ApplicationMgr       INFO Application Manager Terminated successfully
 ==> READY   : run finished
Example>

Take particular notice of the "initialize()", "execute()" and "finalize()" messages, and look in the source code for the corresponding statements. The algorithm is printing the contents of two of its properties, "message" and "value."

7.1.3. Running interactively

In order to be able to run interactively, the python script, as used above, needs to return a prompt before exiting. Open the script (here Example_jobOptions.py in tutorial/Example/Example-00-00-01/run) in your favourite editor and look for the "theApp.exit()" call. Comment that call as well as the "theApp.run()" call by prepending the line with a '#', then rerun the script. That is:

Example> cat run/Example_jobOptions.py
[...]
# run the algorithm
theApp.EvtMax = 3
theApp.run( theApp.EvtMax )

# done, bye bye!
theApp.exit()

becomes:

Example> cat run/Example_jobOptions.py
[...]
# run the algorithm
theApp.EvtMax = 3
#theApp.run( theApp.EvtMax )

# done, bye bye!
#theApp.exit()

Then, after running the script again, you are presented with the ATHENA prompt:

Example> athena.py -i run/Example_jobOptions.py
 ==>         : starting Athena ...
[...]
ApplicationMgr       INFO Successfully loaded modules: Example
Athena               INFO entered interactive mode
Athena               INFO you can call theApp.run() to execute and use ctrl-d to exit
athena>

At this point, ATHENA is ready to receive commands.

If you read the script, you can see that it has instantiated an algorithm with the proper name (here "Hello"), and you can now look at its properties. However, if you do so, you will find that it has no properties yet, because the algorithm has not yet been instantiated on the C++ side. In order to do that, the ATHENA application manager must first be initialized,[14] a process that will load and instantiate the algorithm on the C++ side, after which its properties can be listed.

[Note]Note

Although the algorithm can not respond until it has been instantiated on the C++ side, it is entirely possible to work with it, including setting its properties, on the python side.

Feel free to explore. An example session could look like:

athena> theApp.algorithms()
[]
athena> Hello.properties()
{}
athena> Hello.value = 42
athena> Hello.properties()
{'value': 42}
athena> theApp.initialize()
[...]
Hello                INFO initialize() ... my message: <none> (42)
ApplicationMgr       INFO Application Manager Initialized successfully
SUCCESS
athena> theApp.algorithms()
['Hello']
athena> Hello.properties()
{'ErrorMax': 1, 'Enable': 1, 'AuditInitialize': 0,
 'AuditExecute': 1, 'OutputLevel': 0,
 'value': 42, 'AuditFinalize': 0,
 'message': '<none>', 'ErrorCount': 0}
athena> theApp.nextEvent()
AthenaEventLoopMgr   INFO   ===>>>  start of run 0    <<<===
AthenaEventLoopMgr   INFO   ===>>>  start of event 1  <<<===
Hello                INFO execute() ... my message: <none> (42)
AthenaEventLoopMgr   INFO   ===>>>  end of event 1    <<<===
SUCCESS
athena> Hello.message = 'hello!'
athena> theApp.nextEvent()
AthenaEventLoopMgr   INFO   ===>>>  start of event 2  <<<===
Hello                INFO execute() ... my message: hello! (42)
AthenaEventLoopMgr   INFO   ===>>>  end of event 2    <<<===
SUCCESS
athena> theApp.exit()
[...]
ApplicationMgr       INFO Application Manager Finalized successfully
ApplicationMgr       INFO Application Manager Terminated successfully
 ==> READY   : run finished
Example>

This concludes the basics part of running ATHENA interactively, next up is adding histogramming to the algorithm.



[14] Initialization of the application manager will also initialize all declared services. It can therefore not be done automatically, as it would deprive the user of setting any options for services before their initialization.