Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JFileRecorder.hh
Go to the documentation of this file.
1 #ifndef __JSUPPORT__JFILERECORDER__
2 #define __JSUPPORT__JFILERECORDER__
3 
4 #include <string>
5 #include <iostream>
6 
7 #include <TString.h>
8 
9 #include "JLang/JObjectWriter.hh"
10 #include "JLang/JObjectOutput.hh"
11 #include "JLang/JException.hh"
12 #include "JROOT/JRootFileWriter.hh"
13 #include "JIO/JBinaryFileWriter.hh"
17 
18 
19 /**
20  * \file
21  * Recording of objects on file according a format that follows from the file name extension.
22  * \author mdejong
23  */
24 namespace JSUPPORT {}
25 namespace JPP { using namespace JSUPPORT; }
26 
27 namespace JSUPPORT {
28 
32 
33 
34  /**
35  * Object writing to file.
36  *
37  * This class implements the method open of the JLANG::JAccessible interface.
38  * The file format is derived from the file name extension.
39  * The method open can be called to open a file after reading the file name.
40  */
41  template<class T>
42  class JFileRecorder :
43  public JAccessibleObjectWriter<T>
44  {
45  public:
46  /**
47  * Default constructor.
48  */
51  fileName("")
52  {}
53 
54 
55  /**
56  * Constructor.
57  *
58  * \param file_name file name
59  */
60  JFileRecorder(const char* file_name) :
62  fileName(file_name)
63  {}
64 
65 
66  /**
67  * Get file name.
68  *
69  * \return file name
70  */
71  const std::string& getFilename() const
72  {
73  return fileName;
74  }
75 
76 
77  /**
78  * Set file name.
79  *
80  * \param file_name file name
81  */
82  const void setFilename(const std::string& file_name)
83  {
84  fileName = file_name;
85  }
86 
87 
88  /**
89  * Open file.
90  *
91  * \param file_name file name
92  */
93  virtual void open(const char* file_name) override
94  {
95  fileName = file_name;
96 
97  open();
98  }
99 
100 
101  /**
102  * Open file.
103  */
104  void open()
105  {
106  using namespace JPP;
107 
108  this->reset();
109 
110  if (isROOTFile(fileName.c_str())) {
111 
112  this->reset(new JRootFileWriter<T>());
113 
114  } else if (isDAQFile(fileName.c_str())) {
115 
116  this->reset(new JDAQFileWriter<T>());
117 
118  } else if (isJppFile(fileName.c_str())) {
119 
120  this->reset(new JBinaryFileWriter<T>());
121 
122  } else if (isMonteCarloFile(fileName.c_str())) {
123 
124  this->reset(new JMonteCarloFileWriter<T>());
125 
126  } else if (fileName.empty() || fileName == "/dev/null") {
127 
128  this->reset(new JNullAccessibleOutput<T>());
129 
130  } else {
131 
132  THROW(JProtocolException, "Protocol not defined: " << fileName);
133  }
134 
135  if (this->is_valid()) {
136  this->get()->open(fileName.c_str());
137  }
138 
139  if (!this->get()->is_open()) {
140  THROW(JFileOpenException, "Error opening file: " << fileName);
141  }
142  }
143 
144 
145  /**
146  * Read file recorder from input.
147  *
148  * \param in input stream
149  * \param recorder JFileRecorder
150  * \return input stream
151  */
152  friend inline std::istream& operator>>(std::istream& in, JFileRecorder& recorder)
153  {
154  in >> recorder.fileName;
155 
156  return in;
157  }
158 
159 
160  /**
161  * Write file recorder to output.
162  *
163  * \param out output stream
164  * \param recorder JFileRecorder
165  * \return output stream
166  */
167  friend inline std::ostream& operator<<(std::ostream& out, const JFileRecorder& recorder)
168  {
169  out << recorder.fileName;
170 
171  return out;
172  }
173 
174  protected:
175  std::string fileName;
176  };
177 }
178 
179 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Specifications of file name extensions.
Object(s) writing to binary file (i.e. .jpp).
bool is_valid() const
Check validity of pointer.
Exception for opening of file.
Definition: JException.hh:360
virtual JClass_t * get() const override
Get pointer.
Definition: JPointer.hh:64
Protocol exception.
Definition: JException.hh:666
virtual void reset() override
Reset pointer.
ROOT file object output.
Object(s) writing to binary file (i.e. .dat).
Object writing to file.
JFileRecorder(const char *file_name)
Constructor.
friend std::ostream & operator<<(std::ostream &out, const JFileRecorder &recorder)
Write file recorder to output.
JFileRecorder()
Default constructor.
const std::string & getFilename() const
Get file name.
const void setFilename(const std::string &file_name)
Set file name.
void open()
Open file.
virtual void open(const char *file_name) override
Open file.
friend std::istream & operator>>(std::istream &in, JFileRecorder &recorder)
Read file recorder from input.
Object(s) writing to Monte Carlo ASCII file (i.e. .evt)
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Support classes and methods for experiment specific I/O.
Definition: JDataWriter.cc:38
bool isROOTFile(const char *file_name)
Check file format.
bool isJppFile(const char *file_name)
Check file format.
bool isDAQFile(const char *file_name)
Check file format.
bool isMonteCarloFile(const char *file_name)
Check file format.
virtual bool is_open() const override
Check is device is open.
Auxiliary class for object writing with named access.
Implementation for null output with null access.