ROOT File .root

Albert Cheung

General

ROOT files contain C++ objects that are stored to disk. You can open files when starting ROOT

1
$ root file.root

Create

Create ROOT file by

1
std::unique_ptr<TFile> myFile( TFile::Open("file.root", "RECREATE") );

or

1
TFile *file1 = new TFile("file.root", "RECREATE");

Here is the second argument:

  • “RECREATE”: create a ROOT file, replacing it if it already exists.
  • “CREATE” or “NEW”: create a ROOT file.
  • “UPDATE”: updates the ROOT file.
  • “READ”: opens an existing ROOT file for reading.

Read

Read it by

1
File *file2 = TFile::Open("file.root", "READ");

or you can use

1
2
std::unique_ptr<TFile> myFile(TFile::Open("file.root") );
std::unique_ptr<TH1> hist(myFile -> Get<TH1>("MyHist"));

Storing an Object in a ROOT File

You can save any object, for instance canvases or histograms, into a ROOT file. You can even store your own types.

TFile derives from TDirectory; use TDirectory::WriteObject() to write an object to a ROOT file.

Example:

1
myFile->WriteObject(&myObject, "MyObject");

or

1
aPointerOfHist -> Write();

ROOT Object Browser

Use

1
2
$ root file.root
root[0] TBrowser b

Other Cmd Line Tools

  • rootls: Lists the content of a ROOT file.
  • rootcp: Copies objects stored in a ROOT file to another ROOT file.
  • rootrm: Deletes objects contained in a ROOT file.
  • rootmv: Moves objects stored in a ROOT file to another ROOT file.
  • rootmkdir: Creates a “directory” inside a ROOT file.
  • rootbrowse: Opens a TBrowser directly with the contents of a ROOT file.
  • rooteventselector: Extracts a range of events of a tree contained in a ROOT file and put them as a new tree in another ROOT file.
  • rootprint: Plots objects in an image ROOT file.
  • rootslimtree: Copies trees with a subset of branches from source ROOT files.
  • Title: ROOT File .root
  • Author: Albert Cheung
  • Created at : 2024-10-19 17:04:38
  • Updated at : 2024-10-19 19:16:49
  • Link: https://www.albertc9.github.io/2024/10/19/rootfile/
  • License: This work is licensed under CC BY-NC-SA 4.0.