VROOTFile.cc
Go to the documentation of this file.
1 #include <io/VROOTFile.h>
2 
3 #include <utl/ErrorLogger.h>
4 #include <utl/SaveCurrentTDirectory.h>
5 
6 #include <TFile.h>
7 #include <TTree.h>
8 
9 #include <cstddef>
10 #include <iostream>
11 #include <string>
12 
13 using namespace io;
14 using namespace utl;
15 using namespace std;
16 
17 
18 VROOTFile::VROOTFile(const string& filename, const Mode mode, utl::Branch* const b) :
19  VEventFile(filename, mode, b)
20 {
21  DefaultOpen(filename, mode);
22 
23  const int result = OpenROOTFile();
24 
25  if (result == -1) {
26 
27  const string error =
28  "Unable to create in-memory image of ROOT file " + fFilename;
29  throw utl::IOFailureException(error);
30 
31  } else if (result == -2) {
32 
33  const string error =
34  "In-memory file image created corresponds to zombie ROOT file " + fFilename;
35  throw utl::IOFailureException(error);
36 
37  }
38 }
39 
40 
42 {
43  if (fFile) {
44  if (fFile->IsOpen())
45  fFile->Close();
46  delete fFile;
47  }
48 }
49 
50 
51 void
52 VROOTFile::Open(const string& filename, const Mode mode, utl::Branch* const /*b*/)
53 {
54  DefaultOpen(filename, mode);
55 
56  const int ret = OpenROOTFile();
57 
58  if (ret < 0) {
59 
60  ostringstream error;
61  error << "Cannot open root file: " << filename;
62  throw utl::IOFailureException(error.str());
63 
64  }
65 }
66 
67 
68 int
70 {
71  Close();
72 
73  const string rootMode = GetRootMode(fMode);
74 
75  const SaveCurrentTDirectory save;
76  fFile = TFile::Open(fFilename.c_str(), rootMode.c_str());
77 
78  if (!fFile)
79  return -1;
80 
81  if (fFile->IsZombie())
82  return -2;
83 
84  return 0;
85 }
86 
87 
88 void
90 {
91  if (fFile && fFile->IsOpen()) {
92 
93  if (fTree && fMode != eRead) {
94  const SaveCurrentTDirectory save;
95  fFile->cd();
96  fTree->Write();
97  }
98 
99  fFile->Close();
100  }
101 }
102 
103 
104 int
105 VROOTFile::GetTree(const string& name)
106 {
107  fTree = dynamic_cast<TTree*>(fFile->Get(name.c_str()));
108 
109  if (!fTree) {
110 
111  ostringstream fat;
112  fat << "Cannot find Tree " << name;
113  FATAL(fat);
114  return -1; // TODO: make error handling for IO consistent: use exceptions
115 
116  }
117 
118  return 0;
119 }
120 
121 
122 string
124 {
125  switch (mode) {
126  case eNew:
127  return "recreate";
128  case eWrite:
129  return "new";
130  case eAppend:
131  return "update";
132  default:
133  return "read";
134  }
135 }
static std::string GetRootMode(const Mode mode)
Definition: VROOTFile.cc:123
TTree * fTree
Definition: VROOTFile.h:46
std::string fFilename
Definition: VEventFile.h:74
Open file for write, fail if exists.
Definition: IoCodes.h:19
Mode
Available open modes.
Definition: IoCodes.h:16
int OpenROOTFile()
Definition: VROOTFile.cc:69
#define FATAL(message)
Macro for logging fatal messages.
Definition: ErrorLogger.h:167
virtual ~VROOTFile()
Definition: VROOTFile.cc:41
Read Only access.
Definition: IoCodes.h:18
virtual void Close()
Definition: VROOTFile.cc:89
Base class to report exceptions in IO.
int DefaultOpen(const std::string &filename, const Mode mode=eRead)
Definition: VEventFile.cc:31
Class representing a document branch.
Definition: Branch.h:107
const Data result[]
TFile * fFile
Definition: VROOTFile.h:45
int GetTree(const std::string &name)
Definition: VROOTFile.cc:105
virtual void Open(const std::string &filename, const Mode mode=eRead, utl::Branch *const b=nullptr)
Definition: VROOTFile.cc:52
Overwrite if exist and open for write.
Definition: IoCodes.h:20
char * filename
Definition: dump1090.h:266

, generated on Tue Sep 26 2023.