KAHIBARO
Discord Login Register

4.1. Introduction to ROOT Classes

TObject

In ROOT, almost everything you work with is an object in the C++ sense, and most of these objects inherit from a common base class called TObject. This base class provides a standard interface and a shared set of capabilities that many ROOT classes use.

Whenever a class derives from TObject, it automatically gains support for features such as automatic memory management in some contexts, input and output to ROOT files, run-time type information through the ROOT type system, and integration with ROOT tools like the browser and the interpreter.

Some of the most important abilities that TObject provides are related to identifying and describing objects. A TObject can carry a name and a title, which are simple strings used to identify the object and to describe it in a human-readable way. Many high-level classes like histograms and graphs use these two strings systematically. The name is typically used for programmatic access, whereas the title is often used as a label in plots or in the ROOT browser.

TObject also defines common utility methods. A few you will encounter very early are Print(), Draw(), and ClassName(). The exact behavior of these methods depends on the class that actually implements or overrides them, but the interface is shared. For example, almost any drawable object that inherits from TObject will have a Draw() method, which lets you visualize the object on a canvas. Similarly, Print() is used for textual descriptions and summaries.

Many ROOT containers and collections, such as TList and TObjArray, are designed to store pointers to TObject or to classes derived from it. This only works because TObject provides a common base type. As a result, TObject is central to the ROOT object model and is one of the reasons ROOT can manage such a diverse set of classes in a uniform way.

Although not every single class in ROOT derives from TObject for technical reasons, the majority of analysis-oriented classes that you will use in this course do. When you create histograms, canvases, graphs, files, or trees, nearly all of them are part of the TObject hierarchy.

In ROOT, most analysis classes inherit from TObject, which provides common features like names and titles, drawing and printing functions, and integration with ROOT I/O and collections.

Understanding that TObject is the common ancestor that ties many classes together will help you make sense of why seemingly different objects share similar methods and can be handled in similar ways.

ROOT class naming conventions

ROOT uses a rather distinctive naming style for its classes. Learning this style early makes it much easier to guess the purpose of a class, to remember class names, and to navigate the documentation.

Every ROOT class name begins with an uppercase T, short for "Type" in the original design of ROOT. This leading T immediately tells you that you are looking at a ROOT class rather than a standard C++ class. After the T, the rest of the name uses mixed-case words without underscores.

Here are some common examples from different parts of ROOT:

Class nameRough meaning
TH1F1D histogram with float bin contents
TCanvasDrawing canvas for plots
TFileROOT file object
TTreeColumn-oriented event data container
TGraphX–Y data points graph
TF1One-dimensional mathematical function
TLegendLegend box for plots
TDirectoryDirectory inside a ROOT file

Within a group of related classes, ROOT often encodes more information in the class name. Histograms are a good example:

So, TH1F is a one-dimensional histogram with float bin contents, and TH2D is a two-dimensional histogram with double bin contents.

Similarly, function classes follow a pattern where TF1 describes a 1D function, TF2 a 2D function, and so on. Tree-related classes start with TTree or closely related names like TBranch and TLeaf. File and directory related classes start with TFile and TDirectory.

Even within ROOT, standard C++ library classes such as std::vector or std::string are used without a leading T. This difference makes it visually clear which types are part of ROOT and which are from standard C++.

Sometimes, ROOT classes also contain abbreviations that reflect their historical origin in particle and nuclear physics. For example, TLorentzVector represents a four-vector in relativistic kinematics, and THStack represents a stack of histograms. With experience, these names become more intuitive.

When you explore the ROOT documentation, the class reference is essentially a long list of names starting with T. Knowing that they follow these conventions means you can often find the right class by reasoning from its purpose. If you need a 2D plot of data points with errors, for example, you might look for something like TGraphErrors or TGraphAsymmErrors. If you have a set of objects to store in a list-like container, classes such as TList or TObjArray are natural candidates.

ROOT class names usually start with T, and related classes use consistent patterns, such as TH1F for 1D histograms and TF1 for 1D functions. Learning these patterns helps you quickly identify and remember ROOT classes.

Recognizing these naming conventions is not only convenient but also speeds up learning, since you can guess and recall class names rather than constantly looking them up.

Creating ROOT objects

To use ROOT effectively, you need to create objects that represent the things you want to work with, such as histograms, canvases, graphs, trees, and files. All of these are classes, so you create them using the standard C++ object construction syntax.

The most direct way to create an object is by calling a constructor. In C++, the constructor is a special function that has the same name as the class. You can create objects either with automatic storage duration, which is similar to local variables, or dynamically on the heap using the new operator.

For example, to create a one-dimensional histogram as a local variable inside the ROOT interpreter or inside a macro, you can write something like:

cpp
TH1F h("h", "Example histogram", 100, 0.0, 1.0);

This line constructs a TH1F object called h. The string "h" is the object name and "Example histogram" is the object title. The remaining arguments specify the number of bins and the histogram range. Since TH1F inherits from TObject, this name and title can later be used by ROOT to organize and display the histogram.

If you want the histogram to persist beyond the current scope, or you want ROOT to manage its ownership in certain contexts, you might prefer to create it dynamically:

cpp
TH1F *h = new TH1F("h", "Example histogram", 100, 0.0, 1.0);

Here h is a pointer to a TH1F object that lives on the heap. Many ROOT interfaces and collections expect pointers to objects, often of type TObject* or a pointer to a derived class. For example, when you add an object to a TList, you usually pass a pointer. Using new is therefore common when working with ROOT.

Creating a canvas works similarly. A canvas is a window or page on which you draw histograms and other graphical objects. A typical construction looks like:

cpp
TCanvas *c = new TCanvas("c", "My canvas", 800, 600);

Again, the first two arguments are the name and title, and the last two define the width and height of the canvas in pixels.

Objects that interact with files use constructors that include file names and modes. For example, to open a ROOT file for writing you can construct a TFile like this:

cpp
TFile *f = new TFile("output.root", "RECREATE");

Here, "output.root" is the file name and "RECREATE" is the mode that tells ROOT to create the file or overwrite an existing one.

Some ROOT classes provide multiple constructors with different sets of arguments, a concept known in C++ as overloaded constructors. For instance, TGraph can be constructed from arrays of x and y data, or it can be created empty and then filled point by point. A simple creation from arrays might look like:

cpp
double x[3] = {1.0, 2.0, 3.0};
double y[3] = {2.0, 4.0, 6.0};
TGraph *g = new TGraph(3, x, y);

Alternatively, you can create an empty graph with:

cpp
TGraph *g = new TGraph();

and later add points with methods provided by TGraph.

In interactive ROOT sessions, you can also create objects directly on the command line of the ROOT prompt using the same syntax. The ROOT C++ interpreter handles the compilation of these lines for you. The moment you press Enter, the object is created and you can immediately call methods on it, such as:

cpp
root [0] TH1F *h = new TH1F("h", "Example histogram", 100, 0.0, 1.0);
root [1] h->Fill(0.3);
root [2] h->Draw();

The choice between creating objects as local variables or on the heap and how you manage their lifetime is an important topic in ROOT. Many ROOT services, such as automatic memory management and object ownership, assume that objects are created with new and stored in ROOT-managed lists. As a beginner, it is useful to be aware that object lifetime matters and to prefer simple, clear patterns where you create an object, use it, and let ROOT or your code clean it up in an organized way.

When working with ROOT, you usually create objects by calling constructors, often with a name and title as the first two arguments, and many ROOT interfaces expect pointers to objects created with new.

By recognizing typical constructor patterns and understanding that ROOT objects are ordinary C++ objects with some extra capabilities from TObject, you will be able to create and start using histograms, canvases, files, and other ROOT objects naturally in both interactive sessions and macros.

Views: 12

Comments

Please login to add a comment.

Don't have an account? Register now!