You are here

Interact with cling at ROOT's prompt

ROOT's prompt root [0] is in fact cling! It allows you to call functions, load files, etc. Lets look at the power of the prompt:

Running C++

You can simply type C++ code at the prompt: sin(0.1) will print the sin of 0.1, as one would expect. All ROOT classes and their functions are available, too, so you can use them at the prompt.

Interacting with cling

cling meta commands
quit ROOT
.L file.C
Load file.C
.x file.C
Load file.C and run its function file()
.U file.C
Unload file.C
.class C
Print what cling knows about class C
Show all loaded files
Show include path
.O0 (dot oh-zero)
Disable cling's optimization (for debugging)
.! cmd
Run shell command cmd

You can interact with ROOT's C++ interpreter with a set of meta commands that all start with a ".". To see the list of available commands type .?. Here is the set of the most relevant commands:
meta commands

You can also redirect cling's standard and error output to files: cout << sin(1.2) << endl; > sin12.txt writes the result of sin(1.2) into the file sin12.txt. myfunc(); 2> myfuncerr.txt writes the error output from myfunc() to myfuncerr.txt. myfunc(); >& myfunc.txt writes both standard and error output to myfunc.txt.

Using Macros / C++ Source Files

Soon you will be tired of typing everything at the prompt again and again. Instead you will want to put the code into C++ source files! As we have seen above, you can load the file with .L myFile, you can then use any function and any type it it defines.

Often there is just one function in a file that you want to run whenever you load it. cling has a shortcut for that case: if you give that function the same name as the script (e.g. myfunc() for myfunc.C) then a simple .x myfunc.C will load myfunc.C and immediately after call myfunc(). You can also pass arguments: .x myfunc.C(12) works if myfunc expects e.g. an int as the first parameter.

Compiling Code on-the-fly (ACLiC)

ROOT offers a fantastic way to invoke your system's compiler, and to build a shared library / DLL / dylib from your code and load it. To you it will look like it was simply loading a file with ".L" or ".x". But behind the scenes ROOT checks that the library really needs to be build, it calls the compiler and linker (and dictionary generator), and then loads a native shared library. The tool doing all that is called ACLiC; it is implemented in TSystem::CompileMacro().

This powerful feature has an amazingly simple interface: you just call .L myfunc.C+. Note the + at the end - that did it! You can specify two pluses to force ROOT to rebuild the library, even if it thinks that it can take the previously build one. And you can even pass arguments: .L myfunc.C+(12) does it.