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