Jpp
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
77  {
78  return (is_valid() && getFile()->IsOpen());
79  }
80 
81 
82  /**
83  * Close file.
84  */
85  virtual void close()
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)
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)
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()
192  {
193  if (is_open()) {
194  getFile()->Write();
195  getFile()->Close();
196  }
197 
198  reset();
199  }
200  };
201 }
202 
203 #endif
JException.hh
JLANG::JPointer::set
virtual void set(JClass_t *p)
Set pointer.
Definition: JPointer.hh:75
JROOT::JRootFile::close
virtual void close()
Close file.
Definition: JRootFile.hh:85
JLANG::JPointer::get
virtual JClass_t * get() const
Get pointer.
Definition: JPointer.hh:64
JROOT::JRootOutputFile::open
virtual void open(const char *file_name)
Open file.
Definition: JRootFile.hh:177
JROOT
Auxiliary classes and methods for ROOT I/O.
Definition: JAbstractStreamer.hh:13
JLANG::JAccessible
Interface for named access of a device.
Definition: JAccessible.hh:20
JROOT::JRootFile
ROOT file.
Definition: JRootFile.hh:33
JROOT::JRootInputFile::JRootInputFile
JRootInputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:118
JROOT::JRootOutputFile
ROOT output file.
Definition: JRootFile.hh:147
JROOT::JRootFile::~JRootFile
~JRootFile()
Destructor.
Definition: JRootFile.hh:53
JLANG::JFileOpenException
Exception for opening of file.
Definition: JException.hh:342
JStorage.hh
JLANG::JThrow
Auxiliary base class for controling the throwing of exceptions.
Definition: JThrow.hh:25
JROOT::JRootOutputFile::JRootOutputFile
JRootOutputFile(const char *file_name)
Constructor.
Definition: JRootFile.hh:164
JROOT::JRootFile::getFile
TFile * getFile() const
Get file.
Definition: JRootFile.hh:65
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JROOT::JRootOutputFile::JRootOutputFile
JRootOutputFile()
Default constructor.
Definition: JRootFile.hh:154
JROOT::JRootInputFile
ROOT input file.
Definition: JRootFile.hh:101
JAccessible.hh
JThrow.hh
JLANG::JThrow< JAccessible >::Throw
static void Throw(const bool option)
Enable/disable throw option.
Definition: JThrow.hh:37
JROOT::JRootInputFile::open
virtual void open(const char *file_name)
Open file.
Definition: JRootFile.hh:131
JROOT::JRootOutputFile::close
virtual void close()
Close file.
Definition: JRootFile.hh:191
JLANG::JStorage
Template storage class.
Definition: JStorage.hh:26
JROOT::JRootInputFile::JRootInputFile
JRootInputFile()
Default constructor.
Definition: JRootFile.hh:108
JLANG::JStorage::reset
virtual void reset()
Reset pointer.
Definition: JStorage.hh:42
JROOT::JRootFile::is_open
virtual bool is_open() const
Check is file is open.
Definition: JRootFile.hh:76
JROOT::JRootFile::JRootFile
JRootFile()
Default constructor.
Definition: JRootFile.hh:42
JLANG::JAbstractPointer::is_valid
bool is_valid() const
Check validity of pointer.
Definition: JAbstractPointer.hh:83