Jpp  16.0.0-rc.2
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 methods is_open() and close() 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  /**
61  * Destructor.
62  *
63  * The destructor closes the file if it is still open.
64  */
66  {
67  close();
68  }
69 
70 
71  public:
72  /**
73  * Get file.
74  *
75  * \return pointer to file
76  */
77  TFile* getFile() const
78  {
79  return get();
80  }
81 
82 
83  /**
84  * Check is file is open.
85  *
86  * \return true if open; else false
87  */
88  virtual bool is_open() const override
89  {
90  return (is_valid() && getFile()->IsOpen());
91  }
92 
93 
94  /**
95  * Close file.
96  */
97  virtual void close() override
98  {
99  if (is_open()) {
100  getFile()->Close();
101  }
102 
103  reset();
104  }
105  };
106 
107 
108  /**
109  * ROOT input file.
110  *
111  * This class implements the method open() of the JLANG::JAccessible interface.
112  */
114  public JRootFile
115  {
116  public:
117  /**
118  * Default constructor.
119  */
121  JRootFile()
122  {}
123 
124 
125  /**
126  * Constructor.
127  *
128  * \param file_name file name
129  */
130  JRootInputFile(const char* file_name) :
131  JRootFile()
132  {
133  open(file_name);
134  }
135 
136 
137  /**
138  * Open file.
139  * The file is not opened when no file exists with the given name.
140  *
141  * \param file_name file name
142  */
143  virtual void open(const char* file_name) override
144  {
145  set(TFile::Open(file_name, "READ"));
146 
147  if (!is_open()) {
148  Throw(MAKE_EXCEPTION(JFileOpenException, "Error opening file " << file_name));
149  }
150 
151  if (getFile()->TestBit(TFile::kRecovered)) {
152 
153  const char* const value = getenv(ROOT_FILE_RECOVERY_DISABLE);
154 
155  if (value != NULL && strcmp(value,"0") != 0) {
156  Throw(MAKE_EXCEPTION(JFileRecoveryException, "Error recovery file " << file_name << " disabled"));
157  }
158  }
159  }
160  };
161 
162 
163  /**
164  * ROOT output file.
165  *
166  * This class implements the method open() of the JLANG::JAccessible interface.
167  */
169  public JRootFile
170  {
171  public:
172  /**
173  * Default constructor.
174  */
176  JRootFile()
177  {}
178 
179 
180  /**
181  * Constructor.
182  *
183  * \param file_name file name
184  */
185  JRootOutputFile(const char* file_name) :
186  JRootFile()
187  {
188  open(file_name);
189  }
190 
191 
192  /**
193  * Open file.
194  * The file is not opened when a file exists with the given name.
195  *
196  * \param file_name file name
197  */
198  virtual void open(const char* file_name) override
199  {
200  set(TFile::Open(file_name, "CREATE"));
201 
202  if (!is_open()) {
203  Throw(MAKE_EXCEPTION(JFileOpenException, "Error opening file " << file_name));
204  }
205  }
206 
207 
208  /**
209  * Close file.
210  * This method calls the TFile::Write method before closing the file.
211  */
212  virtual void close() override
213  {
214  if (is_open()) {
215  getFile()->Write();
216  getFile()->Close();
217  }
218 
219  reset();
220  }
221  };
222 }
223 
224 #endif
Exception for opening of file.
Definition: JException.hh:342
JRootOutputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:185
Template storage class.
Definition: JStorage.hh:26
Exceptions.
JRootInputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:130
Auxiliary base class for controling the throwing of exceptions.
Definition: JThrow.hh:25
virtual void reset() override
Reset pointer.
Definition: JStorage.hh:42
bool is_valid() const
Check validity of pointer.
virtual bool is_open() const override
Check is file is open.
Definition: JRootFile.hh:88
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:143
TFile * getFile() const
Get file.
Definition: JRootFile.hh:77
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:113
#define MAKE_EXCEPTION(JException_t, A)
Make exception.
Definition: JException.hh:687
ROOT output file.
Definition: JRootFile.hh:168
virtual void close() override
Close file.
Definition: JRootFile.hh:97
JRootOutputFile()
Default constructor.
Definition: JRootFile.hh:175
JRootFile()
Default constructor.
Definition: JRootFile.hh:54
Exception handling.
virtual void close() override
Close file.
Definition: JRootFile.hh:212
Exception for recovery of file.
Definition: JException.hh:360
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:198
JRootInputFile()
Default constructor.
Definition: JRootFile.hh:120
Interface for named access of a device.
Definition: JAccessible.hh:20
~JRootFile()
Destructor.
Definition: JRootFile.hh:65
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