Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_tfile.pyzdoc
Go to the documentation of this file.
1\pythondoc TFile
2
3In the same way as for TDirectory, it is possible to get the content of a
4TFile object with the familiar item-getting syntax.
5For more information, please refer to the TDirectory documentation.
6
7In addition, TFile instances can be inspected via the `Get` method, a feature
8that is inherited from TDirectoryFile (please see the documentation of
9TDirectoryFile for examples on how to use it).
10
11In order to write objects into a TFile, the `WriteObject` Python method can
12be used (more information in the documentation of TDirectoryFile).
13
14PyROOT modifies the TFile constructor and the TFile::Open method to make them
15behave in a more pythonic way. In particular, they both throw an `OSError` if
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
21example:
22\code{.py}
23from ROOT import TFile
24with TFile("file1.root", "recreate") as outfile:
25 hout = ROOT.TH1F(...)
26 outfile.WriteObject(hout, "myhisto")
27\endcode
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())
44\endcode
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
51\endpythondoc
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
#define gROOT
Definition TROOT.h:406
A ROOT file is structured in Directories (like a file system).
Describe directory structure in memory.
Definition TDirectory.h:45
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:4086
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:947
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:634
constexpr std::array< decltype(std::declval< F >()(std::declval< int >())), N > make(F f)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
TString as(SEXP s)
Definition RExports.h:86