Jpp  test_elongated_shower_pde
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  private:
107  JRootFile(const JRootFile&);
108  JRootFile(JRootFile&&);
109  JRootFile& operator=(const JRootFile&);
111  };
112 
113 
114  /**
115  * ROOT input file.
116  *
117  * This class implements the method open() of the JLANG::JAccessible interface.
118  */
120  public JRootFile
121  {
122  public:
123  /**
124  * Default constructor.
125  */
127  JRootFile()
128  {}
129 
130 
131  /**
132  * Constructor.
133  *
134  * \param file_name file name
135  */
136  JRootInputFile(const char* file_name) :
137  JRootFile()
138  {
139  open(file_name);
140  }
141 
142 
143  /**
144  * Open file.
145  * The file is not opened when no file exists with the given name.
146  *
147  * \param file_name file name
148  */
149  virtual void open(const char* file_name) override
150  {
151  set(TFile::Open(file_name, "READ"));
152 
153  if (!is_open()) {
154  Throw(MAKE_EXCEPTION(JFileOpenException, "Error opening file " << file_name));
155  }
156 
157  if (getFile()->TestBit(TFile::kRecovered)) {
158 
159  const char* const value = getenv(ROOT_FILE_RECOVERY_DISABLE);
160 
161  if (value != NULL && strcmp(value,"0") != 0) {
162  Throw(MAKE_EXCEPTION(JFileRecoveryException, "Error recovery file " << file_name << " disabled"));
163  }
164  }
165  }
166  };
167 
168 
169  /**
170  * ROOT output file.
171  *
172  * This class implements the method open() of the JLANG::JAccessible interface.
173  */
175  public JRootFile
176  {
177  public:
178  /**
179  * Default constructor.
180  */
182  JRootFile()
183  {}
184 
185 
186  /**
187  * Constructor.
188  *
189  * \param file_name file name
190  */
191  JRootOutputFile(const char* file_name) :
192  JRootFile()
193  {
194  open(file_name);
195  }
196 
197 
198  /**
199  * Open file.
200  * The file is not opened when a file exists with the given name.
201  *
202  * \param file_name file name
203  */
204  virtual void open(const char* file_name) override
205  {
206  set(TFile::Open(file_name, "CREATE"));
207 
208  if (!is_open()) {
209  Throw(MAKE_EXCEPTION(JFileOpenException, "Error opening file " << file_name));
210  }
211  }
212 
213 
214  /**
215  * Close file.
216  * This method calls the TFile::Write method before closing the file.
217  */
218  virtual void close() override
219  {
220  if (is_open()) {
221  getFile()->Write();
222  getFile()->Close();
223  }
224 
225  reset();
226  }
227  };
228 }
229 
230 #endif
Exception for opening of file.
Definition: JException.hh:342
JRootOutputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:191
Template storage class.
Definition: JStorage.hh:26
Exceptions.
JRootInputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:136
Auxiliary base class for controling the throwing of exceptions.
Definition: JThrow.hh:25
virtual void reset() override
Reset pointer.
Definition: JStorage.hh:42
JRootFile & operator=(const JRootFile &)
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:149
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:119
#define MAKE_EXCEPTION(JException_t, A)
Make exception.
Definition: JException.hh:687
ROOT output file.
Definition: JRootFile.hh:174
virtual void close() override
Close file.
Definition: JRootFile.hh:97
JRootOutputFile()
Default constructor.
Definition: JRootFile.hh:181
JRootFile()
Default constructor.
Definition: JRootFile.hh:54
Exception handling.
virtual void close() override
Close file.
Definition: JRootFile.hh:218
Exception for recovery of file.
Definition: JException.hh:360
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:204
JRootInputFile()
Default constructor.
Definition: JRootFile.hh:126
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