Jpp  15.0.5
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"
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) override
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
Exception for opening of file.
Definition: JException.hh:342
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:670
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
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:630
Object(s) writing to Monte Carlo ASCII file (i.e. .evt)
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 source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:41
Specifications of file name extensions.
Object(s) writing to binary file (i.e. .dat).