import os
import ROOT
from ROOT import TFile, gROOT
print("Current directory: '{}'.\n".format(ROOT.gDirectory.GetName()))
histo_1 = ROOT.TH1F("histo_1", "histo_1", 10, 0, 10)
print(
"Histogram '{}' is attached to: '{}'.\n".format(
histo_1.GetName(), "Nothing" if not histo_1.GetDirectory() else histo_1.GetDirectory().GetName()
)
)
path = str(gROOT.GetTutorialDir()) + '/io/'
filename = path+"tfile_1.root"
histo_2 = ROOT.TH1F("histo_2", "histo_2", 10, 0, 10)
print("Current directory: '{}'.\n".format(ROOT.gDirectory.GetName()))
print(
"Histogram '{}' is attached to: '{}'.\n".format(
histo_2.GetName(), histo_2.GetDirectory().GetName() if histo_2.GetDirectory() else "Nothing"
)
)
histo_2.SetDirectory(f)
print(
"After setting the directory, histogram '{}' is attached to: '{}'.\n".format(
histo_2.GetName(), histo_2.GetDirectory().GetName()
)
)
f.WriteObject(histo_2, "my_histogram")
print("Status after the first TFile context manager:")
print(" Current directory: '{}'.".format(ROOT.gDirectory.GetName()))
histo_2_fromfile = f["my_histogram"]
print("Retrieved '{}' histogram from file '{}'.\n".format(histo_2_fromfile.GetName(), f.GetName()))
os.remove(filename)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.