00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _H_ResponseFile_hsg
00011 #define _H_ResponseFile_hsg
00012
00013 #include "instrument/DetectorConverter.h"
00014 #include "instrument/GlastDetector.h"
00015 #include "facilities/bpkt_streams.h"
00016 #include <strstream>
00017
00018
00019 namespace xml {
00020 class IFile;
00021 }
00022
00026 class ResponseFile : public DetectorConverter {
00027 public:
00028
00029 typedef enum { none_mode = 0, ascii_mode, binary_mode, dual_mode }
00030 ios_mode;
00031
00032
00033
00034 ResponseFile(std::istream*, bool owns = false, ios_mode mode = ascii_mode);
00035 ResponseFile(std::ostream*, bool owns = false, ios_mode mode = ascii_mode);
00036 ResponseFile(const char* fname, bool read = false,
00037 ios_mode mode = ascii_mode);
00038
00040 virtual ~ResponseFile ();
00041
00042
00043
00045 ResponseFile& operator >> (GlastDetector&);
00046 ResponseFile& operator << (const GlastDetector&);
00047
00049 bool read_event ();
00050
00052 void write_event ();
00053
00055 void write_event( std::vector<int> header);
00056
00058 bool good () const;
00059
00061 int mode () const;
00062
00063
00064 std::string version () const;
00065 std::string revision () const;
00066
00067
00068 static void loadParameters (xml::IFile& ini);
00069
00071 int ndet_read()const { return m_det_read;}
00072
00074 static void createFactories();
00075
00076 protected:
00077
00078 void setin (std::istream* i, ios_mode mode, bool owns = false);
00079 void setout (std::ostream* o, ios_mode mode, bool owns = false);
00080
00081
00082 bool verify(std::istream&, ios_mode);
00083 void verify(std::ostream&, ios_mode);
00084
00085
00086 void end_record ();
00087 void end_event ();
00088 int next_id ();
00089 std::string flush_record ();
00090
00091
00092 static int event_end_id ();
00093 static char record_end_char ();
00094
00095
00096 void backward (CsIDetector&);
00097 void backward (MCTruth&);
00098 void backward (Scintillator&);
00099 void backward (SiDetector&);
00100 void forward (const CsIDetector&);
00101 void forward (const MCTruth&);
00102 void forward (const Scintillator&);
00103 void forward (const SiDetector&);
00104
00105
00106 friend class CsIDetector;
00107 friend class MCTruth;
00108 friend class Scintillator;
00109 friend class SiDetector;
00110
00111 private:
00112
00113 typedef std::map <GlastDetector::id_type, void*> DataMap;
00114
00115
00116 bpkt::istream* m_binin;
00117 std::istream* m_stdin;
00118 bool m_ownsin;
00119 bpkt::ostream* m_binout;
00120 std::ostream* m_stdout;
00121 bool m_ownsout;
00122 DataMap m_data;
00123 std::string m_version;
00124 std::string m_revision;
00125
00126 int m_det_read;
00127
00128 template <class _Ty> friend ResponseFile& operator <<
00129 (ResponseFile&, const _Ty&);
00130 template <class _Ty> friend ResponseFile& operator >>
00131 (ResponseFile&, _Ty&);
00132
00133
00134 static const char* s_version;
00135 static const char* s_revision;
00136 };
00137
00138
00139
00140 template <class _Ty>
00141 ResponseFile& operator << (ResponseFile& f, const _Ty& t) {
00142 if (f.m_stdout) *f.m_stdout << t << ' ';
00143 if (f.m_binout) *f.m_binout << t;
00144 return f;
00145 }
00146
00147 template <class _Ty>
00148 ResponseFile& operator >> (ResponseFile& f, _Ty& t) {
00149 if (f.m_stdin) *f.m_stdin >> t;
00150 if (f.m_binin) *f.m_binin >> t;
00151 return f;
00152 }
00153
00154
00155
00156 inline ResponseFile& ResponseFile::operator >> (GlastDetector& d)
00157 {
00158 d.accept (*this);
00159 return *this;
00160 }
00161
00162 inline ResponseFile& ResponseFile::operator << (const GlastDetector& p)
00163 {
00164 if (!p.empty()) {
00165 if (m_stdout) (*m_stdout) << '\n';
00166 (*this) << p.id();
00167 p.accept (*this);
00168 end_record ();
00169 }
00170 return *this;
00171 }
00172
00173 inline bool ResponseFile::good () const
00174 {
00175 if (m_binin) return m_binin->good();
00176 if (m_binout) return m_binout->good();
00177 if (m_stdin) return m_stdin->good();
00178 if (m_stdout) return m_stdout->good();
00179 return false;
00180 }
00181
00182 inline int ResponseFile::event_end_id ()
00183 {
00184 return -1;
00185 }
00186
00187 inline char ResponseFile::record_end_char ()
00188 {
00189 return '|';
00190 }
00191
00192 inline void ResponseFile::end_record ()
00193 {
00194
00195
00196 if (m_binout) *(m_binout) << std::flush;
00197 if (m_stdout) *(m_stdout) << record_end_char();
00198 }
00199
00200 inline void ResponseFile::end_event ()
00201 {
00202 if (m_stdout) *m_stdout << '\n';
00203 (*this) << event_end_id();
00204 if (m_binout) *m_binout << std::flush;
00205 if (m_stdout) *m_stdout << std::flush;
00206 }
00207
00208 inline int ResponseFile::next_id ()
00209 {
00210 int i;
00211 (*this) >> i;
00212 return i;
00213 }
00214
00215 inline std::string ResponseFile::flush_record ()
00216 {
00217 std::string s = "";
00218
00219 if (m_binin) m_binin->sync();
00220 if (m_stdin) {
00221 std::getline (*m_stdin, s, record_end_char());
00222 }
00223
00224 return s;
00225 }
00226
00227 inline std::string ResponseFile::version () const
00228 {
00229 return m_version;
00230 }
00231
00232 inline std::string ResponseFile::revision () const
00233 {
00234 return m_revision;
00235 }
00236
00237 inline int ResponseFile::mode () const
00238 {
00239 return ((m_binout || m_binin) ? binary_mode : none_mode) +
00240 ((m_stdin || m_stdout) ? ascii_mode : none_mode);
00241 }
00242
00243 #endif // _H_ResponseFile_hsg