Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  * Open file.
78  *
79  * \param file_name file name
80  */
81  virtual void open(const char* file_name)
82  {
83  fileName = file_name;
84 
85  open();
86  }
87 
88 
89  /**
90  * Open file.
91  */
92  void open()
93  {
96 
97  this->reset();
98 
99  if (isROOTFile(fileName.c_str())) {
100 
101  this->reset(new JRootFileWriter<T>());
102 
103  } else if (isDAQFile(fileName.c_str())) {
104 
105  this->reset(new JDAQFileWriter<T>());
106 
107  } else if (isMonteCarloFile(fileName.c_str())) {
108 
109  this->reset(new JMonteCarloFileWriter<T>());
110 
111  } else if (fileName.empty() || fileName == "/dev/null") {
112 
113  this->reset(new JNullAccessibleOutput<T>());
114 
115  } else {
116 
117  THROW(JProtocolException, "Protocol not defined: " << fileName);
118  }
119 
120  if (this->is_valid()) {
121  this->get()->open(fileName.c_str());
122  }
123 
124  if (!this->get()->is_open()) {
125  THROW(JFileOpenException, "Error opening file: " << fileName);
126  }
127  }
128 
129 
130  /**
131  * Read file recorder from input.
132  *
133  * \param in input stream
134  * \param recorder JFileRecorder
135  * \return input stream
136  */
137  friend inline std::istream& operator>>(std::istream& in, JFileRecorder& recorder)
138  {
139  in >> recorder.fileName;
140 
141  return in;
142  }
143 
144 
145  /**
146  * Write file recorder to output.
147  *
148  * \param out output stream
149  * \param recorder JFileRecorder
150  * \return output stream
151  */
152  friend inline std::ostream& operator<<(std::ostream& out, const JFileRecorder& recorder)
153  {
154  out << recorder.fileName;
155 
156  return out;
157  }
158 
159  protected:
160  std::string fileName;
161  };
162 }
163 
164 #endif
Exception for opening of file.
Definition: JException.hh:324
Object writing to file.
Exceptions.
bool isDAQFile(const char *file_name)
Check file format.
const std::string & getFilename() const
Get file name.
JFileRecorder(const char *file_name)
Constructor.
virtual void open(const char *file_name)
Open file.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
ROOT file object output.
bool is_valid() const
Check validity of pointer.
Implementation for null output with null access.
JFileRecorder()
Default constructor.
bool isROOTFile(const char *file_name)
Check file format.
void open()
Open file.
friend std::ostream & operator<<(std::ostream &out, const JFileRecorder &recorder)
Write file recorder to output.
Auxiliary class for object writing with named access.
Protocol exception.
Definition: JException.hh:612
Object(s) writing to Monte Carlo ASCII file (i.e .evt)
friend std::istream & operator>>(std::istream &in, JFileRecorder &recorder)
Read file recorder from input.
bool isMonteCarloFile(const char *file_name)
Check file format.
Specifications of file name extensions.
Object(s) writing to binary file (i.e.