13.6. Inspecting TTrees
Table of Contents
`Print()`
Before you loop over a TTree or start drawing histograms from it, it is useful to inspect its structure. TTree::Print() gives you a compact summary of the tree itself. This includes the number of entries, the list of branches, and how those branches are organized.
You typically call it from an interactive ROOT session or inside a macro after you have a pointer to a TTree. For example, if your file contains a tree called Events:
TFile *f = TFile::Open("data.root");
TTree *t = (TTree*)f->Get("Events");
t->Print();The default output lists the tree name and title, the total number of entries, and a table of branches with their types and leaf names. This is particularly helpful after creating a tree or after reading one from someone else, because you can quickly see what variables are available.
Print() has options that control how much information is displayed. The most common patterns are:
t->Print(); // Summary of branches and leaves
t->Print("*"); // More detailed listing for all branches
t->Print("branch"); // Print detailed info for a specific branchIf you know the name of a branch and want to inspect only that branch, pass its name:
t->Print("energy");
This prints the branch structure, type, and leaves for energy without cluttering the output with the rest of the tree. For large trees with many branches this selective printing is very useful.
Print() only describes the structure, it does not print actual entry values. Use it whenever you want to verify that the tree indeed has the branches and types you expect before proceeding to more detailed inspection.
Rule: Use TTree::Print() to inspect the structure of a tree, not the data values. It shows tree name, number of entries, and branches with their types.
`Scan()`
While Print() tells you what is stored in the tree, TTree::Scan() lets you see the actual contents for selected entries. It prints a small table where each row is an entry and each column is one of the variables you request.
The simplest use without arguments prints all leaves for the first few entries:
t->Scan();
By default, Scan() limits the number of entries printed so that you do not flood the terminal when the tree is large. This makes it suitable for a quick glance at the data layout and values, such as verifying that branches are filled as expected and checking basic ranges.
More commonly, you specify which branches you want to see using an expression string. The variables are separated by colons:
t->Scan("energy:time");
This prints a table with two columns, energy and time, and one row per entry (up to the internal limit). The leftmost column is the entry index. You can list more variables as needed:
t->Scan("run:event:energy:time");
Scan() can also apply selection cuts. The second argument is a cut expression, in the same syntax used by TTree::Draw(). For example, to see only events with energy greater than 1.0:
t->Scan("energy:time", "energy > 1.0");You can combine multiple conditions using logical operators:
t->Scan("run:event:energy", "energy > 1.0 && time < 50");If you want to control the number of entries scanned or the starting entry, use the optional numeric arguments:
t->Scan("energy:time", "energy > 1.0", "", 20); // first 20 matching entries
t->Scan("energy", "", "", 100, 1000); // 100 entries starting at entry 1000Here the third argument is an option string, which you can usually leave empty for basic use.
Scan() is especially helpful for data debugging. You can quickly spot strange values, missing data, or confirm that your cuts behave as intended before turning them into full analysis code or histograms.
Rule: Use TTree::Scan("var1:var2", "selection") to print actual data values for chosen branches, optionally under selection cuts, for a limited number of entries.
`Show()`
TTree::Show() focuses on a single entry. Instead of printing a table of many entries, it prints all leaves for one chosen entry index in a human readable form. This is useful when you want to inspect a particular event in detail.
The simplest use is to call Show() with an entry number:
t->Show(0); // Show first entry
t->Show(42); // Show entry 42
The output lists each branch and its leaf values for that specific entry. For example, you might see all detector channels, positions, energies, and flags for one event together. This is very convenient when you have identified a suspicious entry number from a histogram or from a Scan() output and want to understand exactly how all related variables look for that event.
You can also request only a subset of branches using an optional pattern:
t->Show(42, "energy:time");In this case, only the specified branches are printed for entry 42. This keeps the output compact when your tree has many branches but you are interested in just a few of them.
Show() is often used in the following workflow: you first use Scan() to see where something unusual happens, note the entry index, and then call Show() on that entry to examine all its variables. This helps track down problems such as unexpected zeros, out of range values, or correlated anomalies across multiple branches.
Show() does not modify the tree; it is purely diagnostic. You can call it anytime after opening the file and retrieving the tree.
Rule: Use TTree::Show(entry) to inspect all branch values for a single entry in detail, optionally restricted to selected branches.
Views: 9
KAHIBARO