KAHIBARO
Discord Login Register

4.4 Inspecting ROOT Objects

`Print()`

To understand what is inside a ROOT object, the first tool you will use is the Print() method. Almost every ROOT class inherits from TObject, so it usually provides its own version of Print(). This makes Print() a universal way to inspect an object from the command line or from a macro.

You call it with the usual C++ method syntax. For example, if you have a histogram:

cpp
TH1F *h = new TH1F("h", "Example", 100, 0, 1);
h->Fill(0.3);
h->Fill(0.7);
h->Print();

ROOT prints a short summary that depends on the class. For a histogram this includes the name and title, number of bins, statistics such as the number of entries, and sometimes the memory address. For other objects, such as graphs or trees, it prints an overview of their internal content.

Many classes support an optional argument to Print() that switches between summary and detailed output. The exact strings are class specific, but you will often see patterns like:

cpp
h->Print("all");     // more detailed info for some classes
tree->Print();       // summary of branches
tree->Print("*");    // list of branches and leaves in detail

If you are not sure what a particular object holds, or you have just retrieved it from a file and want to check that it is the right one, Print() is the quickest textual inspection tool.

Always use Print() when you want a fast textual summary of a ROOT object. It is safe, does not modify the object, and works for almost all classes.

`Draw()`

Where Print() gives text, Draw() gives you a graphical representation. Many ROOT objects can represent themselves visually: histograms as binned distributions, graphs as sets of points, trees as projected variables, functions as curves, and so on. For such classes, Draw() is the standard way to send the object to a TCanvas.

The simplest usage is just:

cpp
h->Draw();

If there is no current canvas, ROOT creates one automatically and draws the histogram. If a canvas already exists, ROOT draws into the current pad. Repeating Draw() updates the same pad unless you change to a different one.

Most drawable classes accept an optional string argument that controls the drawing style or options:

cpp
h->Draw("E");     // draw histogram with error bars
gr->Draw("AP");   // TGraph: axes and points
tree->Draw("x");  // draw a 1D histogram of branch "x"

The list of available options depends on the class, and later chapters will describe them in detail. In the context of inspecting objects, the important point is that Draw() is often the fastest way to get an intuitive feel for the content. For example, if you open a file and retrieve an unknown histogram:

cpp
TH1 *h = (TH1*)f->Get("some_hist");
h->Draw();

you immediately see its distribution and range, and you can interact with it in the canvas, check axes, zoom, or open the statistics box.

If you draw several objects, inspect their overlay with the "SAME" option:

cpp
h1->Draw();
h2->Draw("SAME");

This is useful to compare their shapes during an interactive session.

Use Draw() whenever you need a quick visual check of an object. It does not permanently change the data, but it helps you spot obvious problems such as empty histograms, wrong units, or unexpected ranges.

`ClassName()`

ROOT uses a rich class hierarchy, so it is common to encounter pointers or references to base classes such as TH1, TObject, or TNamed, while the actual object is of a more specific derived type. The ClassName() method tells you the dynamic type of the object at runtime.

For any TObject *obj, you can ask:

cpp
cout << obj->ClassName() << endl;

This prints the name of the real class, for example:

text
TH1F
TGraphErrors
TTree
TDirectoryFile

This is especially useful when you retrieve objects from a file or a collection without knowing their exact type in advance:

cpp
TObject *obj = f->Get("my_object");
obj->Print();
cout << "Class is: " << obj->ClassName() << endl;

Once you know the class, you can decide how to cast it correctly and which methods make sense:

cpp
if (strcmp(obj->ClassName(), "TH1F") == 0) {
    TH1F *h = (TH1F*)obj;
    h->Draw();
}

You will use ClassName() frequently when exploring unfamiliar files or debugging code that manipulates generic TObject pointers.

ClassName() always reports the true dynamic type of a ROOT object, not just the static type of the pointer. Use it to confirm what kind of object you are dealing with before casting or calling class-specific methods.

ROOT object browser

ROOT also provides a graphical object browser to explore files, directories, canvases, and objects interactively. You start it from the ROOT prompt with:

cpp
new TBrowser();

This opens a window with a tree-like view on the left and a display area on the right. The browser can show:

When you click on a .root file in the browser, ROOT opens it and shows its contents as a hierarchy of directories and objects. Clicking on an object usually triggers a default action, for example drawing a histogram in a new canvas. Right-clicking gives a context menu with operations such as Draw, Inspect, Dump, or Print.

The browser is particularly helpful when you are not sure what is stored in a file. Instead of writing code, you can:

  1. Start ROOT.
  2. Create a TBrowser.
  3. Navigate to your file and click to open it.
  4. Inspect object names, types, and quickly draw them.

You can also inspect canvases and pads. If a canvas is open, it will appear in the browser, and expanding it shows the individual primitives that have been drawn. This helps you understand how a complicated plot is built from histograms, graphs, and text objects.

Although the object browser is a graphical tool, it is tightly connected to the same inspection methods described above. For many objects, the context menu entries internally call methods like Draw() or Print(). You can use it as a visual companion to your command line exploration.

Use the ROOT object browser as an interactive map of your ROOT environment. It lets you explore files, objects, and canvases without writing code, and complements Print(), Draw(), and ClassName() for quick inspection.

Views: 12

Comments

Please login to add a comment.

Don't have an account? Register now!