Pyroot 0 0 5_Tfile_Context_Manager

This tutorial demonstrates the usage of the TFile class as a Python context manager.

Author: Vincenzo Eduardo Padulano CERN/UPV
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Thursday, June 30, 2022 at 08:55 AM.

In [1]:
import os

import ROOT
from ROOT import TFile
Welcome to JupyROOT 6.27/01

By default, objects of some ROOT types such as TH1 and its derived types are automatically attached to a ROOT.TDirectory when they are created. Specifically, at any given point of a ROOT application, the ROOT.gDirectory object tells which is the current directory where objects will be attached to. The next line will print 'PyROOT' as the name of the current directory. That is the global directory created when using ROOT from Python, which is the ROOT.gROOT object.

In [2]:
print("Current directory: '{}'.\n".format(ROOT.gDirectory.GetName()))
Current directory: 'PyROOT'.

We can check to which directory a newly created histogram is attached.

In [3]:
histo_1 = ROOT.TH1F("histo_1", "histo_1", 10, 0, 10)
print("Histogram '{}' is attached to: '{}'.\n".format(histo_1.GetName(), histo_1.GetDirectory().GetName()))
Histogram 'histo_1' is attached to: 'PyROOT'.

For quick saving and forgetting of objects into ROOT files, it is possible to open a TFile as a Python context manager. In the context, objects can be created, modified and finally written to the file. At the end of the context, the file will be automatically closed.

In [4]:
with TFile.Open("pyroot005_file_1.root", "recreate") as f:
    histo_2 = ROOT.TH1F("histo_2", "histo_2", 10, 0, 10)
    # Inside the context, the current directory is the open file
    print("Current directory: '{}'.\n".format(ROOT.gDirectory.GetName()))
    # And the created histogram is automatically attached to the file
    print("Histogram '{}' is attached to: '{}'.\n".format(histo_2.GetName(), histo_2.GetDirectory().GetName()))
    # Before exiting the context, objects can be written to the file
    f.WriteObject(histo_2, "my_histogram")
Current directory: 'pyroot005_file_1.root'.

Histogram 'histo_2' is attached to: 'pyroot005_file_1.root'.

When the TFile.Close method is called, the current directory is automatically set again to ROOT.gROOT. Objects that were attached to the file inside the context are automatically deleted and made 'None' when the file is closed.

In [5]:
print("Status after the first TFile context manager:")
print(" Current directory: '{}'.".format(ROOT.gDirectory.GetName()))
print(" Accessing 'histo_2' gives: '{}'.\n".format(histo_2))
Status after the first TFile context manager:
 Current directory: 'PyROOT'.
 Accessing 'histo_2' gives: 'None'.

Also reading data from a TFile can be done in a context manager. Information stored in the objects of the file can be queried and used inside the context. After the context, the objects are not usable anymore because the file is automatically closed. This means you should use this pattern as a quick way to get information or modify objects from a certain file, without needing to keep the histograms alive afterwards.

In [6]:
with TFile.Open("pyroot005_file_1.root", "read") as f:
    # Retrieve histogram using the name given to f.WriteObject in the previous
    # with statement
    histo_2_fromfile = f.my_histogram
    print("Retrieved '{}' histogram from file '{}'.\n".format(histo_2_fromfile.GetName(), f.GetName()))
Retrieved 'histo_2' histogram from file 'pyroot005_file_1.root'.

Cleanup the file created for this tutorial

In [7]: