SLAC PEP-II
BABAR
SLAC<->RAL
Babar logo
HEPIC E,S & H Databases PDG HEP preprints
Organization Detector Computing Physics Documentation
Personnel Glossary Sitemap Search Hypernews
Workbook Home Introduction Account Setup QuickTour Packages Modules Unwrap page!
Event Information Tcl Commands Editing Compile and Link Run the Job Debugging
Check this page with the
W3C Validator
(More checks...)
Parameters Tcl Files Find Data Batch Analysis ROOT Tutorial

A Quick Guide to Tcl for users of BaBar code

BaBar's Tcl scripts mostly use the special BaBar Tcl commands. So you do not need to know much ordinary Tcl to be able to read and understand BaBar Tcl scripts. The following super-quick intro to Tcl should provide you with all the background you need.

Tcl basics

Tcl scripts are made up of commands separated by newlines or semicolons. The basic syntax for a Tcl command is:

command arg1 arg2 arg3

The first word of a Tcl statement must be either one of the standard Tcl commands, or a procedure (proc), which is a user-defined command. (The special BaBar Tcl commands are in fact just procs.)

Comments begin with #. Unlike many programming languages, Tcl comments cannot be placed on the same line as a command:

# This comment is OK.
set x 10     # This comment is not OK.

Variables

To assign a value to a variable, use the set command

set elephant "babar"
set pi 3.142

You don't need to declare variables in Tcl: a variable is created automatically the first time it is set. Tcl variables don't have types: any variable can hold any value. Here, the variable elephant is assigned the string value "babar", while the variable pi is assigned the number value 3.142.

To use the value of a variable in a command, you have to put a dollar sign ($) in front of it:

set a 5
set b 8
set c [expr $a + $b]

The dollar sign tells Tcl to use the value of the variable. This is called variable substitution.

The expr command evaluates the expression, and the square brackets, [ ], cause command substitution. So the the value of c is set to 13.

if/else

The syntax for the if command is similar to other programming languages:

if cond1 action1  
if cond1 action1 elseif action2 body2
if cond1 action1 elseif action2 body2 else action3

In BaBar code, the if command is often used to see if a variable exists:

if [info exists varName] {
      action1
      action2
      action3
}

The curly braces cause grouping. That is, as in C++, they make the "if" command apply to everything between the curly braces (all three actions), not just the first line (action1).

A note about the "set" command

Notice that the usual Tcl syntax for setting variables is inverted when you talk to a module:

In ordinary Tcl:

set bubble 20
But in a module talk session:
mod talk PillowModule
pillow set 100
exit

You don't really need to know why this is, as long as you remember it.

However if you're interested, it's because in the ordinary Tcl example, the command is "set" and the argument is "bubble." But in the module talk session, the command is "pillow set". This is a proc that has been defined thanks to the fact that "pillow" was appended to the module's command list. (Again, if this makes no sense to you, don't worry - you don't need to understand it.)


Back to Tcl files section