KAHIBARO
Discord Login Register

Your First ROOT Session

Performing calculations

When you start ROOT you enter an interactive C++ environment. You can type expressions, press Enter, and ROOT will immediately evaluate them. This is the quickest way to explore how ROOT works.

You can use ROOT like a calculator. For example, you can type:

cpp
2+3
10*4
(5+3)*2
10.0/3.0

ROOT uses C++ rules, so integer division and floating point division behave differently. If both operands are integers, the result is an integer. If at least one operand is a floating point number, the result is a floating point number. For example:

cpp
10/3      // integer division, result 3
10.0/3    // floating point division, result 3.33333...
10/3.0    // same, result 3.33333...

You can also use mathematical functions from C and C++, such as sin, cos, sqrt, and exp. For example:

cpp
sqrt(2)
sin(3.14159/2)
log(10.0)
pow(2.0, 10.0)

These functions expect angles in radians when they are trigonometric functions. ROOT automatically prints the result of an expression if it is a complete statement and not assigned to a variable.

You can also combine several operations in one expression. C++ operator precedence applies, so multiplication and division are evaluated before addition and subtraction, and parentheses can be used to control the order:

cpp
1+2*3       // result 7
(1+2)*3     // result 9
cos(0.5)*exp(-0.5)

If you do not want ROOT to print the result, you can end the line with a semicolon. For example:

cpp
2+3;        // ROOT evaluates but does not print the result

This is useful when you only care about side effects such as modifying variables or filling histograms.

Creating variables

To keep intermediate results, you can create variables. ROOT uses C++ syntax, so you must choose a type and a name. For simple numerical work the most common types are int for integers and double for floating point numbers. For example:

cpp
int n = 10;
double x = 3.14;

After you create a variable, you can use it in further expressions:

cpp
n*2
x*x
n + x

If you enter the name of a variable on its own, ROOT will print its value. If you use the name in an expression, ROOT will use the current value. You can change variables at any time:

cpp
n = 20;
x = 1.5;

The type does not change when you assign a new value. Once n is an int, it stays an int for that ROOT session. If you assign a floating point value to an int, C++ will convert it to an integer, usually by truncation:

cpp
n = 3.9;   // n becomes 3

For readability and later analysis, you should pick descriptive variable names. For example, energy, count, or time_ns are more informative than single letters. Names must start with a letter or underscore and can contain letters, digits, and underscores.

You can also create bool variables to store logical conditions, which can be useful when you start working with selections later on:

cpp
bool passCut = true;
bool highEnergy = (energy > 1.0);

For simple text, ROOT supports C++ strings, but this topic will be covered in more detail in later chapters. In an interactive session, you can already use them as follows:

cpp
std::string label = "run1";
label

ROOT prints the content when you enter label on its own.

Calling ROOT functions

In addition to basic C++ operations, ROOT provides its own functions and classes. Even in your first session you can call some simple ROOT functions to see that you are really inside ROOT, not just a plain C++ interpreter.

Many ROOT functions are grouped in classes, so you call them with the form ClassName::FunctionName(...). For mathematical functions, ROOT offers a helper class TMath. For example:

cpp
TMath::Pi()
TMath::Sqrt(2.0)
TMath::Sin(TMath::Pi()/4)

ROOT also has global functions that do not belong to a class. One very common example in an interactive session is gROOT->ProcessLine, but for a first session it is more useful to call simple functions that produce visible results, such as random number generators or time queries, which will be discussed in separate chapters.

You can already use ROOT classes in your first session by creating objects. For instance, you can create a simple 1D histogram and fill it, even before you know all the details:

cpp
TH1F h1("h1","Example histogram",100,0,10);
h1.Fill(3.2);
h1.Fill(4.7);
h1.Draw();

Here TH1F is a ROOT class, h1 is a ROOT object, and Fill and Draw are ROOT member functions. You do not need to understand all of this yet, but typing these commands in your first session shows how calling ROOT functions produces graphical output.

Some ROOT functions also help you inspect the environment. For example:

cpp
gROOT->GetVersion()

prints the ROOT version you are running. The pointer gROOT and similar globals are provided automatically in an interactive session.

Important: ROOT functions usually follow C++ syntax. Parentheses are required around argument lists, semicolons are needed to end statements that create or modify objects, and namespaces or class names must be written explicitly unless already in scope.

Running simple commands

During your first ROOT session you will typically combine calculations, variable definitions, and ROOT specific commands. Everything you type at the prompt is interpreted as C++ code. Simple one line commands are the easiest way to interact with ROOT.

A simple command can be a single expression, a variable definition, or a function call. Examples are:

cpp
int n = 100;
double step = 0.1;
TH1F h("h","First ROOT histogram",n,0,n*step);

You can also write short loops directly in the interactive shell. For instance, you can fill a histogram with random numbers:

cpp
TH1F h("h","Gaussian example",100,-5,5);
for (int i = 0; i < 10000; ++i) {
   double x = gRandom->Gaus(0,1);
   h.Fill(x);
}
h.Draw();

This is already a small analysis put together from simple commands. The loop runs line by line just like a script. ROOT allows multi line input, and it will wait for the closing brace } before executing.

When you enter a command that ends with a semicolon, ROOT executes it but does not show the return value. When you omit the semicolon, ROOT both executes the command and prints the result. You can use this behavior to quickly check that an expression or function call returns what you expect:

cpp
sqrt(5.0)        // prints the result
sqrt(5.0);       // does not print

Longer pieces of code are better stored in macros, which are files with C++ code that ROOT can run. That topic is covered in its own chapter. In your first session, you only need to remember that anything you can write in C++ you can also enter interactively as simple commands, including if statements and loops, as soon as you start learning the C++ basics.

Finally, if you make a mistake, such as missing a semicolon or mistyping a function name, ROOT will show an error message. You can simply correct your line and try again. The interactive shell is designed for experimentation, so do not hesitate to try short commands and see what happens.

Views: 11

Comments

Please login to add a comment.

Don't have an account? Register now!