Jpp  18.2.1-ARCA-DF-PATCH
the software that should make you happy
 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"
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:
176  };
177 }
178 
179 #endif
Exception for opening of file.
Definition: JException.hh:358
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.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
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.
const void setFilename(const std::string &file_name)
Set file name.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
then awk string
void open()
Open file.
friend std::ostream & operator<<(std::ostream &out, const JFileRecorder &recorder)
Write file recorder to output.
Object(s) writing to binary file (i.e. .jpp).
Auxiliary class for object writing with named access.
Protocol exception.
Definition: JException.hh:664
Object(s) writing to Monte Carlo ASCII file (i.e. .evt)
bool isJppFile(const char *file_name)
Check file format.
virtual void open(const char *file_name) override
Open file.
friend std::istream & operator>>(std::istream &in, JFileRecorder &recorder)
Read file recorder from input.
bool isMonteCarloFile(const char *file_name)
Check file format.
virtual void reset() override
Reset pointer.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
Specifications of file name extensions.
Object(s) writing to binary file (i.e. .dat).