Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JRootFile.hh
Go to the documentation of this file.
1#ifndef __JROOTFILE__
2#define __JROOTFILE__
3
4#include <string>
5#include <stdlib.h>
6#include <string.h>
7
8#include "TFile.h"
9#include "TError.h"
10
11#include "JLang/JAccessible.hh"
12#include "JLang/JException.hh"
13#include "JLang/JThrow.hh"
14#include "JLang/JStorage.hh"
15
16
17/**
18 * \author mdejong
19 */
20
21namespace JROOT {}
22namespace JPP { using namespace JROOT; }
23
24namespace JROOT {
25
27 using JLANG::JStorage;
30 using JLANG::JThrow;
31
32 /**
33 * Environment variable to disable ROOT file recovery.
34 *
35 * If not set or set to 0, allow recovery of ROOT file, else do not.
36 */
37 static const char* const ROOT_FILE_RECOVERY_DISABLE = "ROOT_FILE_RECOVERY_DISABLE";
38
39
40 /**
41 * ROOT file.
42 *
43 * This class implements the method is_open() of the JLANG::JAccessible interface.
44 */
45 class JRootFile :
46 public virtual JAccessible,
47 public JStorage<TFile>
48 {
49 protected:
50 /**
51 * Default constructor.
52 * The printing of ROOT errors is suppressed.
53 */
55 {
56 gErrorIgnoreLevel = kError;
57 }
58
59
60 public:
61 /**
62 * Get file.
63 *
64 * \return pointer to file
65 */
66 TFile* getFile() const
67 {
68 return get();
69 }
70
71
72 /**
73 * Check is file is open.
74 *
75 * \return true if open; else false
76 */
77 virtual bool is_open() const override
78 {
79 return (getFile() != NULL && getFile()->IsOpen());
80 }
81
82 private:
87 };
88
89
90 /**
91 * ROOT input file.
92 *
93 * This class implements the methods open() and close() of the JLANG::JAccessible interface.
94 */
96 public JRootFile
97 {
98 public:
99 /**
100 * Default constructor.
101 */
103 JRootFile()
104 {}
105
106
107 /**
108 * Constructor.
109 *
110 * \param file_name file name
111 */
112 JRootInputFile(const char* file_name) :
113 JRootFile()
114 {
115 do_open(file_name);
116 }
117
118
119 /**
120 * Destructor.
121 *
122 * The destructor closes the file if it is still open.
123 */
125 {
126 do_close();
127 }
128
129
130 /**
131 * Open file.
132 * The file is not opened when no file exists with the given name.
133 *
134 * \param file_name file name
135 */
136 virtual void open(const char* file_name) override
137 {
138 do_open(file_name);
139 }
140
141
142 /**
143 * Close file.
144 */
145 virtual void close() override
146 {
147 do_close();
148 }
149
150
151 private:
152 /**
153 * Open file.
154 * The file is not opened when no file exists with the given name.
155 *
156 * \param file_name file name
157 */
158 void do_open(const char* file_name)
159 {
160 set(TFile::Open(file_name, "READ"));
161
162 if (!is_open()) {
163 Throw(MAKE_EXCEPTION(JFileOpenException, "Error opening file " << file_name));
164 }
165
166 if (getFile()->TestBit(TFile::kRecovered)) {
167
168 const char* const value = getenv(ROOT_FILE_RECOVERY_DISABLE);
169
170 if (value != NULL && strcmp(value,"0") != 0) {
171 Throw(MAKE_EXCEPTION(JFileRecoveryException, "Error recovery file " << file_name << " disabled"));
172 }
173 }
174 }
175
176
177 /**
178 * Close file.
179 */
180 void do_close()
181 {
182 if (getFile() != NULL && getFile()->IsOpen()) {
183 getFile()->Close();
184 }
185
186 reset();
187 }
188 };
189
190
191 /**
192 * ROOT output file.
193 *
194 * This class implements the methods open() and close() of the JLANG::JAccessible interface.
195 */
197 public JRootFile
198 {
199 public:
200 /**
201 * Default constructor.
202 */
204 JRootFile()
205 {}
206
207
208 /**
209 * Constructor.
210 *
211 * \param file_name file name
212 */
213 JRootOutputFile(const char* file_name) :
214 JRootFile()
215 {
216 do_open(file_name);
217 }
218
219
220 /**
221 * Destructor.
222 *
223 * The destructor writes and closes the file if it is still open.
224 */
226 {
227 do_close();
228 }
229
230
231 /**
232 * Open file.
233 * The file is not opened when a file exists with the given name.
234 *
235 * \param file_name file name
236 */
237 virtual void open(const char* file_name) override
238 {
239 do_open(file_name);
240 }
241
242
243 /**
244 * Close file.
245 * This method calls the TFile::Write method before closing the file.
246 */
247 virtual void close() override
248 {
249 do_close();
250 }
251
252
253 private:
254 /**
255 * Open file.
256 * The file is not opened when a file exists with the given name.
257 *
258 * \param file_name file name
259 */
260 void do_open(const char* file_name)
261 {
262 set(TFile::Open(file_name, "CREATE"));
263
264 if (!is_open()) {
265 Throw(MAKE_EXCEPTION(JFileOpenException, "Error opening file " << file_name));
266 }
267 }
268
269
270 /**
271 * Close file.
272 * This method calls the TFile::Write method before closing the file.
273 */
274 void do_close()
275 {
276 if (getFile() != NULL && getFile()->IsOpen()) {
277 getFile()->Write();
278 getFile()->Close();
279 }
280
281 reset();
282 }
283 };
284}
285
286#endif
Exceptions.
#define MAKE_EXCEPTION(JException_t, A)
Make exception.
Exception handling.
Interface for named access of a device.
Exception for opening of file.
Exception for recovery of file.
virtual void set(JClass_t *p) override
Set pointer.
Definition JPointer.hh:75
virtual JClass_t * get() const override
Get pointer.
Definition JPointer.hh:64
Template storage class.
Definition JStorage.hh:29
virtual void reset() override
Reset pointer.
Definition JStorage.hh:42
Auxiliary base class for controling the throwing of exceptions.
Definition JThrow.hh:25
static void Throw(const bool option)
Definition JThrow.hh:37
ROOT file.
Definition JRootFile.hh:48
JRootFile(JRootFile &&)
TFile * getFile() const
Get file.
Definition JRootFile.hh:66
JRootFile(const JRootFile &)
JRootFile & operator=(const JRootFile &)
virtual bool is_open() const override
Check is file is open.
Definition JRootFile.hh:77
JRootFile & operator=(JRootFile &&)
JRootFile()
Default constructor.
Definition JRootFile.hh:54
ROOT input file.
Definition JRootFile.hh:97
JRootInputFile()
Default constructor.
Definition JRootFile.hh:102
virtual void open(const char *file_name) override
Open file.
Definition JRootFile.hh:136
void do_close()
Close file.
Definition JRootFile.hh:180
JRootInputFile(const char *file_name)
Constructor.
Definition JRootFile.hh:112
~JRootInputFile()
Destructor.
Definition JRootFile.hh:124
virtual void close() override
Close file.
Definition JRootFile.hh:145
void do_open(const char *file_name)
Open file.
Definition JRootFile.hh:158
ROOT output file.
Definition JRootFile.hh:198
~JRootOutputFile()
Destructor.
Definition JRootFile.hh:225
virtual void open(const char *file_name) override
Open file.
Definition JRootFile.hh:237
virtual void close() override
Close file.
Definition JRootFile.hh:247
JRootOutputFile()
Default constructor.
Definition JRootFile.hh:203
void do_open(const char *file_name)
Open file.
Definition JRootFile.hh:260
JRootOutputFile(const char *file_name)
Constructor.
Definition JRootFile.hh:213
void do_close()
Close file.
Definition JRootFile.hh:274
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
static const char *const ROOT_FILE_RECOVERY_DISABLE
Environment variable to disable ROOT file recovery.
Definition JRootFile.hh:37