15from ROOT
import TFile, gROOT
27print(
"Current directory: '{}'.\n".format(ROOT.gDirectory.GetName()))
30histo_1 = ROOT.TH1F(
"histo_1",
"histo_1", 10, 0, 10)
32 "Histogram '{}' is attached to: '{}'.\n".format(
33 histo_1.GetName(),
"Nothing" if not histo_1.GetDirectory()
else histo_1.GetDirectory().GetName()
41path = str(gROOT.GetTutorialDir()) +
'/io/'
42filename = path+
"tfile_1.root"
44 histo_2 = ROOT.TH1F(
"histo_2",
"histo_2", 10, 0, 10)
46 print(
"Current directory: '{}'.\n".format(ROOT.gDirectory.GetName()))
49 "Histogram '{}' is attached to: '{}'.\n".format(
50 histo_2.GetName(), histo_2.GetDirectory().GetName()
if histo_2.GetDirectory()
else "Nothing"
54 histo_2.SetDirectory(f)
56 "After setting the directory, histogram '{}' is attached to: '{}'.\n".format(
57 histo_2.GetName(), histo_2.GetDirectory().GetName()
61 f.WriteObject(histo_2,
"my_histogram")
67print(
"Status after the first TFile context manager:")
68print(
" Current directory: '{}'.".format(ROOT.gDirectory.GetName()))
81 histo_2_fromfile = f[
"my_histogram"]
82 print(
"Retrieved '{}' histogram from file '{}'.\n".format(histo_2_fromfile.GetName(), f.GetName()))
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.