Jpp  18.0.1-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
21 namespace JROOT {}
22 namespace JPP { using namespace JROOT; }
23 
24 namespace JROOT {
25 
26  using JLANG::JAccessible;
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:
83  JRootFile(const JRootFile&);
85  JRootFile& operator=(const JRootFile&);
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
Exception for opening of file.
Definition: JException.hh:342
void do_open(const char *file_name)
Open file.
Definition: JRootFile.hh:260
JRootOutputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:213
Template storage class.
Definition: JStorage.hh:26
Exceptions.
JRootInputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:112
Auxiliary base class for controling the throwing of exceptions.
Definition: JThrow.hh:25
void do_close()
Close file.
Definition: JRootFile.hh:274
virtual void reset() override
Reset pointer.
Definition: JStorage.hh:42
JRootFile & operator=(const JRootFile &)
virtual bool is_open() const override
Check is file is open.
Definition: JRootFile.hh:77
void do_open(const char *file_name)
Open file.
Definition: JRootFile.hh:158
void do_close()
Close file.
Definition: JRootFile.hh:180
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:136
TFile * getFile() const
Get file.
Definition: JRootFile.hh:66
~JRootInputFile()
Destructor.
Definition: JRootFile.hh:124
static const char *const ROOT_FILE_RECOVERY_DISABLE
Environment variable to disable ROOT file recovery.
Definition: JRootFile.hh:37
ROOT input file.
Definition: JRootFile.hh:95
#define MAKE_EXCEPTION(JException_t, A)
Make exception.
Definition: JException.hh:687
ROOT output file.
Definition: JRootFile.hh:196
JRootOutputFile()
Default constructor.
Definition: JRootFile.hh:203
JRootFile()
Default constructor.
Definition: JRootFile.hh:54
Exception handling.
~JRootOutputFile()
Destructor.
Definition: JRootFile.hh:225
virtual void close() override
Close file.
Definition: JRootFile.hh:247
Exception for recovery of file.
Definition: JException.hh:360
virtual void close() override
Close file.
Definition: JRootFile.hh:145
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:237
JRootInputFile()
Default constructor.
Definition: JRootFile.hh:102
Interface for named access of a device.
Definition: JAccessible.hh:20
virtual void set(JClass_t *p) override
Set pointer.
Definition: JPointer.hh:75
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
ROOT file.
Definition: JRootFile.hh:45