Types & Methods

RootIO.TTreeType
`TTree`

Type representing a ROOT tree. It must be used in place of the TTree type of the ROOT module.

source
RootIO.TTreeMethod
TTree(file::CxxWrap.CxxWrapCore.CxxPtr{ROOT.TFile}, name::String, title::String, rowtype)

Create a ROOT TTree to store instances of a composite type, rowtype. Each field of the type is mapped to a TTree branch (aka column) of the same name. Each field must be annotated with its type in the declaration of rowtype declaration. The Fill function must be used to store the instance. Each instance will be stored in a TTree entry (aka row).

Note: for convenience, providing an instance of the row type instead of the type itself is currently supported. This support might eventually be dropped if we find that it leads to confusion. The instance is used solely to retrieve the type and is not nserted in the tree. See TTree(::CxxWrap.CxxWrapCore.CxxPtr{ROOT.TFile}, ::String, ::String; columns...) to create and fill a tree in one go.

Example


using CxxWrap, ROOT, RootIO
mutable struct Event
    x::Float32
    y::Float32
    z::Float32
    v::StdVector{Float32}
end
Event()  = Event(0., 0., 0., StdVector{Float32}())

# Create the tree
f = ROOT.TFile!Open("data.root", "RECREATE")
tree = RootIO.TTree(f, "mytree", "mytreetitle", Event)

# Fill the tree
e = Event()
for i in 1:10
    e.x, e.y, e.z = rand(3)
    n = rand(1:5)
    # Two next lines are an optimized version of e.v = rand(Float32)
    # by limiting time consuming memory allocation
    resize!(e.v, n)
    e.v .= rand(Float32)
    Fill(tree, e)
end

# Display tree contents
Scan(tree)
source
RootIO.TTreeMethod
TTree(file::CxxWrap.CxxWrapCore.CxxPtr{ROOT.TFile}, name::String, title::String; columns...)

Creates a new ROOT tree and fill it with the provided data.

Arguments

  • file: A pointer to a ROOT file where the TTree will be stored.
  • name: The name of the TTree.
  • title: The title of the TTree.
  • columns...: column definitions passed as named argument, in the form columnname = columncontent or columnname = elementtype

Creation of an empty TTree

If the columns values are data types, then an empty TTree is created. The argument names are used for the column (aka branch) names and their value specify the type of elements to store in the column.

Example

using ROOT, RootIO

# Create the tree
file = ROOT.TFile!Open("example.root", "RECREATE")
tree = RootIO.TTree(file, "mytree", "My tree"; col_int = Int64, col_float = Float64)

# Display the tree definition
Print(tree)

Creation and filling of a TTree

If the columns values are vectors, a TTree is created and filled with the data provided in the vectors. A branch is created for each columns argument with the name of the argument and filled with each element of the vector provided as the argument value. All the vectors must be of the same length.

Example

file = ROOT.TFile!Open("example.root", "RECREATE")
name = "example_tree"
title = "Example TTree"
data = (col_float=rand(Float64, 3), col_int=rand(Int32, 3))
tree = RootIO.TTree(file, name, title; data...)
source
ROOT.FillMethod
Fill(tree::TTree, data)

Append one or more rows (aka entries) to the ROOT tree.

Single row can be provided as an instance of a composite type or of a Tuple.

Example

using ROOT, RootIO

# Create the tree
file = ROOT.TFile!Open("example.root", "RECREATE")
tree = RootIO.TTree(file, "mytree", "My tree"; col_int = Int64, col_float = Float64)

# Fill the tree
for i in 1:10
    Fill(tree, (i, i*π))
end

# Display the tree contents
Scan(tree)
source
ROOT.GetEntriesMethod

GetEntries(tree)

Returns the number of entries (aka rows) stored in the tree.

source
ROOT.PrintMethod
Print(tree, options = "")

Print a summary of the tree contents.

  • If option contains "all" friend trees are also printed.
  • If option contains "toponly" only the top level branches are printed.
  • If option contains "clusters" information about the cluster of baskets is printed.

Wildcarding can be used to print only a subset of the branches, e.g., Print(tree, "Elec*") will print all branches with name starting with "Elec".

source
ROOT.ScanMethod

Scan(tree, varexp = "", selection = "", option = "", nentries = -1, firstentry = 0)

Loop over tree entries and print entries passing selection.

  • If varexp is 0 (or "") then print only first 8 columns.
  • If varexp = "*" print all columns.

Otherwise a column selection can be made using "var1:var2:var3".

source
ROOT.WriteMethod
Write(tree::TTree)

Save the tree into the associated ROOT file. This method needs to be called to finalize the writing to disk.

source