Jpp  pmt_effective_area_update
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 "TFile.h"
5 #include "TError.h"
6 
7 #include "JLang/JAccessible.hh"
8 #include "JLang/JException.hh"
9 #include "JLang/JThrow.hh"
10 #include "JLang/JStorage.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JROOT {}
18 namespace JPP { using namespace JROOT; }
19 
20 namespace JROOT {
21 
22  using JLANG::JAccessible;
23  using JLANG::JStorage;
25  using JLANG::JThrow;
26 
27 
28  /**
29  * ROOT file.
30  *
31  * This class implements the methods is_open() and close() of the JLANG::JAccessible interface.
32  */
33  class JRootFile :
34  public virtual JAccessible,
35  public JStorage<TFile>
36  {
37  protected:
38  /**
39  * Default constructor.
40  * The printing of ROOT errors is suppressed.
41  */
43  {
44  gErrorIgnoreLevel = kError;
45  }
46 
47 
48  /**
49  * Destructor.
50  *
51  * The destructor closes the file if it is still open.
52  */
54  {
55  close();
56  }
57 
58 
59  public:
60  /**
61  * Get file.
62  *
63  * \return pointer to file
64  */
65  TFile* getFile() const
66  {
67  return get();
68  }
69 
70 
71  /**
72  * Check is file is open.
73  *
74  * \return true if open; else false
75  */
76  virtual bool is_open() const override
77  {
78  return (is_valid() && getFile()->IsOpen());
79  }
80 
81 
82  /**
83  * Close file.
84  */
85  virtual void close() override
86  {
87  if (is_open()) {
88  getFile()->Close();
89  }
90 
91  reset();
92  }
93  };
94 
95 
96  /**
97  * ROOT input file.
98  *
99  * This class implements the method open() of the JLANG::JAccessible interface.
100  */
102  public JRootFile
103  {
104  public:
105  /**
106  * Default constructor.
107  */
109  JRootFile()
110  {}
111 
112 
113  /**
114  * Constructor.
115  *
116  * \param file_name file name
117  */
118  JRootInputFile(const char* file_name) :
119  JRootFile()
120  {
121  open(file_name);
122  }
123 
124 
125  /**
126  * Open file.
127  * The file is not opened when no file exists with the given name.
128  *
129  * \param file_name file name
130  */
131  virtual void open(const char* file_name) override
132  {
133  set(TFile::Open(file_name, "READ"));
134 
135  if (!is_open()) {
136  Throw(JFileOpenException(std::string("Error opening file ") + file_name));
137  }
138  }
139  };
140 
141 
142  /**
143  * ROOT output file.
144  *
145  * This class implements the method open() of the JLANG::JAccessible interface.
146  */
148  public JRootFile
149  {
150  public:
151  /**
152  * Default constructor.
153  */
155  JRootFile()
156  {}
157 
158 
159  /**
160  * Constructor.
161  *
162  * \param file_name file name
163  */
164  JRootOutputFile(const char* file_name) :
165  JRootFile()
166  {
167  open(file_name);
168  }
169 
170 
171  /**
172  * Open file.
173  * The file is not opened when a file exists with the given name.
174  *
175  * \param file_name file name
176  */
177  virtual void open(const char* file_name) override
178  {
179  set(TFile::Open(file_name, "CREATE"));
180 
181  if (!is_open()) {
182  Throw(JFileOpenException(std::string("Error opening file ") + file_name));
183  }
184  }
185 
186 
187  /**
188  * Close file.
189  * This method calls the TFile::Write method before closing the file.
190  */
191  virtual void close() override
192  {
193  if (is_open()) {
194  getFile()->Write();
195  getFile()->Close();
196  }
197 
198  reset();
199  }
200  };
201 }
202 
203 #endif
Exception for opening of file.
Definition: JException.hh:342
JRootOutputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:164
Template storage class.
Definition: JStorage.hh:26
Exceptions.
JRootInputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:118
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:76
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:131
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
ROOT input file.
Definition: JRootFile.hh:101
ROOT output file.
Definition: JRootFile.hh:147
virtual void close() override
Close file.
Definition: JRootFile.hh:85
JRootOutputFile()
Default constructor.
Definition: JRootFile.hh:154
JRootFile()
Default constructor.
Definition: JRootFile.hh:42
Exception handling.
virtual void close() override
Close file.
Definition: JRootFile.hh:191
virtual void open(const char *file_name) override
Open file.
Definition: JRootFile.hh:177
JRootInputFile()
Default constructor.
Definition: JRootFile.hh:108
Interface for named access of a device.
Definition: JAccessible.hh:20
~JRootFile()
Destructor.
Definition: JRootFile.hh:53
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:33