import ROOT # Create a new ROOT file f = ROOT.TFile("example.root", "RECREATE") # Create a new TTree and add branches tree = ROOT.TTree("myTree", "My Tree") x = ROOT.Double(0) y = ROOT.Double(0) tree.Branch("x", x, "x/D") tree.Branch("y", y, "y/D") # Fill the tree with data for i in range(10): x.value = i y.value = i*i tree.Fill() # Write the tree to the file and close it tree.Write() f.Close()
import ROOT # Open an existing ROOT file f = ROOT.TFile("example.root", "READ") # Get the TTree from the file tree = f.Get("myTree") # Get the values of the branches and print them x = ROOT.Double(0) y = ROOT.Double(0) tree.SetBranchAddress("x", x) tree.SetBranchAddress("y", y) for i in range(tree.GetEntries()): tree.GetEntry(i) print("x = ", x.value, ", y = ", y.value) # Close the file f.Close()This example reads data from an existing ROOT file containing a TTree. We retrieve the tree from the file and then loop over its entries to print the values of its branches. Package Library: ROOT library.