Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_tfile.pyzdoc
Go to the documentation of this file.
2
3In the same way as for TDirectory, it is possible to get the content of a
6
10
11In order to write objects into a TFile, the `WriteObject` Python method can
13
16there was a problem accessing the file (e.g. non-existent or corrupted file).
17
18This class can also be used as a context manager, with the goal of opening a
19file and doing some quick manipulations of the objects inside it. The
20TFile::Close method will be automatically called at the end of the context. For
22\code{.py}
23from ROOT import TFile
24with TFile("file1.root", "recreate") as outfile:
25 hout = ROOT.TH1F(...)
26 outfile.WriteObject(hout, "myhisto")
28
29Since the file is closed at the end of the context, all objects created or read
30from the file inside the context are not accessible anymore in the application
31(but they will be stored in the file if they were written to it). ROOT objects
32like histograms can be detached from a file with the SetDirectory method. This
33will leave the object untouched so that it can be accessed after the end of the
34context:
35\code{.py}
36import ROOT
37from ROOT import TFile
38with TFile("file1.root", "read") as infile:
39 hin = infile.Get("myhisto")
40 hin.SetDirectory(ROOT.nullptr)
41
42# Use the histogram afterwards
43print(hin.GetName())
45
46\note The TFile::Close method automatically sets the current directory in
47the program to the gROOT object. If you want to restore the status of the
48current directory to some other file that was opened prior to the `with`
49statement, you can use the context manager functionality offered by TContext.
50
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
#define gROOT
Definition TROOT.h:426
A ROOT file is structured in Directories (like a file system).
Describe directory structure in memory.
Definition TDirectory.h:45
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
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.
Definition TFile.cxx:3787
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:878
constexpr std::array< decltype(std::declval< F >()(std::declval< int >())), N > make(F f)
CoordSystem::Scalar get(DisplacementVector2D< CoordSystem, Tag > const &p)
TString as(SEXP s)
Definition RExports.h:87